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
|
/*
* 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 android.app.AlertDialog;
import android.app.Dialog;
import android.app.NotificationGroup;
import cyanogenmod.app.ProfileManager;
import android.content.DialogInterface;
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.preference.Preference;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
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.ListView;
import android.widget.Toast;
import com.android.settings.R;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.cyanogenmod.PackageListAdapter;
import com.android.settings.cyanogenmod.PackageListAdapter.PackageItem;
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 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;
private PackageListAdapter 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();
mAppAdapter = new PackageListAdapter(getActivity());
updatePackages();
setHasOptionsMenu(true);
}
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
MenuItem delete = menu.add(0, MENU_DELETE, 0, R.string.profile_menu_delete_title)
.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 View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
return super.onCreateView(inflater, container, savedInstanceState);
}
@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_title, 0, R.string.profile_menu_delete_title);
}
@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_title:
deleteAppFromGroup(selectedGroup);
return true;
}
return super.onOptionsItemSelected(item);
}
private void deleteAppFromGroup(PackageItem selectedGroup) {
if (selectedGroup != null) {
mNotificationGroup.removePackage(selectedGroup.packageName);
updatePackages();
}
}
@Override
protected int getMetricsCategory() {
return 0;
}
@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_title);
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_title);
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();
}
}
|