• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.ComponentName;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.SharedPreferences;
23 import android.content.pm.PackageManager;
24 import android.preference.PreferenceManager;
25 import android.util.Log;
26 
27 import com.android.inputmethod.latin.settings.Settings;
28 
29 /**
30  * This class handles the {@link Intent#ACTION_MY_PACKAGE_REPLACED} broadcast intent when this IME
31  * package has been replaced by a newer version of the same package. This class also handles
32  * {@link Intent#ACTION_BOOT_COMPLETED} and {@link Intent#ACTION_USER_INITIALIZE} broadcast intent.
33  *
34  * If this IME has already been installed in the system image and a new version of this IME has
35  * been installed, {@link Intent#ACTION_MY_PACKAGE_REPLACED} is received to this class to hide the
36  * setup wizard's icon.
37  *
38  * If this IME has already been installed in the data partition and a new version of this IME has
39  * been installed, {@link Intent#ACTION_MY_PACKAGE_REPLACED} is forwarded to this class but it
40  * will not hide the setup wizard's icon, and the icon will appear on the launcher.
41  *
42  * If this IME hasn't been installed yet and has been newly installed, no
43  * {@link Intent#ACTION_MY_PACKAGE_REPLACED} will be sent and the setup wizard's icon will appear
44  * on the launcher.
45  *
46  * When the device has been booted, {@link Intent#ACTION_BOOT_COMPLETED} is forwarded to this class
47  * to check whether the setup wizard's icon should be appeared or not on the launcher
48  * depending on which partition this IME is installed.
49  *
50  * When a multiuser account has been created, {@link Intent#ACTION_USER_INITIALIZE} is forwarded to
51  * this class to check 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 public final class LauncherIconVisibilityManager {
55     private static final String TAG = LauncherIconVisibilityManager.class.getSimpleName();
56 
updateSetupWizardIconVisibility(final Context context)57     public static void updateSetupWizardIconVisibility(final Context context) {
58         final ComponentName setupWizardActivity = new ComponentName(context, SetupActivity.class);
59         final SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context);
60         final boolean stateHasSet;
61         if (Settings.readShowSetupWizardIcon(prefs, context)) {
62             stateHasSet = setActivityState(context, setupWizardActivity,
63                     PackageManager.COMPONENT_ENABLED_STATE_ENABLED);
64             Log.i(TAG, (stateHasSet ? "Enable activity: " : "Activity has already been enabled: ")
65                     + setupWizardActivity);
66         } else {
67             stateHasSet = setActivityState(context, setupWizardActivity,
68                     PackageManager.COMPONENT_ENABLED_STATE_DISABLED);
69             Log.i(TAG, (stateHasSet ? "Disable activity: " : "Activity has already been disabled: ")
70                     + setupWizardActivity);
71         }
72     }
73 
setActivityState(final Context context, final ComponentName activityComponent, final int activityState)74     private static boolean setActivityState(final Context context,
75             final ComponentName activityComponent, final int activityState) {
76         final PackageManager pm = context.getPackageManager();
77         final int activityComponentState = pm.getComponentEnabledSetting(activityComponent);
78         if (activityComponentState == activityState) {
79             return false;
80         }
81         pm.setComponentEnabledSetting(
82                 activityComponent, activityState, PackageManager.DONT_KILL_APP);
83         return true;
84     }
85 }
86