DictionaryPackInstallBroadcastReceiver.java revision 76d5f512f99700a963aa20a02590833e37221bff
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; 200cc0544a2995c7eb54a830ae54db60af89d4073dJean Chalard 21cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.BroadcastReceiver; 22cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.Context; 23cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.Intent; 24cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.pm.PackageInfo; 25cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.pm.PackageManager; 26cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.content.pm.ProviderInfo; 27cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalardimport android.net.Uri; 2876d5f512f99700a963aa20a02590833e37221bffJean Chalardimport android.util.Log; 29cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard 30cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard/** 3176d5f512f99700a963aa20a02590833e37221bffJean Chalard * Receives broadcasts pertaining to dictionary management and takes the appropriate action. 3276d5f512f99700a963aa20a02590833e37221bffJean Chalard * 3376d5f512f99700a963aa20a02590833e37221bffJean Chalard * This object receives three types of broadcasts. 3476d5f512f99700a963aa20a02590833e37221bffJean Chalard * - Package installed/added. When a dictionary provider application is added or removed, we 3576d5f512f99700a963aa20a02590833e37221bffJean Chalard * need to query the dictionaries. 3676d5f512f99700a963aa20a02590833e37221bffJean Chalard * - New dictionary broadcast. The dictionary provider broadcasts new dictionary availability. When 3776d5f512f99700a963aa20a02590833e37221bffJean Chalard * this happens, we need to re-query the dictionaries. 3876d5f512f99700a963aa20a02590833e37221bffJean Chalard * - Unknown client. If the dictionary provider is in urgent need of data about some client that 3976d5f512f99700a963aa20a02590833e37221bffJean Chalard * it does not know, it sends this broadcast. When we receive this, we need to tell the dictionary 4076d5f512f99700a963aa20a02590833e37221bffJean Chalard * provider about ourselves. This happens when the settings for the dictionary pack are accessed, 4176d5f512f99700a963aa20a02590833e37221bffJean Chalard * but Latin IME never got a chance to register itself. 42cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard */ 43a28a05e971cc242b338331a3b78276fa95188d19Tadashi G. Takaokapublic final class DictionaryPackInstallBroadcastReceiver extends BroadcastReceiver { 4476d5f512f99700a963aa20a02590833e37221bffJean Chalard private static final String TAG = DictionaryPackInstallBroadcastReceiver.class.getSimpleName(); 45cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard 46cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard final LatinIME mService; 47cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard 4876d5f512f99700a963aa20a02590833e37221bffJean Chalard public DictionaryPackInstallBroadcastReceiver() { 4976d5f512f99700a963aa20a02590833e37221bffJean Chalard // This empty constructor is necessary for the system to instantiate this receiver. 5076d5f512f99700a963aa20a02590833e37221bffJean Chalard // This happens when the dictionary pack says it can't find a record for our client, 5176d5f512f99700a963aa20a02590833e37221bffJean Chalard // which happens when the dictionary pack settings are called before the keyboard 5276d5f512f99700a963aa20a02590833e37221bffJean Chalard // was ever started once. 5376d5f512f99700a963aa20a02590833e37221bffJean Chalard Log.i(TAG, "Latin IME dictionary broadcast receiver instantiated from the framework."); 5476d5f512f99700a963aa20a02590833e37221bffJean Chalard mService = null; 5576d5f512f99700a963aa20a02590833e37221bffJean Chalard } 5676d5f512f99700a963aa20a02590833e37221bffJean Chalard 57cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard public DictionaryPackInstallBroadcastReceiver(final LatinIME service) { 58cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard mService = service; 59cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard } 60cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard 61cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard @Override 62cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard public void onReceive(Context context, Intent intent) { 63cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard final String action = intent.getAction(); 64cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard final PackageManager manager = context.getPackageManager(); 65cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard 66cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // We need to reread the dictionary if a new dictionary package is installed. 67cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard if (action.equals(Intent.ACTION_PACKAGE_ADDED)) { 6876d5f512f99700a963aa20a02590833e37221bffJean Chalard if (null == mService) { 6976d5f512f99700a963aa20a02590833e37221bffJean Chalard Log.e(TAG, "Called with intent " + action + " but we don't know the service: this " 7076d5f512f99700a963aa20a02590833e37221bffJean Chalard + "should never happen"); 7176d5f512f99700a963aa20a02590833e37221bffJean Chalard return; 7276d5f512f99700a963aa20a02590833e37221bffJean Chalard } 73cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard final Uri packageUri = intent.getData(); 74cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard if (null == packageUri) return; // No package name : we can't do anything 75cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard final String packageName = packageUri.getSchemeSpecificPart(); 76cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard if (null == packageName) return; 77eb696d288ea19d3d64f932fd017da7d7187ba6e1Jean Chalard // TODO: do this in a more appropriate place 78eb696d288ea19d3d64f932fd017da7d7187ba6e1Jean Chalard TargetApplicationGetter.removeApplicationInfoCache(packageName); 79cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard final PackageInfo packageInfo; 80cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard try { 81cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard packageInfo = manager.getPackageInfo(packageName, PackageManager.GET_PROVIDERS); 82cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard } catch (android.content.pm.PackageManager.NameNotFoundException e) { 83cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard return; // No package info : we can't do anything 84cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard } 85cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard final ProviderInfo[] providers = packageInfo.providers; 86cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard if (null == providers) return; // No providers : it is not a dictionary. 87cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard 88cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // Search for some dictionary pack in the just-installed package. If found, reread. 89cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard for (ProviderInfo info : providers) { 901061bfdb34bbcb63bf0046eec42313d264ac33faJean Chalard if (DictionaryPackConstants.AUTHORITY.equals(info.authority)) { 91cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard mService.resetSuggestMainDict(); 92cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard return; 93cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard } 94cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard } 95cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // If we come here none of the authorities matched the one we searched for. 96cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // We can exit safely. 97cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard return; 98cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED) 99cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard && !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { 10076d5f512f99700a963aa20a02590833e37221bffJean Chalard if (null == mService) { 10176d5f512f99700a963aa20a02590833e37221bffJean Chalard Log.e(TAG, "Called with intent " + action + " but we don't know the service: this " 10276d5f512f99700a963aa20a02590833e37221bffJean Chalard + "should never happen"); 10376d5f512f99700a963aa20a02590833e37221bffJean Chalard return; 10476d5f512f99700a963aa20a02590833e37221bffJean Chalard } 105cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // When the dictionary package is removed, we need to reread dictionary (to use the 106cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // next-priority one, or stop using a dictionary at all if this was the only one, 107cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // since this is the user request). 108cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // If we are replacing the package, we will receive ADDED right away so no need to 109cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // remove the dictionary at the moment, since we will do it when we receive the 110cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // ADDED broadcast. 111cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard 112cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // TODO: Only reload dictionary on REMOVED when the removed package is the one we 113cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard // read dictionary from? 114cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard mService.resetSuggestMainDict(); 1151061bfdb34bbcb63bf0046eec42313d264ac33faJean Chalard } else if (action.equals(DictionaryPackConstants.NEW_DICTIONARY_INTENT_ACTION)) { 11676d5f512f99700a963aa20a02590833e37221bffJean Chalard if (null == mService) { 11776d5f512f99700a963aa20a02590833e37221bffJean Chalard Log.e(TAG, "Called with intent " + action + " but we don't know the service: this " 11876d5f512f99700a963aa20a02590833e37221bffJean Chalard + "should never happen"); 11976d5f512f99700a963aa20a02590833e37221bffJean Chalard return; 12076d5f512f99700a963aa20a02590833e37221bffJean Chalard } 121646d950ed8d2b2555df92855e18b350fd7761b21Jean Chalard mService.resetSuggestMainDict(); 12276d5f512f99700a963aa20a02590833e37221bffJean Chalard } else if (action.equals(DictionaryPackConstants.UNKNOWN_DICTIONARY_PROVIDER_CLIENT)) { 12376d5f512f99700a963aa20a02590833e37221bffJean Chalard if (null != mService) { 12476d5f512f99700a963aa20a02590833e37221bffJean Chalard // Careful! This is returning if the service is NOT null. This is because we 12576d5f512f99700a963aa20a02590833e37221bffJean Chalard // should come here instantiated by the framework in reaction to a broadcast of 12676d5f512f99700a963aa20a02590833e37221bffJean Chalard // the above action, so we should gave gone through the no-args constructor. 12776d5f512f99700a963aa20a02590833e37221bffJean Chalard Log.e(TAG, "Called with intent " + action + " but we have a reference to the " 12876d5f512f99700a963aa20a02590833e37221bffJean Chalard + "service: this should never happen"); 12976d5f512f99700a963aa20a02590833e37221bffJean Chalard return; 13076d5f512f99700a963aa20a02590833e37221bffJean Chalard } 13176d5f512f99700a963aa20a02590833e37221bffJean Chalard // The dictionary provider does not know about some client. We check that it's really 13276d5f512f99700a963aa20a02590833e37221bffJean Chalard // us that it needs to know about, and if it's the case, we register with the provider. 13376d5f512f99700a963aa20a02590833e37221bffJean Chalard final String wantedClientId = 13476d5f512f99700a963aa20a02590833e37221bffJean Chalard intent.getStringExtra(DictionaryPackConstants.DICTIONARY_PROVIDER_CLIENT_EXTRA); 13576d5f512f99700a963aa20a02590833e37221bffJean Chalard final String myClientId = context.getString(R.string.dictionary_pack_client_id); 13676d5f512f99700a963aa20a02590833e37221bffJean Chalard if (!wantedClientId.equals(myClientId)) return; // Not for us 13776d5f512f99700a963aa20a02590833e37221bffJean Chalard BinaryDictionaryFileDumper.initializeClientRecordHelper(context, myClientId); 138cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard } 139cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard } 140cba93f50c3d46ada773ec49435689dc3e2094385Jean Chalard} 141