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
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
|
/*
* 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.ArrayList;
import java.util.UUID;
import android.app.AirplaneModeSettings;
import android.app.AlertDialog;
import android.app.ConnectionSettings;
import android.app.Profile;
import android.app.ProfileGroup;
import android.app.ProfileManager;
import android.app.RingModeSettings;
import android.app.StreamSettings;
import android.app.admin.DevicePolicyManager;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.media.AudioManager;
import android.net.wimax.WimaxHelper;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceGroup;
import android.preference.PreferenceScreen;
import android.telephony.TelephonyManager;
import android.util.Log;
import android.view.Menu;
import android.view.MenuInflater;
import android.view.MenuItem;
import android.widget.Toast;
import com.android.settings.R;
import com.android.settings.SettingsActivity;
import static com.android.settings.profiles.ProfilesUtils.*;
import com.android.settings.SettingsPreferenceFragment;
public class ProfileConfig extends SettingsPreferenceFragment
implements Preference.OnPreferenceChangeListener {
static final String TAG = "ProfileConfig";
public static final String PROFILE_SERVICE = "profile";
private ProfileManager mProfileManager;
private static final int MENU_NFC_WRITE = Menu.FIRST;
private static final int MENU_DELETE = Menu.FIRST + 1;
private static final int MENU_TRIGGERS = Menu.FIRST + 2;
private Profile mProfile;
private NamePreference mNamePreference;
private ListPreference mScreenLockModePreference;
// constant value that can be used to check return code from sub activity.
private static final int PROFILE_GROUP_DETAILS = 1;
private StreamItem[] mStreams;
private ArrayList<ConnectionItem> mConnections;
private RingModeItem mRingMode;
private AirplaneModeItem mAirplaneMode;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mStreams = new StreamItem[] {
new StreamItem(AudioManager.STREAM_ALARM, getString(R.string.alarm_volume_title)),
new StreamItem(AudioManager.STREAM_MUSIC, getString(R.string.media_volume_title)),
new StreamItem(AudioManager.STREAM_RING, getString(R.string.incoming_call_volume_title)),
new StreamItem(AudioManager.STREAM_NOTIFICATION, getString(R.string.notification_volume_title))
};
mConnections = new ArrayList<ConnectionItem>();
if (deviceSupportsBluetooth()) {
mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_BLUETOOTH, getString(R.string.toggleBluetooth)));
}
mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_GPS, getString(R.string.toggleGPS)));
mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_WIFI, getString(R.string.toggleWifi)));
mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_SYNC, getString(R.string.toggleSync)));
if (deviceSupportsMobileData(getActivity())) {
mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_MOBILEDATA, getString(R.string.toggleData)));
mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_WIFIAP, getString(R.string.toggleWifiAp)));
final TelephonyManager tm = (TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_2G3G, getString(R.string.toggle2g3g), R.array.profile_networkmode_entries));
}
}
if (WimaxHelper.isWimaxSupported(getActivity())) {
mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_WIMAX, getString(R.string.toggleWimax)));
}
if (deviceSupportsNfc(getActivity())) {
mConnections.add(new ConnectionItem(ConnectionSettings.PROFILE_CONNECTION_NFC, getString(R.string.toggleNfc)));
}
addPreferencesFromResource(R.xml.profile_config);
mProfileManager = (ProfileManager) getActivity().getSystemService(PROFILE_SERVICE);
final Bundle args = getArguments();
mProfile = (args != null) ? (Profile) args.getParcelable("Profile") : null;
if (mProfile == null) {
mProfile = new Profile(getString(R.string.new_profile_name));
mProfileManager.addProfile(mProfile);
}
setHasOptionsMenu(true);
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
if (deviceSupportsNfc(getActivity())) {
MenuItem nfc = menu.add(0, MENU_NFC_WRITE, 0, R.string.profile_write_nfc_tag)
.setIcon(R.drawable.ic_menu_nfc_writer_dark);
nfc.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
MenuItem.SHOW_AS_ACTION_WITH_TEXT);
}
MenuItem triggers = menu.add(0, MENU_TRIGGERS, 0, R.string.profile_triggers)
.setIcon(R.drawable.ic_location);
triggers.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
MenuItem.SHOW_AS_ACTION_WITH_TEXT);
MenuItem delete = menu.add(0, MENU_DELETE, 1, 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);
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_DELETE:
deleteProfile();
return true;
case MENU_NFC_WRITE:
startNFCProfileWriter();
return true;
case MENU_TRIGGERS:
startTriggerFragment();
return true;
default:
return false;
}
}
@Override
public void onResume() {
super.onResume();
mProfile = mProfileManager.getProfile(mProfile.getUuid());
fillList();
}
@Override
public void onPause() {
super.onPause();
// Save profile here
if (mProfile != null) {
mProfileManager.updateProfile(mProfile);
}
}
private void startNFCProfileWriter() {
SettingsActivity pa = (SettingsActivity) getActivity();
Intent i = new Intent(this.getActivity(), NFCProfileWriter.class);
i.putExtra(NFCProfileWriter.EXTRA_PROFILE_UUID, mProfile.getUuid().toString());
i.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP);
pa.startActivity(i);
}
private void startTriggerFragment() {
final SettingsActivity pa = (SettingsActivity) getActivity();
final Bundle args = new Bundle();
args.putParcelable("profile", mProfile);
pa.startPreferencePanel(TriggersFragment.class.getName(), args, 0, "", null, 0);
}
private void fillList() {
PreferenceScreen prefSet = getPreferenceScreen();
// Add the General section
PreferenceGroup generalPrefs = (PreferenceGroup) prefSet.findPreference("profile_general_section");
if (generalPrefs != null) {
generalPrefs.removeAll();
// Name preference
mNamePreference = new NamePreference(getActivity(), mProfile.getName());
mNamePreference.setOnPreferenceChangeListener(this);
generalPrefs.addPreference(mNamePreference);
}
// Populate system settings
PreferenceGroup systemPrefs = (PreferenceGroup) prefSet.findPreference("profile_system_settings");
if (systemPrefs != null) {
systemPrefs.removeAll();
// Ring mode preference
if (mRingMode == null) {
mRingMode = new RingModeItem();
}
RingModeSettings rms = mProfile.getRingMode();
if (rms == null) {
rms = new RingModeSettings();
mProfile.setRingMode(rms);
}
mRingMode.mSettings = rms;
ProfileRingModePreference rmp = new ProfileRingModePreference(getActivity());
rmp.setRingModeItem(mRingMode);
rmp.setTitle(R.string.ring_mode_title);
rmp.setPersistent(false);
rmp.setSummary(getActivity());
rmp.setOnPreferenceChangeListener(this);
mRingMode.mCheckbox = rmp;
systemPrefs.addPreference(rmp);
// Airplane mode preference
if (mAirplaneMode == null) {
mAirplaneMode = new AirplaneModeItem();
}
AirplaneModeSettings ams = mProfile.getAirplaneMode();
if (ams == null) {
ams = new AirplaneModeSettings();
mProfile.setAirplaneMode(ams);
}
mAirplaneMode.mSettings = ams;
ProfileAirplaneModePreference amp = new ProfileAirplaneModePreference(getActivity());
amp.setAirplaneModeItem(mAirplaneMode);
amp.setTitle(R.string.profile_airplanemode_title);
amp.setPersistent(false);
amp.setSummary(getActivity());
amp.setOnPreferenceChangeListener(this);
mAirplaneMode.mCheckbox = amp;
systemPrefs.addPreference(amp);
// Lockscreen mode preference
mScreenLockModePreference = new ListPreference(getActivity());
mScreenLockModePreference.setTitle(R.string.profile_lockmode_title);
mScreenLockModePreference.setEntries(R.array.profile_lockmode_entries);
mScreenLockModePreference.setEntryValues(R.array.profile_lockmode_values);
mScreenLockModePreference.setPersistent(false);
mScreenLockModePreference.setSummary(getResources().getStringArray(
R.array.profile_lockmode_summaries)[mProfile.getScreenLockMode()]);
mScreenLockModePreference.setValue(String.valueOf(mProfile.getScreenLockMode()));
mScreenLockModePreference.setOnPreferenceChangeListener(this);
// DevicePolicyManager dpm = (DevicePolicyManager) getSystemService(Context.DEVICE_POLICY_SERVICE);
// if (dpm.requireSecureKeyguard()) {
// mScreenLockModePreference.setEnabled(false);
// mScreenLockModePreference.setSummary(R.string.unlock_set_unlock_disabled_summary);
// }
systemPrefs.addPreference(mScreenLockModePreference);
}
// Populate the audio streams list
final AudioManager am = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
PreferenceGroup streamList = (PreferenceGroup) prefSet.findPreference("profile_volumeoverrides");
if (streamList != null) {
streamList.removeAll();
for (StreamItem stream : mStreams) {
StreamSettings settings = mProfile.getSettingsForStream(stream.mStreamId);
if (settings == null) {
settings = new StreamSettings(stream.mStreamId);
mProfile.setStreamSettings(settings);
}
stream.mSettings = settings;
StreamVolumePreference pref = new StreamVolumePreference(getActivity());
pref.setKey("stream_" + stream.mStreamId);
pref.setTitle(stream.mLabel);
pref.setSummary(getString(R.string.volume_override_summary) + " " + settings.getValue()
+ "/" + am.getStreamMaxVolume(stream.mStreamId));
pref.setPersistent(false);
pref.setStreamItem(stream);
stream.mCheckbox = pref;
streamList.addPreference(pref);
}
}
// Populate Connections list
PreferenceGroup connectionList = (PreferenceGroup) prefSet.findPreference("profile_connectionoverrides");
if (connectionList != null) {
connectionList.removeAll();
for (ConnectionItem connection : mConnections) {
String[] connectionstrings = getResources().getStringArray(connection.mChoices);
ConnectionSettings settings = mProfile.getSettingsForConnection(connection.mConnectionId);
if (settings == null) {
settings = new ConnectionSettings(connection.mConnectionId);
mProfile.setConnectionSettings(settings);
}
connection.mSettings = settings;
ProfileConnectionPreference pref = new ProfileConnectionPreference(getActivity());
pref.setKey("connection_" + connection.mConnectionId);
pref.setTitle(connection.mLabel);
pref.setSummary(connectionstrings[settings.getValue()]);
pref.setPersistent(false);
pref.setConnectionItem(connection);
connection.mCheckbox = pref;
connectionList.addPreference(pref);
}
}
// Populate Application groups
PreferenceGroup groupList = (PreferenceGroup) prefSet.findPreference("profile_appgroups");
if (groupList != null) {
groupList.removeAll();
for (ProfileGroup profileGroup : mProfile.getProfileGroups()) {
PreferenceScreen pref = new PreferenceScreen(getActivity(), null);
UUID uuid = profileGroup.getUuid();
pref.setKey(uuid.toString());
pref.setTitle(mProfileManager.getNotificationGroup(uuid).getName());
//pref.setSummary(R.string.profile_summary); // summary is repetitive, consider removing
pref.setPersistent(false);
pref.setSelectable(true);
groupList.addPreference(pref);
}
}
}
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
if (preference instanceof StreamVolumePreference) {
for (StreamItem stream : mStreams) {
if (preference == stream.mCheckbox) {
stream.mSettings.setOverride((Boolean) newValue);
}
}
} else if (preference instanceof ProfileConnectionPreference) {
for (ConnectionItem connection : mConnections) {
if (preference == connection.mCheckbox) {
connection.mSettings.setOverride((Boolean) newValue);
}
}
} else if (preference == mRingMode.mCheckbox) {
mRingMode.mSettings.setOverride((Boolean) newValue);
} else if (preference == mAirplaneMode.mCheckbox) {
mAirplaneMode.mSettings.setOverride((Boolean) newValue);
} else if (preference == mNamePreference) {
String name = mNamePreference.getName().toString();
if (!name.equals(mProfile.getName())) {
if (!mProfileManager.profileExists(name)) {
mProfile.setName(name);
} else {
mNamePreference.setName(mProfile.getName());
Toast.makeText(getActivity(), R.string.duplicate_profile_name, Toast.LENGTH_LONG).show();
}
}
} else if (preference == mScreenLockModePreference) {
mProfile.setScreenLockMode(Integer.valueOf((String) newValue));
mScreenLockModePreference.setSummary(getResources().getStringArray(
R.array.profile_lockmode_summaries)[mProfile.getScreenLockMode()]);
}
return true;
}
@Override
public boolean onPreferenceTreeClick(PreferenceScreen preferenceScreen, Preference preference) {
Log.d(TAG, "onPreferenceTreeClick(): entered" + preferenceScreen.getKey() + preference.getKey());
if (preference instanceof PreferenceScreen) {
startProfileGroupActivity(preference.getKey(), preference.getTitle().toString());
return true;
}
return super.onPreferenceTreeClick(preferenceScreen, preference);
}
private void startProfileGroupActivity(String key, String title) {
Bundle args = new Bundle();
args.putString("ProfileGroup", key.toString());
args.putParcelable("Profile", mProfile);
String header = mProfile.getName().toString() + ": " + title.toString();
SettingsActivity pa = (SettingsActivity) getActivity();
pa.startPreferencePanel(ProfileGroupConfig.class.getName(), args,
0, header, this, PROFILE_GROUP_DETAILS);
}
private void deleteProfile() {
if (mProfile.getUuid().equals(mProfileManager.getActiveProfile().getUuid())) {
Toast toast = Toast.makeText(getActivity(), getString(R.string.profile_cannot_delete),
Toast.LENGTH_SHORT);
toast.show();
} else {
AlertDialog.Builder alert = new AlertDialog.Builder(getActivity());
alert.setTitle(R.string.profile_menu_delete);
alert.setIconAttribute(android.R.attr.alertDialogIcon);
alert.setMessage(R.string.profile_delete_confirm);
alert.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
doDelete();
}
});
alert.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dialog, int id) {
dialog.cancel();
}
});
alert.create().show();
}
}
private void doDelete() {
mProfileManager.removeProfile(mProfile);
mProfile = null;
finish();
}
static class StreamItem {
int mStreamId;
String mLabel;
StreamSettings mSettings;
StreamVolumePreference mCheckbox;
public StreamItem(int streamId, String label) {
mStreamId = streamId;
mLabel = label;
}
}
static class ConnectionItem {
int mConnectionId;
String mLabel;
ConnectionSettings mSettings;
ProfileConnectionPreference mCheckbox;
int mChoices;
public ConnectionItem(int connectionId, String label) {
mConnectionId = connectionId;
mChoices = R.array.profile_connection_entries;
mLabel = label;
}
public ConnectionItem(int connectionId, String label, int choices) {
mConnectionId = connectionId;
mLabel = label;
mChoices = choices;
}
}
static class RingModeItem {
RingModeSettings mSettings;
ProfileRingModePreference mCheckbox;
public RingModeItem() {
// nothing to do
}
}
static class AirplaneModeItem {
AirplaneModeSettings mSettings;
ProfileAirplaneModePreference mCheckbox;
public AirplaneModeItem() {
// nothing to do
}
}
}
|