Home | History | Annotate | Download | only in research
      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 {
     35     private JsonUtils() {
     36         // This utility class is not publicly instantiable.
     37     }
     38 
     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 
     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 
     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 
     76     private static void writeJson(final Key key, final JsonWriter jsonWriter) throws IOException {
     77         jsonWriter.beginObject();
     78         jsonWriter.name("code").value(key.getCode());
     79         jsonWriter.name("altCode").value(key.getAltCode());
     80         jsonWriter.name("x").value(key.getX());
     81         jsonWriter.name("y").value(key.getY());
     82         jsonWriter.name("w").value(key.getWidth());
     83         jsonWriter.name("h").value(key.getHeight());
     84         jsonWriter.endObject();
     85     }
     86 
     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("suggestedWords");
     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.beginObject();
    103             jsonWriter.name("word").value(wordInfo.toString());
    104             jsonWriter.name("score").value(wordInfo.mScore);
    105             jsonWriter.name("kind").value(wordInfo.mKind);
    106             jsonWriter.name("sourceDict").value(wordInfo.mSourceDict.mDictType);
    107             jsonWriter.endObject();
    108         }
    109         jsonWriter.endArray();
    110         jsonWriter.endObject();
    111     }
    112 
    113     /* package */ static void writeJson(final MotionEvent me, final JsonWriter jsonWriter)
    114             throws IOException {
    115         jsonWriter.beginObject();
    116         jsonWriter.name("pointerIds");
    117         jsonWriter.beginArray();
    118         final int pointerCount = me.getPointerCount();
    119         for (int index = 0; index < pointerCount; index++) {
    120             jsonWriter.value(me.getPointerId(index));
    121         }
    122         jsonWriter.endArray();
    123 
    124         jsonWriter.name("xyt");
    125         jsonWriter.beginArray();
    126         final int historicalSize = me.getHistorySize();
    127         for (int index = 0; index < historicalSize; index++) {
    128             jsonWriter.beginObject();
    129             jsonWriter.name("t");
    130             jsonWriter.value(me.getHistoricalEventTime(index));
    131             jsonWriter.name("d");
    132             jsonWriter.beginArray();
    133             for (int pointerIndex = 0; pointerIndex < pointerCount; pointerIndex++) {
    134                 jsonWriter.beginObject();
    135                 jsonWriter.name("x");
    136                 jsonWriter.value(me.getHistoricalX(pointerIndex, index));
    137                 jsonWriter.name("y");
    138                 jsonWriter.value(me.getHistoricalY(pointerIndex, index));
    139                 jsonWriter.endObject();
    140             }
    141             jsonWriter.endArray();
    142             jsonWriter.endObject();
    143         }
    144         jsonWriter.beginObject();
    145         jsonWriter.name("t");
    146         jsonWriter.value(me.getEventTime());
    147         jsonWriter.name("d");
    148         jsonWriter.beginArray();
    149         for (int pointerIndex = 0; pointerIndex < pointerCount; pointerIndex++) {
    150             jsonWriter.beginObject();
    151             jsonWriter.name("x");
    152             jsonWriter.value(me.getX(pointerIndex));
    153             jsonWriter.name("y");
    154             jsonWriter.value(me.getY(pointerIndex));
    155             jsonWriter.endObject();
    156         }
    157         jsonWriter.endArray();
    158         jsonWriter.endObject();
    159         jsonWriter.endArray();
    160         jsonWriter.endObject();
    161     }
    162 }
    163