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 static com.android.documentsui.base.SharedMinimal.VERBOSE;
20 
21 import android.util.Log;
22 import android.util.Pair;
23 
24 import com.android.documentsui.Injector;
25 import com.android.documentsui.R;
26 
27 /**
28  * Debug menu tools requested by QA Fred.
29  */
30 public class DebugHelper {
31 
32     private static final String TAG = "DebugHelper";
33 
34     private static final int[][] sCode = new int[][] {
35             {19, 19, 20, 20, 21, 22, 21, 22, 30, 29},
36             {51, 51, 47, 47, 29, 32, 29, 32, 30, 29}
37     };
38 
39     private static final int[][] sColors = new int[][] {
40             {0xFFDB3236, 0xFFB71C1C},
41             {0xFF3cba54, 0xFF1B5E20},
42             {0xFFf4c20d, 0xFFF9A825},
43             {0xFF4885ed, 0xFF0D47A1}
44     };
45 
46     @SuppressWarnings("unchecked")
47     private static final Pair<String, Integer>[] sMessages = new Pair[]{
48             new Pair<>("Woof Woof", R.drawable.debug_msg_1),
49             new Pair<>("ワンワン", R.drawable.debug_msg_2)
50     };
51 
52     private final Injector<?> mInjector;
53 
54     private boolean mDebugEnabled;
55     private long mLastTime;
56     private int mPosition;
57     private int mCodeIndex;
58     private int mColorIndex;
59     private int mMessageIndex;
60 
DebugHelper(Injector<?> injector)61     public DebugHelper(Injector<?> injector) {
62         mInjector = injector;
63     }
64 
getNextColors()65     public int[] getNextColors() {
66         assert (mInjector.features.isDebugSupportEnabled());
67 
68         if (mColorIndex == sColors.length) {
69             mColorIndex = 0;
70         }
71 
72         return sColors[mColorIndex++];
73     }
74 
getNextMessage()75     public Pair<String, Integer> getNextMessage() {
76         assert (mInjector.features.isDebugSupportEnabled());
77 
78         if (mMessageIndex == sMessages.length) {
79             mMessageIndex = 0;
80         }
81 
82         return sMessages[mMessageIndex++];
83     }
84 
debugCheck(long time, int keyCode)85     public void debugCheck(long time, int keyCode) {
86         if (time == mLastTime) {
87             return;
88         }
89         mLastTime = time;
90 
91         if (mPosition == 0) {
92             for (int i = 0; i < sCode.length; i++) {
93                 if (keyCode == sCode[i][0]) {
94                     mCodeIndex = i;
95                     break;
96                 }
97             }
98         }
99 
100         if (keyCode == sCode[mCodeIndex][mPosition]) {
101             mPosition++;
102         } else if (mPosition  > 2 || (mPosition == 2 && keyCode != sCode[mCodeIndex][0])) {
103             mPosition = 0;
104         }
105 
106         if (mPosition == sCode[mCodeIndex].length) {
107             mPosition = 0;
108             toggleDebugMode();
109         }
110     }
111 
toggleDebugMode()112     public void toggleDebugMode() {
113         mDebugEnabled = !mDebugEnabled;
114         // Actions is content-scope, so it can technically be null, though
115         // not likely.
116         if (mInjector.actions != null) {
117             mInjector.actions.setDebugMode(mDebugEnabled);
118         }
119 
120         if (VERBOSE) {
121             Log.v(TAG, "Debug mode " + (mDebugEnabled ? "on" : "off"));
122         }
123     }
124 }
125