Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 0ebc5226 authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Automerger Merge Worker
Browse files

Merge "Move Lockdown to second place, after Emergency" into rvc-dev am:...

Merge "Move Lockdown to second place, after Emergency" into rvc-dev am: e38716c6 am: e2470870 am: 1a0da78f

Original change: https://googleplex-android-review.googlesource.com/c/platform/frameworks/base/+/11920904

Change-Id: I2b44a1ab5ed3d8c645590f413a6a7f0a4d00f1d8
parents 29b5a9fa 1a0da78f
Loading
Loading
Loading
Loading
+1 −1
Original line number Original line Diff line number Diff line
@@ -2860,9 +2860,9 @@
         -->
         -->
    <string-array translatable="false" name="config_globalActionsList">
    <string-array translatable="false" name="config_globalActionsList">
        <item>emergency</item>
        <item>emergency</item>
        <item>lockdown</item>
        <item>power</item>
        <item>power</item>
        <item>restart</item>
        <item>restart</item>
        <item>lockdown</item>
        <item>logout</item>
        <item>logout</item>
        <item>bugreport</item>
        <item>bugreport</item>
    </string-array>
    </string-array>
+95 −68
Original line number Original line Diff line number Diff line
@@ -172,7 +172,7 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
    @VisibleForTesting
    @VisibleForTesting
    static final String GLOBAL_ACTION_KEY_POWER = "power";
    static final String GLOBAL_ACTION_KEY_POWER = "power";
    private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
    private static final String GLOBAL_ACTION_KEY_AIRPLANE = "airplane";
    private static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
    static final String GLOBAL_ACTION_KEY_BUGREPORT = "bugreport";
    private static final String GLOBAL_ACTION_KEY_SILENT = "silent";
    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_USERS = "users";
    private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
    private static final String GLOBAL_ACTION_KEY_SETTINGS = "settings";
