2 * Copyright (C) 2009 ZXing authors
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.google.zxing.client.android.share;
19 import android.content.pm.ApplicationInfo;
20 import android.content.pm.PackageManager;
21 import android.os.AsyncTask;
22 import android.widget.ArrayAdapter;
23 import android.widget.ListAdapter;
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.List;
31 * Loads a list of packages installed on the device asynchronously.
35 final class LoadPackagesAsyncTask extends AsyncTask<List<String[]>,Void,List<String[]>> {
37 private static final String[] PKG_PREFIX_WHITELIST = {
38 "com.google.android.apps.",
40 private static final String[] PKG_PREFIX_BLACKLIST = {
43 "com.google.android.",
48 private final AppPickerActivity appPickerActivity;
50 LoadPackagesAsyncTask(AppPickerActivity appPickerActivity) {
51 this.appPickerActivity = appPickerActivity;
55 protected List<String[]> doInBackground(List<String[]>... objects) {
56 List<String[]> labelsPackages = objects[0];
57 PackageManager packageManager = appPickerActivity.getPackageManager();
58 List<ApplicationInfo> appInfos = packageManager.getInstalledApplications(0);
59 for (ApplicationInfo appInfo : appInfos) {
60 CharSequence label = appInfo.loadLabel(packageManager);
62 String packageName = appInfo.packageName;
63 if (!isHidden(packageName)) {
64 labelsPackages.add(new String[]{label.toString(), packageName});
68 Collections.sort(labelsPackages, new Comparator<String[]>() {
69 public int compare(String[] o1, String[] o2) {
70 return o1[0].compareTo(o2[0]);
73 return labelsPackages;
76 private static boolean isHidden(String packageName) {
77 if (packageName == null) {
80 for (String prefix : PKG_PREFIX_WHITELIST) {
81 if (packageName.startsWith(prefix)) {
85 for (String prefix : PKG_PREFIX_BLACKLIST) {
86 if (packageName.startsWith(prefix)) {
94 protected void onPostExecute(List<String[]> results) {
95 List<String> labels = new ArrayList<String>(results.size());
96 for (String[] result : results) {
97 labels.add(result[0]);
99 ListAdapter listAdapter = new ArrayAdapter<String>(
100 appPickerActivity, android.R.layout.simple_list_item_1, labels);
101 appPickerActivity.setListAdapter(listAdapter);
102 appPickerActivity.getProgressDialog().dismiss();