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.ActivityManager;
21import android.app.ListActivity;
22import android.content.Context;
23import android.content.Intent;
24import android.content.pm.PackageManager;
25import android.os.Bundle;
26import android.view.View;
27import android.view.ViewGroup;
28import android.view.LayoutInflater;
29import android.widget.BaseAdapter;
30import android.widget.ListView;
31import android.widget.TextView;
32import java.text.Collator;
33import java.util.ArrayList;
34import java.util.Collections;
35import java.util.Comparator;
36import java.util.List;
37
38public class RunningProcesses extends ListActivity {
39    PackageManager mPm;
40
41    @Override
42    protected void onCreate(Bundle icicle) {
43        super.onCreate(icicle);
44
45        mPm = getPackageManager();
46        mAdapter = new AppListAdapter(this);
47        if (mAdapter.getCount() <= 0) {
48            finish();
49        } else {
50            setListAdapter(mAdapter);
51        }
52    }
53
54    @Override
55    protected void onResume() {
56        super.onResume();
57    }
58
59    @Override
60    protected void onStop() {
61        super.onStop();
62    }
63
64    @Override
65    protected void onListItemClick(ListView l, View v, int position, long id) {
66        ListItem app = mAdapter.appForPosition(position);
67        // Create intent to start new activity
68        Intent intent = new Intent(Intent.ACTION_VIEW);
69        intent.setClass(this, ProcessInfo.class);
70        intent.putExtra("processName", app.procInfo.processName);
71        intent.putExtra("packageList", app.procInfo.pkgList);
72        // start new activity to display extended information
73        startActivity(intent);
74    }
75
76    private final class AppListAdapter extends BaseAdapter {
77        public AppListAdapter(Context context) {
78            mContext = context;
79            mInflater = (LayoutInflater)context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
80
81            ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
82            List<ActivityManager.RunningAppProcessInfo> appList = am.getRunningAppProcesses();
83            for (ActivityManager.RunningAppProcessInfo app : appList) {
84                if(mList == null) {
85                    mList = new ArrayList<ListItem>();
86                }
87                mList.add(new ListItem(app));
88            }
89            if (mList != null) {
90                Collections.sort(mList, sDisplayNameComparator);
91            }
92        }
93
94        public ListItem appForPosition(int position) {
95            if (mList == null) {
96                return null;
97            }
98            return mList.get(position);
99        }
100
101        public int getCount() {
102            return mList != null ? mList.size() : 0;
103        }
104
105        public Object getItem(int position) {
106            return position;
107        }
108
109        public long getItemId(int position) {
110            return position;
111        }
112
113        public View getView(int position, View convertView, ViewGroup parent) {
114            View view;
115            if (convertView == null) {
116                view = mInflater.inflate(
117                        android.R.layout.simple_list_item_1, parent, false);
118            } else {
119                view = convertView;
120            }
121            bindView(view, mList.get(position));
122            return view;
123        }
124
125        private final void bindView(View view, ListItem info) {
126            TextView text = (TextView)view.findViewById(android.R.id.text1);
127            text.setText(info != null ? info.procInfo.processName : "(none)");
128        }
129
130        protected final Context mContext;
131        protected final LayoutInflater mInflater;
132        protected List<ListItem> mList;
133
134    }
135
136    private final Comparator sDisplayNameComparator = new Comparator() {
137        public final int compare(Object a, Object b) {
138            CharSequence  sa = ((ListItem) a).procInfo.processName;
139            CharSequence  sb = ((ListItem) b).procInfo.processName;
140            return collator.compare(sa, sb);
141        }
142        private final Collator   collator = Collator.getInstance();
143    };
144
145    private class ListItem {
146        ActivityManager.RunningAppProcessInfo procInfo;
147        public ListItem(ActivityManager.RunningAppProcessInfo pInfo) {
148            procInfo = pInfo;
149        }
150    }
151
152    private AppListAdapter mAdapter;
153}
154
155