@@ -564,20 +564,24 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
     * whether controls are enabled and whether the max number of shown items has been reached.
     * whether controls are enabled and whether the max number of shown items has been reached.
     */
     */
    private void addActionItem(Action action) {
    private void addActionItem(Action action) {
        if (shouldShowAction(action)) {
        if (mItems.size() < getMaxShownPowerItems()) {
        if (mItems.size() < getMaxShownPowerItems()) {
            mItems.add(action);
            mItems.add(action);
        } else {
        } else {
            mOverflowItems.add(action);
            mOverflowItems.add(action);
        }
        }
    }
    }
    }


    @VisibleForTesting
    @VisibleForTesting
    protected String[] getDefaultActions() {
    protected String[] getDefaultActions() {
        return mResources.getStringArray(R.array.config_globalActionsList);
        return mResources.getStringArray(R.array.config_globalActionsList);
    }
    }


    private void addIfShouldShowAction(List<Action> actions, Action action) {
        if (shouldShowAction(action)) {
            actions.add(action);
        }
    }

    @VisibleForTesting
    @VisibleForTesting
    protected void createActionItems() {
    protected void createActionItems() {
        // Simple toggle style if there's no vibrator, otherwise use a tri-state
        // Simple toggle style if there's no vibrator, otherwise use a tri-state
@@ -597,10 +601,12 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
        ShutDownAction shutdownAction = new ShutDownAction();
        ShutDownAction shutdownAction = new ShutDownAction();
        RestartAction restartAction = new RestartAction();
        RestartAction restartAction = new RestartAction();
        ArraySet<String> addedKeys = new ArraySet<String>();
        ArraySet<String> addedKeys = new ArraySet<String>();
        List<Action> tempActions = new ArrayList<>();
        CurrentUserProvider currentUser = new CurrentUserProvider();


        // make sure emergency affordance action is first, if needed
        // make sure emergency affordance action is first, if needed
        if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
        if (mEmergencyAffordanceManager.needsEmergencyAffordance()) {
            addActionItem(new EmergencyAffordanceAction());
            addIfShouldShowAction(tempActions, new EmergencyAffordanceAction());
            addedKeys.add(GLOBAL_ACTION_KEY_EMERGENCY);
            addedKeys.add(GLOBAL_ACTION_KEY_EMERGENCY);
        }
        }


@@ -611,43 +617,43 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
                continue;
                continue;
            }
            }
            if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
            if (GLOBAL_ACTION_KEY_POWER.equals(actionKey)) {
                addActionItem(shutdownAction);
                addIfShouldShowAction(tempActions, shutdownAction);
            } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_AIRPLANE.equals(actionKey)) {
                addActionItem(mAirplaneModeOn);
                addIfShouldShowAction(tempActions, mAirplaneModeOn);
            } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_BUGREPORT.equals(actionKey)) {
                if (Settings.Global.getInt(mContentResolver,
                if (shouldDisplayBugReport(currentUser.get())) {
                        Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0 && isCurrentUserOwner()) {
                    addIfShouldShowAction(tempActions, new BugReportAction());
                    addActionItem(new BugReportAction());
                }
                }
            } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_SILENT.equals(actionKey)) {
                if (mShowSilentToggle) {
                if (mShowSilentToggle) {
                    addActionItem(mSilentModeAction);
                    addIfShouldShowAction(tempActions, mSilentModeAction);
                }
                }
            } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_USERS.equals(actionKey)) {
                if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
                if (SystemProperties.getBoolean("fw.power_user_switcher", false)) {
                    addUsersToMenu();
                    addUserActions(tempActions, currentUser.get());
                }
                }
            } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_SETTINGS.equals(actionKey)) {
                addActionItem(getSettingsAction());
                addIfShouldShowAction(tempActions, getSettingsAction());
            } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_LOCKDOWN.equals(actionKey)) {
                if (shouldDisplayLockdown(getCurrentUser())) {
                if (shouldDisplayLockdown(currentUser.get())) {
                    addActionItem(getLockdownAction());
                    addIfShouldShowAction(tempActions, new LockDownAction());
                }
                }
            } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_VOICEASSIST.equals(actionKey)) {
                addActionItem(getVoiceAssistAction());
                addIfShouldShowAction(tempActions, getVoiceAssistAction());
            } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_ASSIST.equals(actionKey)) {
                addActionItem(getAssistAction());
                addIfShouldShowAction(tempActions, getAssistAction());
            } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_RESTART.equals(actionKey)) {
                addActionItem(restartAction);
                addIfShouldShowAction(tempActions, restartAction);
            } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_SCREENSHOT.equals(actionKey)) {
                addActionItem(new ScreenshotAction());
                addIfShouldShowAction(tempActions, new ScreenshotAction());
            } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_LOGOUT.equals(actionKey)) {
                if (mDevicePolicyManager.isLogoutEnabled()
                if (mDevicePolicyManager.isLogoutEnabled()
                        && getCurrentUser().id != UserHandle.USER_SYSTEM) {
                        && currentUser.get() != null
                    addActionItem(new LogoutAction());
                        && currentUser.get().id != UserHandle.USER_SYSTEM) {
                    addIfShouldShowAction(tempActions, new LogoutAction());
                }
                }
            } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
            } else if (GLOBAL_ACTION_KEY_EMERGENCY.equals(actionKey)) {
                addActionItem(new EmergencyDialerAction());
                addIfShouldShowAction(tempActions, new EmergencyDialerAction());
            } else {
            } else {
                Log.e(TAG, "Invalid global action key " + actionKey);
                Log.e(TAG, "Invalid global action key " + actionKey);
            }
            }
