Added volume control
This commit is contained in:
@@ -69,7 +69,7 @@ public class AccessibilityWatchDogService extends AccessibilityService {
|
||||
}
|
||||
|
||||
@Override
|
||||
public synchronized void onAccessibilityEvent(AccessibilityEvent event) {
|
||||
public void onAccessibilityEvent(AccessibilityEvent event) {
|
||||
Log.v(TAG, "onAccessibilityEvent: " + event);
|
||||
synchronized (mDelegates) {
|
||||
for (Map.Entry<Integer, AccessibilityDelegate> entry : mDelegates.entrySet()) {
|
||||
|
||||
@@ -0,0 +1,86 @@
|
||||
package com.stardust.scriptdroid.service;
|
||||
|
||||
import android.app.Service;
|
||||
import android.content.BroadcastReceiver;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.IntentFilter;
|
||||
import android.os.IBinder;
|
||||
import android.support.annotation.Nullable;
|
||||
|
||||
import com.stardust.scriptdroid.Pref;
|
||||
import com.stardust.scriptdroid.external.notification.record.ActionRecordSwitchHandleService;
|
||||
import com.stardust.scriptdroid.record.AccessibilityRecorderDelegate;
|
||||
|
||||
import java.lang.ref.WeakReference;
|
||||
|
||||
import static com.stardust.scriptdroid.external.notification.record.ActionRecordSwitchView.STOPPED;
|
||||
|
||||
/**
|
||||
* Created by Stardust on 2017/2/16.
|
||||
*/
|
||||
|
||||
public class VolumeChangeListenService extends Service {
|
||||
|
||||
private static final String ACTION_VOLUME_CHANGE = "android.media.VOLUME_CHANGED_ACTION";
|
||||
|
||||
private VolumeChangeReceiver mVolumeChangeReceiver;
|
||||
private static WeakReference<VolumeChangeListenService> instance;
|
||||
|
||||
public static void stopServiceIfNeeded() {
|
||||
if (instance != null || instance.get() != null) {
|
||||
instance.get().stopSelf();
|
||||
}
|
||||
}
|
||||
|
||||
public static void startServiceIfNeeded(Context context) {
|
||||
if (Pref.isRecordVolumeControlEnable() && (instance == null || instance.get() == null)) {
|
||||
context.startService(new Intent(context, VolumeChangeListenService.class));
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@Override
|
||||
public void onCreate() {
|
||||
super.onCreate();
|
||||
if (instance != null && instance.get() != null) {
|
||||
instance.get().stopSelf();
|
||||
}
|
||||
instance = new WeakReference<>(this);
|
||||
mVolumeChangeReceiver = new VolumeChangeReceiver();
|
||||
registerReceiver(mVolumeChangeReceiver, new IntentFilter(ACTION_VOLUME_CHANGE));
|
||||
}
|
||||
|
||||
@Nullable
|
||||
@Override
|
||||
public IBinder onBind(Intent intent) {
|
||||
return null;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDestroy() {
|
||||
super.onDestroy();
|
||||
unregisterReceiver(mVolumeChangeReceiver);
|
||||
}
|
||||
|
||||
private class VolumeChangeReceiver extends BroadcastReceiver {
|
||||
|
||||
private long mLastChangeMillis;
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
if (intent.getAction().equals(ACTION_VOLUME_CHANGE)) {
|
||||
if (System.currentTimeMillis() - mLastChangeMillis < 400) {
|
||||
return;
|
||||
}
|
||||
mLastChangeMillis = System.currentTimeMillis();
|
||||
if (AccessibilityRecorderDelegate.getInstance().getState() == STOPPED) {
|
||||
ActionRecordSwitchHandleService.startRecord(VolumeChangeListenService.this);
|
||||
} else {
|
||||
ActionRecordSwitchHandleService.stopRecord(VolumeChangeListenService.this);
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user