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