@@ -656,22 +662,21 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
        }
        }


        // replace power and restart with a single power options action, if needed
        // replace power and restart with a single power options action, if needed
        if (mItems.contains(shutdownAction) && mItems.contains(restartAction)
        if (tempActions.contains(shutdownAction) && tempActions.contains(restartAction)
                && mOverflowItems.size() > 0) {
                && tempActions.size() > getMaxShownPowerItems()) {
            // transfer shutdown and restart to their own list of power actions
            // transfer shutdown and restart to their own list of power actions
            mItems.remove(shutdownAction);
            int powerOptionsIndex = Math.min(tempActions.indexOf(restartAction),
            mItems.remove(restartAction);
                    tempActions.indexOf(shutdownAction));
            tempActions.remove(shutdownAction);
            tempActions.remove(restartAction);
            mPowerItems.add(shutdownAction);
            mPowerItems.add(shutdownAction);
            mPowerItems.add(restartAction);
            mPowerItems.add(restartAction);


            // add the PowerOptionsAction after Emergency, if present
            // add the PowerOptionsAction after Emergency, if present
            int powerIndex = addedKeys.contains(GLOBAL_ACTION_KEY_EMERGENCY) ? 1 : 0;
            tempActions.add(powerOptionsIndex, new PowerOptionsAction());
            mItems.add(powerIndex, new PowerOptionsAction());
        }

        for (Action action : tempActions) {
            // transfer the first overflow action to the main set of items
            addActionItem(action);
            Action firstOverflowAction = mOverflowItems.get(0);
            mOverflowItems.remove(0);
            mItems.add(firstOverflowAction);
        }
        }
    }
    }


@@ -716,7 +721,7 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
    }
    }


    @VisibleForTesting
    @VisibleForTesting
    protected boolean shouldDisplayLockdown(UserInfo user) {
    boolean shouldDisplayLockdown(UserInfo user) {
        if (user == null) {
        if (user == null) {
            return false;
            return false;
        }
        }
@@ -740,6 +745,13 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
                || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
                || state == SOME_AUTH_REQUIRED_AFTER_USER_REQUEST);
    }
    }


    @VisibleForTesting
    boolean shouldDisplayBugReport(UserInfo currentUser) {
        return Settings.Global.getInt(
                mContentResolver, Settings.Global.BUGREPORT_IN_POWER_MENU, 0) != 0
                && (currentUser == null || currentUser.isPrimary());
    }

    @Override
    @Override
    public void onUiModeChanged() {
    public void onUiModeChanged() {
        mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
        mContext.getTheme().applyStyle(mContext.getThemeResId(), true);
@@ -804,7 +816,8 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
        }
        }
    }
    }


    private final class ShutDownAction extends SinglePressAction implements LongPressAction {
    @VisibleForTesting
    final class ShutDownAction extends SinglePressAction implements LongPressAction {
        private ShutDownAction() {
        private ShutDownAction() {
            super(R.drawable.ic_lock_power_off,
            super(R.drawable.ic_lock_power_off,
                    R.string.global_action_power_off);
                    R.string.global_action_power_off);
@@ -916,7 +929,8 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
        return new EmergencyDialerAction();
        return new EmergencyDialerAction();
    }
    }


    private final class RestartAction extends SinglePressAction implements LongPressAction {
    @VisibleForTesting
    final class RestartAction extends SinglePressAction implements LongPressAction {
        private RestartAction() {
        private RestartAction() {
            super(R.drawable.ic_restart, R.string.global_action_restart);
            super(R.drawable.ic_restart, R.string.global_action_restart);
        }
        }
@@ -1161,9 +1175,11 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
        };
        };
    }
    }


    private Action getLockdownAction() {
    @VisibleForTesting
        return new SinglePressAction(R.drawable.ic_lock_lockdown,
    class LockDownAction extends SinglePressAction {
                R.string.global_action_lockdown) {
        LockDownAction() {
            super(R.drawable.ic_lock_lockdown, R.string.global_action_lockdown);
        }


        @Override
        @Override
        public void onPress() {
        public void onPress() {
@@ -1187,7 +1203,6 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
        public boolean showBeforeProvisioning() {
        public boolean showBeforeProvisioning() {
            return false;
            return false;
        }
        }
        };
    }
    }


    private void lockProfiles() {
    private void lockProfiles() {
@@ -1208,15 +1223,27 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
        }
        }
    }
    }


    private boolean isCurrentUserOwner() {
    /**
        UserInfo currentUser = getCurrentUser();
     * Non-thread-safe current user provider that caches the result - helpful when a method needs
        return currentUser == null || currentUser.isPrimary();
     * to fetch it an indeterminate number of times.
     */
    private class CurrentUserProvider {
        private UserInfo mUserInfo = null;
        private boolean mFetched = false;

        @Nullable
        UserInfo get() {
            if (!mFetched) {
                mFetched = true;
                mUserInfo = getCurrentUser();
            }
            return mUserInfo;
        }
    }
    }


    private void addUsersToMenu() {
    private void addUserActions(List<Action> actions, UserInfo currentUser) {
        if (mUserManager.isUserSwitcherEnabled()) {
        if (mUserManager.isUserSwitcherEnabled()) {
            List<UserInfo> users = mUserManager.getUsers();
            List<UserInfo> users = mUserManager.getUsers();
            UserInfo currentUser = getCurrentUser();
            for (final UserInfo user : users) {
            for (final UserInfo user : users) {
                if (user.supportsSwitchToByUser()) {
                if (user.supportsSwitchToByUser()) {
                    boolean isCurrentUser = currentUser == null
                    boolean isCurrentUser = currentUser == null
@@ -1243,7 +1270,7 @@ public class GlobalActionsDialog implements DialogInterface.OnDismissListener,
                            return false;
                            return false;
                        }
                        }
                    };
                    };
                    addActionItem(switchToUser);
                    addIfShouldShowAction(actions, switchToUser);
                }
                }
            }
            }
        }
        }
