Home | History | Annotate | Download | only in setup
      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 
     29 import com.android.inputmethod.compat.IntentCompatUtils;
     30 import com.android.inputmethod.latin.RichInputMethodManager;
     31 import com.android.inputmethod.latin.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 been booted,
     69         // 3) a multiuser has been created.
     70         // There is no good reason to keep the process alive if this IME isn't a current IME.
     71         final boolean isCurrentImeOfCurrentUser;
     72         if (RichInputMethodManager.isInputMethodManagerValidForUserOfThisProcess(context)) {
     73             RichInputMethodManager.init(context);
     74             isCurrentImeOfCurrentUser = SetupActivity.isThisImeCurrent(context);
     75         } else {
     76             isCurrentImeOfCurrentUser = false;
     77         }
     78 
     79         if (!isCurrentImeOfCurrentUser) {
     80             final int myPid = Process.myPid();
     81             Log.i(TAG, "Killing my process: pid=" + myPid);
     82             Process.killProcess(myPid);
     83         }
     84     }
     85 
     86     private static boolean shouldHandleThisIntent(final Intent intent, final Context context) {
     87         final String action = intent.getAction();
     88         if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(action)) {
     89             Log.i(TAG, "Package has been replaced: " + context.getPackageName());
     90             return true;
     91         } else if (Intent.ACTION_BOOT_COMPLETED.equals(action)) {
     92             Log.i(TAG, "Boot has been completed");
     93             return true;
     94         } else if (IntentCompatUtils.has_ACTION_USER_INITIALIZE(intent)) {
     95             Log.i(TAG, "User initialize");
     96             return true;
     97         }
     98         return false;
     99     }
    100 
    101     public static void updateSetupWizardIconVisibility(final Context context) {
    102         final ComponentName setupWizardActivity = new ComponentName(context, SetupActivity.class);
    103         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
    104         final boolean stateHasSet;
    105         if (Settings.readShowSetupWizardIcon(prefs, context)) {
    106             stateHasSet = setActivityState(context, setupWizardActivity,
    107                     PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
    108             Log.i(TAG, (stateHasSet ? "Enable activity: " : "Activity has already been enabled: ")
    109                     + setupWizardActivity);
    110         } else {
    111             stateHasSet = setActivityState(context, setupWizardActivity,
    112                     PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
    113             Log.i(TAG, (stateHasSet ? "Disable activity: " : "Activity has already been disabled: ")
    114                     + setupWizardActivity);
    115         }
    116     }
    117 
    118     private static boolean setActivityState(final Context context,
    119             final ComponentName activityComponent, final int activityState) {
    120         final PackageManager pm = context.getPackageManager();
    121         final int activityComponentState = pm.getComponentEnabledSetting(activityComponent);
    122         if (activityComponentState == activityState) {
    123             return false;
    124         }
    125         pm.setComponentEnabledSetting(
    126                 activityComponent, activityState, PackageManager.DONT_KILL_APP);
    127         return true;
    128     }
    129 }
    130