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

Commit 53916eff authored by Steve McKay's avatar Steve McKay Committed by Ben Lin
Browse files

Disable debug commands by default.

Enable only when debug mode is enabled.
Don't enable any debug/fun stuff when respective user policies disallow.

Bug: 36837295
Test: Build and test manually.
Change-Id: Id89836f970f9af8f3561d2648ef3109b234282bf
parent a413c355
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -30,7 +30,7 @@
        methods in Features class.
    -->
    <bool name="feature_archive_creation">false</bool>
    <bool name="feature_command_interceptor">true</bool>
    <bool name="feature_command_interceptor">false</bool>
    <bool name="feature_content_paging">true</bool>
    <bool name="feature_content_refresh">true</bool>
    <bool name="feature_folders_in_search_results">true</bool>
+10 −2
Original line number Diff line number Diff line
@@ -42,6 +42,7 @@ import com.android.documentsui.LoadDocStackTask.LoadDocStackCallback;
import com.android.documentsui.base.BooleanConsumer;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.DocumentStack;
import com.android.documentsui.base.Features;
import com.android.documentsui.base.Lookup;
import com.android.documentsui.base.Providers;
import com.android.documentsui.base.RootInfo;
@@ -89,7 +90,7 @@ public abstract class AbstractActionHandler<T extends Activity & CommonAddons>
    protected final SelectionManager mSelectionMgr;
    protected final SearchViewManager mSearchMgr;
    protected final Lookup<String, Executor> mExecutors;
    protected final Injector mInjector;
    protected final Injector<?> mInjector;

    private final LoaderBindings mBindings;

