1 /* 2 * Copyright (C) 2008 The Android Open Source Project 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.android.settings; 18 19 import java.text.Collator; 20 import java.util.ArrayList; 21 import java.util.Collections; 22 import java.util.Comparator; 23 import java.util.List; 24 25 import com.android.settings.applications.AppViewHolder; 26 27 import android.app.ActivityManagerNative; 28 import android.app.ListActivity; 29 import android.content.Context; 30 import android.content.Intent; 31 import android.content.pm.ApplicationInfo; 32 import android.os.Build; 33 import android.os.Bundle; 34 import android.os.Process; 35 import android.os.RemoteException; 36 import android.provider.Settings; 37 import android.view.LayoutInflater; 38 import android.view.View; 39 import android.view.ViewGroup; 40 import android.widget.ArrayAdapter; 41 import android.widget.ListView; 42 43 public class AppPicker extends ListActivity { 44 private AppListAdapter mAdapter; 45 46 @Override onCreate(Bundle icicle)47 protected void onCreate(Bundle icicle) { 48 super.onCreate(icicle); 49 50 mAdapter = new AppListAdapter(this); 51 if (mAdapter.getCount() <= 0) { 52 finish(); 53 } else { 54 setListAdapter(mAdapter); 55 } 56 } 57 58 @Override onResume()59 protected void onResume() { 60 super.onResume(); 61 } 62 63 @Override onStop()64 protected void onStop() { 65 super.onStop(); 66 } 67 68 @Override onListItemClick(ListView l, View v, int position, long id)69 protected void onListItemClick(ListView l, View v, int position, long id) { 70 MyApplicationInfo app = mAdapter.getItem(position); 71 Intent intent = new Intent(); 72 if (app.info != null) intent.setAction(app.info.packageName); 73 setResult(RESULT_OK, intent); 74 finish(); 75 } 76 77 class MyApplicationInfo { 78 ApplicationInfo info; 79 CharSequence label; 80 } 81 82 public class AppListAdapter extends ArrayAdapter<MyApplicationInfo> { 83 private final List<MyApplicationInfo> mPackageInfoList = new ArrayList<MyApplicationInfo>(); 84 private final LayoutInflater mInflater; 85 AppListAdapter(Context context)86 public AppListAdapter(Context context) { 87 super(context, 0); 88 mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE); 89 List<ApplicationInfo> pkgs = context.getPackageManager().getInstalledApplications(0); 90 for (int i=0; i<pkgs.size(); i++) { 91 ApplicationInfo ai = pkgs.get(i); 92 if (ai.uid == Process.SYSTEM_UID) { 93 continue; 94 } 95 // On a user build, we only allow debugging of apps that 96 // are marked as debuggable. Otherwise (for platform development) 97 // we allow all apps. 98 if ((ai.flags&ApplicationInfo.FLAG_DEBUGGABLE) == 0 99 && "user".equals(Build.TYPE)) { 100 continue; 101 } 102 MyApplicationInfo info = new MyApplicationInfo(); 103 info.info = ai; 104 info.label = info.info.loadLabel(getPackageManager()).toString(); 105 mPackageInfoList.add(info); 106 } 107 Collections.sort(mPackageInfoList, sDisplayNameComparator); 108 MyApplicationInfo info = new MyApplicationInfo(); 109 info.label = context.getText(R.string.no_application); 110 mPackageInfoList.add(0, info); 111 addAll(mPackageInfoList); 112 } 113 114 @Override getView(int position, View convertView, ViewGroup parent)115 public View getView(int position, View convertView, ViewGroup parent) { 116 // A ViewHolder keeps references to children views to avoid unnecessary calls 117 // to findViewById() on each row. 118 AppViewHolder holder = AppViewHolder.createOrRecycle(mInflater, convertView); 119 convertView = holder.rootView; 120 MyApplicationInfo info = getItem(position); 121 holder.appName.setText(info.label); 122 if (info.info != null) { 123 holder.appIcon.setImageDrawable(info.info.loadIcon(getPackageManager())); 124 holder.appSize.setText(info.info.packageName); 125 } else { 126 holder.appIcon.setImageDrawable(null); 127 holder.appSize.setText(""); 128 } 129 holder.disabled.setVisibility(View.GONE); 130 holder.checkBox.setVisibility(View.GONE); 131 return convertView; 132 } 133 } 134 135 private final static Comparator<MyApplicationInfo> sDisplayNameComparator 136 = new Comparator<MyApplicationInfo>() { 137 public final int 138 compare(MyApplicationInfo a, MyApplicationInfo b) { 139 return collator.compare(a.label, b.label); 140 } 141 142 private final Collator collator = Collator.getInstance(); 143 }; 144 } 145