7f17c13fd5d400be3c8ada81dc0232bab58e748c
[zxing.git] / android / src / com / google / zxing / client / android / share / LoadPackagesAsyncTask.java
1 /*
2  * Copyright (C) 2009 ZXing authors
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.google.zxing.client.android.share;
18
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;
24
25 import java.util.ArrayList;
26 import java.util.Collections;
27 import java.util.Comparator;
28 import java.util.List;
29
30 /**
31  * Loads a list of packages installed on the device asynchronously.
32  *
33  * @author Sean Owen
34  */
35 final class LoadPackagesAsyncTask extends AsyncTask<List<String[]>,Void,List<String[]>> {
36
37   private static final String[] PKG_PREFIX_WHITELIST = {
38       "com.google.android.apps.",
39   };
40   private static final String[] PKG_PREFIX_BLACKLIST = {
41       "com.android.",
42       "android",
43       "com.google.android.",
44       "com.htc",
45   };
46
47
48   private final AppPickerActivity appPickerActivity;
49
50   LoadPackagesAsyncTask(AppPickerActivity appPickerActivity) {
51     this.appPickerActivity = appPickerActivity;
52   }
53
54   @Override
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);
61       if (label != null) {
62         String packageName = appInfo.packageName;
63         if (!isHidden(packageName)) {
64           labelsPackages.add(new String[]{label.toString(), packageName});
65         }
66       }
67     }
68     Collections.sort(labelsPackages, new Comparator<String[]>() {
69       public int compare(String[] o1, String[] o2) {
70         return o1[0].compareTo(o2[0]);
71       }
72     });
73     return labelsPackages;
74   }
75
76   private static boolean isHidden(String packageName) {
77     if (packageName == null) {
78       return true;
79     }
80     for (String prefix : PKG_PREFIX_WHITELIST) {
81       if (packageName.startsWith(prefix)) {
82         return false;
83       }
84     }
85     for (String prefix : PKG_PREFIX_BLACKLIST) {
86       if (packageName.startsWith(prefix)) {
87         return true;
88       }
89     }
90     return false;
91   }
92
93   @Override
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]);
98     }
99     ListAdapter listAdapter = new ArrayAdapter<String>(
100         appPickerActivity, android.R.layout.simple_list_item_1, labels);
101     appPickerActivity.setListAdapter(listAdapter);
102     appPickerActivity.getProgressDialog().dismiss();
103   }
104
105 }