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