+105 −53
Original line number Original line Diff line number Diff line
@@ -20,12 +20,10 @@ import static com.android.internal.widget.LockPatternUtils.StrongAuthTracker.STR


import static com.google.common.truth.Truth.assertThat;
import static com.google.common.truth.Truth.assertThat;


import static junit.framework.Assert.assertEquals;
import static junit.framework.Assert.assertTrue;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.times;
import static org.mockito.Mockito.times;
@@ -85,6 +83,7 @@ import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.MockitoAnnotations;


import java.util.List;
import java.util.concurrent.Executor;
import java.util.concurrent.Executor;


@SmallTest
@SmallTest
@@ -248,6 +247,14 @@ public class GlobalActionsDialogTest extends SysuiTestCase {
                .log(event);
                .log(event);
    }
    }


    @SafeVarargs
    private static <T> void assertItemsOfType(List<T> stuff, Class<? extends T>... classes) {
        assertThat(stuff).hasSize(classes.length);
        for (int i = 0; i < stuff.size(); i++) {
            assertThat(stuff.get(i)).isInstanceOf(classes[i]);
        }
    }

    @Test
    @Test
    public void testCreateActionItems_maxThree_noOverflow() {
    public void testCreateActionItems_maxThree_noOverflow() {
        mGlobalActionsDialog = spy(mGlobalActionsDialog);
        mGlobalActionsDialog = spy(mGlobalActionsDialog);
@@ -263,9 +270,12 @@ public class GlobalActionsDialogTest extends SysuiTestCase {
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        mGlobalActionsDialog.createActionItems();
        mGlobalActionsDialog.createActionItems();


        assertEquals(3, mGlobalActionsDialog.mItems.size());
        assertItemsOfType(mGlobalActionsDialog.mItems,
        assertEquals(0, mGlobalActionsDialog.mOverflowItems.size());
                GlobalActionsDialog.EmergencyAction.class,
        assertEquals(0, mGlobalActionsDialog.mPowerItems.size());
                GlobalActionsDialog.ShutDownAction.class,
                GlobalActionsDialog.RestartAction.class);
        assertThat(mGlobalActionsDialog.mOverflowItems).isEmpty();
        assertThat(mGlobalActionsDialog.mPowerItems).isEmpty();
    }
    }


    @Test
    @Test
@@ -279,55 +289,53 @@ public class GlobalActionsDialogTest extends SysuiTestCase {
        doReturn(true).when(mGlobalActionsDialog).shouldDisplayLockdown(any());
        doReturn(true).when(mGlobalActionsDialog).shouldDisplayLockdown(any());
        String[] actions = {
        String[] actions = {
                GlobalActionsDialog.GLOBAL_ACTION_KEY_EMERGENCY,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_EMERGENCY,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_LOCKDOWN,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_RESTART,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_RESTART,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_LOCKDOWN,
        };
        };
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        mGlobalActionsDialog.createActionItems();
        mGlobalActionsDialog.createActionItems();


        assertEquals(3, mGlobalActionsDialog.mItems.size());
        assertItemsOfType(mGlobalActionsDialog.mItems,
        assertEquals(0, mGlobalActionsDialog.mOverflowItems.size());
                GlobalActionsDialog.EmergencyAction.class,
        assertEquals(2, mGlobalActionsDialog.mPowerItems.size());
                GlobalActionsDialog.LockDownAction.class,

                GlobalActionsDialog.PowerOptionsAction.class);
        // PowerOptionsAction should appear immediately after the Emergency action
        assertThat(mGlobalActionsDialog.mOverflowItems).isEmpty();

        assertItemsOfType(mGlobalActionsDialog.mPowerItems,
        GlobalActionsDialog.Action firstItem = mGlobalActionsDialog.mItems.get(0);
                GlobalActionsDialog.ShutDownAction.class,
        GlobalActionsDialog.Action secondItem = mGlobalActionsDialog.mItems.get(1);
                GlobalActionsDialog.RestartAction.class);

        assertTrue(firstItem instanceof GlobalActionsDialog.EmergencyAction);
        assertTrue(secondItem instanceof GlobalActionsDialog.PowerOptionsAction);
    }
    }


    @Test
    @Test
    public void testCreateActionItems_maxThree_condensePower_noEmergency() {
    public void testCreateActionItems_maxThree_condensePower_splitPower() {
        mGlobalActionsDialog = spy(mGlobalActionsDialog);
        mGlobalActionsDialog = spy(mGlobalActionsDialog);
        // allow 3 items to be shown
        // allow 3 items to be shown
        doReturn(3).when(mGlobalActionsDialog).getMaxShownPowerItems();
        doReturn(3).when(mGlobalActionsDialog).getMaxShownPowerItems();
        // make sure lockdown action will be shown
        // make sure lockdown action will be shown
        doReturn(true).when(mGlobalActionsDialog).shouldDisplayLockdown(any());
        doReturn(true).when(mGlobalActionsDialog).shouldDisplayLockdown(any());
        // make sure bugreport also shown
        doReturn(true).when(mGlobalActionsDialog).shouldDisplayBugReport(any());
        // ensure items are not blocked by keyguard or device provisioning
        // ensure items are not blocked by keyguard or device provisioning
        doReturn(true).when(mGlobalActionsDialog).shouldShowAction(any());
        doReturn(true).when(mGlobalActionsDialog).shouldShowAction(any());
        String[] actions = {
        String[] actions = {
                GlobalActionsDialog.GLOBAL_ACTION_KEY_EMERGENCY,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_LOCKDOWN,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_BUGREPORT,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_RESTART,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_RESTART,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_SCREENSHOT,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_LOCKDOWN,
        };
        };
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        mGlobalActionsDialog.createActionItems();
        mGlobalActionsDialog.createActionItems();


        assertEquals(3, mGlobalActionsDialog.mItems.size());
        assertItemsOfType(mGlobalActionsDialog.mItems,
        assertEquals(0, mGlobalActionsDialog.mOverflowItems.size());
                GlobalActionsDialog.EmergencyAction.class,
        assertEquals(2, mGlobalActionsDialog.mPowerItems.size());
                GlobalActionsDialog.LockDownAction.class,

                GlobalActionsDialog.PowerOptionsAction.class);
        // When Emergency isn't used, PowerOptionsAction should be first
        assertItemsOfType(mGlobalActionsDialog.mOverflowItems,

                GlobalActionsDialog.BugReportAction.class);
        GlobalActionsDialog.Action firstItem = mGlobalActionsDialog.mItems.get(0);
        assertItemsOfType(mGlobalActionsDialog.mPowerItems,
        GlobalActionsDialog.Action secondItem = mGlobalActionsDialog.mItems.get(1);
                GlobalActionsDialog.ShutDownAction.class,

                GlobalActionsDialog.RestartAction.class);
        assertTrue(firstItem instanceof GlobalActionsDialog.PowerOptionsAction);
        assertTrue(secondItem instanceof GlobalActionsDialog.ScreenshotAction);
    }
    }


    @Test
    @Test