@@ -115,7 +116,7 @@ public abstract class AbstractActionHandler<T extends Activity & CommonAddons>
            DocumentsAccess docs,
            SearchViewManager searchMgr,
            Lookup<String, Executor> executors,
            Injector injector) {
            Injector<?> injector) {

        assert(activity != null);
        assert(state != null);
@@ -355,7 +356,12 @@ public abstract class AbstractActionHandler<T extends Activity & CommonAddons>

    @Override
    public void setDebugMode(boolean enabled) {
        if (!mInjector.features.isDebugSupportEnabled()) {
            return;
        }

        mState.debugMode = enabled;
        mInjector.features.forceFeature(R.bool.feature_command_interceptor, enabled);
        mActivity.invalidateOptionsMenu();

        if (enabled) {
@@ -370,6 +376,8 @@ public abstract class AbstractActionHandler<T extends Activity & CommonAddons>

    @Override
    public void showDebugMessage() {
        assert (mInjector.features.isDebugSupportEnabled());

        int[] colors = mInjector.debugHelper.getNextColors();
        Pair<String, Integer> messagePair = mInjector.debugHelper.getNextMessage();

+11 −3
Original line number Diff line number Diff line
@@ -43,18 +43,20 @@ public class DebugHelper {
    };

    private boolean debugEnabled = false;
    private Injector mInjector;
    private long lastTime = 0;
    private int position = 0;
    private int codeIndex = 0;
    private int colorIndex = 0;
    private int messageIndex = 0;
    private Injector<?> mInjector;

    public DebugHelper(Injector injector) {
    public DebugHelper(Injector<?> injector) {
        mInjector = injector;
    }

    public int[] getNextColors() {
        assert (mInjector.features.isDebugSupportEnabled());

        if (colorIndex == colors.length) {
            colorIndex = 0;
        }
@@ -63,6 +65,8 @@ public class DebugHelper {
    }

    public Pair<String, Integer> getNextMessage() {
        assert (mInjector.features.isDebugSupportEnabled());

        if (messageIndex == messages.length) {
            messageIndex = 0;
        }
@@ -94,7 +98,11 @@ public class DebugHelper {
        if (position == code[codeIndex].length) {
            position = 0;
            debugEnabled = !debugEnabled;
            // Actions is content-scope, so it can technically be null, though
            // not likely.
            if (mInjector.actions != null) {
                mInjector.actions.setDebugMode(debugEnabled);
            }

            if (Shared.VERBOSE) {
                Log.v(TAG, "Debug mode " + (debugEnabled ? "on" : "off"));
+14 −18
Original line number Diff line number Diff line
@@ -36,6 +36,7 @@ public interface Features {
    boolean isCommandInterceptorEnabled();
    boolean isContentPagingEnabled();
    boolean isContentRefreshEnabled();
    boolean isDebugSupportEnabled();
    boolean isFoldersInSearchResultsEnabled();
    boolean isGestureScaleEnabled();
    boolean isLaunchToDocumentEnabled();
@@ -43,12 +44,8 @@ public interface Features {
    boolean isSystemKeyboardNavigationEnabled();
    boolean isVirtualFilesSharingEnabled();

    public static Features create(Context context) {
        return new RuntimeFeatures(context.getResources(), UserManager.get(context));
    }

    /**
     * Call this to force-enable any particular feature known by this class.
     * Call this to force-enable any particular feature known by this instance.
     * Note that all feature may not support being enabled at runtime as
     * they may depend on runtime initialization guarded by feature check.
     *
@@ -56,13 +53,14 @@ public interface Features {
     *
     * @param feature int reference to a boolean feature resource.
     */
    public static void forceFeature(@BoolRes int feature, boolean enabled) {
        RuntimeFeatures.sDebugEnabled.put(feature, enabled);
    void forceFeature(@BoolRes int feature, boolean enabled);

    public static Features create(Context context) {
        return new RuntimeFeatures(context.getResources(), UserManager.get(context));
    }

    final class RuntimeFeatures implements Features {

        private static final SparseBooleanArray sDebugEnabled = new SparseBooleanArray();
        private final SparseBooleanArray mDebugEnabled = new SparseBooleanArray();

        private final Resources mRes;
@@ -73,21 +71,13 @@ public interface Features {
            mUserMgr = userMgr;
        }

        /**
         * Call this to force-enable any particular feature known by this instance.
         * Note that all feature may not support being enabled at runtime as
         * they may depend on runtime initialization guarded by feature check.
         *
         * <p>Feature changes will be persisted across activities, but not app restarts.
         *
         * @param feature int reference to a boolean feature resource.
         */
        @Override
        public void forceFeature(@BoolRes int feature, boolean enabled) {
            mDebugEnabled.put(feature, enabled);
        }

        private boolean isEnabled(@BoolRes int feature) {
            return mDebugEnabled.get(feature, sDebugEnabled.get(feature, mRes.getBoolean(feature)));
            return mDebugEnabled.get(feature, mRes.getBoolean(feature));
        }

        @Override
@@ -111,6 +101,12 @@ public interface Features {
            return isEnabled(R.bool.feature_content_refresh);
        }

        @Override
        public boolean isDebugSupportEnabled() {
            return !mUserMgr.hasUserRestriction(UserManager.DISALLOW_DEBUGGING_FEATURES)
                    && !mUserMgr.hasUserRestriction(UserManager.DISALLOW_FUN);
        }

        @Override
        public boolean isFoldersInSearchResultsEnabled() {
            return isEnabled(R.bool.feature_folders_in_search_results);
+18 −15
Original line number Diff line number Diff line
@@ -15,6 +15,8 @@
 */
package com.android.documentsui.queries;

import static com.android.documentsui.base.Shared.DEBUG;

import android.content.Context;
import android.support.annotation.VisibleForTesting;
import android.text.TextUtils;
@@ -32,9 +34,9 @@ import java.util.List;
public final class CommandInterceptor implements EventHandler<String> {

    @VisibleForTesting
    static final String COMMAND_PREFIX = "dbg:";
    static final String COMMAND_PREFIX = ":";

    private static final String TAG = "DebugCommandProcessor";
    private static final String TAG = "CommandInterceptor";

    private final List<EventHandler<String[]>> mCommands = new ArrayList<>();

@@ -43,11 +45,11 @@ public final class CommandInterceptor implements EventHandler<String> {
    public CommandInterceptor(Features features) {
        mFeatures = features;

        mCommands.add(CommandInterceptor::quickViewer);
        mCommands.add(CommandInterceptor::gestureScale);
        mCommands.add(CommandInterceptor::archiveCreation);
        mCommands.add(CommandInterceptor::docDetails);
        mCommands.add(CommandInterceptor::forcePaging);
        mCommands.add(this::quickViewer);
        mCommands.add(this::gestureScale);
        mCommands.add(this::archiveCreation);
        mCommands.add(this::docDetails);
        mCommands.add(this::forcePaging);
    }

    public void add(EventHandler<String[]> handler) {
@@ -57,6 +59,7 @@ public final class CommandInterceptor implements EventHandler<String> {
    @Override
    public boolean accept(String query) {
        if (!mFeatures.isCommandInterceptorEnabled()) {
            if (DEBUG) Log.v(TAG, "Skipping input, command interceptor disabled.");
            return false;
        }

@@ -72,7 +75,7 @@ public final class CommandInterceptor implements EventHandler<String> {
        return false;
    }

    private static boolean quickViewer(String[] tokens) {
    private boolean quickViewer(String[] tokens) {
        if ("qv".equals(tokens[0])) {
            if (tokens.length == 2 && !TextUtils.isEmpty(tokens[1])) {
                DebugFlags.setQuickViewer(tokens[1]);
@@ -89,11 +92,11 @@ public final class CommandInterceptor implements EventHandler<String> {
        return false;
    }

    private static boolean gestureScale(String[] tokens) {
    private boolean gestureScale(String[] tokens) {
        if ("gs".equals(tokens[0])) {
            if (tokens.length == 2 && !TextUtils.isEmpty(tokens[1])) {
                boolean enabled = asBool(tokens[1]);
                Features.forceFeature(R.bool.feature_gesture_scale, enabled);
                mFeatures.forceFeature(R.bool.feature_gesture_scale, enabled);
                Log.i(TAG, "Set gesture scale enabled to: " + enabled);
                return true;
            }
@@ -102,11 +105,11 @@ public final class CommandInterceptor implements EventHandler<String> {
        return false;
    }

    private static boolean archiveCreation(String[] tokens) {
    private boolean archiveCreation(String[] tokens) {
        if ("zip".equals(tokens[0])) {
            if (tokens.length == 2 && !TextUtils.isEmpty(tokens[1])) {
                boolean enabled = asBool(tokens[1]);
                Features.forceFeature(R.bool.feature_archive_creation, enabled);
                mFeatures.forceFeature(R.bool.feature_archive_creation, enabled);
                Log.i(TAG, "Set gesture scale enabled to: " + enabled);
                return true;
            }
@@ -115,7 +118,7 @@ public final class CommandInterceptor implements EventHandler<String> {
        return false;
    }

    private static boolean docDetails(String[] tokens) {
    private boolean docDetails(String[] tokens) {
        if ("docinfo".equals(tokens[0])) {
            if (tokens.length == 2 && !TextUtils.isEmpty(tokens[1])) {
                boolean enabled = asBool(tokens[1]);
@@ -128,7 +131,7 @@ public final class CommandInterceptor implements EventHandler<String> {
        return false;
    }

    private static boolean forcePaging(String[] tokens) {
    private boolean forcePaging(String[] tokens) {
        if ("page".equals(tokens[0])) {
            if (tokens.length >= 2) {
                try {
@@ -153,7 +156,7 @@ public final class CommandInterceptor implements EventHandler<String> {
        return false;
    }

    private static final boolean asBool(String val) {
    private final boolean asBool(String val) {
        if (val == null || val.equals("0")) {
            return false;
        }
Loading