1 /* 2 * Copyright (C) 2013 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.latin.setup; 18 19 import android.content.BroadcastReceiver; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.SharedPreferences; 24 import android.content.pm.PackageManager; 25 import android.os.Process; 26 import android.preference.PreferenceManager; 27 import android.util.Log; 28 import android.view.inputmethod.InputMethodManager; 29 30 import com.android.inputmethod.compat.IntentCompatUtils; 31 import com.android.inputmethod.latin.settings.Settings; 32 33 /** 34 * This class detects the {@link Intent#ACTION_MY_PACKAGE_REPLACED} broadcast intent when this IME 35 * package has been replaced by a newer version of the same package. This class also detects 36 * {@link Intent#ACTION_BOOT_COMPLETED} and {@link Intent#ACTION_USER_INITIALIZE} broadcast intent. 37 * 38 * If this IME has already been installed in the system image and a new version of this IME has 39 * been installed, {@link Intent#ACTION_MY_PACKAGE_REPLACED} is received by this receiver and it 40 * will hide the setup wizard's icon. 41 * 42 * If this IME has already been installed in the data partition and a new version of this IME has 43 * been installed, {@link Intent#ACTION_MY_PACKAGE_REPLACED} is received by this receiver but it 44 * will not hide the setup wizard's icon, and the icon will appear on the launcher. 45 * 46 * If this IME hasn't been installed yet and has been newly installed, no 47 * {@link Intent#ACTION_MY_PACKAGE_REPLACED} will be sent and the setup wizard's icon will appear 48 * on the launcher. 49 * 50 * When the device has been booted, {@link Intent#ACTION_BOOT_COMPLETED} is received by this 51 * receiver and it checks whether the setup wizard's icon should be appeared or not on the launcher 52 * depending on which partition this IME is installed. 53 * 54 * When a multiuser account has been created, {@link Intent#ACTION_USER_INITIALIZE} is received 55 * by this receiver and it checks the whether the setup wizard's icon should be appeared or not on 56 * the launcher depending on which partition this IME is installed. 57 */ 58 public final class LauncherIconVisibilityManager extends BroadcastReceiver { 59 private static final String TAG = LauncherIconVisibilityManager.class.getSimpleName(); 60 61 @Override 62 public void onReceive(final Context context, final Intent intent) { 63 if (shouldHandleThisIntent(intent, context)) { 64 updateSetupWizardIconVisibility(context); 65 } 66 67 // The process that hosts this broadcast receiver is invoked and remains alive even after 68 // 1) the package has been re-installed, 2) the device has just booted, 69 // 3) a new user has been created. 70 // There is no good reason to keep the process alive if this IME isn't a current IME. 71 final InputMethodManager imm = 72 (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE); 73 // Called to check whether this IME has been triggered by the current user or not 74 final boolean isInputMethodManagerValidForUserOfThisProcess = 75 !imm.getInputMethodList().isEmpty(); 76 final boolean isCurrentImeOfCurrentUser = isInputMethodManagerValidForUserOfThisProcess 77 && SetupActivity.isThisImeCurrent(context, imm); 78 if (!isCurrentImeOfCurrentUser) { 79 final int myPid = Process.myPid(); 80 Log.i(TAG, "Killing my process: pid=" + myPid); 81 Process.killProcess(myPid); 82 } 83 } 84 85 private static boolean shouldHandleThisIntent(final Intent intent, final Context context) { 86 final String action = intent.getAction(); 87 if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)) { 88 Log.i(TAG, "Package has been replaced: " + context.getPackageName()); 89 return true; 90 } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) { 91 Log.i(TAG, "Boot has been completed"); 92 return true; 93 } else if (IntentCompatUtils.is_ACTION_USER_INITIALIZE(action)) { 94 Log.i(TAG, "User initialize"); 95 return true; 96 } 97 return false; 98 } 99 100 public static void updateSetupWizardIconVisibility(final Context context) { 101 final ComponentName setupWizardActivity = new ComponentName(context, SetupActivity.class); 102 final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); 103 final boolean stateHasSet; 104 if (Settings.readShowSetupWizardIcon(prefs, context)) { 105 stateHasSet = setActivityState(context, setupWizardActivity, 106 PackageManager.COMPONENT_ENABLED_STATE_ENABLED); 107 Log.i(TAG, (stateHasSet ? "Enable activity: " : "Activity has already been enabled: ") 108 + setupWizardActivity); 109 } else { 110 stateHasSet = setActivityState(context, setupWizardActivity, 111 PackageManager.COMPONENT_ENABLED_STATE_DISABLED); 112 Log.i(TAG, (stateHasSet ? "Disable activity: " : "Activity has already been disabled: ") 113 + setupWizardActivity); 114 } 115 } 116 117 private static boolean setActivityState(final Context context, 118 final ComponentName activityComponent, final int activityState) { 119 final PackageManager pm = context.getPackageManager(); 120 final int activityComponentState = pm.getComponentEnabledSetting(activityComponent); 121 if (activityComponentState == activityState) { 122 return false; 123 } 124 pm.setComponentEnabledSetting( 125 activityComponent, activityState, PackageManager.DONT_KILL_APP); 126 return true; 127 } 128 } 129