@@ -341,24 +349,23 @@ public class GlobalActionsDialogTest extends SysuiTestCase {
        doReturn(true).when(mGlobalActionsDialog).shouldShowAction(any());
        doReturn(true).when(mGlobalActionsDialog).shouldShowAction(any());
        String[] actions = {
        String[] actions = {
                GlobalActionsDialog.GLOBAL_ACTION_KEY_EMERGENCY,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_EMERGENCY,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_LOCKDOWN,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_RESTART,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_RESTART,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_LOCKDOWN,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_SCREENSHOT
                GlobalActionsDialog.GLOBAL_ACTION_KEY_SCREENSHOT
        };
        };
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        mGlobalActionsDialog.createActionItems();
        mGlobalActionsDialog.createActionItems();


        assertEquals(4, mGlobalActionsDialog.mItems.size());
        assertItemsOfType(mGlobalActionsDialog.mItems,
        assertEquals(0, mGlobalActionsDialog.mOverflowItems.size());
                GlobalActionsDialog.EmergencyAction.class,
        assertEquals(2, mGlobalActionsDialog.mPowerItems.size());
                GlobalActionsDialog.LockDownAction.class,

                GlobalActionsDialog.PowerOptionsAction.class,
        // with four items, make sure power still shows up immediately after Emergency
                GlobalActionsDialog.ScreenshotAction.class);
        GlobalActionsDialog.Action firstItem = mGlobalActionsDialog.mItems.get(0);
        assertThat(mGlobalActionsDialog.mOverflowItems).isEmpty();
        GlobalActionsDialog.Action secondItem = mGlobalActionsDialog.mItems.get(1);
        assertItemsOfType(mGlobalActionsDialog.mPowerItems,

                GlobalActionsDialog.ShutDownAction.class,
        assertTrue(firstItem instanceof GlobalActionsDialog.EmergencyAction);
                GlobalActionsDialog.RestartAction.class);
        assertTrue(secondItem instanceof GlobalActionsDialog.PowerOptionsAction);
    }
    }


    @Test
    @Test
