DictionaryPackInstallBroadcastReceiver.java revision cba93f50c3d46ada773ec49435689dc3e2094385
1/*
2 * Copyright (C) 2011 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5 * use this file except in compliance with the License. You may obtain a copy of
6 * 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, WITHOUT
12 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13 * License for the specific language governing permissions and limitations under
14 * the License.
15 */
16
17package com.android.inputmethod.latin;
18
19import android.content.BroadcastReceiver;
20import android.content.Context;
21import android.content.Intent;
22import android.content.pm.PackageInfo;
23import android.content.pm.PackageManager;
24import android.content.pm.ProviderInfo;
25import android.net.Uri;
26
27/**
28 * Takes action to reload the necessary data when a dictionary pack was added/removed.
29 */
30public class DictionaryPackInstallBroadcastReceiver extends BroadcastReceiver {
31
32    final LatinIME mService;
33
34    public DictionaryPackInstallBroadcastReceiver(final LatinIME service) {
35        mService = service;
36    }
37
38    @Override
39    public void onReceive(Context context, Intent intent) {
40        final String action = intent.getAction();
41        final PackageManager manager = context.getPackageManager();
42
43        // We need to reread the dictionary if a new dictionary package is installed.
44        if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
45            final Uri packageUri = intent.getData();
46            if (null == packageUri) return; // No package name : we can't do anything
47            final String packageName = packageUri.getSchemeSpecificPart();
48            if (null == packageName) return;
49            final PackageInfo packageInfo;
50            try {
51                packageInfo = manager.getPackageInfo(packageName, PackageManager.GET_PROVIDERS);
52            } catch (android.content.pm.PackageManager.NameNotFoundException e) {
53                return; // No package info : we can't do anything
54            }
55            final ProviderInfo[] providers = packageInfo.providers;
56            if (null == providers) return; // No providers : it is not a dictionary.
57
58            // Search for some dictionary pack in the just-installed package. If found, reread.
59            boolean found = false;
60            for (ProviderInfo info : providers) {
61                if (BinaryDictionary.DICTIONARY_PACK_AUTHORITY.equals(info.authority)) {
62                    mService.resetSuggestMainDict();
63                    return;
64                }
65            }
66            // If we come here none of the authorities matched the one we searched for.
67            // We can exit safely.
68            return;
69        } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
70                && !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
71            // When the dictionary package is removed, we need to reread dictionary (to use the
72            // next-priority one, or stop using a dictionary at all if this was the only one,
73            // since this is the user request).
74            // If we are replacing the package, we will receive ADDED right away so no need to
75            // remove the dictionary at the moment, since we will do it when we receive the
76            // ADDED broadcast.
77
78            // TODO: Only reload dictionary on REMOVED when the removed package is the one we
79            // read dictionary from?
80            mService.resetSuggestMainDict();
81        }
82    }
83}
84