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
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
|
/*
* Copyright (C) 2014 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.Activity;
import android.app.AirplaneModeSettings;
import android.app.AlertDialog;
import android.app.ConnectionSettings;
import android.app.Dialog;
import android.app.Fragment;
import android.app.Profile;
import android.app.ProfileManager;
import android.app.RingModeSettings;
import android.app.StreamSettings;
import android.bluetooth.BluetoothAdapter;
import android.content.ContentResolver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.location.LocationManager;
import android.media.AudioManager;
import android.media.RingtoneManager;
import android.net.ConnectivityManager;
import android.net.wifi.WifiManager;
import android.net.wimax.WimaxHelper;
import android.nfc.NfcManager;
import android.os.AsyncTask;
import android.os.Bundle;
import android.provider.Settings;
import android.telecom.TelecomManager;
import android.telephony.TelephonyManager;
import android.text.Editable;
import android.text.TextUtils;
import android.text.TextWatcher;
import android.util.Log;
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.Button;
import android.widget.CheckBox;
import android.widget.CompoundButton;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SeekBar;
import android.widget.TextView;
import com.android.settings.R;
import com.android.settings.SubSettings;
import com.android.settings.cyanogenmod.DeviceUtils;
import com.android.settings.SettingsPreferenceFragment;
import com.android.settings.profiles.actions.ItemListAdapter;
import com.android.settings.profiles.actions.item.AirplaneModeItem;
import com.android.settings.profiles.actions.item.ConnectionOverrideItem;
import com.android.settings.profiles.actions.item.Header;
import com.android.settings.profiles.actions.item.Item;
import com.android.settings.profiles.actions.item.LockModeItem;
import com.android.settings.profiles.actions.item.ProfileNameItem;
import com.android.settings.profiles.actions.item.RingModeItem;
import com.android.settings.profiles.actions.item.TriggerItem;
import com.android.settings.profiles.actions.item.VolumeStreamItem;
import java.util.ArrayList;
import java.util.List;
import static android.app.ConnectionSettings.PROFILE_CONNECTION_2G3G4G;
import static android.app.ConnectionSettings.PROFILE_CONNECTION_BLUETOOTH;
import static android.app.ConnectionSettings.PROFILE_CONNECTION_GPS;
import static android.app.ConnectionSettings.PROFILE_CONNECTION_MOBILEDATA;
import static android.app.ConnectionSettings.PROFILE_CONNECTION_NFC;
import static android.app.ConnectionSettings.PROFILE_CONNECTION_SYNC;
import static android.app.ConnectionSettings.PROFILE_CONNECTION_WIFI;
import static android.app.ConnectionSettings.PROFILE_CONNECTION_WIFIAP;
import static android.app.ConnectionSettings.PROFILE_CONNECTION_WIMAX;
public class SetupActionsFragment extends SettingsPreferenceFragment
implements AdapterView.OnItemClickListener {
private static final int RINGTONE_REQUEST_CODE = 1000;
private static final int MENU_REMOVE = Menu.FIRST;
private static final int MENU_FILL_PROFILE = Menu.FIRST + 1;
Profile mProfile;
ItemListAdapter mAdapter;
ProfileManager mProfileManager;
ListView mListView;
boolean mNewProfileMode;
private static final int[] LOCKMODE_MAPPING = new int[] {
Profile.LockMode.DEFAULT, Profile.LockMode.INSECURE, Profile.LockMode.DISABLE
};
private static final int[] EXPANDED_DESKTOP_MAPPING = new int[] {
Profile.ExpandedDesktopMode.DEFAULT,
Profile.ExpandedDesktopMode.ENABLE,
Profile.ExpandedDesktopMode.DISABLE
};
private List<Item> mItems = new ArrayList<Item>();
public static SetupActionsFragment newInstance(Profile profile, boolean newProfile) {
SetupActionsFragment fragment = new SetupActionsFragment();
Bundle args = new Bundle();
args.putParcelable(ProfilesSettings.EXTRA_PROFILE, profile);
args.putBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, newProfile);
fragment.setArguments(args);
return fragment;
}
public SetupActionsFragment() {
// Required empty public constructor
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getArguments() != null) {
mProfile = getArguments().getParcelable(ProfilesSettings.EXTRA_PROFILE);
mNewProfileMode = getArguments().getBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, false);
}
mProfileManager = (ProfileManager) getActivity().getSystemService(Context.PROFILE_SERVICE);
mAdapter = new ItemListAdapter(getActivity(), mItems);
rebuildItemList();
setHasOptionsMenu(true);
if (mNewProfileMode) {
requestFillProfileFromSettingsDialog();
}
}
private void rebuildItemList() {
mItems.clear();
// general prefs
mItems.add(new Header(getString(R.string.profile_name_title)));
mItems.add(new ProfileNameItem(mProfile));
// triggers
mItems.add(new Header(getString(R.string.profile_triggers_header)));
mItems.add(generateTriggerItem(TriggerItem.WIFI));
if (DeviceUtils.deviceSupportsBluetooth()) {
mItems.add(generateTriggerItem(TriggerItem.BLUETOOTH));
}
if (DeviceUtils.deviceSupportsNfc(getActivity())) {
mItems.add(generateTriggerItem(TriggerItem.NFC));
}
// connection overrides
mItems.add(new Header(getString(R.string.profile_connectionoverrides_title)));
if (DeviceUtils.deviceSupportsBluetooth()) {
mItems.add(new ConnectionOverrideItem(PROFILE_CONNECTION_BLUETOOTH,
mProfile.getSettingsForConnection(PROFILE_CONNECTION_BLUETOOTH)));
}
mItems.add(generateConnectionOverrideItem(PROFILE_CONNECTION_GPS));
mItems.add(generateConnectionOverrideItem(PROFILE_CONNECTION_WIFI));
mItems.add(generateConnectionOverrideItem(PROFILE_CONNECTION_SYNC));
if (DeviceUtils.deviceSupportsMobileData(getActivity())) {
mItems.add(generateConnectionOverrideItem(PROFILE_CONNECTION_MOBILEDATA));
mItems.add(generateConnectionOverrideItem(PROFILE_CONNECTION_WIFIAP));
final TelephonyManager tm =
(TelephonyManager) getActivity().getSystemService(Context.TELEPHONY_SERVICE);
if (tm.getPhoneType() == TelephonyManager.PHONE_TYPE_GSM) {
mItems.add(generateConnectionOverrideItem(PROFILE_CONNECTION_2G3G4G));
}
}
if (WimaxHelper.isWimaxSupported(getActivity())) {
mItems.add(generateConnectionOverrideItem(PROFILE_CONNECTION_WIMAX));
}
if (DeviceUtils.deviceSupportsNfc(getActivity())) {
mItems.add(generateConnectionOverrideItem(PROFILE_CONNECTION_NFC));
}
// add volume streams
mItems.add(new Header(getString(R.string.profile_volumeoverrides_title)));
mItems.add(generateVolumeStreamItem(AudioManager.STREAM_ALARM));
mItems.add(generateVolumeStreamItem(AudioManager.STREAM_MUSIC));
mItems.add(generateVolumeStreamItem(AudioManager.STREAM_RING));
mItems.add(generateVolumeStreamItem(AudioManager.STREAM_NOTIFICATION));
// system settings
mItems.add(new Header(getString(R.string.profile_system_settings_title)));
mItems.add(new RingModeItem(mProfile.getRingMode()));
mItems.add(new AirplaneModeItem(mProfile.getAirplaneMode()));
mItems.add(new LockModeItem(mProfile));
mAdapter.notifyDataSetChanged();
}
@Override
public void onResume() {
super.onResume();
}
@Override
public void onCreateOptionsMenu(Menu menu, MenuInflater inflater) {
super.onCreateOptionsMenu(menu, inflater);
if (!mNewProfileMode) {
menu.add(0, MENU_REMOVE, 0, R.string.profile_menu_delete_title)
.setIcon(R.drawable.ic_actionbar_delete)
.setAlphabeticShortcut('d')
.setEnabled(true)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_IF_ROOM |
MenuItem.SHOW_AS_ACTION_WITH_TEXT);
menu.add(0, MENU_FILL_PROFILE, 0, R.string.profile_menu_fill_from_state)
.setAlphabeticShortcut('f')
.setEnabled(true)
.setShowAsAction(MenuItem.SHOW_AS_ACTION_NEVER);
}
}
@Override
public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_REMOVE:
requestRemoveProfileDialog();
return true;
case MENU_FILL_PROFILE:
requestFillProfileFromSettingsDialog();
return true;
}
return super.onOptionsItemSelected(item);
}
private ConnectionOverrideItem generateConnectionOverrideItem(int connectionId) {
ConnectionSettings settings = mProfile.getSettingsForConnection(connectionId);
if (settings == null) {
settings = new ConnectionSettings(connectionId);
mProfile.setConnectionSettings(settings);
}
return new ConnectionOverrideItem(connectionId, settings);
}
private VolumeStreamItem generateVolumeStreamItem(int stream) {
StreamSettings settings = mProfile.getSettingsForStream(stream);
if (settings == null) {
settings = new StreamSettings(stream);
mProfile.setStreamSettings(settings);
}
return new VolumeStreamItem(stream, settings);
}
private TriggerItem generateTriggerItem(int whichTrigger) {
return new TriggerItem(mProfile, whichTrigger);
}
@Override
public void onViewCreated(View view, Bundle savedInstanceState) {
super.onViewCreated(view, savedInstanceState);
if (mNewProfileMode) {
TextView desc = new TextView(getActivity());
int descPadding = getResources().getDimensionPixelSize(
R.dimen.profile_instruction_padding);
desc.setPadding(descPadding, descPadding, descPadding, descPadding);
desc.setText(R.string.profile_setup_actions_description);
getListView().addHeaderView(desc, null, false);
}
}
private void updateProfile() {
mProfileManager.updateProfile(mProfile);
}
@Override
public void onActivityCreated(Bundle savedInstanceState) {
super.onActivityCreated(savedInstanceState);
mListView.setAdapter(mAdapter);
getActivity().getActionBar().setTitle(mNewProfileMode
? R.string.profile_setup_actions_title
: R.string.profile_setup_actions_title_config);
}
private void requestFillProfileFromSettingsDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(R.string.profile_populate_profile_from_state);
builder.setNegativeButton(R.string.no, null);
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
fillProfileFromCurrentSettings();
dialog.dismiss();
}
});
builder.show();
}
private void fillProfileFromCurrentSettings() {
new AsyncTask<Profile, Void, Void>() {
@Override
protected Void doInBackground(Profile... params) {
// bt
if (DeviceUtils.deviceSupportsBluetooth()) {
mProfile.setConnectionSettings(
new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_BLUETOOTH,
BluetoothAdapter.getDefaultAdapter().isEnabled() ? 1 : 0,
true));
}
// gps
LocationManager locationManager = (LocationManager)
getSystemService(Context.LOCATION_SERVICE);
boolean gpsEnabled = locationManager.
isProviderEnabled(LocationManager.GPS_PROVIDER);
mProfile.setConnectionSettings(
new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_GPS,
gpsEnabled ? 1 : 0, true));
// wifi
WifiManager wifiManager = (WifiManager) getSystemService(Context.WIFI_SERVICE);
mProfile.setConnectionSettings(
new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_WIFI,
wifiManager.isWifiEnabled() ? 1 : 0, true));
// auto sync data
mProfile.setConnectionSettings(
new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_SYNC,
ContentResolver.getMasterSyncAutomatically() ? 1 : 0, true));
// mobile data
if (DeviceUtils.deviceSupportsMobileData(getActivity())) {
ConnectivityManager cm = (ConnectivityManager)
getSystemService(Context.CONNECTIVITY_SERVICE);
mProfile.setConnectionSettings(
new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_MOBILEDATA,
cm.getMobileDataEnabled() ? 1 : 0, true));
}
// wifi hotspot
mProfile.setConnectionSettings(
new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_WIFIAP,
wifiManager.isWifiApEnabled() ? 1 : 0, true));
// 2g/3g/4g
// skipping this one
// nfc
if (DeviceUtils.deviceSupportsNfc(getActivity())) {
NfcManager nfcManager = (NfcManager) getSystemService(Context.NFC_SERVICE);
mProfile.setConnectionSettings(
new ConnectionSettings(ConnectionSettings.PROFILE_CONNECTION_NFC,
nfcManager.getDefaultAdapter().isEnabled() ? 1 : 0, true));
}
// alarm volume
final AudioManager am = (AudioManager) getActivity()
.getSystemService(Context.AUDIO_SERVICE);
mProfile.setStreamSettings(new StreamSettings(AudioManager.STREAM_ALARM,
am.getStreamVolume(AudioManager.STREAM_ALARM), true));
// media volume
mProfile.setStreamSettings(new StreamSettings(AudioManager.STREAM_MUSIC,
am.getStreamVolume(AudioManager.STREAM_MUSIC), true));
// ringtone volume
mProfile.setStreamSettings(new StreamSettings(AudioManager.STREAM_RING,
am.getStreamVolume(AudioManager.STREAM_RING), true));
// notification volume
mProfile.setStreamSettings(new StreamSettings(AudioManager.STREAM_NOTIFICATION,
am.getStreamVolume(AudioManager.STREAM_NOTIFICATION), true));
// ring mode
String ringValue;
switch (am.getRingerMode()) {
default:
case AudioManager.RINGER_MODE_NORMAL:
ringValue = "normal";
break;
case AudioManager.RINGER_MODE_SILENT:
ringValue = "mute";
break;
case AudioManager.RINGER_MODE_VIBRATE:
ringValue = "vibrate";
break;
}
mProfile.setRingMode(new RingModeSettings(ringValue, true));
// airplane mode
boolean airplaneMode = Settings.Global.getInt(getActivity().getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON, 0) != 0;
mProfile.setAirplaneMode(new AirplaneModeSettings(airplaneMode ? 1 : 0, true));
// lock screen mode
// populated only from profiles, so we can read the current profile,
// but let's skip this one
updateProfile();
return null;
}
@Override
protected void onPostExecute(Void aVoid) {
super.onPostExecute(aVoid);
rebuildItemList();
}
}.execute(mProfile);
}
private void requestRemoveProfileDialog() {
Profile current = mProfileManager.getActiveProfile();
if (mProfile.getUuid().equals(current.getUuid())) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getString(R.string.profile_remove_current_profile));
builder.setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
return;
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setMessage(getString(R.string.profile_remove_dialog_message, mProfile.getName()));
builder.setPositiveButton(R.string.yes, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
mProfileManager.removeProfile(mProfile);
finishFragment();
}
});
builder.setNegativeButton(R.string.no, null);
builder.show();
}
private void requestLockscreenModeDialog() {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final String[] lockEntries =
getResources().getStringArray(R.array.profile_lockmode_entries);
int defaultIndex = 0; // no action
for (int i = 0; i < LOCKMODE_MAPPING.length; i++) {
if (LOCKMODE_MAPPING[i] == mProfile.getScreenLockMode()) {
defaultIndex = i;
break;
}
}
builder.setTitle(R.string.profile_lockmode_title);
builder.setSingleChoiceItems(lockEntries, defaultIndex,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
mProfile.setScreenLockMode(LOCKMODE_MAPPING[item]);
updateProfile();
mAdapter.notifyDataSetChanged();
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}
private void requestAirplaneModeDialog(final AirplaneModeSettings setting) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final String[] connectionNames =
getResources().getStringArray(R.array.profile_action_generic_connection_entries);
int defaultIndex = 0; // no action
if (setting.isOverride()) {
if (setting.getValue() == 1) {
defaultIndex = 2; // enabled
} else {
defaultIndex = 1; // disabled
}
}
builder.setTitle(R.string.profile_airplanemode_title);
builder.setSingleChoiceItems(connectionNames, defaultIndex,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0: // disable override
setting.setOverride(false);
break;
case 1: // enable override, disable
setting.setOverride(true);
setting.setValue(0);
break;
case 2: // enable override, enable
setting.setOverride(true);
setting.setValue(1);
break;
}
mProfile.setAirplaneMode(setting);
mAdapter.notifyDataSetChanged();
updateProfile();
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}
private void requestProfileRingMode() {
// Launch the ringtone picker
Intent intent = new Intent(RingtoneManager.ACTION_RINGTONE_PICKER);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_DEFAULT, false);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_SHOW_SILENT, true);
intent.putExtra(RingtoneManager.EXTRA_RINGTONE_TYPE, RingtoneManager.TYPE_RINGTONE);
startActivityForResult(intent, RINGTONE_REQUEST_CODE);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
}
private void requestRingModeDialog(final RingModeSettings setting) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final String[] values = getResources().getStringArray(R.array.ring_mode_values);
final String[] names = getResources().getStringArray(R.array.ring_mode_entries);
int defaultIndex = 0; // normal by default
if (setting.isOverride()) {
if (setting.getValue().equals(values[0] /* normal */)) {
defaultIndex = 0;
} else if (setting.getValue().equals(values[1] /* vibrate */)) {
defaultIndex = 1; // enabled
} else if (setting.getValue().equals(values[2] /* mute */)) {
defaultIndex = 2; // mute
}
} else {
defaultIndex = 3;
}
builder.setTitle(R.string.ring_mode_title);
builder.setSingleChoiceItems(names, defaultIndex,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0: // enable override, normal
setting.setOverride(true);
setting.setValue(values[0]);
break;
case 1: // enable override, vibrate
setting.setOverride(true);
setting.setValue(values[1]);
break;
case 2: // enable override, mute
setting.setOverride(true);
setting.setValue(values[2]);
break;
case 3:
setting.setOverride(false);
break;
}
mProfile.setRingMode(setting);
mAdapter.notifyDataSetChanged();
updateProfile();
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}
private void requestConnectionOverrideDialog(final ConnectionSettings setting) {
if (setting == null) {
throw new UnsupportedOperationException("connection setting cannot be null yo");
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final String[] connectionNames =
getResources().getStringArray(R.array.profile_action_generic_connection_entries);
int defaultIndex = 0; // no action
if (setting.isOverride()) {
if (setting.getValue() == 1) {
defaultIndex = 2; // enabled
} else {
defaultIndex = 1; // disabled
}
}
builder.setTitle(ConnectionOverrideItem.getConnectionTitle(setting.getConnectionId()));
builder.setSingleChoiceItems(connectionNames, defaultIndex,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case 0: // disable override
setting.setOverride(false);
break;
case 1: // enable override, disable
setting.setOverride(true);
setting.setValue(0);
break;
case 2: // enable override, enable
setting.setOverride(true);
setting.setValue(1);
break;
}
mProfile.setConnectionSettings(setting);
mAdapter.notifyDataSetChanged();
updateProfile();
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}
private void requestMobileConnectionOverrideDialog(final ConnectionSettings setting) {
if (setting == null) {
throw new UnsupportedOperationException("connection setting cannot be null yo");
}
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
final String[] connectionNames =
getResources().getStringArray(R.array.profile_networkmode_entries_4g);
int defaultIndex = ConnectionOverrideItem.CM_MODE_UNCHANGED; // no action
if (setting.isOverride()) {
defaultIndex = setting.getValue();
}
builder.setTitle(ConnectionOverrideItem.getConnectionTitle(setting.getConnectionId()));
builder.setSingleChoiceItems(connectionNames, defaultIndex,
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int item) {
switch (item) {
case ConnectionOverrideItem.CM_MODE_UNCHANGED:
setting.setOverride(false);
break;
default:
setting.setOverride(true);
setting.setValue(item);
}
mProfile.setConnectionSettings(setting);
mAdapter.notifyDataSetChanged();
updateProfile();
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, null);
builder.show();
}
public void requestVolumeDialog(int streamId,
final StreamSettings streamSettings) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(VolumeStreamItem.getNameForStream(streamId));
final AudioManager am = (AudioManager) getActivity().getSystemService(Context.AUDIO_SERVICE);
final LayoutInflater inflater = LayoutInflater.from(getActivity());
final View view = inflater.inflate(R.layout.dialog_profiles_volume_override, null);
final SeekBar seekBar = (SeekBar) view.findViewById(R.id.seekbar);
final CheckBox override = (CheckBox) view.findViewById(R.id.checkbox);
override.setChecked(streamSettings.isOverride());
override.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
@Override
public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
streamSettings.setOverride(isChecked);
seekBar.setEnabled(isChecked);
mProfile.setStreamSettings(streamSettings);
mAdapter.notifyDataSetChanged();
updateProfile();
}
});
seekBar.setEnabled(streamSettings.isOverride());
seekBar.setMax(am.getStreamMaxVolume(streamId));
seekBar.setProgress(streamSettings.getValue());
builder.setView(view);
builder.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
int value = seekBar.getProgress();
streamSettings.setValue(value);
mProfile.setStreamSettings(streamSettings);
mAdapter.notifyDataSetChanged();
updateProfile();
dialog.dismiss();
}
});
builder.setNegativeButton(android.R.string.cancel, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
dialog.dismiss();
}
});
builder.show();
}
private void requestProfileName() {
LayoutInflater inflater = LayoutInflater.from(getActivity());
View dialogView = inflater.inflate(R.layout.profile_name_dialog, null);
final EditText entry = (EditText) dialogView.findViewById(R.id.name);
entry.setText(mProfile.getName());
entry.setSelectAllOnFocus(true);
final AlertDialog alertDialog = new AlertDialog.Builder(getActivity())
.setTitle(R.string.rename_dialog_title)
.setView(dialogView)
.setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
String value = entry.getText().toString();
mProfile.setName(value);
mAdapter.notifyDataSetChanged();
updateProfile();
}
})
.setNegativeButton(android.R.string.cancel, null)
.create();
entry.addTextChangedListener(new TextWatcher() {
@Override
public void beforeTextChanged(CharSequence s, int start, int count, int after) {
}
@Override
public void onTextChanged(CharSequence s, int start, int before, int count) {
}
@Override
public void afterTextChanged(Editable s) {
final String str = s.toString();
final boolean empty = TextUtils.isEmpty(str)
|| TextUtils.getTrimmedLength(str) == 0;
alertDialog.getButton(AlertDialog.BUTTON_POSITIVE).setEnabled(!empty);
}
});
alertDialog.show();
}
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.fragment_setup_actions, container, false);
mListView = (ListView) view.findViewById(android.R.id.list);
mListView.setOnItemClickListener(this);
if (mNewProfileMode) {
view.findViewById(R.id.back).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
getActivity().setResult(Activity.RESULT_CANCELED);
finishFragment();
}
});
view.findViewById(R.id.finish).setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
mProfileManager.addProfile(mProfile);
getActivity().setResult(Activity.RESULT_OK);
finishFragment();
}
});
} else {
view.findViewById(R.id.bottom_buttons).setVisibility(View.GONE);
}
return view;
}
@Override
public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
final Item itemAtPosition = (Item) parent.getItemAtPosition(position);
if (itemAtPosition instanceof AirplaneModeItem) {
AirplaneModeItem item = (AirplaneModeItem) itemAtPosition;
requestAirplaneModeDialog(item.getSettings());
} else if (itemAtPosition instanceof LockModeItem) {
requestLockscreenModeDialog();
} else if (itemAtPosition instanceof RingModeItem) {
RingModeItem item = (RingModeItem) itemAtPosition;
requestRingModeDialog(item.getSettings());
} else if (itemAtPosition instanceof ConnectionOverrideItem) {
ConnectionOverrideItem item = (ConnectionOverrideItem) itemAtPosition;
if (item.getConnectionType() == ConnectionSettings.PROFILE_CONNECTION_2G3G4G) {
requestMobileConnectionOverrideDialog(item.getSettings());
} else {
requestConnectionOverrideDialog(item.getSettings());
}
} else if (itemAtPosition instanceof VolumeStreamItem) {
VolumeStreamItem item = (VolumeStreamItem) itemAtPosition;
requestVolumeDialog(item.getStreamType(), item.getSettings());
} else if (itemAtPosition instanceof ProfileNameItem) {
requestProfileName();
} else if (itemAtPosition instanceof TriggerItem) {
TriggerItem item = (TriggerItem) itemAtPosition;
openTriggersFragment(item.getTriggerType());
}
}
private void openTriggersFragment(int openTo) {
Bundle args = new Bundle();
args.putParcelable(ProfilesSettings.EXTRA_PROFILE, mProfile);
args.putBoolean(ProfilesSettings.EXTRA_NEW_PROFILE, false);
args.putInt(SetupTriggersFragment.EXTRA_INITIAL_PAGE, openTo);
SubSettings pa = (SubSettings) getActivity();
pa.startPreferencePanel(SetupTriggersFragment.class.getCanonicalName(), args,
R.string.profile_profile_manage, null, null, 0);
}
}
|