Home | History | Annotate | Download | only in utils
      1 /*
      2  * Copyright (C) 2012 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 
     17 package com.android.inputmethod.latin.utils;
     18 
     19 import android.content.Context;
     20 import android.content.pm.PackageInfo;
     21 import android.content.pm.PackageManager;
     22 import android.os.AsyncTask;
     23 import android.util.LruCache;
     24 
     25 public final class TargetPackageInfoGetterTask extends
     26         AsyncTask<String, Void, PackageInfo> {
     27     private static final int MAX_CACHE_ENTRIES = 64; // arbitrary
     28     private static final LruCache<String, PackageInfo> sCache =
     29             new LruCache<String, PackageInfo>(MAX_CACHE_ENTRIES);
     30 
     31     public static PackageInfo getCachedPackageInfo(final String packageName) {
     32         if (null == packageName) return null;
     33         return sCache.get(packageName);
     34     }
     35 
     36     public static void removeCachedPackageInfo(final String packageName) {
     37         sCache.remove(packageName);
     38     }
     39 
     40     public interface OnTargetPackageInfoKnownListener {
     41         public void onTargetPackageInfoKnown(final PackageInfo info);
     42     }
     43 
     44     private Context mContext;
     45     private final OnTargetPackageInfoKnownListener mListener;
     46 
     47     public TargetPackageInfoGetterTask(final Context context,
     48             final OnTargetPackageInfoKnownListener listener) {
     49         mContext = context;
     50         mListener = listener;
     51     }
     52 
     53     @Override
     54     protected PackageInfo doInBackground(final String... packageName) {
     55         final PackageManager pm = mContext.getPackageManager();
     56         mContext = null; // Bazooka-powered anti-leak device
     57         try {
     58             final PackageInfo packageInfo = pm.getPackageInfo(packageName[0], 0 /* flags */);
     59             sCache.put(packageName[0], packageInfo);
     60             return packageInfo;
     61         } catch (android.content.pm.PackageManager.NameNotFoundException e) {
     62             return null;
     63         }
     64     }
     65 
     66     @Override
     67     protected void onPostExecute(final PackageInfo info) {
     68         mListener.onTargetPackageInfoKnown(info);
     69     }
     70 }
     71