summaryrefslogtreecommitdiffstats
path: root/core/java/android/app/LauncherActivity.java
blob: 8f0a4f58fe5fc553bc567ee3ffeeb3bb63940a89 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package android.app;

import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Bundle;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.Filter;
import android.widget.Filterable;
import android.widget.ListView;
import android.widget.TextView;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;


/**
 * Displays a list of all activities which can be performed
 * for a given intent. Launches when clicked.
 *
 */
public abstract class LauncherActivity extends ListActivity {
    
    /**
     * Adapter which shows the set of activities that can be performed for a given intent.
     */
    private class ActivityAdapter extends BaseAdapter implements Filterable {
        private final Object lock = new Object();
        private ArrayList<ResolveInfo> mOriginalValues;

        protected final Context mContext;
        protected final Intent mIntent;
        protected final LayoutInflater mInflater;

        protected List<ResolveInfo> mActivitiesList;

        private Filter mFilter;

        public ActivityAdapter(Context context, Intent intent) {
            mContext = context;
            mIntent = new Intent(intent);
            mIntent.setComponent(null);
            mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);

            PackageManager pm = context.getPackageManager();
            mActivitiesList = pm.queryIntentActivities(intent, 0);
            if (mActivitiesList != null) {
                Collections.sort(mActivitiesList, new ResolveInfo.DisplayNameComparator(pm));
            }
        }

        public Intent intentForPosition(int position) {
            if (mActivitiesList == null) {
                return null;
            }

            Intent intent = new Intent(mIntent);
            ActivityInfo ai = mActivitiesList.get(position).activityInfo;
            intent.setClassName(ai.applicationInfo.packageName, ai.name);
            return intent;
        }

        public int getCount() {
            return mActivitiesList != null ? mActivitiesList.size() : 0;
        }

        public Object getItem(int position) {
            return position;
        }

        public long getItemId(int position) {
            return position;
        }

        public View getView(int position, View convertView, ViewGroup parent) {
            View view;
            if (convertView == null) {
                view = mInflater.inflate(
                        com.android.internal.R.layout.simple_list_item_1, parent, false);
            } else {
                view = convertView;
            }
            bindView(view, mActivitiesList.get(position));
            return view;
        }

        private char getCandidateLetter(ResolveInfo info) {
            PackageManager pm = mContext.getPackageManager();
            CharSequence label = info.loadLabel(pm);

            if (label == null) {
                label = info.activityInfo.name;
            }

            return Character.toLowerCase(label.charAt(0));
        }

        private void bindView(View view, ResolveInfo info) {
            TextView text = (TextView) view.findViewById(com.android.internal.R.id.text1);

            PackageManager pm = mContext.getPackageManager();
            CharSequence label = info.loadLabel(pm);
            text.setText(label != null ? label : info.activityInfo.name);
        }

        public Filter getFilter() {
            if (mFilter == null) {
                mFilter = new ArrayFilter();
            }
            return mFilter;
        }

        /**
         * <p>An array filters constrains the content of the array adapter with a prefix. Each item that
         * does not start with the supplied prefix is removed from the list.</p>
         */
        private class ArrayFilter extends Filter {
            @Override
            protected FilterResults performFiltering(CharSequence prefix) {
                FilterResults results = new FilterResults();

                if (mOriginalValues == null) {
                    synchronized (lock) {
                        mOriginalValues = new ArrayList<ResolveInfo>(mActivitiesList);
                    }
                }

                if (prefix == null || prefix.length() == 0) {
                    synchronized (lock) {
                        ArrayList<ResolveInfo> list = new ArrayList<ResolveInfo>(mOriginalValues);
                        results.values = list;
                        results.count = list.size();
                    }
                } else {
                    final PackageManager pm = mContext.getPackageManager();
                    final String prefixString = prefix.toString().toLowerCase();

                    ArrayList<ResolveInfo> values = mOriginalValues;
                    int count = values.size();

                    ArrayList<ResolveInfo> newValues = new ArrayList<ResolveInfo>(count);

                    for (int i = 0; i < count; i++) {
                        ResolveInfo value = values.get(i);

                        final CharSequence label = value.loadLabel(pm);
                        final CharSequence name = label != null ? label : value.activityInfo.name;

                        String[] words = name.toString().toLowerCase().split(" ");
                        int wordCount = words.length;

                        for (int k = 0; k < wordCount; k++) {
                            final String word = words[k];

                            if (word.startsWith(prefixString)) {
                                newValues.add(value);
                                break;
                            }
                        }
                    }

                    results.values = newValues;
                    results.count = newValues.size();
                }

                return results;
            }

            @Override
            protected void publishResults(CharSequence constraint, FilterResults results) {
                //noinspection unchecked
                mActivitiesList = (List<ResolveInfo>) results.values;
                if (results.count > 0) {
                    notifyDataSetChanged();
                } else {
                    notifyDataSetInvalidated();
                }
            }
        }
    }
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mAdapter = new ActivityAdapter(this, getTargetIntent());
        
        setListAdapter(mAdapter);
        getListView().setTextFilterEnabled(true);
    }


    @Override
    protected void onListItemClick(ListView l, View v, int position, long id) {
        Intent intent = ((ActivityAdapter)mAdapter).intentForPosition(position);

        startActivity(intent);
    }
    
    protected abstract Intent getTargetIntent();
   
}