summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/profiles/AppGroupConfig.java
blob: b5ea87c9a5f5f46dc4dc1aeb54e3aa82554847e5 (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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
/*
 * Copyright (C) 2012 The CyanogenMod 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 com.android.settings.profiles;

import java.util.Collections;
import java.util.LinkedList;
import java.util.List;

import android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationGroup;
import android.app.ProfileManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.preference.Preference;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.util.Log;
import android.view.ContextMenu;
import android.view.ContextMenu.ContextMenuInfo;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.AdapterContextMenuInfo;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;

public class AppGroupConfig extends SettingsPreferenceFragment
    implements Preference.OnPreferenceChangeListener {

    private static String TAG = "AppGroupConfig";

    private static final int DIALOG_APPS = 0;

    private static final int DELETE_CONFIRM = 1;

    private static final int DELETE_GROUP_CONFIRM = 2;

    public static final String PROFILE_SERVICE = "profile";

    private ListView mListView;

    private PackageManager mPackageManager;

    private List<PackageInfo> mInstalledPackages;

    private NotificationGroup mNotificationGroup;

    private ProfileManager mProfileManager;

    private NamePreference mNamePreference;

    private static final int MENU_DELETE = Menu.FIRST;

    private static final int MENU_ADD = Menu.FIRST + 1;

    PackageAdaptor mAppAdapter;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mProfileManager = (ProfileManager) getActivity().getSystemService(PROFILE_SERVICE);
        addPreferencesFromResource(R.xml.application_list);

        final Bundle args = getArguments();
        if (args != null) {
            mNotificationGroup = (NotificationGroup) args.getParcelable("NotificationGroup");
            mPackageManager = getPackageManager();
            mInstalledPackages = mPackageManager.getInstalledPackages(0);
            mAppAdapter = new PackageAdaptor(mInstalledPackages);
            mAppAdapter.update();

            updatePackages();

            setHasOptionsMenu(true);
        }
    }

    @Override
    public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
        MenuItem delete = menu.add(0, MENU_DELETE, 0, R.string.profile_menu_delete)
                .setIcon(R.drawable.ic_menu_trash_holo_dark);
        delete.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
                MenuItem.SHOW_AS_ACTION_WITH_TEXT);

        MenuItem addApplication = menu.add(0, MENU_ADD, 0, R.string.profiles_add)
                .setIcon(R.drawable.ic_menu_add)
                .setAlphabeticShortcut('a');
        addApplication.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
                MenuItem.SHOW_AS_ACTION_WITH_TEXT);
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        switch (item.getItemId()) {
            case MENU_DELETE:
                deleteNotificationGroup();
                return true;
            case MENU_ADD:
                addNewApp();
                return true;
            default:
                return false;
        }
    }

    Preference mAddPreference;

    Preference mDeletePreference;

    private void updatePackages() {
        PreferenceScreen prefSet = getPreferenceScreen();

        // Add the General section
        PreferenceGroup generalPrefs = (PreferenceGroup) prefSet.findPreference("general_section");
        if (generalPrefs != null) {
            generalPrefs.removeAll();

            // Name preference
            mNamePreference = new NamePreference(getActivity(), mNotificationGroup.getName());
            mNamePreference.setOnPreferenceChangeListener(this);
            generalPrefs.addPreference(mNamePreference);
        }

        PreferenceGroup applicationsList = (PreferenceGroup) prefSet.findPreference("applications_list");
        if (applicationsList != null) {
            applicationsList.removeAll();
            for (String pkg : mNotificationGroup.getPackages()) {
                Preference pref = new Preference(getActivity());
                try {
                    PackageInfo group = mPackageManager.getPackageInfo(pkg, 0);
                    pref.setKey(group.packageName);
                    pref.setTitle(group.applicationInfo.loadLabel(mPackageManager));
                    Drawable icon = group.applicationInfo.loadIcon(mPackageManager);
                    pref.setIcon(icon);
                    pref.setSelectable(true);
                    pref.setPersistent(false);
                    applicationsList.addPreference(pref);
                } catch (NameNotFoundException e) {
                    e.printStackTrace();
                }
            }
        }
    }

    @Override
    public void onCreateContextMenu(ContextMenu menu, View v, ContextMenuInfo menuInfo) {
        menu.add(0, R.string.profile_menu_delete, 0, R.string.profile_menu_delete);
    }

    @Override
    public boolean onContextItemSelected(MenuItem item) {
        AdapterContextMenuInfo aMenuInfo = (AdapterContextMenuInfo) item.getMenuInfo();
        PackageItem selectedGroup = (PackageItem) mListView.getItemAtPosition(aMenuInfo.position);
        switch (item.getItemId()) {
            case R.string.profile_menu_delete:
                deleteAppFromGroup(selectedGroup);
                return true;
        }
        return super.onOptionsItemSelected(item);
    }

    private void deleteAppFromGroup(PackageItem selectedGroup) {
        if (selectedGroup != null) {
            mNotificationGroup.removePackage(selectedGroup.packageName);
            updatePackages();
        }
    }

    @Override
    public void onPause() {
        if (mNotificationGroup != null) {
            mProfileManager.addNotificationGroup(mNotificationGroup);
        }
        super.onPause();
    }

    @Override
    public boolean onPreferenceChange(Preference preference, Object newValue) {
        if (preference == mNamePreference) {
            String name = mNamePreference.getName().toString();
            if (!name.equals(mNotificationGroup.getName())) {
                if (!mProfileManager.notificationGroupExists(name)) {
                    mNotificationGroup.setName(name);
                } else {
                    mNamePreference.setName(mNotificationGroup.getName());
                    Toast.makeText(getActivity(), R.string.duplicate_appgroup_name, Toast.LENGTH_LONG).show();
                }
            }
        }
        return true;
    }

    @Override
    public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
        if (preference instanceof Preference) {
            String deleteItem = preference.getKey();
            removeApp(deleteItem);
            return true;
        }
        return super.onPreferenceTreeClick(preferenceScreen, preference);
    }

    private void addNewApp() {
        showDialog(DIALOG_APPS);
        // TODO: switch to using the built in app list rather than dialog box?
    }

    private void removeApp(String key) {
        mPackageToDelete = key.toString();
        showDialog(DELETE_CONFIRM);
    }

    private void deleteNotificationGroup() {
        showDialog(DELETE_GROUP_CONFIRM);
    }

    @Override
    public Dialog onCreateDialog(int id) {
        AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
        final Dialog dialog;
        switch (id) {
            case DIALOG_APPS:
                final ListView list = new ListView(getActivity());
                list.setAdapter(mAppAdapter);
                builder.setTitle(R.string.profile_choose_app);
                builder.setView(list);
                dialog = builder.create();
                list.setOnItemClickListener(new OnItemClickListener() {
                    @Override
                    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                        PackageItem info = (PackageItem) parent.getItemAtPosition(position);
                        mNotificationGroup.addPackage(info.packageName);
                        updatePackages();
                        dialog.cancel();
                    }
                });
                break;
            case DELETE_CONFIRM:
                builder.setMessage(R.string.profile_app_delete_confirm);
                builder.setTitle(R.string.profile_menu_delete);
                builder.setIconAttribute(android.R.attr.alertDialogIcon);
                builder.setPositiveButton(android.R.string.yes,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                doDelete();
                            }
                        });
                builder.setNegativeButton(android.R.string.no,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                dialog = builder.create();
                break;
            case DELETE_GROUP_CONFIRM:
                builder.setMessage(R.string.profile_delete_appgroup);
                builder.setTitle(R.string.profile_menu_delete);
                builder.setIconAttribute(android.R.attr.alertDialogIcon);
                builder.setPositiveButton(android.R.string.yes,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                                mProfileManager.removeNotificationGroup(mNotificationGroup);
                                mNotificationGroup = null;
                                finish();
                            }
                        });
                builder.setNegativeButton(android.R.string.no,
                        new DialogInterface.OnClickListener() {
                            @Override
                            public void onClick(DialogInterface dialog, int which) {
                            }
                        });
                dialog = builder.create();
                break;
            default:
                dialog = null;
        }
        return dialog;
    }

    String mPackageToDelete;

    private void doDelete() {
        mNotificationGroup.removePackage(mPackageToDelete);
        updatePackages();
    }

    class PackageItem implements Comparable<PackageItem> {
        CharSequence title;

        String packageName;

        Drawable icon;

        @Override
        public int compareTo(PackageItem another) {
            return this.title.toString().compareTo(another.title.toString());
        }
    }

    class PackageAdaptor extends BaseAdapter {

        protected List<PackageInfo> mInstalledPackageInfo;

        protected List<PackageItem> mInstalledPackages = new LinkedList<PackageItem>();

        private void reloadList() {
            final Handler handler = new Handler();
            new Thread(new Runnable() {

                @Override
                public void run() {
                    synchronized (mInstalledPackages) {
                        mInstalledPackages.clear();
                        for (PackageInfo info : mInstalledPackageInfo) {
                            final PackageItem item = new PackageItem();
                            ApplicationInfo applicationInfo = info.applicationInfo;
                            item.title = applicationInfo.loadLabel(mPackageManager);
                            item.icon = applicationInfo.loadIcon(mPackageManager);
                            item.packageName = applicationInfo.packageName;
                            handler.post(new Runnable() {

                                @Override
                                public void run() {
                                    int index = Collections.binarySearch(mInstalledPackages, item);
                                    if (index < 0) {
                                        index = -index - 1;
                                        mInstalledPackages.add(index, item);
                                    }
                                    notifyDataSetChanged();
                                }
                            });
                        }
                    }
                }
            }).start();
        }

        public PackageAdaptor(List<PackageInfo> installedPackagesInfo) {
            mInstalledPackageInfo = installedPackagesInfo;
        }

        public void update() {
            reloadList();
        }

        @Override
        public int getCount() {
            return mInstalledPackages.size();
        }

        @Override
        public PackageItem getItem(int position) {
            return mInstalledPackages.get(position);
        }

        @Override
        public long getItemId(int position) {
            return mInstalledPackages.get(position).packageName.hashCode();
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {
            ViewHolder holder;
            if (convertView != null) {
                holder = (ViewHolder) convertView.getTag();
            } else {
                final LayoutInflater layoutInflater = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
                convertView = layoutInflater.inflate(R.layout.preference_icon, null, false);
                holder = new ViewHolder();
                convertView.setTag(holder);
                holder.title = (TextView) convertView.findViewById(com.android.internal.R.id.title);
                holder.summary = (TextView) convertView
                        .findViewById(com.android.internal.R.id.summary);
                holder.icon = (ImageView) convertView.findViewById(R.id.icon);
            }
            PackageItem applicationInfo = getItem(position);

            if (holder.title != null) {
                holder.title.setText(applicationInfo.title);
            }
            if (holder.summary != null) {
                holder.summary.setVisibility(View.GONE);
            }
            if (holder.icon != null) {
                Drawable loadIcon = applicationInfo.icon;
                holder.icon.setImageDrawable(loadIcon);
            }
            return convertView;
        }
    }

    static class ViewHolder {
        TextView title;
        TextView summary;
        ImageView icon;
    }
}