1cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard/*
2cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard * Copyright (C) 2011 The Android Open Source Project
3cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard *
48aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * Licensed under the Apache License, Version 2.0 (the "License");
58aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * you may not use this file except in compliance with the License.
68aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * You may obtain a copy of the License at
7cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard *
88aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka *      http://www.apache.org/licenses/LICENSE-2.0
9cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard *
10cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard * Unless required by applicable law or agreed to in writing, software
118aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * distributed under the License is distributed on an "AS IS" BASIS,
128aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
138aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * See the License for the specific language governing permissions and
148aa9963a895f9dd5bb1bc92ab2e4f461e058f87aTadashi G. Takaoka * limitations under the License.
15cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard */
16cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard
17cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardpackage com.android.inputmethod.latin;
18cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard
191061bfdb34bbcb63bf0046eec42313d264ac33faJean Chalardimport com.android.inputmethod.dictionarypack.DictionaryPackConstants;
20e28eba5074664d5716b8e58b8d0a235746b261ebKen Wakasaimport com.android.inputmethod.latin.utils.TargetPackageInfoGetterTask;
210cc0544a2995c7eb54a830ae54db60af89d4073dJean Chalard
22cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.BroadcastReceiver;
23cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.Context;
24cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.Intent;
25cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.pm.PackageInfo;
26cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.pm.PackageManager;
27cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.pm.ProviderInfo;
28cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.net.Uri;
2976d5f512f99700a963aa20a02590833e37221bffJean Chalardimport android.util.Log;
30cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard
31cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard/**
3276d5f512f99700a963aa20a02590833e37221bffJean Chalard * Receives broadcasts pertaining to dictionary management and takes the appropriate action.
3376d5f512f99700a963aa20a02590833e37221bffJean Chalard *
3476d5f512f99700a963aa20a02590833e37221bffJean Chalard * This object receives three types of broadcasts.
3576d5f512f99700a963aa20a02590833e37221bffJean Chalard * - Package installed/added. When a dictionary provider application is added or removed, we
3676d5f512f99700a963aa20a02590833e37221bffJean Chalard * need to query the dictionaries.
3776d5f512f99700a963aa20a02590833e37221bffJean Chalard * - New dictionary broadcast. The dictionary provider broadcasts new dictionary availability. When
3876d5f512f99700a963aa20a02590833e37221bffJean Chalard * this happens, we need to re-query the dictionaries.
3976d5f512f99700a963aa20a02590833e37221bffJean Chalard * - Unknown client. If the dictionary provider is in urgent need of data about some client that
4076d5f512f99700a963aa20a02590833e37221bffJean Chalard * it does not know, it sends this broadcast. When we receive this, we need to tell the dictionary
4176d5f512f99700a963aa20a02590833e37221bffJean Chalard * provider about ourselves. This happens when the settings for the dictionary pack are accessed,
4276d5f512f99700a963aa20a02590833e37221bffJean Chalard * but Latin IME never got a chance to register itself.
43cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard */
44a28a05e971cc242b338331a3b78276fa95188d19Tadashi G. Takaokapublic final class DictionaryPackInstallBroadcastReceiver extends BroadcastReceiver {
4576d5f512f99700a963aa20a02590833e37221bffJean Chalard    private static final String TAG = DictionaryPackInstallBroadcastReceiver.class.getSimpleName();
46cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard
47cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard    final LatinIME mService;
48cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard
4976d5f512f99700a963aa20a02590833e37221bffJean Chalard    public DictionaryPackInstallBroadcastReceiver() {
5076d5f512f99700a963aa20a02590833e37221bffJean Chalard        // This empty constructor is necessary for the system to instantiate this receiver.
5176d5f512f99700a963aa20a02590833e37221bffJean Chalard        // This happens when the dictionary pack says it can't find a record for our client,
5276d5f512f99700a963aa20a02590833e37221bffJean Chalard        // which happens when the dictionary pack settings are called before the keyboard
5376d5f512f99700a963aa20a02590833e37221bffJean Chalard        // was ever started once.
5476d5f512f99700a963aa20a02590833e37221bffJean Chalard        Log.i(TAG, "Latin IME dictionary broadcast receiver instantiated from the framework.");
5576d5f512f99700a963aa20a02590833e37221bffJean Chalard        mService = null;
5676d5f512f99700a963aa20a02590833e37221bffJean Chalard    }
5776d5f512f99700a963aa20a02590833e37221bffJean Chalard
58cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard    public DictionaryPackInstallBroadcastReceiver(final LatinIME service) {
59cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard        mService = service;
60cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard    }
61cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard
62cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard    @Override
63cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard    public void onReceive(Context context, Intent intent) {
64cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard        final String action = intent.getAction();
65cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard        final PackageManager manager = context.getPackageManager();
66cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard
67cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard        // We need to reread the dictionary if a new dictionary package is installed.
68cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard        if (action.equals(Intent.ACTION_PACKAGE_ADDED)) {
6976d5f512f99700a963aa20a02590833e37221bffJean Chalard            if (null == mService) {
7076d5f512f99700a963aa20a02590833e37221bffJean Chalard                Log.e(TAG, "Called with intent " + action + " but we don't know the service: this "
7176d5f512f99700a963aa20a02590833e37221bffJean Chalard                        + "should never happen");
7276d5f512f99700a963aa20a02590833e37221bffJean Chalard                return;
7376d5f512f99700a963aa20a02590833e37221bffJean Chalard            }
74cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            final Uri packageUri = intent.getData();
75cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            if (null == packageUri) return; // No package name : we can't do anything
76cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            final String packageName = packageUri.getSchemeSpecificPart();
77cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            if (null == packageName) return;
78eb696d288ea19d3d64f932fd017da7d7187ba6e1Jean Chalard            // TODO: do this in a more appropriate place
797058b02a9c798c21b169b778be2befc7739f4e9bJean Chalard            TargetPackageInfoGetterTask.removeCachedPackageInfo(packageName);
80cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            final PackageInfo packageInfo;
81cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            try {
82cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard                packageInfo = manager.getPackageInfo(packageName, PackageManager.GET_PROVIDERS);
83cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            } catch (android.content.pm.PackageManager.NameNotFoundException e) {
84cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard                return; // No package info : we can't do anything
85cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            }
86cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            final ProviderInfo[] providers = packageInfo.providers;
87cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            if (null == providers) return; // No providers : it is not a dictionary.
88cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard
89cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // Search for some dictionary pack in the just-installed package. If found, reread.
90cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            for (ProviderInfo info : providers) {
911061bfdb34bbcb63bf0046eec42313d264ac33faJean Chalard                if (DictionaryPackConstants.AUTHORITY.equals(info.authority)) {
92cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard                    mService.resetSuggestMainDict();
93cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard                    return;
94cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard                }
95cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            }
96cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // If we come here none of the authorities matched the one we searched for.
97cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // We can exit safely.
98cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            return;
99cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard        } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
100cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard                && !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
10176d5f512f99700a963aa20a02590833e37221bffJean Chalard            if (null == mService) {
10276d5f512f99700a963aa20a02590833e37221bffJean Chalard                Log.e(TAG, "Called with intent " + action + " but we don't know the service: this "
10376d5f512f99700a963aa20a02590833e37221bffJean Chalard                        + "should never happen");
10476d5f512f99700a963aa20a02590833e37221bffJean Chalard                return;
10576d5f512f99700a963aa20a02590833e37221bffJean Chalard            }
106cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // When the dictionary package is removed, we need to reread dictionary (to use the
107cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // next-priority one, or stop using a dictionary at all if this was the only one,
108cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // since this is the user request).
109cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // If we are replacing the package, we will receive ADDED right away so no need to
110cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // remove the dictionary at the moment, since we will do it when we receive the
111cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // ADDED broadcast.
112cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard
113cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // TODO: Only reload dictionary on REMOVED when the removed package is the one we
114cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            // read dictionary from?
115cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard            mService.resetSuggestMainDict();
1161061bfdb34bbcb63bf0046eec42313d264ac33faJean Chalard        } else if (action.equals(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION)) {
11776d5f512f99700a963aa20a02590833e37221bffJean Chalard            if (null == mService) {
11876d5f512f99700a963aa20a02590833e37221bffJean Chalard                Log.e(TAG, "Called with intent " + action + " but we don't know the service: this "
11976d5f512f99700a963aa20a02590833e37221bffJean Chalard                        + "should never happen");
12076d5f512f99700a963aa20a02590833e37221bffJean Chalard                return;
12176d5f512f99700a963aa20a02590833e37221bffJean Chalard            }
122646d950ed8d2b2555df92855e18b350fd7761b21Jean Chalard            mService.resetSuggestMainDict();
12376d5f512f99700a963aa20a02590833e37221bffJean Chalard        } else if (action.equals(DictionaryPackConstants.UNKNOWN_DICTIONARY_PROVIDER_CLIENT)) {
12476d5f512f99700a963aa20a02590833e37221bffJean Chalard            if (null != mService) {
12576d5f512f99700a963aa20a02590833e37221bffJean Chalard                // Careful! This is returning if the service is NOT null. This is because we
12676d5f512f99700a963aa20a02590833e37221bffJean Chalard                // should come here instantiated by the framework in reaction to a broadcast of
12776d5f512f99700a963aa20a02590833e37221bffJean Chalard                // the above action, so we should gave gone through the no-args constructor.
12876d5f512f99700a963aa20a02590833e37221bffJean Chalard                Log.e(TAG, "Called with intent " + action + " but we have a reference to the "
12976d5f512f99700a963aa20a02590833e37221bffJean Chalard                        + "service: this should never happen");
13076d5f512f99700a963aa20a02590833e37221bffJean Chalard                return;
13176d5f512f99700a963aa20a02590833e37221bffJean Chalard            }
13276d5f512f99700a963aa20a02590833e37221bffJean Chalard            // The dictionary provider does not know about some client. We check that it's really
13376d5f512f99700a963aa20a02590833e37221bffJean Chalard            // us that it needs to know about, and if it's the case, we register with the provider.
13476d5f512f99700a963aa20a02590833e37221bffJean Chalard            final String wantedClientId =
13576d5f512f99700a963aa20a02590833e37221bffJean Chalard                    intent.getStringExtra(DictionaryPackConstants.DICTIONARY_PROVIDER_CLIENT_EXTRA);
13676d5f512f99700a963aa20a02590833e37221bffJean Chalard            final String myClientId = context.getString(R.string.dictionary_pack_client_id);
13776d5f512f99700a963aa20a02590833e37221bffJean Chalard            if (!wantedClientId.equals(myClientId)) return; // Not for us
13876d5f512f99700a963aa20a02590833e37221bffJean Chalard            BinaryDictionaryFileDumper.initializeClientRecordHelper(context, myClientId);
139cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard        }
140cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard    }
141cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard}
142