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 Original line Diff line number Diff line
@@ -30,7 +30,7 @@
        methods in Features class.
        methods in Features class.
    -->
    -->
    <bool name="feature_archive_creation">false</bool>
    <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_paging">true</bool>
    <bool name="feature_content_refresh">true</bool>
    <bool name="feature_content_refresh">true</bool>
    <bool name="feature_folders_in_search_results">true</bool>
    <bool name="feature_folders_in_search_results">true</bool>
+10 −2
Original line number Original line 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.BooleanConsumer;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.DocumentInfo;
import com.android.documentsui.base.DocumentStack;
import com.android.documentsui.base.DocumentStack;
import com.android.documentsui.base.Features;
import com.android.documentsui.base.Lookup;
import com.android.documentsui.base.Lookup;
import com.android.documentsui.base.Providers;
import com.android.documentsui.base.Providers;
import com.android.documentsui.base.RootInfo;
import com.android.documentsui.base.RootInfo;
@@ -89,7 +90,7 @@ public abstract class AbstractActionHandler<T extends Activity & CommonAddons>
    protected final SelectionManager mSelectionMgr;
    protected final SelectionManager mSelectionMgr;
    protected final SearchViewManager mSearchMgr;
    protected final SearchViewManager mSearchMgr;
    protected final Lookup<String, Executor> mExecutors;
    protected final Lookup<String, Executor> mExecutors;
    protected final Injector mInjector;
    protected final Injector<?> mInjector;


    private final LoaderBindings mBindings;
    private final LoaderBindings mBindings;


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


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


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

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


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


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

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


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


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


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


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

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


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

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


            if (Shared.VERBOSE) {
            if (Shared.VERBOSE) {
                Log.v(TAG, "Debug mode " + (debugEnabled ? "on" : "off"));
                Log.v(TAG, "Debug mode " + (debugEnabled ? "on" : "off"));
+14 −18
Original line number Original line Diff line number Diff line
@@ -36,6 +36,7 @@ public interface Features {
    boolean isCommandInterceptorEnabled();
    boolean isCommandInterceptorEnabled();
    boolean isContentPagingEnabled();
    boolean isContentPagingEnabled();
    boolean isContentRefreshEnabled();
    boolean isContentRefreshEnabled();
    boolean isDebugSupportEnabled();
    boolean isFoldersInSearchResultsEnabled();
    boolean isFoldersInSearchResultsEnabled();
    boolean isGestureScaleEnabled();
    boolean isGestureScaleEnabled();
    boolean isLaunchToDocumentEnabled();
    boolean isLaunchToDocumentEnabled();
@@ -43,12 +44,8 @@ public interface Features {
    boolean isSystemKeyboardNavigationEnabled();
    boolean isSystemKeyboardNavigationEnabled();
    boolean isVirtualFilesSharingEnabled();
    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
     * Note that all feature may not support being enabled at runtime as
     * they may depend on runtime initialization guarded by feature check.
     * 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.
     * @param feature int reference to a boolean feature resource.
     */
     */
    public static void forceFeature(@BoolRes int feature, boolean enabled) {
    void forceFeature(@BoolRes int feature, boolean enabled);
        RuntimeFeatures.sDebugEnabled.put(feature, enabled);

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


    final class RuntimeFeatures implements Features {
    final class RuntimeFeatures implements Features {


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


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


        /**
        @Override
         * 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.
         */
        public void forceFeature(@BoolRes int feature, boolean enabled) {
        public void forceFeature(@BoolRes int feature, boolean enabled) {
            mDebugEnabled.put(feature, enabled);
            mDebugEnabled.put(feature, enabled);
        }
        }


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


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


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

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


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

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


    @VisibleForTesting
    @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<>();
    private final List<EventHandler<String[]>> mCommands = new ArrayList<>();


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


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


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


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


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


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


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


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


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


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