fix: volume key control not working when accessibility service is disabled
This commit is contained in:
@@ -9,8 +9,8 @@ android {
|
|||||||
applicationId "com.stardust.scriptdroid"
|
applicationId "com.stardust.scriptdroid"
|
||||||
minSdkVersion 17
|
minSdkVersion 17
|
||||||
targetSdkVersion 23
|
targetSdkVersion 23
|
||||||
versionCode 160
|
versionCode 161
|
||||||
versionName "2.0.16 Alpha3"
|
versionName "2.0.16 Alpha4"
|
||||||
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
|
||||||
multiDexEnabled true
|
multiDexEnabled true
|
||||||
ndk {
|
ndk {
|
||||||
|
|||||||
@@ -11,6 +11,7 @@ import com.squareup.leakcanary.LeakCanary;
|
|||||||
import com.stardust.app.SimpleActivityLifecycleCallbacks;
|
import com.stardust.app.SimpleActivityLifecycleCallbacks;
|
||||||
import com.stardust.app.VolumeChangeObserver;
|
import com.stardust.app.VolumeChangeObserver;
|
||||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||||
|
import com.stardust.scriptdroid.autojs.key.GlobalKeyObserver;
|
||||||
import com.stardust.scriptdroid.autojs.record.GlobalRecorder;
|
import com.stardust.scriptdroid.autojs.record.GlobalRecorder;
|
||||||
import com.stardust.scriptdroid.statics.ScriptStatics;
|
import com.stardust.scriptdroid.statics.ScriptStatics;
|
||||||
import com.stardust.scriptdroid.tool.CrashHandler;
|
import com.stardust.scriptdroid.tool.CrashHandler;
|
||||||
@@ -69,22 +70,10 @@ public class App extends MultiDexApplication {
|
|||||||
ThemeColorManager.init(this);
|
ThemeColorManager.init(this);
|
||||||
AutoJs.initInstance(this);
|
AutoJs.initInstance(this);
|
||||||
JsBeautifierFactory.initJsBeautify(this, "js/jsbeautify.js");
|
JsBeautifierFactory.initJsBeautify(this, "js/jsbeautify.js");
|
||||||
initVolumeChangeObserver();
|
GlobalKeyObserver.getSingleton();
|
||||||
GlobalRecorder.initSingleton(this);
|
GlobalRecorder.initSingleton(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void initVolumeChangeObserver() {
|
|
||||||
AccessibilityService.getStickOnKeyObserver().addListener(new OnKeyListener() {
|
|
||||||
@Override
|
|
||||||
public void onKeyEvent(int keyCode, KeyEvent event) {
|
|
||||||
if (event.getAction() == KeyEvent.ACTION_DOWN &&
|
|
||||||
(keyCode == KeyEvent.KEYCODE_VOLUME_UP)
|
|
||||||
&& Pref.isRunningVolumeControlEnabled()) {
|
|
||||||
AutoJs.getInstance().getScriptEngineService().stopAllAndToast();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
public UiHandler getUiHandler() {
|
public UiHandler getUiHandler() {
|
||||||
|
|||||||
@@ -0,0 +1,115 @@
|
|||||||
|
package com.stardust.scriptdroid.autojs.key;
|
||||||
|
|
||||||
|
import android.util.Log;
|
||||||
|
import android.view.KeyEvent;
|
||||||
|
|
||||||
|
import com.stardust.autojs.core.inputevent.InputEventObserver;
|
||||||
|
import com.stardust.autojs.core.inputevent.ShellKeyObserver;
|
||||||
|
import com.stardust.event.EventDispatcher;
|
||||||
|
import com.stardust.scriptdroid.Pref;
|
||||||
|
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||||
|
import com.stardust.view.accessibility.AccessibilityService;
|
||||||
|
import com.stardust.view.accessibility.OnKeyListener;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Stardust on 2017/8/14.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class GlobalKeyObserver implements OnKeyListener, ShellKeyObserver.KeyListener {
|
||||||
|
|
||||||
|
|
||||||
|
public interface OnVolumeDownListener {
|
||||||
|
void onVolumeDown();
|
||||||
|
}
|
||||||
|
|
||||||
|
private static final EventDispatcher.Event<OnVolumeDownListener> VOLUME_DOWN_EVENT = new EventDispatcher.Event<OnVolumeDownListener>() {
|
||||||
|
@Override
|
||||||
|
public void notify(OnVolumeDownListener l) {
|
||||||
|
l.onVolumeDown();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
private static final String LOG_TAG = "GlobalKeyObserver";
|
||||||
|
private static final long EVENT_TIMEOUT = 200;
|
||||||
|
private static GlobalKeyObserver sSingleton = new GlobalKeyObserver();
|
||||||
|
private EventDispatcher<OnVolumeDownListener> mVolumeDownEventDispatcher = new EventDispatcher<>();
|
||||||
|
private boolean mVolumeDownFromShell, mVolumeDownFromAccessibility;
|
||||||
|
private boolean mVolumeUpFromShell, mVolumeUpFromAccessibility;
|
||||||
|
|
||||||
|
GlobalKeyObserver() {
|
||||||
|
AccessibilityService.getStickOnKeyObserver()
|
||||||
|
.addListener(this);
|
||||||
|
ShellKeyObserver observer = new ShellKeyObserver();
|
||||||
|
observer.setKeyListener(this);
|
||||||
|
InputEventObserver.getGlobal().addListener(observer);
|
||||||
|
}
|
||||||
|
|
||||||
|
public static GlobalKeyObserver getSingleton() {
|
||||||
|
return sSingleton;
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onVolumeUp() {
|
||||||
|
Log.d(LOG_TAG, "onVolumeUp at " + System.currentTimeMillis());
|
||||||
|
if (Pref.isRunningVolumeControlEnabled()) {
|
||||||
|
AutoJs.getInstance().getScriptEngineService().stopAllAndToast();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
public void onVolumeDown() {
|
||||||
|
Log.d(LOG_TAG, "onVolumeDown at " + System.currentTimeMillis());
|
||||||
|
mVolumeDownEventDispatcher.dispatchEvent(VOLUME_DOWN_EVENT);
|
||||||
|
}
|
||||||
|
|
||||||
|
public void addVolumeDownListener(OnVolumeDownListener listener) {
|
||||||
|
mVolumeDownEventDispatcher.addListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
public boolean removeVolumeDownListener(OnVolumeDownListener listener) {
|
||||||
|
return mVolumeDownEventDispatcher.removeListener(listener);
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKeyEvent(int keyCode, KeyEvent event) {
|
||||||
|
if (event.getAction() != KeyEvent.ACTION_UP)
|
||||||
|
return;
|
||||||
|
if (keyCode == KeyEvent.KEYCODE_VOLUME_DOWN) {
|
||||||
|
if (mVolumeDownFromShell) {
|
||||||
|
mVolumeDownFromShell = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mVolumeUpFromAccessibility = true;
|
||||||
|
onVolumeDown();
|
||||||
|
} else if (keyCode == KeyEvent.KEYCODE_VOLUME_UP) {
|
||||||
|
if (mVolumeUpFromShell) {
|
||||||
|
mVolumeUpFromShell = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mVolumeUpFromAccessibility = true;
|
||||||
|
onVolumeUp();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKeyDown(String keyName) {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void onKeyUp(String keyName) {
|
||||||
|
if ("KEY_VOLUMEUP".equals(keyName)) {
|
||||||
|
if (mVolumeUpFromAccessibility) {
|
||||||
|
mVolumeUpFromAccessibility = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mVolumeUpFromShell = true;
|
||||||
|
onVolumeUp();
|
||||||
|
} else if ("KEY_VOLUMEDOWN".equals(keyName)) {
|
||||||
|
if (mVolumeDownFromAccessibility) {
|
||||||
|
mVolumeDownFromAccessibility = false;
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
mVolumeDownFromShell = true;
|
||||||
|
onVolumeDown();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -24,6 +24,7 @@ import com.stardust.scriptdroid.Pref;
|
|||||||
import com.stardust.scriptdroid.R;
|
import com.stardust.scriptdroid.R;
|
||||||
import com.stardust.scriptdroid.accessibility.AccessibilityEventHelper;
|
import com.stardust.scriptdroid.accessibility.AccessibilityEventHelper;
|
||||||
import com.stardust.scriptdroid.autojs.AutoJs;
|
import com.stardust.scriptdroid.autojs.AutoJs;
|
||||||
|
import com.stardust.scriptdroid.autojs.key.GlobalKeyObserver;
|
||||||
import com.stardust.scriptdroid.autojs.record.GlobalRecorder;
|
import com.stardust.scriptdroid.autojs.record.GlobalRecorder;
|
||||||
import com.stardust.scriptdroid.external.floatingwindow.menu.HoverMenuService;
|
import com.stardust.scriptdroid.external.floatingwindow.menu.HoverMenuService;
|
||||||
import com.stardust.scriptdroid.ui.common.ScriptOperations;
|
import com.stardust.scriptdroid.ui.common.ScriptOperations;
|
||||||
@@ -48,7 +49,7 @@ import io.mattcarroll.hover.NavigatorContent;
|
|||||||
* Created by Stardust on 2017/3/12.
|
* Created by Stardust on 2017/3/12.
|
||||||
*/
|
*/
|
||||||
|
|
||||||
public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStateChangedListener, OnKeyListener {
|
public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStateChangedListener, GlobalKeyObserver.OnVolumeDownListener {
|
||||||
|
|
||||||
private View mView;
|
private View mView;
|
||||||
@BindView(R.id.sw_recorded_by_root)
|
@BindView(R.id.sw_recorded_by_root)
|
||||||
@@ -78,10 +79,10 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
|
|||||||
mRecorder = GlobalRecorder.getSingleton(context);
|
mRecorder = GlobalRecorder.getSingleton(context);
|
||||||
mRecorder.addOnStateChangedListener(this);
|
mRecorder.addOnStateChangedListener(this);
|
||||||
setState(mRecorder.getState());
|
setState(mRecorder.getState());
|
||||||
AccessibilityService.getStickOnKeyObserver().addListener(this);
|
GlobalKeyObserver.getSingleton().addVolumeDownListener(this);
|
||||||
}
|
}
|
||||||
|
|
||||||
private void onVolumeDown() {
|
public void onVolumeDown() {
|
||||||
if (!Pref.isRecordVolumeControlEnable()) {
|
if (!Pref.isRecordVolumeControlEnable()) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
@@ -106,7 +107,6 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onShown(@NonNull Navigator navigator) {
|
public void onShown(@NonNull Navigator navigator) {
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -179,7 +179,6 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
|
|||||||
public void onMenuExit() {
|
public void onMenuExit() {
|
||||||
HoverMenuService.getEventBus().unregister(this);
|
HoverMenuService.getEventBus().unregister(this);
|
||||||
mRecorder.removeOnStateChangedListener(this);
|
mRecorder.removeOnStateChangedListener(this);
|
||||||
AccessibilityService.getStickOnKeyObserver().removeListener(this);
|
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -202,12 +201,5 @@ public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStat
|
|||||||
setState(Recorder.STATE_RECORDING);
|
setState(Recorder.STATE_RECORDING);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onKeyEvent(int keyCode, KeyEvent event) {
|
|
||||||
if (event.getAction() == KeyEvent.ACTION_DOWN &&
|
|
||||||
(keyCode == KeyEvent.KEYCODE_VOLUME_DOWN)) {
|
|
||||||
onVolumeDown();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user