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