@@ -368,20 +375,26 @@ public class GlobalActionsDialogTest extends SysuiTestCase {
        doReturn(3).when(mGlobalActionsDialog).getMaxShownPowerItems();
        doReturn(3).when(mGlobalActionsDialog).getMaxShownPowerItems();
        // make sure lockdown action will be shown
        // make sure lockdown action will be shown
        doReturn(true).when(mGlobalActionsDialog).shouldDisplayLockdown(any());
        doReturn(true).when(mGlobalActionsDialog).shouldDisplayLockdown(any());
        // make sure bugreport is also shown
        doReturn(true).when(mGlobalActionsDialog).shouldDisplayBugReport(any());
        // ensure items are not blocked by keyguard or device provisioning
        // ensure items are not blocked by keyguard or device provisioning
        doReturn(true).when(mGlobalActionsDialog).shouldShowAction(any());
        doReturn(true).when(mGlobalActionsDialog).shouldShowAction(any());
        String[] actions = {
        String[] actions = {
                GlobalActionsDialog.GLOBAL_ACTION_KEY_EMERGENCY,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_EMERGENCY,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_SCREENSHOT,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_BUGREPORT,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_LOCKDOWN,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_LOCKDOWN,
        };
        };
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        mGlobalActionsDialog.createActionItems();
        mGlobalActionsDialog.createActionItems();


        assertEquals(3, mGlobalActionsDialog.mItems.size());
        assertItemsOfType(mGlobalActionsDialog.mItems,
        assertEquals(1, mGlobalActionsDialog.mOverflowItems.size());
                GlobalActionsDialog.EmergencyAction.class,
        assertEquals(0, mGlobalActionsDialog.mPowerItems.size());
                GlobalActionsDialog.ShutDownAction.class,
                GlobalActionsDialog.BugReportAction.class);
        assertItemsOfType(mGlobalActionsDialog.mOverflowItems,
                GlobalActionsDialog.LockDownAction.class);
        assertThat(mGlobalActionsDialog.mPowerItems).isEmpty();
    }
    }


    @Test
    @Test
