Fix dictionary persistence with Direct Boot

Trigger app restart when credential-encrypted storage is unlocked, to refresh dictionary from credential encrypted storage. This fixes an issue where learned dictionary and frequencies are reset after device restart.
This commit is contained in:
Trevor Terris 2021-08-12 13:59:57 -04:00
parent cf45b5b907
commit 1fed180950
2 changed files with 32 additions and 0 deletions

View file

@ -32,6 +32,7 @@ import android.os.Build;
import android.os.Debug;
import android.os.IBinder;
import android.os.Message;
import android.os.Process;
import android.text.InputType;
import android.util.Log;
import android.util.PrintWriterPrinter;
@ -186,6 +187,24 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
}
final HideSoftInputReceiver mHideSoftInputReceiver = new HideSoftInputReceiver(this);
final static class RestartAfterDeviceUnlockReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
// Restart the keyboard if credential encrypted storage is unlocked. This reloads the
// dictionary and other data from credential-encrypted storage (with the onCreate()
// method).
if (Intent.ACTION_USER_UNLOCKED.equals(action)) {
final int myPid = Process.myPid();
Log.i(TAG, "Killing my process: pid=" + myPid);
Process.killProcess(myPid);
} else {
Log.e(TAG, "Unexpected intent " + intent);
}
}
}
final RestartAfterDeviceUnlockReceiver mRestartAfterDeviceUnlockReceiver = new RestartAfterDeviceUnlockReceiver();
private AlertDialog mOptionsDialog;
private final boolean mIsHardwareAcceleratedDrawingEnabled;
@ -625,6 +644,10 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
registerReceiver(mHideSoftInputReceiver, hideSoftInputFilter, PERMISSION_HIDE_SOFT_INPUT,
null /* scheduler */);
final IntentFilter restartAfterUnlockFilter = new IntentFilter();
restartAfterUnlockFilter.addAction(Intent.ACTION_USER_UNLOCKED);
registerReceiver(mRestartAfterDeviceUnlockReceiver, restartAfterUnlockFilter);
StatsUtils.onCreate(mSettings.getCurrent(), mRichImm);
}
@ -733,6 +756,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
unregisterReceiver(mRingerModeChangeReceiver);
unregisterReceiver(mDictionaryPackInstallReceiver);
unregisterReceiver(mDictionaryDumpBroadcastReceiver);
unregisterReceiver(mRestartAfterDeviceUnlockReceiver);
mStatsUtilsManager.onDestroy(this /* context */);
super.onDestroy();
}
@ -742,6 +766,7 @@ public class LatinIME extends InputMethodService implements KeyboardActionListen
unregisterReceiver(mDictionaryPackInstallReceiver);
unregisterReceiver(mDictionaryDumpBroadcastReceiver);
unregisterReceiver(mRingerModeChangeReceiver);
unregisterReceiver(mRestartAfterDeviceUnlockReceiver);
mInputLogic.recycle();
}

View file

@ -18,14 +18,20 @@ package org.dslul.openboard.inputmethod.latin.utils;
import android.content.Context;
import android.content.SharedPreferences;
import android.os.Build;
import android.preference.PreferenceManager;
import android.util.Log;
import androidx.annotation.RequiresApi;
public final class DeviceProtectedUtils {
static final String TAG = DeviceProtectedUtils.class.getSimpleName();
public static SharedPreferences getSharedPreferences(final Context context) {
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
return PreferenceManager.getDefaultSharedPreferences(context);
}
Context deviceProtectedContext = getDeviceProtectedContext(context);
SharedPreferences deviceProtectedPreferences = PreferenceManager.getDefaultSharedPreferences(deviceProtectedContext);
if (deviceProtectedPreferences.getAll().isEmpty()) {
@ -35,6 +41,7 @@ public final class DeviceProtectedUtils {
return deviceProtectedPreferences;
}
@RequiresApi(api = Build.VERSION_CODES.N)
private static Context getDeviceProtectedContext(final Context context) {
return context.isDeviceProtectedStorage()
? context : context.createDeviceProtectedStorageContext();