Release 2.0.1Beta

This commit is contained in:
hyb1996
2017-03-19 16:21:07 +08:00
parent 85affed538
commit dd787236e7
110 changed files with 2428 additions and 4710 deletions

View File

@@ -33,9 +33,11 @@ public class FloatingWindowManger {
Intent intent;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
intent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION,
Uri.parse("package:" + packageName));
Uri.parse("package:" + packageName))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
} else {
intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + packageName));
intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS, Uri.parse("package:" + packageName))
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
}
App.getApp().startActivity(intent);
}

View File

@@ -37,11 +37,9 @@ import io.mattcarroll.hover.NavigatorContent;
public class HoverMenuAdapter implements io.mattcarroll.hover.HoverMenuAdapter {
public static final String ID_MAIN = "intro";
public static final String ID_MAIN = "main";
public static final String ID_RECORD = "record";
public static final String ID_SCRIPT_LIST = "script_list";
public static final String MENU_ID = "menu";
public static final String PLACEHOLDER_ID = "placeholder";
private final Context mContext;
private final List<String> mTabIds;

View File

@@ -7,6 +7,7 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.view.ContextThemeWrapper;
import android.view.View;
import android.view.WindowManager;
import android.widget.Toast;
@@ -50,8 +51,10 @@ public class HoverMenuService extends Service {
public static final String MESSAGE_SHOW_LAYOUT_BOUNDS = "MESSAGE_SHOW_LAYOUT_BOUNDS";
public static final String MESSAGE_COLLAPSE_MENU = "MESSAGE_COLLAPSE_MENU";
public static final String MESSAGE_MENU_COLLAPSING = "MESSAGE_MENU_COLLAPSING";
public static final String MESSAGE_MENU_EXPANDING = "MESSAGE_MENU_EXPANDING";
public static final String MESSAGE_MENU_EXIT = "MESSAGE_MENU_EXIT";
private static WeakReference<HoverMenuService> service = new WeakReference<>(null);
private static HoverMenuService service = null;
private static boolean sIsRunning;
public static void startService(Context context) {
@@ -64,8 +67,8 @@ public class HoverMenuService extends Service {
}
public static void stopService() {
if (isServiceRunning() && service.get() != null) {
service.get().stopSelf();
if (isServiceRunning() && service != null) {
service.stopSelf();
setIsRunning(false);
}
}
@@ -92,6 +95,7 @@ public class HoverMenuService extends Service {
private HoverMenu.OnExitListener mWindowHoverMenuMenuExitListener = new HoverMenu.OnExitListener() {
@Override
public void onExitByUserRequest() {
EventBus.getDefault().post(new MessageEvent(MESSAGE_MENU_EXIT));
savePreferredLocation();
mWindowHoverMenu.hide();
stopSelf();
@@ -101,11 +105,11 @@ public class HoverMenuService extends Service {
@Override
public void onCreate() {
super.onCreate();
if (service.get() != null) {
if (service != null) {
stopSelf();
return;
}
service = new WeakReference<>(this);
service = this;
EventBus.getDefault().register(this);
mPrefs = getSharedPreferences(PREF_FILE, Context.MODE_PRIVATE);
try {
@@ -127,7 +131,7 @@ public class HoverMenuService extends Service {
}
private void initWindowMenu() {
mWindowHoverMenu = (WindowHoverMenu) new HoverMenuBuilder(this)
mWindowHoverMenu = (WindowHoverMenu) new HoverMenuBuilder(new ContextThemeWrapper(this, R.style.AppTheme))
.displayWithinWindow()
.useAdapter(new HoverMenuAdapter(this))
.restoreVisualState(loadPreferredLocation())
@@ -138,6 +142,7 @@ public class HoverMenuService extends Service {
mWindowHoverMenu.setHoverMenuTransitionListener(new SimpleHoverMenuTransitionListener() {
@Override
public void onExpanding() {
EventBus.getDefault().post(new MessageEvent(MESSAGE_MENU_EXPANDING));
captureCurrentWindow();
}
@@ -166,17 +171,20 @@ public class HoverMenuService extends Service {
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
mWindowHoverMenu.show();
if (mWindowHoverMenu != null)
mWindowHoverMenu.show();
return START_STICKY;
}
@Override
public void onDestroy() {
mWindowHoverMenu.hide();
EventBus.getDefault().unregister(this);
if (mWindowHoverMenu != null)
mWindowHoverMenu.hide();
if (EventBus.getDefault().isRegistered(this))
EventBus.getDefault().unregister(this);
setIsRunning(false);
if (service.get() == this)
service.clear();
if (service == this)
service = null;
}

View File

@@ -1,21 +1,28 @@
package com.stardust.scriptdroid.external.floating_window.content;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import android.support.design.widget.Snackbar;
import android.view.View;
import android.widget.TextView;
import android.widget.Toast;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.accessibility.AccessibilityInfoProvider;
import com.stardust.scriptdroid.droid.Droid;
import com.stardust.scriptdroid.external.floating_window.HoverMenuService;
import com.stardust.scriptdroid.layout_inspector.LayoutInspector;
import com.stardust.scriptdroid.tool.ClipboardTool;
import com.stardust.scriptdroid.ui.main.MainActivity;
import com.stardust.util.MessageEvent;
import com.stardust.view.ViewBinder;
import com.stardust.view.ViewBinding;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import java.util.ArrayList;
import java.util.Arrays;
@@ -36,11 +43,19 @@ public class MainMenuNavigatorContent implements NavigatorContent {
private View mView;
@ViewBinding.Id(R.id.current_package)
private TextView mCurrentPackageTextView;
@ViewBinding.Id(R.id.current_activity)
private TextView mCurrentActivityTextView;
private String mCurrentPackage, mCurrentActivity;
private Context mContext;
public MainMenuNavigatorContent(Context context) {
mContext = context;
mView = View.inflate(context, R.layout.floating_window_main_menu, null);
ViewBinder.bind(this);
EventBus.getDefault().register(this);
}
@ViewBinding.Click(R.id.layout_hierarchy)
@@ -70,6 +85,13 @@ public class MainMenuNavigatorContent implements NavigatorContent {
Toast.makeText(mView.getContext(), R.string.text_no_running_script, Toast.LENGTH_SHORT).show();
}
@ViewBinding.Click(R.id.open_launcher)
private void openMainActivity() {
App.getApp().startActivity(new Intent(App.getApp(), MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_LAUNCHED_FROM_HISTORY | Intent.FLAG_ACTIVITY_NEW_TASK));
EventBus.getDefault().post(new MessageEvent(HoverMenuService.MESSAGE_COLLAPSE_MENU));
}
@NonNull
@Override
public View getView() {
@@ -81,11 +103,40 @@ public class MainMenuNavigatorContent implements NavigatorContent {
}
@SuppressLint("SetTextI18n")
private void syncCurrentInfo() {
mCurrentPackage = AccessibilityInfoProvider.getInstance().getLatestPackage();
mCurrentActivity = AccessibilityInfoProvider.getInstance().getLatestActivity();
mCurrentActivityTextView.setText(mContext.getString(R.string.text_current_activity) + mCurrentActivity);
mCurrentPackageTextView.setText(mContext.getString(R.string.text_current_package) + mCurrentPackage);
}
@ViewBinding.Click(R.id.current_activity)
private void copyCurrentActivity() {
ClipboardTool.setClip(mCurrentActivity);
Toast.makeText(mContext, R.string.text_copied, Toast.LENGTH_SHORT).show();
}
@ViewBinding.Click(R.id.current_package)
private void copyCurrentPackage() {
ClipboardTool.setClip(mCurrentPackage);
Toast.makeText(mContext, R.string.text_copied, Toast.LENGTH_SHORT).show();
}
@Override
public void onHidden() {
}
@Subscribe
public void onMessageEvent(MessageEvent event) {
if (event.message.equals(HoverMenuService.MESSAGE_MENU_EXPANDING)) {
syncCurrentInfo();
} else if (event.message.equals(HoverMenuService.MESSAGE_MENU_EXIT)) {
EventBus.getDefault().unregister(this);
}
}
public View findViewById(int id) {
return mView.findViewById(id);
}

View File

@@ -2,24 +2,61 @@ package com.stardust.scriptdroid.external.floating_window.content;
import android.content.Context;
import android.support.annotation.NonNull;
import android.support.v7.widget.SwitchCompat;
import android.view.View;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.accessibility.AccessibilityEventHelper;
import com.stardust.scriptdroid.external.floating_window.HoverMenuService;
import com.stardust.scriptdroid.record.Recorder;
import com.stardust.scriptdroid.record.accessibility.AccessibilityActionRecorder;
import com.stardust.scriptdroid.record.inputevent.InputEventConverter;
import com.stardust.scriptdroid.record.inputevent.TouchRecorder;
import com.stardust.scriptdroid.ui.main.MainActivity;
import com.stardust.util.MessageEvent;
import com.stardust.view.ViewBinder;
import com.stardust.view.ViewBinding;
import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import io.mattcarroll.hover.Navigator;
import io.mattcarroll.hover.NavigatorContent;
/**
* Created by Stardust on 2017/3/12.
*/
public class RecordNavigatorContent implements NavigatorContent {
public class RecordNavigatorContent implements NavigatorContent, Recorder.OnStateChangedListener {
private View mView;
@ViewBinding.Id(R.id.sw_recorded_by_root)
private SwitchCompat mRecordedByRootSwitch;
@ViewBinding.Id(R.id.sw_record_toast)
private SwitchCompat mRecordToastSwitch;
@ViewBinding.Id(R.id.img_start_or_pause)
private ImageView mStartOrPauseRecordIcon;
@ViewBinding.Id(R.id.text_start_or_pause)
private TextView mStartOrPauseRecordText;
@ViewBinding.Id(R.id.stop_record)
private View mStopRecord;
private Recorder mRecorder;
private Context mContext;
public RecordNavigatorContent(Context context) {
mView = new View(context);
mContext = context;
mView = View.inflate(context, R.layout.floating_window_record, null);
ViewBinder.bind(this);
}
@NonNull
@@ -30,11 +67,118 @@ public class RecordNavigatorContent implements NavigatorContent {
@Override
public void onShown(@NonNull Navigator navigator) {
if (!EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().register(this);
}
}
@Override
public void onHidden() {
if (EventBus.getDefault().isRegistered(this)) {
EventBus.getDefault().unregister(this);
}
}
@ViewBinding.Click(R.id.sw_root_container)
private void toggleRecordedByRootSwitch() {
mRecordedByRootSwitch.toggle();
}
@ViewBinding.Click(R.id.sw_record_toast_container)
private void toggleRecordToastSwitch() {
mRecordToastSwitch.toggle();
}
@ViewBinding.Click(R.id.start_or_pause)
private void startOrPauseRecord() {
if (mRecorder == null) {
startRecord();
} else if (mRecorder.getState() == Recorder.STATE_PAUSED) {
resumeRecord();
} else {
pauseRecord();
}
}
private void resumeRecord() {
mRecorder.resume();
setState(Recorder.STATE_RECORDING);
EventBus.getDefault().post(new MessageEvent(HoverMenuService.MESSAGE_COLLAPSE_MENU));
}
private void pauseRecord() {
mRecorder.pause();
setState(Recorder.STATE_PAUSED);
}
private void startRecord() {
mRecorder = mRecordedByRootSwitch.isChecked() ? new TouchRecorder() : AccessibilityActionRecorder.getInstance();
AccessibilityActionRecorder.getInstance().setRecordMessageEnabled(mRecordToastSwitch.isChecked());
mRecorder.setOnStateChangedListener(this);
mRecorder.start();
setState(Recorder.STATE_RECORDING);
EventBus.getDefault().post(new MessageEvent(HoverMenuService.MESSAGE_COLLAPSE_MENU));
}
private void setState(int state) {
mStopRecord.setVisibility(state == Recorder.STATE_STOPPED ? View.GONE : View.VISIBLE);
mStartOrPauseRecordIcon.setImageResource(state == Recorder.STATE_RECORDING ? R.drawable.ic_pause_white_24dp : R.drawable.ic_play_arrow_white_48dp);
//我知道这样写代码会被打 但我懒...
mStartOrPauseRecordText.setText(state == Recorder.STATE_RECORDING ? R.string.text_pause_record :
state == Recorder.STATE_PAUSED ? R.string.text_resume_record : R.string.text_start_record);
}
@ViewBinding.Click(R.id.stop_record)
private void stopRecord() {
mRecorder.stop();
setState(Recorder.STATE_STOPPED);
EventBus.getDefault().post(new MessageEvent(HoverMenuService.MESSAGE_COLLAPSE_MENU));
}
public View findViewById(int id) {
return mView.findViewById(id);
}
@Subscribe
public void onMessageEvent(MessageEvent event) {
if (event.message.equals(HoverMenuService.MESSAGE_MENU_EXPANDING)) {
pauseRecord();
}
}
@Subscribe
public void onAccessibilityActionRecordEvent(AccessibilityActionRecorder.AccessibilityActionRecordEvent event) {
if (mRecordToastSwitch.isChecked()) {
Toast.makeText(mContext, AccessibilityEventHelper.getEventTypeNameResId(event.getAccessibilityEvent()), Toast.LENGTH_SHORT).show();
}
}
@Subscribe
public void onTouchRecorderStateChanged(InputEventConverter.RecordStateChangeEvent event) {
if (!event.isRecording()) {
stopRecord();
}
}
@Override
public void onStart() {
Toast.makeText(mContext, R.string.text_start_record, Toast.LENGTH_SHORT).show();
}
@Override
public void onStop() {
MainActivity.onActionRecordStopped(mContext, mRecorder.getCode());
mRecorder = null;
}
@Override
public void onPause() {
}
@Override
public void onResume() {
}
}

View File

@@ -6,6 +6,7 @@ import android.view.ContextThemeWrapper;
import android.view.View;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.droid.script.file.ScriptFileList;
import com.stardust.scriptdroid.droid.script.file.SharedPrefScriptFileList;
import com.stardust.scriptdroid.external.floating_window.view.FloatingScriptFileListView;
@@ -22,7 +23,7 @@ public class ScritpListNavigatorContent implements NavigatorContent {
public ScritpListNavigatorContent(Context context) {
mFloatingScriptFileListView = new FloatingScriptFileListView(new ContextThemeWrapper(context, R.style.AppTheme));
mFloatingScriptFileListView.setScriptFileList(SharedPrefScriptFileList.getInstance());
mFloatingScriptFileListView.setScriptFileList(ScriptFileList.getImpl());
}
@NonNull

View File

@@ -0,0 +1,171 @@
package com.stardust.scriptdroid.external.notification.record;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.v4.app.NotificationCompat;
import android.widget.Toast;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.record.Recorder;
import com.stardust.scriptdroid.record.accessibility.AccessibilityActionRecorder;
import com.stardust.scriptdroid.service.AccessibilityWatchDogService;
import com.stardust.scriptdroid.service.VolumeChangeObverseService;
import com.stardust.scriptdroid.ui.main.MainActivity;
/**
* Created by Stardust on 2017/2/14.
*/
public class AccessibilityActionRecordNotification {
private static final String INTENT_ACTION = " com.stardust.scriptdroid.Record";
private static final String EXTRA_ACTION = "action";
private static final int ACTION_STOP = 17771;
private static final int ACTION_START_OR_PAUSE = 17772;
private static final int ACTION_DELETE = 17773;
private static final int ACTION_REDO = 17774;
private static final int NOTIFY_ID = 22236;
private static NotificationCompat.Builder builder;
private static BroadcastReceiver broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
int action = intent.getIntExtra(EXTRA_ACTION, 0);
switch (action) {
case ACTION_STOP:
stopRecord(context);
collapseNotificationBar(context);
break;
case ACTION_START_OR_PAUSE:
startOrPauseRecord(context);
collapseNotificationBar(context);
break;
case ACTION_DELETE:
AccessibilityActionRecordNotification.cancelNotification();
stopRecordIfNeeded(context);
VolumeChangeObverseService.stopServiceIfNeeded();
break;
case ACTION_REDO:
redoRecord(context);
break;
}
}
};
static PendingIntent getStartOrPauseIntent() {
Intent intent = new Intent(INTENT_ACTION)
.putExtra(EXTRA_ACTION, ACTION_START_OR_PAUSE);
return PendingIntent.getBroadcast(App.getApp(), ACTION_START_OR_PAUSE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
static PendingIntent getStopIntent() {
Intent intent = new Intent(INTENT_ACTION)
.putExtra(EXTRA_ACTION, ACTION_STOP);
return PendingIntent.getBroadcast(App.getApp(), ACTION_STOP, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
static PendingIntent getDeleteIntent() {
Intent intent = new Intent(INTENT_ACTION)
.putExtra(EXTRA_ACTION, ACTION_DELETE);
return PendingIntent.getBroadcast(App.getApp(), ACTION_DELETE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
static PendingIntent getRedoIntent() {
Intent intent = new Intent(INTENT_ACTION)
.putExtra(EXTRA_ACTION, ACTION_REDO);
return PendingIntent.getService(App.getApp(), ACTION_REDO, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static void showOrUpdateNotification() {
if (builder == null) {
builder = new NotificationCompat.Builder(App.getApp())
.setAutoCancel(false)
.setSmallIcon(R.drawable.ic_robot_head)
.setDeleteIntent(getDeleteIntent())
.setCustomContentView(AccessibilityActionRecordSwitchView.getInstance());
}
showNotification(builder.build());
VolumeChangeObverseService.startServiceIfNeeded(App.getApp());
}
private static void showNotification(Notification notification) {
NotificationManager notificationManager = (NotificationManager) App.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFY_ID, notification);
App.getApp().registerReceiver(broadcastReceiver, new IntentFilter(INTENT_ACTION));
}
public static void cancelNotification() {
NotificationManager notificationManager = (NotificationManager) App.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFY_ID);
App.getApp().unregisterReceiver(broadcastReceiver);
}
private static void collapseNotificationBar(Context context) {
context.sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
private static void stopRecordIfNeeded(Context context) {
int state = AccessibilityActionRecorder.getInstance().getState();
if (state == Recorder.STATE_RECORDING || state == Recorder.STATE_PAUSED) {
stopRecord(context);
}
}
private static void startOrPauseRecord(Context context) {
int state = AccessibilityActionRecorder.getInstance().getState();
if (state == Recorder.STATE_PAUSED) {
resumeRecord(context);
} else if (state == Recorder.STATE_RECORDING) {
pauseRecord(context);
} else {
//state == STOPPED
startRecord(context);
}
}
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;
}
AccessibilityActionRecorder.getInstance().start();
AccessibilityActionRecordSwitchView.getInstance().setState(Recorder.STATE_RECORDING);
Toast.makeText(context, R.string.text_start_record, Toast.LENGTH_SHORT).show();
}
private static void pauseRecord(Context context) {
AccessibilityActionRecorder.getInstance().pause();
AccessibilityActionRecordSwitchView.getInstance().setState(Recorder.STATE_PAUSED);
}
private static void resumeRecord(Context context) {
AccessibilityActionRecorder.getInstance().resume();
AccessibilityActionRecordSwitchView.getInstance().setState(Recorder.STATE_RECORDING);
}
public static void stopRecord(Context context) {
if (AccessibilityActionRecorder.getInstance().getState() != Recorder.STATE_STOPPED) {
AccessibilityActionRecorder.getInstance().stop();
String script = AccessibilityActionRecorder.getInstance().getCode();
AccessibilityActionRecordSwitchView.getInstance().setState(Recorder.STATE_STOPPED);
MainActivity.onActionRecordStopped(context, script);
} else {
Toast.makeText(App.getApp(), R.string.text_not_recording, Toast.LENGTH_SHORT).show();
}
}
private static void redoRecord(Context context) {
if (AccessibilityActionRecorder.getInstance().getState() != Recorder.STATE_STOPPED) {
AccessibilityActionRecorder.getInstance().stop();
AccessibilityActionRecordSwitchView.getInstance().setState(Recorder.STATE_STOPPED);
}
}
}

View File

@@ -4,53 +4,50 @@ import android.widget.RemoteViews;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.record.Recorder;
/**
* Created by Stardust on 2017/2/14.
*/
public class ActionRecordSwitchView extends RemoteViews {
public class AccessibilityActionRecordSwitchView extends RemoteViews {
private static ActionRecordSwitchView instance;
private static AccessibilityActionRecordSwitchView instance;
public static final int STOPPED = 0;
public static final int PAUSED = 1;
public static final int RECORDING = 2;
public static ActionRecordSwitchView getInstance() {
public static AccessibilityActionRecordSwitchView getInstance() {
if (instance == null) {
instance = new ActionRecordSwitchView();
instance = new AccessibilityActionRecordSwitchView();
}
return instance;
}
private ActionRecordSwitchView() {
private AccessibilityActionRecordSwitchView() {
super(App.getApp().getPackageName(), R.layout.remote_views_record_switch);
setUpOnClick();
}
public void setState(int state) {
switch (state) {
case STOPPED:
case Recorder.STATE_STOPPED:
setImageViewResource(R.id.img_start_or_pause, R.drawable.ic_play_arrow_grey600_48dp);
setTextViewText(R.id.text_start_or_pause, App.getApp().getString(R.string.text_start_record));
break;
case RECORDING:
case Recorder.STATE_RECORDING:
setImageViewResource(R.id.img_start_or_pause, R.drawable.ic_pause_grey600_48dp);
setTextViewText(R.id.text_start_or_pause, App.getApp().getString(R.string.text_pause_record));
break;
case PAUSED:
case Recorder.STATE_PAUSED:
setImageViewResource(R.id.img_start_or_pause, R.drawable.ic_play_arrow_grey600_48dp);
setTextViewText(R.id.text_start_or_pause, App.getApp().getString(R.string.text_resume_record));
break;
}
ActionRecordSwitchNotification.showOrUpdateNotification();
AccessibilityActionRecordNotification.showOrUpdateNotification();
}
private void setUpOnClick() {
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());
setOnClickPendingIntent(R.id.stop, AccessibilityActionRecordNotification.getStopIntent());
setOnClickPendingIntent(R.id.start_or_pause, AccessibilityActionRecordNotification.getStartOrPauseIntent());
setOnClickPendingIntent(R.id.close, AccessibilityActionRecordNotification.getDeleteIntent());
setOnClickPendingIntent(R.id.redo, AccessibilityActionRecordNotification.getRedoIntent());
}
}

View File

@@ -1,164 +0,0 @@
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;
import android.widget.Toast;
import com.stardust.scriptdroid.record.AccessibilityRecorderDelegate;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.R;
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;
import static com.stardust.scriptdroid.external.notification.record.ActionRecordSwitchView.RECORDING;
import static com.stardust.scriptdroid.external.notification.record.ActionRecordSwitchView.STOPPED;
/**
* Created by Stardust on 2017/2/15.
*/
public class ActionRecordSwitchHandleService extends Service {
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;
private static final int ACTION_DELETE = 17773;
private static final int ACTION_REDO = 17774;
public static PendingIntent getStartOrPauseIntent() {
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(), 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(), ActionRecordSwitchHandleService.class)
.putExtra(EXTRA_INTENT_VALID, true)
.putExtra(EXTRA_ACTION, ACTION_DELETE);
return PendingIntent.getService(App.getApp(), ACTION_DELETE, intent, PendingIntent.FLAG_UPDATE_CURRENT);
}
public static PendingIntent getRedoIntent() {
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);
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
boolean intentValid = intent.getBooleanExtra(EXTRA_INTENT_VALID, false);
if (intentValid) {
performAction(intent.getIntExtra(EXTRA_ACTION, 0));
}
stopSelf();
return super.onStartCommand(intent, flags, startId);
}
private void performAction(int action) {
switch (action) {
case ACTION_STOP:
stopRecord(this);
collapseNotificationBar();
break;
case ACTION_START_OR_PAUSE:
startOrPauseRecord(this);
collapseNotificationBar();
break;
case ACTION_DELETE:
ActionRecordSwitchNotification.cancelNotification();
stopRecordIfNeeded();
VolumeChangeListenService.stopServiceIfNeeded();
break;
case ACTION_REDO:
redoRecord(this);
break;
}
}
private void collapseNotificationBar() {
sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
private void stopRecordIfNeeded() {
if (AccessibilityRecorderDelegate.getInstance().getState() != STOPPED) {
stopRecord(this);
}
}
private static void startOrPauseRecord(Context context) {
int state = AccessibilityRecorderDelegate.getInstance().getState();
if (state == PAUSED) {
resumeRecord(context);
} else if (state == RECORDING) {
pauseRecord(context);
} else {
//state == STOPPED
startRecord(context);
}
}
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(context, script);
} else {
Toast.makeText(App.getApp(), R.string.text_not_recording, Toast.LENGTH_SHORT).show();
}
}
private static void redoRecord(Context context) {
if (AccessibilityRecorderDelegate.getInstance().getState() != STOPPED) {
AccessibilityRecorderDelegate.getInstance().stopRecord();
ActionRecordSwitchView.getInstance().setState(STOPPED);
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

View File

@@ -1,43 +0,0 @@
package com.stardust.scriptdroid.external.notification.record;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
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.
*/
public class ActionRecordSwitchNotification {
private static final int NOTIFY_ID = 22236;
private static NotificationCompat.Builder builder;
public static void showOrUpdateNotification() {
if (builder == null) {
builder = new NotificationCompat.Builder(App.getApp())
.setAutoCancel(false)
.setSmallIcon(R.drawable.ic_robot_head)
.setDeleteIntent(ActionRecordSwitchHandleService.getDeleteIntent())
.setCustomContentView(ActionRecordSwitchView.getInstance());
}
showNotification(builder.build());
VolumeChangeListenService.startServiceIfNeeded(App.getApp());
}
private static void showNotification(Notification notification) {
NotificationManager notificationManager = (NotificationManager) App.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFY_ID, notification);
}
public static void cancelNotification() {
NotificationManager notificationManager = (NotificationManager) App.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFY_ID);
}
}

View File

@@ -9,7 +9,9 @@ import android.widget.Toast;
import com.afollestad.materialdialogs.MaterialDialog;
import com.stardust.scriptdroid.droid.script.file.ScriptFile;
import com.stardust.scriptdroid.droid.script.file.ScriptFileList;
import com.stardust.scriptdroid.droid.script.file.SharedPrefScriptFileList;
import com.stardust.scriptdroid.tool.FileUtils;
import com.stardust.scriptdroid.ui.BaseActivity;
import com.stardust.scriptdroid.ui.main.MainActivity;
import com.stardust.theme.dialog.ThemeColorMaterialDialogBuilder;
@@ -31,8 +33,8 @@ public class ImportIntentActivity extends BaseActivity {
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, R.string.edit_and_run_handle_intent_error, Toast.LENGTH_LONG).show();
finish();
}
finish();
}
private void handleIntent() {
@@ -41,19 +43,22 @@ public class ImportIntentActivity extends BaseActivity {
if (!TextUtils.isEmpty(path)) {
new ThemeColorMaterialDialogBuilder(this)
.title(R.string.text_please_input_name)
.input(getString(R.string.text_name), new File(path).getName(), new MaterialDialog.InputCallback() {
.input(getString(R.string.text_name), FileUtils.getNameWithoutExtension(path), new MaterialDialog.InputCallback() {
@Override
public void onInput(@NonNull MaterialDialog dialog, CharSequence input) {
SharedPrefScriptFileList.getInstance().add(new ScriptFile(input.toString(), path));
ScriptFileList.getImpl().add(new ScriptFile(input.toString(), path));
startMainActivity();
}
})
.show();
} else {
finish();
}
}
private void startMainActivity() {
startActivity(new Intent(this, MainActivity.class)
.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP | Intent.FLAG_ACTIVITY_NEW_TASK));
finish();
}
}