Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
1 /* This Source Code Form is subject to the terms of the Mozilla Public
2 * License, v. 2.0. If a copy of the MPL was not distributed with this
3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 package org.mozilla.gecko.background.healthreport;
7 import java.util.HashSet;
8 import java.util.Iterator;
9 import java.util.Set;
10 import java.util.SortedSet;
11 import java.util.TreeSet;
12 import java.util.UUID;
14 import org.json.JSONArray;
15 import org.json.JSONException;
16 import org.json.JSONObject;
17 import org.mozilla.apache.commons.codec.digest.DigestUtils;
19 import android.content.ContentUris;
20 import android.net.Uri;
22 public class HealthReportUtils {
23 public static final String LOG_TAG = HealthReportUtils.class.getSimpleName();
25 public static String getEnvironmentHash(final String input) {
26 return DigestUtils.shaHex(input);
27 }
29 /**
30 * Take an environment URI (one that identifies an environment) and produce an
31 * event URI.
32 *
33 * That this is needed is tragic.
34 *
35 * @param environmentURI
36 * the {@link Uri} returned by an environment operation.
37 * @return a {@link Uri} to which insertions can be dispatched.
38 */
39 public static Uri getEventURI(Uri environmentURI) {
40 return environmentURI.buildUpon().path("/events/" + ContentUris.parseId(environmentURI) + "/").build();
41 }
43 /**
44 * Copy the keys from the provided {@link JSONObject} into the provided {@link Set}.
45 */
46 private static <T extends Set<String>> T intoKeySet(T keys, JSONObject o) {
47 if (o == null || o == JSONObject.NULL) {
48 return keys;
49 }
51 @SuppressWarnings("unchecked")
52 Iterator<String> it = o.keys();
53 while (it.hasNext()) {
54 keys.add(it.next());
55 }
56 return keys;
57 }
59 /**
60 * Produce a {@link SortedSet} containing the string keys of the provided
61 * object.
62 *
63 * @param o a {@link JSONObject} with string keys.
64 * @return a sorted set.
65 */
66 public static SortedSet<String> sortedKeySet(JSONObject o) {
67 return intoKeySet(new TreeSet<String>(), o);
68 }
70 /**
71 * Produce a {@link Set} containing the string keys of the provided object.
72 * @param o a {@link JSONObject} with string keys.
73 * @return an unsorted set.
74 */
75 public static Set<String> keySet(JSONObject o) {
76 return intoKeySet(new HashSet<String>(), o);
77 }
79 /**
80 * Just like {@link JSONObject#accumulate(String, Object)}, but doesn't do the wrong thing for single values.
81 * @throws JSONException
82 */
83 public static void append(JSONObject o, String key, Object value) throws JSONException {
84 if (!o.has(key)) {
85 JSONArray arr = new JSONArray();
86 arr.put(value);
87 o.put(key, arr);
88 return;
89 }
90 Object dest = o.get(key);
91 if (dest instanceof JSONArray) {
92 ((JSONArray) dest).put(value);
93 return;
94 }
95 JSONArray arr = new JSONArray();
96 arr.put(dest);
97 arr.put(value);
98 o.put(key, arr);
99 }
101 /**
102 * Accumulate counts for how often each provided value occurs.
103 *
104 * <code>
105 * HealthReportUtils.count(o, "foo", "bar");
106 * </code>
107 *
108 * will change
109 *
110 * <pre>
111 * {"foo", {"bar": 1}}
112 * </pre>
113 *
114 * into
115 *
116 * <pre>
117 * {"foo", {"bar": 2}}
118 * </pre>
119 *
120 */
121 public static void count(JSONObject o, String key,
122 String value) throws JSONException {
123 if (!o.has(key)) {
124 JSONObject counts = new JSONObject();
125 counts.put(value, 1);
126 o.put(key, counts);
127 return;
128 }
129 JSONObject dest = o.getJSONObject(key);
130 dest.put(value, dest.optInt(value, 0) + 1);
131 }
133 public static String generateDocumentId() {
134 return UUID.randomUUID().toString();
135 }
136 }