@@ -402,12 +415,17 @@ public class GlobalActionsDialogTest extends SysuiTestCase {
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        mGlobalActionsDialog.createActionItems();
        mGlobalActionsDialog.createActionItems();


        assertEquals(4, mGlobalActionsDialog.mItems.size());
        assertItemsOfType(mGlobalActionsDialog.mItems,
        assertEquals(0, mGlobalActionsDialog.mOverflowItems.size());
                GlobalActionsDialog.EmergencyAction.class,
                GlobalActionsDialog.ShutDownAction.class,
                GlobalActionsDialog.RestartAction.class,
                GlobalActionsDialog.LockDownAction.class);
        assertThat(mGlobalActionsDialog.mOverflowItems).isEmpty();
        assertThat(mGlobalActionsDialog.mPowerItems).isEmpty();
    }
    }


    @Test
    @Test
    public void testCreateActionItems_maxThree_itemNotShown() {
    public void testCreateActionItems_maxThree_lockdownDisabled_doesNotShowLockdown() {
        mGlobalActionsDialog = spy(mGlobalActionsDialog);
        mGlobalActionsDialog = spy(mGlobalActionsDialog);
        // allow only 3 items to be shown
        // allow only 3 items to be shown
        doReturn(3).when(mGlobalActionsDialog).getMaxShownPowerItems();
        doReturn(3).when(mGlobalActionsDialog).getMaxShownPowerItems();
@@ -423,8 +441,42 @@ public class GlobalActionsDialogTest extends SysuiTestCase {
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        mGlobalActionsDialog.createActionItems();
        mGlobalActionsDialog.createActionItems();


        assertEquals(3, mGlobalActionsDialog.mItems.size());
        assertItemsOfType(mGlobalActionsDialog.mItems,
        assertEquals(0, mGlobalActionsDialog.mOverflowItems.size());
                GlobalActionsDialog.EmergencyAction.class,
                GlobalActionsDialog.ShutDownAction.class,
                GlobalActionsDialog.RestartAction.class);
        assertThat(mGlobalActionsDialog.mOverflowItems).isEmpty();
        assertThat(mGlobalActionsDialog.mPowerItems).isEmpty();
    }

    @Test
    public void testCreateActionItems_shouldShowAction_excludeBugReport() {
        mGlobalActionsDialog = spy(mGlobalActionsDialog);
        // allow only 3 items to be shown
        doReturn(3).when(mGlobalActionsDialog).getMaxShownPowerItems();
        doReturn(true).when(mGlobalActionsDialog).shouldDisplayBugReport(any());
        // exclude bugreport in shouldShowAction to demonstrate how any button can be removed
        doAnswer(
                invocation -> !(invocation.getArgument(0)
                        instanceof GlobalActionsDialog.BugReportAction))
                .when(mGlobalActionsDialog).shouldShowAction(any());

        String[] actions = {
                GlobalActionsDialog.GLOBAL_ACTION_KEY_EMERGENCY,
                // bugreport action not allowed
                GlobalActionsDialog.GLOBAL_ACTION_KEY_BUGREPORT,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_RESTART,
        };
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        mGlobalActionsDialog.createActionItems();

        assertItemsOfType(mGlobalActionsDialog.mItems,
                GlobalActionsDialog.EmergencyAction.class,
                GlobalActionsDialog.ShutDownAction.class,
                GlobalActionsDialog.RestartAction.class);
        assertThat(mGlobalActionsDialog.mOverflowItems).isEmpty();
        assertThat(mGlobalActionsDialog.mPowerItems).isEmpty();
    }
    }


    @Test
    @Test
@@ -503,9 +555,9 @@ public class GlobalActionsDialogTest extends SysuiTestCase {


    private void setupDefaultActions() {
    private void setupDefaultActions() {
        String[] actions = {
        String[] actions = {
                GlobalActionsDialog.GLOBAL_ACTION_KEY_EMERGENCY,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_POWER,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_RESTART,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_RESTART,
                GlobalActionsDialog.GLOBAL_ACTION_KEY_SCREENSHOT,
        };
        };
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
        doReturn(actions).when(mGlobalActionsDialog).getDefaultActions();
    }
    }