DictionaryPackInstallBroadcastReceiver.java revision 0cc0544a2995c7eb54a830ae54db60af89d4073d
1/* 2 * Copyright (C) 2011 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 com.android.inputmethod.dictionarypack.UpdateHandler; 20 21import android.content.BroadcastReceiver; 22import android.content.Context; 23import android.content.Intent; 24import android.content.pm.PackageInfo; 25import android.content.pm.PackageManager; 26import android.content.pm.ProviderInfo; 27import android.net.Uri; 28 29/** 30 * Takes action to reload the necessary data when a dictionary pack was added/removed. 31 */ 32public final class DictionaryPackInstallBroadcastReceiver extends BroadcastReceiver { 33 34 final LatinIME mService; 35 /** 36 * The action of the intent for publishing that new dictionary data is available. 37 */ 38 /* package */ static final String NEW_DICTIONARY_INTENT_ACTION = 39 UpdateHandler.NEW_DICTIONARY_INTENT_ACTION; 40 41 public DictionaryPackInstallBroadcastReceiver(final LatinIME service) { 42 mService = service; 43 } 44 45 @Override 46 public void onReceive(Context context, Intent intent) { 47 final String action = intent.getAction(); 48 final PackageManager manager = context.getPackageManager(); 49 50 // We need to reread the dictionary if a new dictionary package is installed. 51 if (action.equals(Intent.ACTION_PACKAGE_ADDED)) { 52 final Uri packageUri = intent.getData(); 53 if (null == packageUri) return; // No package name : we can't do anything 54 final String packageName = packageUri.getSchemeSpecificPart(); 55 if (null == packageName) return; 56 // TODO: do this in a more appropriate place 57 TargetApplicationGetter.removeApplicationInfoCache(packageName); 58 final PackageInfo packageInfo; 59 try { 60 packageInfo = manager.getPackageInfo(packageName, PackageManager.GET_PROVIDERS); 61 } catch (android.content.pm.PackageManager.NameNotFoundException e) { 62 return; // No package info : we can't do anything 63 } 64 final ProviderInfo[] providers = packageInfo.providers; 65 if (null == providers) return; // No providers : it is not a dictionary. 66 67 // Search for some dictionary pack in the just-installed package. If found, reread. 68 for (ProviderInfo info : providers) { 69 if (BinaryDictionary.DICTIONARY_PACK_AUTHORITY.equals(info.authority)) { 70 mService.resetSuggestMainDict(); 71 return; 72 } 73 } 74 // If we come here none of the authorities matched the one we searched for. 75 // We can exit safely. 76 return; 77 } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED) 78 && !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) { 79 // When the dictionary package is removed, we need to reread dictionary (to use the 80 // next-priority one, or stop using a dictionary at all if this was the only one, 81 // since this is the user request). 82 // If we are replacing the package, we will receive ADDED right away so no need to 83 // remove the dictionary at the moment, since we will do it when we receive the 84 // ADDED broadcast. 85 86 // TODO: Only reload dictionary on REMOVED when the removed package is the one we 87 // read dictionary from? 88 mService.resetSuggestMainDict(); 89 } else if (action.equals(NEW_DICTIONARY_INTENT_ACTION)) { 90 mService.resetSuggestMainDict(); 91 } 92 } 93} 94