Home | History | Annotate | Download | only in latin
      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             final PackageInfo packageInfo;
     55             try {
     56                 packageInfo = manager.getPackageInfo(packageName, PackageManager.GET_PROVIDERS);
     57             } catch (android.content.pm.PackageManager.NameNotFoundException e) {
     58                 return; // No package info : we can't do anything
     59             }
     60             final ProviderInfo[] providers = packageInfo.providers;
     61             if (null == providers) return; // No providers : it is not a dictionary.
     62 
     63             // Search for some dictionary pack in the just-installed package. If found, reread.
     64             for (ProviderInfo info : providers) {
     65                 if (BinaryDictionary.DICTIONARY_PACK_AUTHORITY.equals(info.authority)) {
     66                     mService.resetSuggestMainDict();
     67                     return;
     68                 }
     69             }
     70             // If we come here none of the authorities matched the one we searched for.
     71             // We can exit safely.
     72             return;
     73         } else if (action.equals(Intent.ACTION_PACKAGE_REMOVED)
     74                 && !intent.getBooleanExtra(Intent.EXTRA_REPLACING, false)) {
     75             // When the dictionary package is removed, we need to reread dictionary (to use the
     76             // next-priority one, or stop using a dictionary at all if this was the only one,
     77             // since this is the user request).
     78             // If we are replacing the package, we will receive ADDED right away so no need to
     79             // remove the dictionary at the moment, since we will do it when we receive the
     80             // ADDED broadcast.
     81 
     82             // TODO: Only reload dictionary on REMOVED when the removed package is the one we
     83             // read dictionary from?
     84             mService.resetSuggestMainDict();
     85         } else if (action.equals(NEW_DICTIONARY_INTENT_ACTION)) {
     86             mService.resetSuggestMainDict();
     87         }
     88     }
     89 }
     90