diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/LatinIME.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/LatinIME.java index bb168e0df..4e2f52edf 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/LatinIME.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/LatinIME.java @@ -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(); } diff --git a/app/src/main/java/org/dslul/openboard/inputmethod/latin/utils/DeviceProtectedUtils.java b/app/src/main/java/org/dslul/openboard/inputmethod/latin/utils/DeviceProtectedUtils.java index 02a2071c5..18e5d9457 100644 --- a/app/src/main/java/org/dslul/openboard/inputmethod/latin/utils/DeviceProtectedUtils.java +++ b/app/src/main/java/org/dslul/openboard/inputmethod/latin/utils/DeviceProtectedUtils.java @@ -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();