Added volume control

This commit is contained in:
hyb1996
2017-02-17 11:38:24 +08:00
parent 8993079151
commit 40d6285bed
30 changed files with 309 additions and 78 deletions

View File

@@ -2,6 +2,7 @@ package com.stardust.scriptdroid.external.notification.record;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
@@ -11,6 +12,7 @@ import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.record.AccessibilityRecorderDelegate;
import com.stardust.scriptdroid.service.AccessibilityWatchDogService;
import com.stardust.scriptdroid.service.VolumeChangeListenService;
import com.stardust.scriptdroid.ui.main.MainActivity;
import static com.stardust.scriptdroid.external.notification.record.ActionRecordSwitchView.PAUSED;
@@ -21,10 +23,10 @@ import static com.stardust.scriptdroid.external.notification.record.ActionRecord
* Created by Stardust on 2017/2/15.
*/
public class ActionRecordNotificationHandleService extends Service {
public class ActionRecordSwitchHandleService extends Service {
private static final String EXTRA_INTENT_VALID = "ActionRecordNotificationHandleService.intentValid";
private static final String EXTRA_INTENT_VALID = "ActionRecordSwitchHandleService.intentValid";
private static final String EXTRA_ACTION = "action";
private static final int ACTION_STOP = 17771;
private static final int ACTION_START_OR_PAUSE = 17772;
@@ -32,21 +34,21 @@ public class ActionRecordNotificationHandleService extends Service {
private static final int ACTION_REDO = 17774;
public static PendingIntent getStartOrPauseIntent() {
Intent intent = new Intent(App.getApp(), ActionRecordNotificationHandleService.class)
Intent intent = new Intent(App.getApp(), ActionRecordSwitchHandleService.class)
.putExtra(EXTRA_INTENT_VALID, true)
.putExtra(EXTRA_ACTION, ACTION_START_OR_PAUSE);
return PendingIntent.getService(App.getApp(), ACTION_START_OR_PAUSE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static PendingIntent getStopIntent() {
Intent intent = new Intent(App.getApp(), ActionRecordNotificationHandleService.class)
Intent intent = new Intent(App.getApp(), ActionRecordSwitchHandleService.class)
.putExtra(EXTRA_INTENT_VALID, true)
.putExtra(EXTRA_ACTION, ACTION_STOP);
return PendingIntent.getService(App.getApp(), ACTION_STOP, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static PendingIntent getDeleteIntent() {
Intent intent = new Intent(App.getApp(), ActionRecordNotificationHandleService.class)
Intent intent = new Intent(App.getApp(), ActionRecordSwitchHandleService.class)
.putExtra(EXTRA_INTENT_VALID, true)
.putExtra(EXTRA_ACTION, ACTION_DELETE);
return PendingIntent.getService(App.getApp(), ACTION_DELETE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
@@ -54,7 +56,7 @@ public class ActionRecordNotificationHandleService extends Service {
public static PendingIntent getRedoIntent() {
Intent intent = new Intent(App.getApp(), ActionRecordNotificationHandleService.class)
Intent intent = new Intent(App.getApp(), ActionRecordSwitchHandleService.class)
.putExtra(EXTRA_INTENT_VALID, true)
.putExtra(EXTRA_ACTION, ACTION_REDO);
return PendingIntent.getService(App.getApp(), ACTION_REDO, intent, PendingIntent.FLAG_UPDATE_CURRENT);
@@ -74,19 +76,20 @@ public class ActionRecordNotificationHandleService extends Service {
private void performAction(int action) {
switch (action) {
case ACTION_STOP:
stopRecord();
stopRecord(this);
collapseNotificationBar();
break;
case ACTION_START_OR_PAUSE:
startOrPauseRecord();
startOrPauseRecord(this);
collapseNotificationBar();
break;
case ACTION_DELETE:
ActionRecordSwitchNotification.cancelNotification();
stopRecordIfNeeded();
VolumeChangeListenService.stopServiceIfNeeded();
break;
case ACTION_REDO:
redoRecord();
redoRecord(this);
break;
}
}
@@ -98,41 +101,53 @@ public class ActionRecordNotificationHandleService extends Service {
private void stopRecordIfNeeded() {
if (AccessibilityRecorderDelegate.getInstance().getState() != STOPPED) {
stopRecord();
stopRecord(this);
}
}
private void startOrPauseRecord() {
private static void startOrPauseRecord(Context context) {
int state = AccessibilityRecorderDelegate.getInstance().getState();
if (state == PAUSED) {
AccessibilityRecorderDelegate.getInstance().resumeRecord();
ActionRecordSwitchView.getInstance().setState(RECORDING);
resumeRecord(context);
} else if (state == RECORDING) {
AccessibilityRecorderDelegate.getInstance().pauseRecord();
ActionRecordSwitchView.getInstance().setState(PAUSED);
pauseRecord(context);
} else {
//state == STOPPED
if (AccessibilityWatchDogService.getInstance() == null) {
Toast.makeText(this, R.string.text_need_enable_accessibility_service, Toast.LENGTH_SHORT).show();
return;
}
AccessibilityRecorderDelegate.getInstance().startRecord();
ActionRecordSwitchView.getInstance().setState(RECORDING);
Toast.makeText(this, R.string.text_start_record, Toast.LENGTH_SHORT).show();
startRecord(context);
}
}
private void stopRecord() {
public static void startRecord(Context context) {
if (AccessibilityWatchDogService.getInstance() == null) {
Toast.makeText(context, R.string.text_need_enable_accessibility_service, Toast.LENGTH_SHORT).show();
return;
}
AccessibilityRecorderDelegate.getInstance().startRecord();
ActionRecordSwitchView.getInstance().setState(RECORDING);
Toast.makeText(context, R.string.text_start_record, Toast.LENGTH_SHORT).show();
}
private static void pauseRecord(Context context) {
AccessibilityRecorderDelegate.getInstance().pauseRecord();
ActionRecordSwitchView.getInstance().setState(PAUSED);
}
private static void resumeRecord(Context context) {
AccessibilityRecorderDelegate.getInstance().resumeRecord();
ActionRecordSwitchView.getInstance().setState(RECORDING);
}
public static void stopRecord(Context context) {
if (AccessibilityRecorderDelegate.getInstance().getState() != STOPPED) {
String script = AccessibilityRecorderDelegate.getInstance().stopRecord();
ActionRecordSwitchView.getInstance().setState(STOPPED);
MainActivity.onActionRecordStopped(this, script);
MainActivity.onActionRecordStopped(context, script);
} else {
Toast.makeText(App.getApp(), R.string.text_not_recording, Toast.LENGTH_SHORT).show();
}
}
private void redoRecord() {
private static void redoRecord(Context context) {
if (AccessibilityRecorderDelegate.getInstance().getState() != STOPPED) {
AccessibilityRecorderDelegate.getInstance().stopRecord();
ActionRecordSwitchView.getInstance().setState(STOPPED);

View File

@@ -7,6 +7,7 @@ import android.support.v4.app.NotificationCompat;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.service.VolumeChangeListenService;
/**
* Created by Stardust on 2017/2/14.
@@ -23,10 +24,11 @@ public class ActionRecordSwitchNotification {
builder = new NotificationCompat.Builder(App.getApp())
.setAutoCancel(false)
.setSmallIcon(R.drawable.ic_robot_head)
.setDeleteIntent(ActionRecordNotificationHandleService.getDeleteIntent())
.setDeleteIntent(ActionRecordSwitchHandleService.getDeleteIntent())
.setCustomContentView(ActionRecordSwitchView.getInstance());
}
showNotification(builder.build());
VolumeChangeListenService.startServiceIfNeeded(App.getApp());
}
private static void showNotification(Notification notification) {

View File

@@ -48,9 +48,9 @@ public class ActionRecordSwitchView extends RemoteViews {
}
private void setUpOnClick() {
setOnClickPendingIntent(R.id.stop, ActionRecordNotificationHandleService.getStopIntent());
setOnClickPendingIntent(R.id.start_or_pause, ActionRecordNotificationHandleService.getStartOrPauseIntent());
setOnClickPendingIntent(R.id.close, ActionRecordNotificationHandleService.getDeleteIntent());
setOnClickPendingIntent(R.id.redo, ActionRecordNotificationHandleService.getRedoIntent());
setOnClickPendingIntent(R.id.stop, ActionRecordSwitchHandleService.getStopIntent());
setOnClickPendingIntent(R.id.start_or_pause, ActionRecordSwitchHandleService.getStartOrPauseIntent());
setOnClickPendingIntent(R.id.close, ActionRecordSwitchHandleService.getDeleteIntent());
setOnClickPendingIntent(R.id.redo, ActionRecordSwitchHandleService.getRedoIntent());
}
}

View File

@@ -97,8 +97,11 @@ public class ShortcutActivity extends Activity {
}
private boolean hasStorageReadPermission() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(READ_EXTERNAL_STORAGE) == PERMISSION_GRANTED;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
return Build.VERSION.SDK_INT < Build.VERSION_CODES.M ||
checkSelfPermission(READ_EXTERNAL_STORAGE) == PERMISSION_GRANTED;
}
return true;
}
interface Tile {