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
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
|
/*
* Copyright (C) 2017 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 com.android.systemui.globalactions;
import static android.view.ViewGroup.LayoutParams.MATCH_PARENT;
import static android.view.WindowManager.LayoutParams.LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.SOME_AUTH_REQUIRED_AFTER_USER_REQUEST;
import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_NOT_REQUIRED;
import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN;
import android.app.ActivityManager;
import android.app.Dialog;
import android.app.KeyguardManager;
import android.app.PendingIntent;
import android.app.StatusBarManager;
import android.app.WallpaperManager;
import android.app.admin.DevicePolicyManager;
import android.app.trust.TrustManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.UserInfo;
import android.database.ContentObserver;
import android.graphics.drawable.Drawable;
import android.media.AudioManager;
import android.net.ConnectivityManager;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.Message;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.SystemProperties;
import android.os.UserHandle;
import android.os.UserManager;
import android.os.Vibrator;
import android.provider.Settings;
import android.service.dreams.DreamService;
import android.service.dreams.IDreamManager;
import android.telephony.PhoneStateListener;
import android.telephony.ServiceState;
import android.telephony.TelephonyManager;
import android.text.TextUtils;
import android.util.ArraySet;
import android.util.FeatureFlagUtils;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.view.WindowManagerGlobal;
import android.view.accessibility.AccessibilityEvent;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.ImageView.ScaleType;
import android.widget.TextView;
import com.android.internal.R;
import com.android.internal.colorextraction.ColorExtractor;
import com.android.internal.colorextraction.ColorExtractor.GradientColors;
import com.android.internal.colorextraction.drawable.ScrimDrawable;
import com.android.internal.logging.MetricsLogger;
import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
import com.android.internal.statusbar.IStatusBarService;
import com.android.internal.telephony.TelephonyIntents;
import com.android.internal.telephony.TelephonyProperties;
import com.android.internal.util.EmergencyAffordanceManager;
import com.android.internal.util.ScreenRecordHelper;
import com.android.internal.util.ScreenshotHelper;
import com.android.internal.view.RotationPolicy;
import com.android.internal.widget.LockPatternUtils;
import com.android.systemui.Dependency;
import com.android.systemui.Interpolators;
import com.android.systemui.MultiListLayout;
import com.android.systemui.MultiListLayout.MultiListAdapter;
import com.android.systemui.colorextraction.SysuiColorExtractor;
import com.android.systemui.plugins.ActivityStarter;
import com.android.systemui.plugins.GlobalActions.GlobalActionsManager;
import com.android.systemui.plugins.GlobalActionsPanelPlugin;
import com.android.systemui.statusbar.phone.ScrimController;
import com.android.systemui.statusbar.phone.StatusBarWindowController;
import com.android.systemui.statusbar.phone.UnlockMethodCache;
import com.android.systemui.statusbar.policy.ConfigurationController;
import com.android.systemui.util.EmergencyDialerConstants;
import com.android.systemui.util.leak.RotationUtils;
import com.android.systemui.volume.SystemUIInterpolators.LogAccelerateInterpolator;
import java.util.ArrayList;
import java.util.List;
/**
* Helper to show the global actions dialog. Each item is an {@link Action} that
* may show depending on whether the keyguard is showing, and whether the device
* is provisioned.
*/
public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
DialogInterface.OnShowListener, ConfigurationController.ConfigurationListener {
static public final String SYSTEM_DIALOG_REASON_KEY = "reason";
static public final String SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS = "globalactions";
static public final String SYSTEM_DIALOG_REASON_DREAM = "dream";
private static final String TAG = "GlobalActionsDialog";
private static final boolean SHOW_SILENT_TOGGLE = true;
/* Valid settings for global actions keys.
* see config.xml config_globalActionList */
private static final String GLOBAL_ACTION_KEY_POWER = "power";
private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
private static final String GLOBAL_ACTION_KEY_USERS = "users";
private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
private static final String GLOBAL_ACTION_KEY_LOCKDOWN = "lockdown";
private static final String GLOBAL_ACTION_KEY_VOICEASSIST = "voiceassist";
private static final String GLOBAL_ACTION_KEY_ASSIST = "assist";
private static final String GLOBAL_ACTION_KEY_RESTART = "restart";
private static final String GLOBAL_ACTION_KEY_LOGOUT = "logout";
private static final String GLOBAL_ACTION_KEY_EMERGENCY = "emergency";
private static final String GLOBAL_ACTION_KEY_SCREENSHOT = "screenshot";
private final Context mContext;
private final GlobalActionsManager mWindowManagerFuncs;
private final AudioManager mAudioManager;
private final IDreamManager mDreamManager;
private final DevicePolicyManager mDevicePolicyManager;
private final LockPatternUtils mLockPatternUtils;
private final KeyguardManager mKeyguardManager;
private ArrayList<Action> mItems;
private ActionsDialog mDialog;
private Action mSilentModeAction;
private ToggleAction mAirplaneModeOn;
private MyAdapter mAdapter;
private boolean mKeyguardShowing = false;
private boolean mDeviceProvisioned = false;
private ToggleAction.State mAirplaneState = ToggleAction.State.Off;
private boolean mIsWaitingForEcmExit = false;
private boolean mHasTelephony;
private boolean mHasVibrator;
private boolean mHasLogoutButton;
private boolean mHasLockdownButton;
private final boolean mShowSilentToggle;
private final EmergencyAffordanceManager mEmergencyAffordanceManager;
private final ScreenshotHelper mScreenshotHelper;
private final ScreenRecordHelper mScreenRecordHelper;
private final ActivityStarter mActivityStarter;
private GlobalActionsPanelPlugin mPanelPlugin;
/**
* @param context everything needs a context :(
*/
public GlobalActionsDialog(Context context, GlobalActionsManager windowManagerFuncs) {
mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme);
mWindowManagerFuncs = windowManagerFuncs;
mAudioManager = (AudioManager) mContext.getSystemService(Context.AUDIO_SERVICE);
mDreamManager = IDreamManager.Stub.asInterface(
ServiceManager.getService(DreamService.DREAM_SERVICE));
mDevicePolicyManager = (DevicePolicyManager) mContext.getSystemService(
Context.DEVICE_POLICY_SERVICE);
mLockPatternUtils = new LockPatternUtils(mContext);
mKeyguardManager = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE);
// receive broadcasts
IntentFilter filter = new IntentFilter();
filter.addAction(Intent.ACTION_CLOSE_SYSTEM_DIALOGS);
filter.addAction(Intent.ACTION_SCREEN_OFF);
filter.addAction(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED);
context.registerReceiver(mBroadcastReceiver, filter);
ConnectivityManager cm = (ConnectivityManager)
context.getSystemService(Context.CONNECTIVITY_SERVICE);
mHasTelephony = cm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE);
// get notified of phone state changes
TelephonyManager telephonyManager =
(TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
telephonyManager.listen(mPhoneStateListener, PhoneStateListener.LISTEN_SERVICE_STATE);
mContext.getContentResolver().registerContentObserver(
Settings.Global.getUriFor(Settings.Global.AIRPLANE_MODE_ON), true,
mAirplaneModeObserver);
Vibrator vibrator = (Vibrator) mContext.getSystemService(Context.VIBRATOR_SERVICE);
mHasVibrator = vibrator != null && vibrator.hasVibrator();
mShowSilentToggle = SHOW_SILENT_TOGGLE && !mContext.getResources().getBoolean(
R.bool.config_useFixedVolume);
mEmergencyAffordanceManager = new EmergencyAffordanceManager(context);
mScreenshotHelper = new ScreenshotHelper(context);
mScreenRecordHelper = new ScreenRecordHelper(context);
Dependency.get(ConfigurationController.class).addCallback(this);
mActivityStarter = Dependency.get(ActivityStarter.class);
UnlockMethodCache unlockMethodCache = UnlockMethodCache.getInstance(context);
unlockMethodCache.addListener(
() -> {
if (mDialog != null && mDialog.mPanelController != null) {
boolean locked = !unlockMethodCache.canSkipBouncer();
mDialog.mPanelController.onDeviceLockStateChanged(locked);
}
});
}
/**
* Show the global actions dialog (creating if necessary)
*
* @param keyguardShowing True if keyguard is showing
*/
public void showDialog(boolean keyguardShowing, boolean isDeviceProvisioned,
GlobalActionsPanelPlugin panelPlugin) {
mKeyguardShowing = keyguardShowing;
mDeviceProvisioned = isDeviceProvisioned;
mPanelPlugin = panelPlugin;
if (mDialog != null) {
mDialog.dismiss();
mDialog = null;
// Show delayed, so that the dismiss of the previous dialog completes
mHandler.sendEmptyMessage(MESSAGE_SHOW);
} else {
handleShow();
}
}
/**
* Dismiss the global actions dialog, if it's currently shown
*/
public void dismissDialog() {
mHandler.removeMessages(MESSAGE_DISMISS);
mHandler.sendEmptyMessage(MESSAGE_DISMISS);
}
private void awakenIfNecessary() {
if (mDreamManager != null) {
try {
if (mDreamManager.isDreaming()) {
mDreamManager.awaken();
}
} catch (RemoteException e) {
// we tried
}
}
}
private void handleShow() {
awakenIfNecessary();
mDialog = createDialog();
prepareDialog();
// If we only have 1 item and it's a simple press action, just do this action.
if (mAdapter.getCount() == 1
&& mAdapter.getItem(0) instanceof SinglePressAction
&& !(mAdapter.getItem(0) instanceof LongPressAction)) {
((SinglePressAction) mAdapter.getItem(0)).onPress();
} else {
WindowManager.LayoutParams attrs = mDialog.getWindow().getAttributes();
attrs.setTitle("ActionsDialog");
attrs.layoutInDisplayCutoutMode = LAYOUT_IN_DISPLAY_CUTOUT_MODE_ALWAYS;
mDialog.getWindow().setAttributes(attrs);
mDialog.show();
mWindowManagerFuncs.onGlobalActionsShown();
}
}
/**
* Create the global actions dialog.
*
* @return A new dialog.
*/
private ActionsDialog createDialog() {
// Simple toggle style if there's no vibrator, otherwise use a tri-state
if (!mHasVibrator) {
mSilentModeAction = new SilentModeToggleAction();
} else {
mSilentModeAction = new SilentModeTriStateAction(mAudioManager, mHandler);
}
mAirplaneModeOn = new ToggleAction(
R.drawable.ic_lock_airplane_mode,
R.drawable.ic_lock_airplane_mode_off,
R.string.global_actions_toggle_airplane_mode,
R.string.global_actions_airplane_mode_on_status,
R.string.global_actions_airplane_mode_off_status) {
void onToggle(boolean on) {
if (mHasTelephony && Boolean.parseBoolean(
SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE))) {
mIsWaitingForEcmExit = true;
// Launch ECM exit dialog
Intent ecmDialogIntent =
new Intent(TelephonyIntents.ACTION_SHOW_NOTICE_ECM_BLOCK_OTHERS, null);
ecmDialogIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
mContext.startActivity(ecmDialogIntent);
} else {
changeAirplaneModeSystemSetting(on);
}
}
@Override
protected void changeStateFromPress(boolean buttonOn) {
if (!mHasTelephony) return;
// In ECM mode airplane state cannot be changed
if (!(Boolean.parseBoolean(
SystemProperties.get(TelephonyProperties.PROPERTY_INECM_MODE)))) {
mState = buttonOn ? State.TurningOn : State.TurningOff;
mAirplaneState = mState;
}
}
public boolean showDuringKeyguard() {
return true;
}
public boolean showBeforeProvisioning() {
return false;
}
};
onAirplaneModeChanged();
mItems = new ArrayList<Action>();
String[] defaultActions = mContext.getResources().getStringArray(
R.array.config_globalActionsList);
ArraySet<String> addedKeys = new ArraySet<String>();
mHasLogoutButton = false;
mHasLockdownButton = false;
for (int i = 0; i < defaultActions.length; i++) {
String actionKey = defaultActions[i];
if (addedKeys.contains(actionKey)) {
// If we already have added this, don't add it again.
continue;
}
if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
mItems.add(new PowerAction());
} else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
mItems.add(mAirplaneModeOn);
} else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
if (Settings.Global.getInt(mContext.getContentResolver(),
Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
mItems.add(new BugReportAction());
}
} else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
if (mShowSilentToggle) {
mItems.add(mSilentModeAction);
}
} else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
addUsersToMenu(mItems);
}
} else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
mItems.add(getSettingsAction());
} else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
if (Settings.Secure.getIntForUser(mContext.getContentResolver(),
Settings.Secure.LOCKDOWN_IN_POWER_MENU, 0, getCurrentUser().id) != 0
&& shouldDisplayLockdown()) {
mItems.add(getLockdownAction());
mHasLockdownButton = true;
}
} else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
mItems.add(getVoiceAssistAction());
} else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
mItems.add(getAssistAction());
} else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
mItems.add(new RestartAction());
} else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
mItems.add(new ScreenshotAction());
} else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
if (mDevicePolicyManager.isLogoutEnabled()
&& getCurrentUser().id != UserHandle.USER_SYSTEM) {
mItems.add(new LogoutAction());
mHasLogoutButton = true;
}
} else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
if (!mEmergencyAffordanceManager.needsEmergencyAffordance()) {
mItems.add(new EmergencyDialerAction());
}
} else {
Log.e(TAG, "Invalid global action key " + actionKey);
}
// Add here so we don't add more than one.
addedKeys.add(actionKey);
}
if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
mItems.add(new EmergencyAffordanceAction());
}
mAdapter = new MyAdapter();
GlobalActionsPanelPlugin.PanelViewController panelViewController =
mPanelPlugin != null
? mPanelPlugin.onPanelShown(
new GlobalActionsPanelPlugin.Callbacks() {
@Override
public void dismissGlobalActionsMenu() {
if (mDialog != null) {
mDialog.dismiss();
}
}
@Override
public void startPendingIntentDismissingKeyguard(
PendingIntent intent) {
mActivityStarter
.startPendingIntentDismissingKeyguard(intent);
}
},
mKeyguardManager.isDeviceLocked())
: null;
ActionsDialog dialog = new ActionsDialog(mContext, mAdapter, panelViewController);
dialog.setCanceledOnTouchOutside(false); // Handled by the custom class.
dialog.setKeyguardShowing(mKeyguardShowing);
dialog.setOnDismissListener(this);
dialog.setOnShowListener(this);
return dialog;
}
private boolean shouldDisplayLockdown() {
int userId = getCurrentUser().id;
// Lockdown is meaningless without a place to go.
if (!mKeyguardManager.isDeviceSecure(userId)) {
return false;
}
// Only show the lockdown button if the device isn't locked down (for whatever reason).
int state = mLockPatternUtils.getStrongAuthForUser(userId);
return (state == STRONG_AUTH_NOT_REQUIRED
|| state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
}
@Override
public void onUiModeChanged() {
mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
if (mDialog != null && mDialog.isShowing()) {
mDialog.refreshDialog();
}
}
public void destroy() {
Dependency.get(ConfigurationController.class).removeCallback(this);
}
private final class PowerAction extends SinglePressAction implements LongPressAction {
private PowerAction() {
super(R.drawable.ic_lock_power_off,
R.string.global_action_power_off);
}
@Override
public boolean onLongPress() {
UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
mWindowManagerFuncs.reboot(true);
return true;
}
return false;
}
@Override
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return true;
}
@Override
public void onPress() {
// shutdown by making sure radio and power are handled accordingly.
mWindowManagerFuncs.shutdown();
}
}
private abstract class EmergencyAction extends SinglePressAction {
EmergencyAction(int iconResId, int messageResId) {
super(iconResId, messageResId);
}
@Override
public boolean shouldBeSeparated() {
return shouldUseSeparatedView();
}
@Override
public View create(
Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
View v = super.create(context, convertView, parent, inflater);
int textColor;
if (shouldBeSeparated()) {
textColor = v.getResources().getColor(
com.android.systemui.R.color.global_actions_alert_text);
} else {
textColor = v.getResources().getColor(
com.android.systemui.R.color.global_actions_text);
}
TextView messageView = v.findViewById(R.id.message);
messageView.setTextColor(textColor);
messageView.setSelected(true); // necessary for marquee to work
ImageView icon = (ImageView) v.findViewById(R.id.icon);
icon.getDrawable().setTint(textColor);
return v;
}
@Override
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return true;
}
}
private class EmergencyAffordanceAction extends EmergencyAction {
EmergencyAffordanceAction() {
super(R.drawable.emergency_icon,
R.string.global_action_emergency);
}
@Override
public void onPress() {
mEmergencyAffordanceManager.performEmergencyCall();
}
}
private class EmergencyDialerAction extends EmergencyAction {
private EmergencyDialerAction() {
super(com.android.systemui.R.drawable.ic_emergency_star,
R.string.global_action_emergency);
}
@Override
public void onPress() {
MetricsLogger.action(mContext, MetricsEvent.ACTION_EMERGENCY_DIALER_FROM_POWER_MENU);
Intent intent = new Intent(EmergencyDialerConstants.ACTION_DIAL);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
| Intent.FLAG_ACTIVITY_EXCLUDE_FROM_RECENTS
| Intent.FLAG_ACTIVITY_CLEAR_TOP);
intent.putExtra(EmergencyDialerConstants.EXTRA_ENTRY_TYPE,
EmergencyDialerConstants.ENTRY_TYPE_POWER_MENU);
mContext.startActivityAsUser(intent, UserHandle.CURRENT);
}
}
private final class RestartAction extends SinglePressAction implements LongPressAction {
private RestartAction() {
super(R.drawable.ic_restart, R.string.global_action_restart);
}
@Override
public boolean onLongPress() {
UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
if (!um.hasUserRestriction(UserManager.DISALLOW_SAFE_BOOT)) {
mWindowManagerFuncs.reboot(true);
return true;
}
return false;
}
@Override
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return true;
}
@Override
public void onPress() {
mWindowManagerFuncs.reboot(false);
}
}
private class ScreenshotAction extends SinglePressAction implements LongPressAction {
public ScreenshotAction() {
super(R.drawable.ic_screenshot, R.string.global_action_screenshot);
}
@Override
public void onPress() {
// Add a little delay before executing, to give the
// dialog a chance to go away before it takes a
// screenshot.
// TODO: instead, omit global action dialog layer
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
mScreenshotHelper.takeScreenshot(1, true, true, mHandler);
MetricsLogger.action(mContext,
MetricsEvent.ACTION_SCREENSHOT_POWER_MENU);
}
}, 500);
}
@Override
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return false;
}
@Override
public boolean onLongPress() {
if (FeatureFlagUtils.isEnabled(mContext, FeatureFlagUtils.SCREENRECORD_LONG_PRESS)) {
mScreenRecordHelper.launchRecordPrompt();
} else {
onPress();
}
return true;
}
}
private class BugReportAction extends SinglePressAction implements LongPressAction {
public BugReportAction() {
super(R.drawable.ic_lock_bugreport, R.string.bugreport_title);
}
@Override
public void onPress() {
// don't actually trigger the bugreport if we are running stability
// tests via monkey
if (ActivityManager.isUserAMonkey()) {
return;
}
// Add a little delay before executing, to give the
// dialog a chance to go away before it takes a
// screenshot.
mHandler.postDelayed(new Runnable() {
@Override
public void run() {
try {
// Take an "interactive" bugreport.
MetricsLogger.action(mContext,
MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_INTERACTIVE);
ActivityManager.getService().requestBugReport(
ActivityManager.BUGREPORT_OPTION_INTERACTIVE);
} catch (RemoteException e) {
}
}
}, 500);
}
@Override
public boolean onLongPress() {
// don't actually trigger the bugreport if we are running stability
// tests via monkey
if (ActivityManager.isUserAMonkey()) {
return false;
}
try {
// Take a "full" bugreport.
MetricsLogger.action(mContext, MetricsEvent.ACTION_BUGREPORT_FROM_POWER_MENU_FULL);
ActivityManager.getService().requestBugReport(
ActivityManager.BUGREPORT_OPTION_FULL);
} catch (RemoteException e) {
}
return false;
}
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return false;
}
}
private final class LogoutAction extends SinglePressAction {
private LogoutAction() {
super(R.drawable.ic_logout, R.string.global_action_logout);
}
@Override
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return false;
}
@Override
public void onPress() {
// Add a little delay before executing, to give the dialog a chance to go away before
// switching user
mHandler.postDelayed(() -> {
try {
int currentUserId = getCurrentUser().id;
ActivityManager.getService().switchUser(UserHandle.USER_SYSTEM);
ActivityManager.getService().stopUser(currentUserId, true /*force*/, null);
} catch (RemoteException re) {
Log.e(TAG, "Couldn't logout user " + re);
}
}, 500);
}
}
private Action getSettingsAction() {
return new SinglePressAction(R.drawable.ic_settings,
R.string.global_action_settings) {
@Override
public void onPress() {
Intent intent = new Intent(Settings.ACTION_SETTINGS);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);
}
@Override
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return true;
}
};
}
private Action getAssistAction() {
return new SinglePressAction(R.drawable.ic_action_assist_focused,
R.string.global_action_assist) {
@Override
public void onPress() {
Intent intent = new Intent(Intent.ACTION_ASSIST);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);
}
@Override
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return true;
}
};
}
private Action getVoiceAssistAction() {
return new SinglePressAction(R.drawable.ic_voice_search,
R.string.global_action_voice_assist) {
@Override
public void onPress() {
Intent intent = new Intent(Intent.ACTION_VOICE_ASSIST);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TOP);
mContext.startActivity(intent);
}
@Override
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return true;
}
};
}
private Action getLockdownAction() {
return new SinglePressAction(R.drawable.ic_lock_lockdown,
R.string.global_action_lockdown) {
@Override
public void onPress() {
new LockPatternUtils(mContext)
.requireStrongAuth(STRONG_AUTH_REQUIRED_AFTER_USER_LOCKDOWN,
UserHandle.USER_ALL);
try {
WindowManagerGlobal.getWindowManagerService().lockNow(null);
// Lock profiles (if any) on the background thread.
final Handler bgHandler = new Handler(Dependency.get(Dependency.BG_LOOPER));
bgHandler.post(() -> lockProfiles());
} catch (RemoteException e) {
Log.e(TAG, "Error while trying to lock device.", e);
}
}
@Override
public boolean showDuringKeyguard() {
return true;
}
@Override
public boolean showBeforeProvisioning() {
return false;
}
};
}
private void lockProfiles() {
final UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
final TrustManager tm = (TrustManager) mContext.getSystemService(Context.TRUST_SERVICE);
final int currentUserId = getCurrentUser().id;
final int[] profileIds = um.getEnabledProfileIds(currentUserId);
for (final int id : profileIds) {
if (id != currentUserId) {
tm.setDeviceLockedForUser(id, true);
}
}
}
private UserInfo getCurrentUser() {
try {
return ActivityManager.getService().getCurrentUser();
} catch (RemoteException re) {
return null;
}
}
private boolean isCurrentUserOwner() {
UserInfo currentUser = getCurrentUser();
return currentUser == null || currentUser.isPrimary();
}
private void addUsersToMenu(ArrayList<Action> items) {
UserManager um = (UserManager) mContext.getSystemService(Context.USER_SERVICE);
if (um.isUserSwitcherEnabled()) {
List<UserInfo> users = um.getUsers();
UserInfo currentUser = getCurrentUser();
for (final UserInfo user : users) {
if (user.supportsSwitchToByUser()) {
boolean isCurrentUser = currentUser == null
? user.id == 0 : (currentUser.id == user.id);
Drawable icon = user.iconPath != null ? Drawable.createFromPath(user.iconPath)
: null;
SinglePressAction switchToUser = new SinglePressAction(
R.drawable.ic_menu_cc, icon,
(user.name != null ? user.name : "Primary")
+ (isCurrentUser ? " \u2714" : "")) {
public void onPress() {
try {
ActivityManager.getService().switchUser(user.id);
} catch (RemoteException re) {
Log.e(TAG, "Couldn't switch user " + re);
}
}
public boolean showDuringKeyguard() {
return true;
}
public boolean showBeforeProvisioning() {
return false;
}
};
items.add(switchToUser);
}
}
}
}
private void prepareDialog() {
refreshSilentMode();
mAirplaneModeOn.updateState(mAirplaneState);
mAdapter.notifyDataSetChanged();
if (mShowSilentToggle) {
IntentFilter filter = new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION);
mContext.registerReceiver(mRingerModeReceiver, filter);
}
}
private void refreshSilentMode() {
if (!mHasVibrator) {
final boolean silentModeOn =
mAudioManager.getRingerMode() != AudioManager.RINGER_MODE_NORMAL;
((ToggleAction) mSilentModeAction).updateState(
silentModeOn ? ToggleAction.State.On : ToggleAction.State.Off);
}
}
/** {@inheritDoc} */
public void onDismiss(DialogInterface dialog) {
mWindowManagerFuncs.onGlobalActionsHidden();
if (mShowSilentToggle) {
try {
mContext.unregisterReceiver(mRingerModeReceiver);
} catch (IllegalArgumentException ie) {
// ignore this
Log.w(TAG, ie);
}
}
}
/** {@inheritDoc} */
public void onShow(DialogInterface dialog) {
MetricsLogger.visible(mContext, MetricsEvent.POWER_MENU);
}
/**
* The adapter used for the list within the global actions dialog, taking
* into account whether the keyguard is showing via
* {@link com.android.systemui.globalactions.GlobalActionsDialog#mKeyguardShowing} and whether
* the device is provisioned
* via {@link com.android.systemui.globalactions.GlobalActionsDialog#mDeviceProvisioned}.
*/
public class MyAdapter extends MultiListAdapter {
private int countItems(boolean separated) {
int count = 0;
for (int i = 0; i < mItems.size(); i++) {
final Action action = mItems.get(i);
if (shouldBeShown(action) && action.shouldBeSeparated() == separated) {
count++;
}
}
return count;
}
private boolean shouldBeShown(Action action) {
if (mKeyguardShowing && !action.showDuringKeyguard()) {
return false;
}
if (!mDeviceProvisioned && !action.showBeforeProvisioning()) {
return false;
}
return true;
}
@Override
public int countSeparatedItems() {
return countItems(true);
}
@Override
public int countListItems() {
return countItems(false);
}
@Override
public int getCount() {
return countSeparatedItems() + countListItems();
}
@Override
public boolean isEnabled(int position) {
return getItem(position).isEnabled();
}
@Override
public boolean areAllItemsEnabled() {
return false;
}
@Override
public Action getItem(int position) {
int filteredPos = 0;
for (int i = 0; i < mItems.size(); i++) {
final Action action = mItems.get(i);
if (!shouldBeShown(action)) {
continue;
}
if (filteredPos == position) {
return action;
}
filteredPos++;
}
throw new IllegalArgumentException("position " + position
+ " out of range of showable actions"
+ ", filtered count=" + getCount()
+ ", keyguardshowing=" + mKeyguardShowing
+ ", provisioned=" + mDeviceProvisioned);
}
public long getItemId(int position) {
return position;
}
@Override
public View getView(int position, View convertView, ViewGroup parent) {
Action action = getItem(position);
View view = action.create(mContext, convertView, parent, LayoutInflater.from(mContext));
view.setOnClickListener(v -> onClickItem(position));
view.setOnLongClickListener(v -> onLongClickItem(position));
return view;
}
@Override
public boolean onLongClickItem(int position) {
final Action action = mAdapter.getItem(position);
if (action instanceof LongPressAction) {
mDialog.dismiss();
return ((LongPressAction) action).onLongPress();
}
return false;
}
@Override
public void onClickItem(int position) {
Action item = mAdapter.getItem(position);
if (!(item instanceof SilentModeTriStateAction)) {
mDialog.dismiss();
}
item.onPress();
}
@Override
public boolean shouldBeSeparated(int position) {
return getItem(position).shouldBeSeparated();
}
}
// note: the scheme below made more sense when we were planning on having
// 8 different things in the global actions dialog. seems overkill with
// only 3 items now, but may as well keep this flexible approach so it will
// be easy should someone decide at the last minute to include something
// else, such as 'enable wifi', or 'enable bluetooth'
/**
* What each item in the global actions dialog must be able to support.
*/
public interface Action {
/**
* @return Text that will be announced when dialog is created. null
* for none.
*/
CharSequence getLabelForAccessibility(Context context);
View create(Context context, View convertView, ViewGroup parent, LayoutInflater inflater);
void onPress();
/**
* @return whether this action should appear in the dialog when the keygaurd
* is showing.
*/
boolean showDuringKeyguard();
/**
* @return whether this action should appear in the dialog before the
* device is provisioned.onlongpress
*
*/
boolean showBeforeProvisioning();
boolean isEnabled();
default boolean shouldBeSeparated() {
return false;
}
}
/**
* An action that also supports long press.
*/
private interface LongPressAction extends Action {
boolean onLongPress();
}
/**
* A single press action maintains no state, just responds to a press
* and takes an action.
*/
private static abstract class SinglePressAction implements Action {
private final int mIconResId;
private final Drawable mIcon;
private final int mMessageResId;
private final CharSequence mMessage;
protected SinglePressAction(int iconResId, int messageResId) {
mIconResId = iconResId;
mMessageResId = messageResId;
mMessage = null;
mIcon = null;
}
protected SinglePressAction(int iconResId, Drawable icon, CharSequence message) {
mIconResId = iconResId;
mMessageResId = 0;
mMessage = message;
mIcon = icon;
}
public boolean isEnabled() {
return true;
}
public String getStatus() {
return null;
}
abstract public void onPress();
public CharSequence getLabelForAccessibility(Context context) {
if (mMessage != null) {
return mMessage;
} else {
return context.getString(mMessageResId);
}
}
protected int getActionLayoutId(Context context) {
return com.android.systemui.R.layout.global_actions_grid_item;
}
public View create(
Context context, View convertView, ViewGroup parent, LayoutInflater inflater) {
View v = inflater.inflate(getActionLayoutId(context), parent,
false);
ImageView icon = (ImageView) v.findViewById(R.id.icon);
TextView messageView = (TextView) v.findViewById(R.id.message);
messageView.setSelected(true); // necessary for marquee to work
TextView statusView = (TextView) v.findViewById(R.id.status);
final String status = getStatus();
if (!TextUtils.isEmpty(status)) {
statusView.setText(status);
} else {
statusView.setVisibility(View.GONE);
}
if (mIcon != null) {
icon.setImageDrawable(mIcon);
icon.setScaleType(ScaleType.CENTER_CROP);
} else if (mIconResId != 0) {
icon.setImageDrawable(context.getDrawable(mIconResId));
}
if (mMessage != null) {
messageView.setText(mMessage);
} else {
messageView.setText(mMessageResId);
}
return v;
}
}
/**
* A toggle action knows whether it is on or off, and displays an icon
* and status message accordingly.
*/
private static abstract class ToggleAction implements Action {
enum State {
Off(false),
TurningOn(true),
TurningOff(true),
On(false);
private final boolean inTransition;
State(boolean intermediate) {
inTransition = intermediate;
}
public boolean inTransition() {
return inTransition;
}
}
protected State mState = State.Off;
// prefs
protected int mEnabledIconResId;
protected int mDisabledIconResid;
protected int mMessageResId;
protected int mEnabledStatusMessageResId;
protected int mDisabledStatusMessageResId;
/**
* @param enabledIconResId The icon for when this action is on.
* @param disabledIconResid The icon for when this action is off.
* @param message The general information message, e.g 'Silent Mode'
* @param enabledStatusMessageResId The on status message, e.g 'sound disabled'
* @param disabledStatusMessageResId The off status message, e.g. 'sound enabled'
*/
public ToggleAction(int enabledIconResId,
int disabledIconResid,
int message,
int enabledStatusMessageResId,
int disabledStatusMessageResId) {
mEnabledIconResId = enabledIconResId;
mDisabledIconResid = disabledIconResid;
mMessageResId = message;
mEnabledStatusMessageResId = enabledStatusMessageResId;
mDisabledStatusMessageResId = disabledStatusMessageResId;
}
/**
* Override to make changes to resource IDs just before creating the
* View.
*/
void willCreate() {
}
@Override
public CharSequence getLabelForAccessibility(Context context) {
return context.getString(mMessageResId);
}
public View create(Context context, View convertView, ViewGroup parent,
LayoutInflater inflater) {
willCreate();
View v = inflater.inflate(R
.layout.global_actions_item, parent, false);
ImageView icon = (ImageView) v.findViewById(R.id.icon);
TextView messageView = (TextView) v.findViewById(R.id.message);
TextView statusView = (TextView) v.findViewById(R.id.status);
final boolean enabled = isEnabled();
if (messageView != null) {
messageView.setText(mMessageResId);
messageView.setEnabled(enabled);
messageView.setSelected(true); // necessary for marquee to work
}
boolean on = ((mState == State.On) || (mState == State.TurningOn));
if (icon != null) {
icon.setImageDrawable(context.getDrawable(
(on ? mEnabledIconResId : mDisabledIconResid)));
icon.setEnabled(enabled);
}
if (statusView != null) {
statusView.setText(on ? mEnabledStatusMessageResId : mDisabledStatusMessageResId);
statusView.setVisibility(View.VISIBLE);
statusView.setEnabled(enabled);
}
v.setEnabled(enabled);
return v;
}
public final void onPress() {
if (mState.inTransition()) {
Log.w(TAG, "shouldn't be able to toggle when in transition");
return;
}
final boolean nowOn = !(mState == State.On);
onToggle(nowOn);
changeStateFromPress(nowOn);
}
public boolean isEnabled() {
return !mState.inTransition();
}
/**
* Implementations may override this if their state can be in on of the intermediate
* states until some notification is received (e.g airplane mode is 'turning off' until
* we know the wireless connections are back online
*
* @param buttonOn Whether the button was turned on or off
*/
protected void changeStateFromPress(boolean buttonOn) {
mState = buttonOn ? State.On : State.Off;
}
abstract void onToggle(boolean on);
public void updateState(State state) {
mState = state;
}
}
private class SilentModeToggleAction extends ToggleAction {
public SilentModeToggleAction() {
super(R.drawable.ic_audio_vol_mute,
R.drawable.ic_audio_vol,
R.string.global_action_toggle_silent_mode,
R.string.global_action_silent_mode_on_status,
R.string.global_action_silent_mode_off_status);
}
void onToggle(boolean on) {
if (on) {
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_SILENT);
} else {
mAudioManager.setRingerMode(AudioManager.RINGER_MODE_NORMAL);
}
}
public boolean showDuringKeyguard() {
return true;
}
public boolean showBeforeProvisioning() {
return false;
}
}
private static class SilentModeTriStateAction implements Action, View.OnClickListener {
private final int[] ITEM_IDS = {R.id.option1, R.id.option2, R.id.option3};
private final AudioManager mAudioManager;
private final Handler mHandler;
SilentModeTriStateAction(AudioManager audioManager, Handler handler) {
mAudioManager = audioManager;
mHandler = handler;
}
private int ringerModeToIndex(int ringerMode) {
// They just happen to coincide
return ringerMode;
}
private int indexToRingerMode(int index) {
// They just happen to coincide
return index;
}
@Override
public CharSequence getLabelForAccessibility(Context context) {
return null;
}
public View create(Context context, View convertView, ViewGroup parent,
LayoutInflater inflater) {
View v = inflater.inflate(R.layout.global_actions_silent_mode, parent, false);
int selectedIndex = ringerModeToIndex(mAudioManager.getRingerMode());
for (int i = 0; i < 3; i++) {
View itemView = v.findViewById(ITEM_IDS[i]);
itemView.setSelected(selectedIndex == i);
// Set up click handler
itemView.setTag(i);
itemView.setOnClickListener(this);
}
return v;
}
public void onPress() {
}
public boolean showDuringKeyguard() {
return true;
}
public boolean showBeforeProvisioning() {
return false;
}
public boolean isEnabled() {
return true;
}
void willCreate() {
}
public void onClick(View v) {
if (!(v.getTag() instanceof Integer)) return;
int index = (Integer) v.getTag();
mAudioManager.setRingerMode(indexToRingerMode(index));
mHandler.sendEmptyMessageDelayed(MESSAGE_DISMISS, DIALOG_DISMISS_DELAY);
}
}
private BroadcastReceiver mBroadcastReceiver = new BroadcastReceiver() {
public void onReceive(Context context, Intent intent) {
String action = intent.getAction();
if (Intent.ACTION_CLOSE_SYSTEM_DIALOGS.equals(action)
|| Intent.ACTION_SCREEN_OFF.equals(action)) {
String reason = intent.getStringExtra(SYSTEM_DIALOG_REASON_KEY);
if (!SYSTEM_DIALOG_REASON_GLOBAL_ACTIONS.equals(reason)) {
mHandler.sendMessage(mHandler.obtainMessage(MESSAGE_DISMISS, reason));
}
} else if (TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED.equals(action)) {
// Airplane mode can be changed after ECM exits if airplane toggle button
// is pressed during ECM mode
if (!(intent.getBooleanExtra("PHONE_IN_ECM_STATE", false)) &&
mIsWaitingForEcmExit) {
mIsWaitingForEcmExit = false;
changeAirplaneModeSystemSetting(true);
}
}
}
};
PhoneStateListener mPhoneStateListener = new PhoneStateListener() {
@Override
public void onServiceStateChanged(ServiceState serviceState) {
if (!mHasTelephony) return;
final boolean inAirplaneMode = serviceState.getState() == ServiceState.STATE_POWER_OFF;
mAirplaneState = inAirplaneMode ? ToggleAction.State.On : ToggleAction.State.Off;
mAirplaneModeOn.updateState(mAirplaneState);
mAdapter.notifyDataSetChanged();
}
};
private BroadcastReceiver mRingerModeReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(AudioManager.RINGER_MODE_CHANGED_ACTION)) {
mHandler.sendEmptyMessage(MESSAGE_REFRESH);
}
}
};
private ContentObserver mAirplaneModeObserver = new ContentObserver(new Handler()) {
@Override
public void onChange(boolean selfChange) {
onAirplaneModeChanged();
}
};
private static final int MESSAGE_DISMISS = 0;
private static final int MESSAGE_REFRESH = 1;
private static final int MESSAGE_SHOW = 2;
private static final int DIALOG_DISMISS_DELAY = 300; // ms
private Handler mHandler = new Handler() {
public void handleMessage(Message msg) {
switch (msg.what) {
case MESSAGE_DISMISS:
if (mDialog != null) {
if (SYSTEM_DIALOG_REASON_DREAM.equals(msg.obj)) {
mDialog.dismissImmediately();
} else {
mDialog.dismiss();
}
mDialog = null;
}
break;
case MESSAGE_REFRESH:
refreshSilentMode();
mAdapter.notifyDataSetChanged();
break;
case MESSAGE_SHOW:
handleShow();
break;
}
}
};
private void onAirplaneModeChanged() {
// Let the service state callbacks handle the state.
if (mHasTelephony) return;
boolean airplaneModeOn = Settings.Global.getInt(
mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON,
0) == 1;
mAirplaneState = airplaneModeOn ? ToggleAction.State.On : ToggleAction.State.Off;
mAirplaneModeOn.updateState(mAirplaneState);
}
/**
* Change the airplane mode system setting
*/
private void changeAirplaneModeSystemSetting(boolean on) {
Settings.Global.putInt(
mContext.getContentResolver(),
Settings.Global.AIRPLANE_MODE_ON,
on ? 1 : 0);
Intent intent = new Intent(Intent.ACTION_AIRPLANE_MODE_CHANGED);
intent.addFlags(Intent.FLAG_RECEIVER_REPLACE_PENDING);
intent.putExtra("state", on);
mContext.sendBroadcastAsUser(intent, UserHandle.ALL);
if (!mHasTelephony) {
mAirplaneState = on ? ToggleAction.State.On : ToggleAction.State.Off;
}
}
private static final class ActionsDialog extends Dialog implements DialogInterface,
ColorExtractor.OnColorsChangedListener {
private final Context mContext;
private final MyAdapter mAdapter;
private final IStatusBarService mStatusBarService;
private final IBinder mToken = new Binder();
private MultiListLayout mGlobalActionsLayout;
private Drawable mBackgroundDrawable;
private final SysuiColorExtractor mColorExtractor;
private final GlobalActionsPanelPlugin.PanelViewController mPanelController;
private boolean mKeyguardShowing;
private boolean mShowing;
private float mScrimAlpha;
private ResetOrientationData mResetOrientationData;
private boolean mHadTopUi;
private final StatusBarWindowController mStatusBarWindowController;
ActionsDialog(Context context, MyAdapter adapter,
GlobalActionsPanelPlugin.PanelViewController plugin) {
super(context, com.android.systemui.R.style.Theme_SystemUI_Dialog_GlobalActions);
mContext = context;
mAdapter = adapter;
mColorExtractor = Dependency.get(SysuiColorExtractor.class);
mStatusBarService = Dependency.get(IStatusBarService.class);
mStatusBarWindowController = Dependency.get(StatusBarWindowController.class);
// Window initialization
Window window = getWindow();
window.requestFeature(Window.FEATURE_NO_TITLE);
// Inflate the decor view, so the attributes below are not overwritten by the theme.
window.getDecorView();
window.getAttributes().systemUiVisibility |= View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION;
window.setLayout(MATCH_PARENT, MATCH_PARENT);
window.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
window.addFlags(
WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN
| WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL
| WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR
| WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED
| WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH
| WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED);
window.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY);
setTitle(R.string.global_actions);
mPanelController = plugin;
initializeLayout();
}
private boolean shouldUsePanel() {
return mPanelController != null && mPanelController.getPanelContent() != null;
}
private void initializePanel() {
int rotation = RotationUtils.getRotation(mContext);
boolean rotationLocked = RotationPolicy.isRotationLocked(mContext);
if (rotation != RotationUtils.ROTATION_NONE) {
if (rotationLocked) {
if (mResetOrientationData == null) {
mResetOrientationData = new ResetOrientationData();
mResetOrientationData.locked = true;
mResetOrientationData.rotation = rotation;
}
// Unlock rotation, so user can choose to rotate to portrait to see the panel.
// This call is posted so that the rotation does not change until post-layout,
// otherwise onConfigurationChanged() may not get invoked.
mGlobalActionsLayout.post(() ->
RotationPolicy.setRotationLockAtAngle(
mContext, false, RotationUtils.ROTATION_NONE));
}
} else {
if (!rotationLocked) {
if (mResetOrientationData == null) {
mResetOrientationData = new ResetOrientationData();
mResetOrientationData.locked = false;
}
// Lock to portrait, so the user doesn't accidentally hide the panel.
// This call is posted so that the rotation does not change until post-layout,
// otherwise onConfigurationChanged() may not get invoked.
mGlobalActionsLayout.post(() ->
RotationPolicy.setRotationLockAtAngle(
mContext, true, RotationUtils.ROTATION_NONE));
}
// Disable rotation suggestions, if enabled
setRotationSuggestionsEnabled(false);
FrameLayout panelContainer =
findViewById(com.android.systemui.R.id.global_actions_panel_container);
FrameLayout.LayoutParams panelParams =
new FrameLayout.LayoutParams(
FrameLayout.LayoutParams.MATCH_PARENT,
FrameLayout.LayoutParams.MATCH_PARENT);
panelContainer.addView(mPanelController.getPanelContent(), panelParams);
mBackgroundDrawable = mPanelController.getBackgroundDrawable();
mScrimAlpha = 1f;
}
}
private void initializeLayout() {
setContentView(getGlobalActionsLayoutId(mContext));
fixNavBarClipping();
mGlobalActionsLayout = findViewById(com.android.systemui.R.id.global_actions_view);
mGlobalActionsLayout.setOutsideTouchListener(view -> dismiss());
((View) mGlobalActionsLayout.getParent()).setOnClickListener(view -> dismiss());
mGlobalActionsLayout.setListViewAccessibilityDelegate(new View.AccessibilityDelegate() {
@Override
public boolean dispatchPopulateAccessibilityEvent(
View host, AccessibilityEvent event) {
// Populate the title here, just as Activity does
event.getText().add(mContext.getString(R.string.global_actions));
return true;
}
});
mGlobalActionsLayout.setRotationListener(this::onRotate);
mGlobalActionsLayout.setAdapter(mAdapter);
if (shouldUsePanel()) {
initializePanel();
}
if (mBackgroundDrawable == null) {
mBackgroundDrawable = new ScrimDrawable();
mScrimAlpha = ScrimController.GRADIENT_SCRIM_ALPHA;
}
getWindow().setBackgroundDrawable(mBackgroundDrawable);
}
private void fixNavBarClipping() {
ViewGroup content = findViewById(android.R.id.content);
content.setClipChildren(false);
content.setClipToPadding(false);
ViewGroup contentParent = (ViewGroup) content.getParent();
contentParent.setClipChildren(false);
contentParent.setClipToPadding(false);
}
private int getGlobalActionsLayoutId(Context context) {
int rotation = RotationUtils.getRotation(context);
boolean useGridLayout = isForceGridEnabled(context)
|| (shouldUsePanel() && rotation == RotationUtils.ROTATION_NONE);
if (rotation == RotationUtils.ROTATION_SEASCAPE) {
if (useGridLayout) {
return com.android.systemui.R.layout.global_actions_grid_seascape;
} else {
return com.android.systemui.R.layout.global_actions_column_seascape;
}
} else {
if (useGridLayout) {
return com.android.systemui.R.layout.global_actions_grid;
} else {
return com.android.systemui.R.layout.global_actions_column;
}
}
}
@Override
protected void onStart() {
super.setCanceledOnTouchOutside(true);
super.onStart();
mGlobalActionsLayout.updateList();
if (mBackgroundDrawable instanceof ScrimDrawable) {
mColorExtractor.addOnColorsChangedListener(this);
GradientColors colors = mColorExtractor.getNeutralColors();
updateColors(colors, false /* animate */);
}
}
/**
* Updates background and system bars according to current GradientColors.
* @param colors Colors and hints to use.
* @param animate Interpolates gradient if true, just sets otherwise.
*/
private void updateColors(GradientColors colors, boolean animate) {
if (!(mBackgroundDrawable instanceof ScrimDrawable)) {
return;
}
((ScrimDrawable) mBackgroundDrawable).setColor(colors.getMainColor(), animate);
View decorView = getWindow().getDecorView();
if (colors.supportsDarkText()) {
decorView.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LIGHT_NAVIGATION_BAR |
View.SYSTEM_UI_FLAG_LIGHT_STATUS_BAR);
} else {
decorView.setSystemUiVisibility(0);
}
}
@Override
protected void onStop() {
super.onStop();
mColorExtractor.removeOnColorsChangedListener(this);
}
@Override
public void show() {
super.show();
mShowing = true;
mHadTopUi = mStatusBarWindowController.getForceHasTopUi();
mStatusBarWindowController.setForceHasTopUi(true);
mBackgroundDrawable.setAlpha(0);
mGlobalActionsLayout.setTranslationX(mGlobalActionsLayout.getAnimationOffsetX());
mGlobalActionsLayout.setTranslationY(mGlobalActionsLayout.getAnimationOffsetY());
mGlobalActionsLayout.setAlpha(0);
mGlobalActionsLayout.animate()
.alpha(1)
.translationX(0)
.translationY(0)
.setDuration(300)
.setInterpolator(Interpolators.FAST_OUT_SLOW_IN)
.setUpdateListener(animation -> {
int alpha = (int) ((Float) animation.getAnimatedValue()
* mScrimAlpha * 255);
mBackgroundDrawable.setAlpha(alpha);
})
.start();
}
@Override
public void dismiss() {
if (!mShowing) {
return;
}
mShowing = false;
mGlobalActionsLayout.setTranslationX(0);
mGlobalActionsLayout.setTranslationY(0);
mGlobalActionsLayout.setAlpha(1);
mGlobalActionsLayout.animate()
.alpha(0)
.translationX(mGlobalActionsLayout.getAnimationOffsetX())
.translationY(mGlobalActionsLayout.getAnimationOffsetY())
.setDuration(300)
.withEndAction(this::completeDismiss)
.setInterpolator(new LogAccelerateInterpolator())
.setUpdateListener(animation -> {
int alpha = (int) ((1f - (Float) animation.getAnimatedValue())
* mScrimAlpha * 255);
mBackgroundDrawable.setAlpha(alpha);
})
.start();
dismissPanel();
resetOrientation();
}
void dismissImmediately() {
mShowing = false;
dismissPanel();
resetOrientation();
completeDismiss();
}
private void completeDismiss() {
mStatusBarWindowController.setForceHasTopUi(mHadTopUi);
super.dismiss();
}
private void dismissPanel() {
if (mPanelController != null) {
mPanelController.onDismissed();
}
}
private void setRotationSuggestionsEnabled(boolean enabled) {
try {
final int userId = Binder.getCallingUserHandle().getIdentifier();
final int what = enabled
? StatusBarManager.DISABLE2_NONE
: StatusBarManager.DISABLE2_ROTATE_SUGGESTIONS;
mStatusBarService.disable2ForUser(what, mToken, mContext.getPackageName(), userId);
} catch (RemoteException ex) {
throw ex.rethrowFromSystemServer();
}
}
private void resetOrientation() {
if (mResetOrientationData != null) {
RotationPolicy.setRotationLockAtAngle(mContext, mResetOrientationData.locked,
mResetOrientationData.rotation);
}
setRotationSuggestionsEnabled(true);
}
@Override
public void onColorsChanged(ColorExtractor extractor, int which) {
if (mKeyguardShowing) {
if ((WallpaperManager.FLAG_LOCK & which) != 0) {
updateColors(extractor.getColors(WallpaperManager.FLAG_LOCK),
true /* animate */);
}
} else {
if ((WallpaperManager.FLAG_SYSTEM & which) != 0) {
updateColors(extractor.getColors(WallpaperManager.FLAG_SYSTEM),
true /* animate */);
}
}
}
public void setKeyguardShowing(boolean keyguardShowing) {
mKeyguardShowing = keyguardShowing;
}
public void refreshDialog() {
initializeLayout();
mGlobalActionsLayout.updateList();
}
public void onRotate(int from, int to) {
if (mShowing) {
refreshDialog();
}
}
private static class ResetOrientationData {
public boolean locked;
public int rotation;
}
}
/**
* Determines whether or not debug mode has been activated for the Global Actions Panel.
*/
private static boolean isPanelDebugModeEnabled(Context context) {
return Settings.Secure.getInt(context.getContentResolver(),
Settings.Secure.GLOBAL_ACTIONS_PANEL_DEBUG_ENABLED, 0) == 1;
}
/**
* Determines whether or not the Global Actions menu should be forced to
* use the newer grid-style layout.
*/
private static boolean isForceGridEnabled(Context context) {
return isPanelDebugModeEnabled(context);
}
/**
* Determines whether the Global Actions menu should use a separated view for emergency actions.
*/
private static boolean shouldUseSeparatedView() {
return true;
}
}
|