public class InstalledPackageQuerier {
public static final int VERSION_CODE_NONE = -1;
private static final Integer EMPTY_VERSION = new Integer(VERSION_CODE_NONE);
private static InstalledPackageQuerier sInstance;緩存
private Context mContext;
private Map<String, Integer> mInstalledApps;
private Map<String, List<WeakReference<InstalledPackageQuerierListener>>> mListeners = new HashMap<String, List<WeakReference<InstalledPackageQuerierListener>>>();
private ArrayList<WeakReference<InstalledPackageQuerierListener>> mGlobalListener = new ArrayList<>();app
public static void init (Context context) {
sInstance = new InstalledPackageQuerier(context);
}ide
public static InstalledPackageQuerier getInstance () {
return sInstance;
}spa
private InstalledPackageQuerier (Context context) {
mContext = context.getApplicationContext();
mInstalledApps = new HashMap<>();
registerReceiver();
}rem
private void registerReceiver () {
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_PACKAGE_ADDED);
filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
filter.setPriority(Integer.MAX_VALUE); // 設置級別
filter.addDataScheme("package");
mContext.registerReceiver(new AppInstallReceiver(), filter);
}get
public int getInstalledAppVersionCode (String packageName) {string
Integer versionCode = mInstalledApps.get(packageName);
if (versionCode == null) { //還沒緩存
PackageInfo info = getPackageInfo(packageName);
if (info != null) {
mInstalledApps.put(packageName, info.versionCode);
return info.versionCode;
} else {
mInstalledApps.put(packageName, EMPTY_VERSION);
return VERSION_CODE_NONE;
}
} else {
return versionCode.intValue();
}
}it
public boolean isInstalled (String packageName) {
return getInstalledAppVersionCode(packageName) != VERSION_CODE_NONE;
}io
private PackageInfo getPackageInfo (String packageName) {
try {
return mContext.getPackageManager().getPackageInfo(packageName, 0);
} catch (NameNotFoundException e) {
}
return null;
}ast
public class AppInstallReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
String packageName = intent.getDataString().substring(8);
if (Intent.ACTION_PACKAGE_ADDED.equals(intent.getAction())) {
if (mInstalledApps.containsKey(packageName)) {
PackageInfo info = getPackageInfo(packageName);
if (info != null) {
mInstalledApps.put(packageName, info.versionCode);
}
}
dispatchAppChanged(packageName, true);
} else if (Intent.ACTION_PACKAGE_REMOVED.equals(intent.getAction())) {
if (mInstalledApps.containsKey(packageName)) {
mInstalledApps.put(packageName, EMPTY_VERSION);
}
dispatchAppChanged(packageName, false);
}
}
}
private void dispatchAppChanged (String appPackage, boolean addOrRemove) {
dispatchForList(mGlobalListener, appPackage, addOrRemove);
List<WeakReference<InstalledPackageQuerierListener>> appListeners = mListeners.get(appPackage);
if (appListeners != null) {
dispatchForList(appListeners, appPackage, addOrRemove);
}
}
private void dispatchForList (List<WeakReference<InstalledPackageQuerierListener>> list, String appPackage, boolean addOrRemove) {
Iterator<WeakReference<InstalledPackageQuerierListener>> it = list.iterator();
while (it.hasNext()) {
WeakReference<InstalledPackageQuerierListener> ref = it.next();
InstalledPackageQuerierListener listener = ref.get();
if (listener != null) {
if (addOrRemove) {
listener.onAppAdded(appPackage);
} else {
listener.onAppRemoved(appPackage);
}
} else {
it.remove();
}
}
}
public void addInstalledPackageQuerierListener (InstalledPackageQuerierListener listener) {
if (listener != null) {
mGlobalListener.add(new WeakReference<>(listener));
}
}
public void removeInstalledPackageQuerierListener (InstalledPackageQuerierListener listener) {
if (listener != null) {
Iterator<WeakReference<InstalledPackageQuerierListener>> it = mGlobalListener.iterator();
while (it.hasNext()) {
WeakReference<InstalledPackageQuerierListener> ref = it.next();
if (ref.get() == listener || ref.get() == null) {
it.remove();
break;
}
}
}
}
public void addInstalledPackageQuerierListener (String appPackage, InstalledPackageQuerierListener listener) {
if (listener != null) {
List<WeakReference<InstalledPackageQuerierListener>> appListeners = mListeners.get(appPackage);
if (appListeners == null) {
appListeners = new ArrayList<>();
mListeners.put(appPackage, appListeners);
}
appListeners.add(new WeakReference<>(listener));
}
}
public void removeInstalledPackageQuerierListener (String appPackage, InstalledPackageQuerierListener listener) {
if (listener != null) {
List<WeakReference<InstalledPackageQuerierListener>> appListeners = mListeners.get(appPackage);
if (appListeners != null) {
Iterator<WeakReference<InstalledPackageQuerierListener>> it = appListeners.iterator();
while (it.hasNext()) {
WeakReference<InstalledPackageQuerierListener> ref = it.next();
if (ref.get() == listener || ref.get() == null) {
it.remove();
break;
}
}
}
}
}
public interface InstalledPackageQuerierListener { void onAppAdded(String appPackage); void onAppRemoved(String appPackage); } }