Make DropBox.addText AsyncTask in IMELogger
Change-Id: I84d5915a3197a67cda6bec8b0ef3343df2e510bemain
parent
b373d460c7
commit
9512706adf
|
@ -373,6 +373,7 @@ public class LatinIME extends InputMethodService
|
|||
mVoiceInput.destroy();
|
||||
}
|
||||
LatinImeLogger.commit();
|
||||
LatinImeLogger.onDestroy();
|
||||
super.onDestroy();
|
||||
}
|
||||
|
||||
|
|
|
@ -16,10 +16,25 @@
|
|||
|
||||
package com.android.inputmethod.latin;
|
||||
|
||||
import android.os.AsyncTask;
|
||||
import android.text.format.DateUtils;
|
||||
import android.util.Log;
|
||||
|
||||
public class LatinIMEUtil {
|
||||
|
||||
/**
|
||||
* Cancel an {@link AsyncTask}.
|
||||
*
|
||||
* @param mayInterruptIfRunning <tt>true</tt> if the thread executing this
|
||||
* task should be interrupted; otherwise, in-progress tasks are allowed
|
||||
* to complete.
|
||||
*/
|
||||
public static void cancelTask(AsyncTask<?, ?, ?> task, boolean mayInterruptIfRunning) {
|
||||
if (task != null && task.getStatus() != AsyncTask.Status.FINISHED) {
|
||||
task.cancel(mayInterruptIfRunning);
|
||||
}
|
||||
}
|
||||
|
||||
public static class GCUtils {
|
||||
private static final String TAG = "GCUtils";
|
||||
public static final int GC_TRY_COUNT = 2;
|
||||
|
|
|
@ -20,6 +20,7 @@ import android.content.Context;
|
|||
import android.content.SharedPreferences;
|
||||
import android.content.pm.PackageInfo;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.os.AsyncTask;
|
||||
import android.os.DropBoxManager;
|
||||
import android.preference.PreferenceManager;
|
||||
import android.text.TextUtils;
|
||||
|
@ -88,6 +89,7 @@ public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChang
|
|||
|
||||
private Context mContext = null;
|
||||
private DropBoxManager mDropBox = null;
|
||||
private AddTextToDropBoxTask mAddTextToDropBoxTask;
|
||||
private long mLastTimeActive;
|
||||
private long mLastTimeSend;
|
||||
private long mLastTimeCountEntry;
|
||||
|
@ -126,6 +128,29 @@ public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChang
|
|||
}
|
||||
}
|
||||
|
||||
private class AddTextToDropBoxTask extends AsyncTask<Void, Void, Void> {
|
||||
private final DropBoxManager mDropBox;
|
||||
private final long mTime;
|
||||
private final String mData;
|
||||
public AddTextToDropBoxTask(DropBoxManager db, long time, String data) {
|
||||
mDropBox = db;
|
||||
mTime = time;
|
||||
mData = data;
|
||||
}
|
||||
@Override
|
||||
protected Void doInBackground(Void... params) {
|
||||
if (sLOGPRINT) {
|
||||
Log.d(TAG, "Commit log: " + mData);
|
||||
}
|
||||
mDropBox.addText(TAG, mData);
|
||||
return null;
|
||||
}
|
||||
@Override
|
||||
protected void onPostExecute(Void v) {
|
||||
mLastTimeSend = mTime;
|
||||
}
|
||||
}
|
||||
|
||||
private void initInternal(Context context) {
|
||||
mContext = context;
|
||||
mDropBox = (DropBoxManager) mContext.getSystemService(Context.DROPBOX_SERVICE);
|
||||
|
@ -169,6 +194,10 @@ public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChang
|
|||
mRingCharBuffer.reset();
|
||||
}
|
||||
|
||||
public void destroy() {
|
||||
LatinIMEUtil.cancelTask(mAddTextToDropBoxTask, false);
|
||||
}
|
||||
|
||||
/**
|
||||
* Check if the input string is safe as an entry or not.
|
||||
*/
|
||||
|
@ -374,26 +403,25 @@ public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChang
|
|||
}
|
||||
|
||||
private void commitInternal() {
|
||||
if (sLOGPRINT) {
|
||||
Log.d(TAG, "Commit (" + mLogBuffer.size() + ")");
|
||||
}
|
||||
flushPrivacyLogSafely();
|
||||
long now = System.currentTimeMillis();
|
||||
addCountEntry(now);
|
||||
addThemeIdEntry(now);
|
||||
addLanguagesEntry(now);
|
||||
addSettingsEntry(now);
|
||||
addVersionNameEntry(now);
|
||||
addSuggestionCountEntry(now);
|
||||
String s = LogSerializer.createStringFromEntries(mLogBuffer);
|
||||
if (!TextUtils.isEmpty(s)) {
|
||||
// if there is no log entry in mLogBuffer, will not send logs to DropBox.
|
||||
if (!mLogBuffer.isEmpty() && (mAddTextToDropBoxTask == null
|
||||
|| mAddTextToDropBoxTask.getStatus() == AsyncTask.Status.FINISHED)) {
|
||||
if (sLOGPRINT) {
|
||||
Log.d(TAG, "Commit log: " + s);
|
||||
Log.d(TAG, "Commit (" + mLogBuffer.size() + ")");
|
||||
}
|
||||
mDropBox.addText(TAG, s);
|
||||
flushPrivacyLogSafely();
|
||||
long now = System.currentTimeMillis();
|
||||
addCountEntry(now);
|
||||
addThemeIdEntry(now);
|
||||
addLanguagesEntry(now);
|
||||
addSettingsEntry(now);
|
||||
addVersionNameEntry(now);
|
||||
addSuggestionCountEntry(now);
|
||||
String s = LogSerializer.createStringFromEntries(mLogBuffer);
|
||||
reset();
|
||||
mAddTextToDropBoxTask = (AddTextToDropBoxTask) new AddTextToDropBoxTask(
|
||||
mDropBox, now, s).execute();
|
||||
}
|
||||
reset();
|
||||
mLastTimeSend = now;
|
||||
}
|
||||
|
||||
private void commitInternalAndStopSelf() {
|
||||
|
@ -473,6 +501,11 @@ public class LatinImeLogger implements SharedPreferences.OnSharedPreferenceChang
|
|||
}
|
||||
}
|
||||
|
||||
public static void onDestroy() {
|
||||
sLatinImeLogger.commitInternal();
|
||||
sLatinImeLogger.destroy();
|
||||
}
|
||||
|
||||
// TODO: Handle CharSequence instead of String
|
||||
public static void logOnManualSuggestion(String before, String after, int position
|
||||
, List<CharSequence> suggestions) {
|
||||
|
|
Loading…
Reference in New Issue