SystemBroadcastReceiver.java revision 052ec62abd577182af8d5b50564d8075b18be3c9
1/*
2 * Copyright (C) 2014 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
17package com.android.inputmethod.latin;
18
19import android.content.BroadcastReceiver;
20import android.content.ComponentName;
21import android.content.Context;
22import android.content.Intent;
23import android.content.SharedPreferences;
24import android.content.pm.PackageManager;
25import android.os.Process;
26import android.preference.PreferenceManager;
27import android.util.Log;
28import android.view.inputmethod.InputMethodManager;
29import android.view.inputmethod.InputMethodSubtype;
30
31import com.android.inputmethod.compat.IntentCompatUtils;
32import com.android.inputmethod.latin.settings.Settings;
33import com.android.inputmethod.latin.setup.LauncherIconVisibilityManager;
34import com.android.inputmethod.latin.setup.SetupActivity;
35import com.android.inputmethod.latin.utils.UncachedInputMethodManagerUtils;
36
37/**
38 * This class detects the {@link Intent#ACTION_MY_PACKAGE_REPLACED} broadcast intent when this IME
39 * package has been replaced by a newer version of the same package. This class also detects
40 * {@link Intent#ACTION_BOOT_COMPLETED} and {@link Intent#ACTION_USER_INITIALIZE} broadcast intent.
41 *
42 * If this IME has already been installed in the system image and a new version of this IME has
43 * been installed, {@link Intent#ACTION_MY_PACKAGE_REPLACED} is received by this receiver and it
44 * will hide the setup wizard's icon.
45 *
46 * If this IME has already been installed in the data partition and a new version of this IME has
47 * been installed, {@link Intent#ACTION_MY_PACKAGE_REPLACED} is received by this receiver but it
48 * will not hide the setup wizard's icon, and the icon will appear on the launcher.
49 *
50 * If this IME hasn't been installed yet and has been newly installed, no
51 * {@link Intent#ACTION_MY_PACKAGE_REPLACED} will be sent and the setup wizard's icon will appear
52 * on the launcher.
53 *
54 * When the device has been booted, {@link Intent#ACTION_BOOT_COMPLETED} is received by this
55 * receiver and it checks whether the setup wizard's icon should be appeared or not on the launcher
56 * depending on which partition this IME is installed.
57 *
58 * When a multiuser account has been created, {@link Intent#ACTION_USER_INITIALIZE} is received
59 * by this receiver and it checks the whether the setup wizard's icon should be appeared or not on
60 * the launcher depending on which partition this IME is installed.
61 */
62public final class SystemBroadcastReceiver extends BroadcastReceiver {
63    private static final String TAG = SystemBroadcastReceiver.class.getSimpleName();
64
65    @Override
66    public void onReceive(final Context context, final Intent intent) {
67        final String intentAction = intent.getAction();
68        if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(intentAction)) {
69            Log.i(TAG, "Package has been replaced: " + context.getPackageName());
70        } else if (Intent.ACTION_BOOT_COMPLETED.equals(intentAction)) {
71            Log.i(TAG, "Boot has been completed");
72        } else if (IntentCompatUtils.is_ACTION_USER_INITIALIZE(intentAction)) {
73            Log.i(TAG, "User initialize");
74        }
75
76        LauncherIconVisibilityManager.onReceiveGlobalIntent(intentAction, context);
77
78        if (Intent.ACTION_MY_PACKAGE_REPLACED.equals(intentAction)) {
79            // Need to restore additional subtypes because system always clears additional
80            // subtypes when the package is replaced.
81            RichInputMethodManager.init(context);
82            final RichInputMethodManager richImm = RichInputMethodManager.getInstance();
83            final InputMethodSubtype[] additionalSubtypes = richImm.getAdditionalSubtypes(context);
84            richImm.setAdditionalInputMethodSubtypes(additionalSubtypes);
85        }
86
87        // The process that hosts this broadcast receiver is invoked and remains alive even after
88        // 1) the package has been re-installed, 2) the device has just booted,
89        // 3) a new user has been created.
90        // There is no good reason to keep the process alive if this IME isn't a current IME.
91        final InputMethodManager imm =
92                (InputMethodManager)context.getSystemService(Context.INPUT_METHOD_SERVICE);
93        // Called to check whether this IME has been triggered by the current user or not
94        final boolean isInputMethodManagerValidForUserOfThisProcess =
95                !imm.getInputMethodList().isEmpty();
96        final boolean isCurrentImeOfCurrentUser = isInputMethodManagerValidForUserOfThisProcess
97                && UncachedInputMethodManagerUtils.isThisImeCurrent(context, imm);
98        if (!isCurrentImeOfCurrentUser) {
99            final int myPid = Process.myPid();
100            Log.i(TAG, "Killing my process: pid=" + myPid);
101            Process.killProcess(myPid);
102        }
103    }
104}
105