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.io.Serializable;
26 import java.util.ArrayList;
27 import java.util.Collections;
28 import java.util.Comparator;
29 import java.util.List;
32 * Loads a list of packages installed on the device asynchronously.
36 final class LoadPackagesAsyncTask extends AsyncTask<List<String[]>,Void,List<String[]>> {
38 private static final String[] PKG_PREFIX_WHITELIST = {
39 "com.google.android.apps.",
41 private static final String[] PKG_PREFIX_BLACKLIST = {
44 "com.google.android.",
49 private final AppPickerActivity appPickerActivity;
51 LoadPackagesAsyncTask(AppPickerActivity appPickerActivity) {
52 this.appPickerActivity = appPickerActivity;
56 protected List<String[]> doInBackground(List<String[]>... objects) {
57 List<String[]> labelsPackages = objects[0];
58 PackageManager packageManager = appPickerActivity.getPackageManager();
59 List<ApplicationInfo> appInfos = packageManager.getInstalledApplications(0);
60 for (ApplicationInfo appInfo : appInfos) {
61 CharSequence label = appInfo.loadLabel(packageManager);
63 String packageName = appInfo.packageName;
64 if (!isHidden(packageName)) {
65 labelsPackages.add(new String[]{label.toString(), packageName});
69 Collections.sort(labelsPackages, new ByFirstStringComparator());
70 return labelsPackages;
73 private static boolean isHidden(String packageName) {
74 if (packageName == null) {
77 for (String prefix : PKG_PREFIX_WHITELIST) {
78 if (packageName.startsWith(prefix)) {
82 for (String prefix : PKG_PREFIX_BLACKLIST) {
83 if (packageName.startsWith(prefix)) {
91 protected void onPostExecute(List<String[]> results) {
92 List<String> labels = new ArrayList<String>(results.size());
93 for (String[] result : results) {
94 labels.add(result[0]);
96 ListAdapter listAdapter = new ArrayAdapter<String>(
97 appPickerActivity, android.R.layout.simple_list_item_1, labels);
98 appPickerActivity.setListAdapter(listAdapter);
99 appPickerActivity.getProgressDialog().dismiss();
102 private static class ByFirstStringComparator implements Comparator<String[]>, Serializable {
103 public int compare(String[] o1, String[] o2) {
104 return o1[0].compareTo(o2[0]);