• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2012 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.inputmethod.research;
18 
19 import android.content.SharedPreferences;
20 import android.util.JsonWriter;
21 import android.view.MotionEvent;
22 import android.view.inputmethod.CompletionInfo;
23 
24 import com.android.inputmethod.keyboard.Key;
25 import com.android.inputmethod.latin.SuggestedWords;
26 import com.android.inputmethod.latin.SuggestedWords.SuggestedWordInfo;
27 
28 import java.io.IOException;
29 import java.util.Map;
30 
31 /**
32  * Routines for mapping classes and variables to JSON representations for logging.
33  */
34 /* package */ class JsonUtils {
JsonUtils()35     private JsonUtils() {
36         // This utility class is not publicly instantiable.
37     }
38 
writeJson(final CompletionInfo[] ci, final JsonWriter jsonWriter)39     /* package */ static void writeJson(final CompletionInfo[] ci, final JsonWriter jsonWriter)
40             throws IOException {
41         jsonWriter.beginArray();
42         for (int j = 0; j < ci.length; j++) {
43             jsonWriter.value(ci[j].toString());
44         }
45         jsonWriter.endArray();
46     }
47 
writeJson(final SharedPreferences prefs, final JsonWriter jsonWriter)48     /* package */ static void writeJson(final SharedPreferences prefs, final JsonWriter jsonWriter)
49             throws IOException {
50         jsonWriter.beginObject();
51         for (Map.Entry<String,?> entry : prefs.getAll().entrySet()) {
52             jsonWriter.name(entry.getKey());
53             final Object innerValue = entry.getValue();
54             if (innerValue == null) {
55                 jsonWriter.nullValue();
56             } else if (innerValue instanceof Boolean) {
57                 jsonWriter.value((Boolean) innerValue);
58             } else if (innerValue instanceof Number) {
59                 jsonWriter.value((Number) innerValue);
60             } else {
61                 jsonWriter.value(innerValue.toString());
62             }
63         }
64         jsonWriter.endObject();
65     }
66 
writeJson(final Key[] keys, final JsonWriter jsonWriter)67     /* package */ static void writeJson(final Key[] keys, final JsonWriter jsonWriter)
68             throws IOException {
69         jsonWriter.beginArray();
70         for (Key key : keys) {
71             writeJson(key, jsonWriter);
72         }
73         jsonWriter.endArray();
74     }
75 
writeJson(final Key key, final JsonWriter jsonWriter)76     private static void writeJson(final Key key, final JsonWriter jsonWriter) throws IOException {
77         jsonWriter.beginObject();
78         jsonWriter.name("code").value(key.mCode);
79         jsonWriter.name("altCode").value(key.getAltCode());
80         jsonWriter.name("x").value(key.mX);
81         jsonWriter.name("y").value(key.mY);
82         jsonWriter.name("w").value(key.mWidth);
83         jsonWriter.name("h").value(key.mHeight);
84         jsonWriter.endObject();
85     }
86 
writeJson(final SuggestedWords words, final JsonWriter jsonWriter)87     /* package */ static void writeJson(final SuggestedWords words, final JsonWriter jsonWriter)
88             throws IOException {
89         jsonWriter.beginObject();
90         jsonWriter.name("typedWordValid").value(words.mTypedWordValid);
91         jsonWriter.name("willAutoCorrect")
92                 .value(words.mWillAutoCorrect);
93         jsonWriter.name("isPunctuationSuggestions")
94                 .value(words.mIsPunctuationSuggestions);
95         jsonWriter.name("isObsoleteSuggestions").value(words.mIsObsoleteSuggestions);
96         jsonWriter.name("isPrediction").value(words.mIsPrediction);
97         jsonWriter.name("words");
98         jsonWriter.beginArray();
99         final int size = words.size();
100         for (int j = 0; j < size; j++) {
101             final SuggestedWordInfo wordInfo = words.getInfo(j);
102             jsonWriter.value(wordInfo.toString());
103         }
104         jsonWriter.endArray();
105         jsonWriter.endObject();
106     }
107 
writeJson(final MotionEvent me, final JsonWriter jsonWriter)108     /* package */ static void writeJson(final MotionEvent me, final JsonWriter jsonWriter)
109             throws IOException {
110         jsonWriter.beginObject();
111         jsonWriter.name("pointerIds");
112         jsonWriter.beginArray();
113         final int pointerCount = me.getPointerCount();
114         for (int index = 0; index < pointerCount; index++) {
115             jsonWriter.value(me.getPointerId(index));
116         }
117         jsonWriter.endArray();
118 
119         jsonWriter.name("xyt");
120         jsonWriter.beginArray();
121         final int historicalSize = me.getHistorySize();
122         for (int index = 0; index < historicalSize; index++) {
123             jsonWriter.beginObject();
124             jsonWriter.name("t");
125             jsonWriter.value(me.getHistoricalEventTime(index));
126             jsonWriter.name("d");
127             jsonWriter.beginArray();
128             for (int pointerIndex = 0; pointerIndex < pointerCount; pointerIndex++) {
129                 jsonWriter.beginObject();
130                 jsonWriter.name("x");
131                 jsonWriter.value(me.getHistoricalX(pointerIndex, index));
132                 jsonWriter.name("y");
133                 jsonWriter.value(me.getHistoricalY(pointerIndex, index));
134                 jsonWriter.endObject();
135             }
136             jsonWriter.endArray();
137             jsonWriter.endObject();
138         }
139         jsonWriter.beginObject();
140         jsonWriter.name("t");
141         jsonWriter.value(me.getEventTime());
142         jsonWriter.name("d");
143         jsonWriter.beginArray();
144         for (int pointerIndex = 0; pointerIndex < pointerCount; pointerIndex++) {
145             jsonWriter.beginObject();
146             jsonWriter.name("x");
147             jsonWriter.value(me.getX(pointerIndex));
148             jsonWriter.name("y");
149             jsonWriter.value(me.getY(pointerIndex));
150             jsonWriter.endObject();
151         }
152         jsonWriter.endArray();
153         jsonWriter.endObject();
154         jsonWriter.endArray();
155         jsonWriter.endObject();
156     }
157 }
158