1 /* 2 * Copyright (C) 2014 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.systemui.recents.misc; 18 19 import android.os.Handler; 20 import android.os.SystemClock; 21 import android.view.KeyEvent; 22 import com.android.systemui.recents.Constants; 23 24 /** 25 * A trigger for catching a debug chord. 26 * We currently use volume up then volume down to trigger this mode. 27 */ 28 public class DebugTrigger { 29 30 Handler mHandler; 31 Runnable mTriggeredRunnable; 32 33 int mLastKeyCode; 34 long mLastKeyCodeTime; 35 36 public DebugTrigger(Runnable triggeredRunnable) { 37 mHandler = new Handler(); 38 mTriggeredRunnable = triggeredRunnable; 39 } 40 41 /** Resets the debug trigger */ 42 void reset() { 43 mLastKeyCode = 0; 44 mLastKeyCodeTime = 0; 45 } 46 47 /** 48 * Processes a key event and tests if it is a part of the trigger. If the chord is complete, 49 * then we just call the callback. 50 */ 51 public void onKeyEvent(int keyCode) { 52 if (!Constants.DebugFlags.App.EnableDebugMode) return; 53 54 if (mLastKeyCode == 0) { 55 if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) { 56 mLastKeyCode = keyCode; 57 mLastKeyCodeTime = SystemClock.uptimeMillis(); 58 return; 59 } 60 } else { 61 if (mLastKeyCode == KeyEvent.KEYCODE_VOLUME_UP && 62 keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) { 63 if ((SystemClock.uptimeMillis() - mLastKeyCodeTime) < 750) { 64 mTriggeredRunnable.run(); 65 } 66 } 67 } 68 reset(); 69 } 70 } 71