MainLogBuffer.java revision 0aafbcf879a31afc8361078bd9574915d95694c0
1/*
2 * Copyright (C) 2012 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.research;
18
19import android.util.Log;
20
21import com.android.inputmethod.latin.Dictionary;
22import com.android.inputmethod.latin.Suggest;
23import com.android.inputmethod.latin.define.ProductionFlag;
24
25import java.util.Random;
26
27public class MainLogBuffer extends LogBuffer {
28    private static final String TAG = MainLogBuffer.class.getSimpleName();
29    private static final boolean DEBUG = false && ProductionFlag.IS_EXPERIMENTAL_DEBUG;
30
31    // The size of the n-grams logged.  E.g. N_GRAM_SIZE = 2 means to sample bigrams.
32    private static final int N_GRAM_SIZE = 2;
33    // The number of words between n-grams to omit from the log.
34    private static final int DEFAULT_NUMBER_OF_WORDS_BETWEEN_SAMPLES =
35            ProductionFlag.IS_EXPERIMENTAL_DEBUG ? 2 : 18;
36
37    private final ResearchLog mResearchLog;
38    private Suggest mSuggest;
39
40    // The minimum periodicity with which n-grams can be sampled.  E.g. mWinWordPeriod is 10 if
41    // every 10th bigram is sampled, i.e., words 1-8 are not, but the bigram at words 9 and 10, etc.
42    // for 11-18, and the bigram at words 19 and 20.  If an n-gram is not safe (e.g. it  contains a
43    // number in the middle or an out-of-vocabulary word), then sampling is delayed until a safe
44    // n-gram does appear.
45    /* package for test */ int mMinWordPeriod;
46
47    // Counter for words left to suppress before an n-gram can be sampled.  Reset to mMinWordPeriod
48    // after a sample is taken.
49    /* package for test */ int mWordsUntilSafeToSample;
50
51    public MainLogBuffer(final ResearchLog researchLog) {
52        super(N_GRAM_SIZE);
53        mResearchLog = researchLog;
54        mMinWordPeriod = DEFAULT_NUMBER_OF_WORDS_BETWEEN_SAMPLES + N_GRAM_SIZE;
55        final Random random = new Random();
56        mWordsUntilSafeToSample = random.nextInt(mMinWordPeriod);
57    }
58
59    public void setSuggest(Suggest suggest) {
60        mSuggest = suggest;
61    }
62
63    @Override
64    public void shiftIn(final LogUnit newLogUnit) {
65        super.shiftIn(newLogUnit);
66        if (newLogUnit.hasWord()) {
67            if (mWordsUntilSafeToSample > 0) {
68                mWordsUntilSafeToSample--;
69            }
70        }
71        if (DEBUG) {
72            Log.d(TAG, "shiftedIn " + (newLogUnit.hasWord() ? newLogUnit.getWord() : ""));
73        }
74    }
75
76    public void resetWordCounter() {
77        mWordsUntilSafeToSample = mMinWordPeriod;
78    }
79
80    /**
81     * Determines whether the content of the MainLogBuffer can be safely uploaded in its complete
82     * form and still protect the user's privacy.
83     *
84     * The size of the MainLogBuffer is just enough to hold one n-gram, its corrections, and any
85     * non-character data that is typed between words.  The decision about privacy is made based on
86     * the buffer's entire content.  If it is decided that the privacy risks are too great to upload
87     * the contents of this buffer, a censored version of the LogItems may still be uploaded.  E.g.,
88     * the screen orientation and other characteristics about the device can be uploaded without
89     * revealing much about the user.
90     */
91    public boolean isSafeToLog() {
92        // Check that we are not sampling too frequently.  Having sampled recently might disclose
93        // too much of the user's intended meaning.
94        if (mWordsUntilSafeToSample > 0) {
95            return false;
96        }
97        if (mSuggest == null || !mSuggest.hasMainDictionary()) {
98            // Main dictionary is unavailable.  Since we cannot check it, we cannot tell if a word
99            // is out-of-vocabulary or not.  Therefore, we must judge the entire buffer contents to
100            // potentially pose a privacy risk.
101            return false;
102        }
103        // Reload the dictionary in case it has changed (e.g., because the user has changed
104        // languages).
105        final Dictionary dictionary = mSuggest.getMainDictionary();
106        if (dictionary == null) {
107            return false;
108        }
109        // Check each word in the buffer.  If any word poses a privacy threat, we cannot upload the
110        // complete buffer contents in detail.
111        final int length = mLogUnits.size();
112        for (int i = 0; i < length; i++) {
113            final LogUnit logUnit = mLogUnits.get(i);
114            final String word = logUnit.getWord();
115            if (word == null) {
116                // Digits outside words are a privacy threat.
117                if (logUnit.mayContainDigit()) {
118                    return false;
119                }
120            } else {
121                // Words not in the dictionary are a privacy threat.
122                if (ResearchLogger.hasLetters(word) && !(dictionary.isValidWord(word))) {
123                    if (DEBUG) {
124                        Log.d(TAG, "NOT SAFE!: hasLetters: " + ResearchLogger.hasLetters(word)
125                                + ", isValid: " + (dictionary.isValidWord(word)));
126                    }
127                    return false;
128                }
129            }
130        }
131        // All checks have passed; this buffer's content can be safely uploaded.
132        return true;
133    }
134
135    @Override
136    protected void onShiftOut(LogUnit logUnit) {
137        if (mResearchLog != null) {
138            mResearchLog.publish(logUnit, false /* isIncludingPrivateData */);
139        }
140    }
141}
142