1/*
2**
3** Copyright 2006, The Android Open Source Project
4**
5** Licensed under the Apache License, Version 2.0 (the "License");
6** you may not use this file except in compliance with the License.
7** You may obtain a copy of the License at
8**
9**     http://www.apache.org/licenses/LICENSE-2.0
10**
11** Unless required by applicable law or agreed to in writing, software
12** distributed under the License is distributed on an "AS IS" BASIS,
13** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14** See the License for the specific language governing permissions and
15** limitations under the License.
16*/
17
18package com.android.development;
19
20import android.app.ListActivity;
21import android.content.Context;
22import android.content.Intent;
23import android.content.pm.ApplicationInfo;
24import android.content.pm.PackageInfo;
25import android.content.pm.PackageManager;
26import android.content.pm.PackageManager.NameNotFoundException;
27import android.os.Bundle;
28import android.view.View;
29import android.view.ViewGroup;
30import android.view.LayoutInflater;
31import android.widget.BaseAdapter;
32import android.widget.ListView;
33import android.widget.TextView;
34import java.text.Collator;
35import java.util.ArrayList;
36import java.util.Collections;
37import java.util.Comparator;
38import java.util.List;
39
40public class AppHwConfigList extends ListActivity {
41    private static final String TAG = "AppHwConfigList";
42    PackageManager mPm;
43
44    @Override
45    protected void onCreate(Bundle icicle) {
46        super.onCreate(icicle);
47
48        mPm = getPackageManager();
49        mAdapter = new AppListAdapter(this);
50        if (mAdapter.getCount() <= 0) {
51            finish();
52        } else {
53            setListAdapter(mAdapter);
54        }
55    }
56
57    @Override
58    protected void onResume() {
59        super.onResume();
60    }
61
62    @Override
63    protected void onStop() {
64        super.onStop();
65    }
66
67    @Override
68    protected void onListItemClick(ListView l, View v, int position, long id) {
69        PackageInfo app = mAdapter.appForPosition(position);
70        // TODO display all preference settings
71        Intent intent = new Intent(Intent.ACTION_VIEW);
72        intent.setClass(this, AppHwPref.class);
73        intent.putExtra("packageName", app.packageName);
74        startActivity(intent);
75    }
76
77    private final class AppListAdapter extends BaseAdapter {
78        public AppListAdapter(Context context) {
79            mContext = context;
80            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
81
82            List<ApplicationInfo> appList = mPm.getInstalledApplications(0);
83            for (ApplicationInfo app : appList) {
84                PackageInfo pkgInfo = null;
85                try {
86                    pkgInfo = mPm.getPackageInfo(app.packageName, 0);
87                } catch (NameNotFoundException e) {
88                    // TODO Auto-generated catch block
89                    e.printStackTrace();
90                }
91                if ((pkgInfo != null)) {
92                        if(mList == null) {
93                             mList = new ArrayList<PackageInfo>();
94                         }
95                         mList.add(pkgInfo);
96                }
97            }
98            if (mList != null) {
99                Collections.sort(mList, sDisplayNameComparator);
100            }
101        }
102
103        public PackageInfo appForPosition(int position) {
104            if (mList == null) {
105                return null;
106            }
107            return mList.get(position);
108        }
109
110        public int getCount() {
111            return mList != null ? mList.size() : 0;
112        }
113
114        public Object getItem(int position) {
115            return position;
116        }
117
118        public long getItemId(int position) {
119            return position;
120        }
121
122        public View getView(int position, View convertView, ViewGroup parent) {
123            View view;
124            if (convertView == null) {
125                view = mInflater.inflate(
126                        android.R.layout.simple_list_item_1, parent, false);
127            } else {
128                view = convertView;
129            }
130            bindView(view, mList.get(position));
131            return view;
132        }
133
134        private final void bindView(View view, PackageInfo info) {
135            TextView text = (TextView)view.findViewById(android.R.id.text1);
136            text.setText(info != null ? info.applicationInfo.loadLabel(mPm) : "(none)");
137        }
138
139        protected final Context mContext;
140        protected final LayoutInflater mInflater;
141        protected List<PackageInfo> mList;
142
143    }
144
145    private final Comparator sDisplayNameComparator = new Comparator() {
146        public final int compare(Object a, Object b) {
147            CharSequence  sa = ((PackageInfo) a).applicationInfo.loadLabel(mPm);
148            CharSequence  sb = ((PackageInfo) b).applicationInfo.loadLabel(mPm);
149            return collator.compare(sa, sb);
150        }
151        private final Collator   collator = Collator.getInstance();
152    };
153
154    private AppListAdapter mAdapter;
155}
156
157