Home | History | Annotate | Download | only in base
      1 /*
      2  * Copyright (C) 2017 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.android.documentsui.base;
     18 
     19 import android.util.Log;
     20 import android.util.Pair;
     21 
     22 import com.android.documentsui.Injector;
     23 import com.android.documentsui.R;
     24 
     25 /**
     26  * Debug menu tools requested by QA Fred.
     27  */
     28 public class DebugHelper {
     29 
     30     private static final String TAG = "DebugHelper";
     31 
     32     private static final int[][] sCode = new int[][] {
     33             {19, 19, 20, 20, 21, 22, 21, 22, 30, 29},
     34             {51, 51, 47, 47, 29, 32, 29, 32, 30, 29}
     35     };
     36 
     37     private static final int[][] sColors = new int[][] {
     38             {0xFFDB3236, 0xFFB71C1C},
     39             {0xFF3cba54, 0xFF1B5E20},
     40             {0xFFf4c20d, 0xFFF9A825},
     41             {0xFF4885ed, 0xFF0D47A1}
     42     };
     43 
     44     @SuppressWarnings("unchecked")
     45     private static final Pair<String, Integer>[] sMessages = new Pair[]{
     46             new Pair<>("Woof Woof", R.drawable.debug_msg_1),
     47             new Pair<>("", R.drawable.debug_msg_2)
     48     };
     49 
     50     private final Injector<?> mInjector;
     51 
     52     private boolean mDebugEnabled;
     53     private long mLastTime;
     54     private int mPosition;
     55     private int mCodeIndex;
     56     private int mColorIndex;
     57     private int mMessageIndex;
     58 
     59     public DebugHelper(Injector<?> injector) {
     60         mInjector = injector;
     61     }
     62 
     63     public int[] getNextColors() {
     64         assert (mInjector.features.isDebugSupportEnabled());
     65 
     66         if (mColorIndex == sColors.length) {
     67             mColorIndex = 0;
     68         }
     69 
     70         return sColors[mColorIndex++];
     71     }
     72 
     73     public Pair<String, Integer> getNextMessage() {
     74         assert (mInjector.features.isDebugSupportEnabled());
     75 
     76         if (mMessageIndex == sMessages.length) {
     77             mMessageIndex = 0;
     78         }
     79 
     80         return sMessages[mMessageIndex++];
     81     }
     82 
     83     public void debugCheck(long time, int keyCode) {
     84         if (time == mLastTime) {
     85             return;
     86         }
     87         mLastTime = time;
     88 
     89         if (mPosition == 0) {
     90             for (int i = 0; i < sCode.length; i++) {
     91                 if (keyCode == sCode[i][0]) {
     92                     mCodeIndex = i;
     93                     break;
     94                 }
     95             }
     96         }
     97 
     98         if (keyCode == sCode[mCodeIndex][mPosition]) {
     99             mPosition++;
    100         } else if (mPosition  > 2 || (mPosition == 2 && keyCode != sCode[mCodeIndex][0])) {
    101             mPosition = 0;
    102         }
    103 
    104         if (mPosition == sCode[mCodeIndex].length) {
    105             mPosition = 0;
    106             toggleDebugMode();
    107         }
    108     }
    109 
    110     public void toggleDebugMode() {
    111         mDebugEnabled = !mDebugEnabled;
    112         // Actions is content-scope, so it can technically be null, though
    113         // not likely.
    114         if (mInjector.actions != null) {
    115             mInjector.actions.setDebugMode(mDebugEnabled);
    116         }
    117 
    118         if (Shared.VERBOSE) {
    119             Log.v(TAG, "Debug mode " + (mDebugEnabled ? "on" : "off"));
    120         }
    121     }
    122 }
    123