Added getTexts and action recorder, fixed github issue report token

This commit is contained in:
hyb1996
2017-02-15 18:10:17 +08:00
parent 04b4303899
commit e2d5efa710
82 changed files with 1392 additions and 518 deletions

View File

@@ -0,0 +1,97 @@
package com.stardust.scriptdroid.external.notification.bounds_assist;
import android.app.Notification;
import android.app.NotificationManager;
import android.content.Context;
import android.preference.PreferenceManager;
import android.support.v4.app.NotificationCompat;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.bounds_assist.BoundsAssistant;
import com.stardust.util.StateObserver;
/**
* Created by Stardust on 2017/2/2.
*/
public class BoundsAssistSwitchNotification {
private static final int NOTIFY_ID = 11126;
public static final String KEY_BOUNDS_ASSIST_SWITCH_NOTIFICATION_ENABLE = "KEY_BOUNDS_ASSIST_SWITCH_NOTIFICATION_ENABLE";
private static boolean enable = false;
public static boolean isEnable() {
return enable;
}
static {
App.getStateObserver().register(KEY_BOUNDS_ASSIST_SWITCH_NOTIFICATION_ENABLE, new StateObserver.OnStateChangedListener() {
@Override
public void onStateChanged(boolean newState) {
setEnable(newState);
}
@Override
public void initState(boolean state) {
enable = state;
if (enable) {
showNotification();
}
}
});
App.getStateObserver().register(BoundsAssistant.KEY_BOUNDS_ASSIST_ENABLE, new StateObserver.OnStateChangedListener() {
@Override
public void onStateChanged(boolean newState) {
if (enable) {
setEnable(false);
setEnable(true);
}
}
@Override
public void initState(boolean state) {
}
});
}
public static void setEnable(boolean enable) {
if (BoundsAssistSwitchNotification.enable == enable)
return;
BoundsAssistSwitchNotification.enable = enable;
PreferenceManager.getDefaultSharedPreferences(App.getApp()).edit().putBoolean(KEY_BOUNDS_ASSIST_SWITCH_NOTIFICATION_ENABLE, enable).apply();
if (enable) {
showNotification();
} else {
hideNotification();
}
}
private static void showNotification() {
Notification notification = new NotificationCompat.Builder(App.getApp())
.setAutoCancel(false)
.setSmallIcon(R.drawable.ic_robot_head)
.setDeleteIntent(BoundsAssistSwitchNotificationHandleService.getDeletePendingIntent())
.setContentText(BoundsAssistant.isAssistModeEnable() ?
App.getApp().getString(R.string.text_assist_mode_enabled) :
App.getApp().getString(R.string.text_assist_mode_disabled))
.setContentIntent(BoundsAssistSwitchNotificationHandleService.getStartIntent())
.build();
showNotification(notification);
}
private static void showNotification(Notification notification) {
NotificationManager notificationManager = (NotificationManager) App.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFY_ID, notification);
}
private static void hideNotification() {
NotificationManager notificationManager = (NotificationManager) App.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.cancel(NOTIFY_ID);
}
}

View File

@@ -0,0 +1,67 @@
package com.stardust.scriptdroid.external.notification.bounds_assist;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.bounds_assist.BoundsAssistant;
import static com.stardust.scriptdroid.external.notification.bounds_assist.BoundsAssistSwitchNotification.KEY_BOUNDS_ASSIST_SWITCH_NOTIFICATION_ENABLE;
/**
* Created by Stardust on 2017/2/2.
*/
public class BoundsAssistSwitchNotificationHandleService extends Service {
private static final String EXTRA_INTENT_VALID = "BoundsAssistSwitchNotificationHandleService.intentValid";
private static final String EXTRA_ACTION = "action";
private static final int ACTION_TOGGLE_ASSIST_MODE = 1;
private static final int ACTION_CANCEL_ASSIST_MODE_NOTIFICATION = 2;
public static PendingIntent getStartIntent() {
Intent intent = new Intent(App.getApp(), BoundsAssistSwitchNotificationHandleService.class)
.putExtra(EXTRA_INTENT_VALID, true)
.putExtra(EXTRA_ACTION, ACTION_TOGGLE_ASSIST_MODE);
return PendingIntent.getService(App.getApp(), 0, 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_TOGGLE_ASSIST_MODE:
BoundsAssistant.setAssistModeEnable(!BoundsAssistant.isAssistModeEnable());
break;
case ACTION_CANCEL_ASSIST_MODE_NOTIFICATION:
App.getStateObserver().setState(KEY_BOUNDS_ASSIST_SWITCH_NOTIFICATION_ENABLE, false);
break;
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
public static PendingIntent getDeletePendingIntent() {
Intent deleteIntent = new Intent(App.getApp(), BoundsAssistSwitchNotificationHandleService.class)
.putExtra(EXTRA_INTENT_VALID, true)
.putExtra(EXTRA_ACTION, ACTION_CANCEL_ASSIST_MODE_NOTIFICATION);
return PendingIntent.getService(App.getApp(), 0, deleteIntent, PendingIntent.FLAG_ONE_SHOT);
}
}

View File

@@ -0,0 +1,123 @@
package com.stardust.scriptdroid.external.notification.record;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.widget.Toast;
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.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 ActionRecordNotificationHandleService extends Service {
private static final String EXTRA_INTENT_VALID = "ActionRecordNotificationHandleService.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;
public static PendingIntent getStartOrPauseIntent() {
Intent intent = new Intent(App.getApp(), ActionRecordNotificationHandleService.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)
.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)
.putExtra(EXTRA_INTENT_VALID, true)
.putExtra(EXTRA_ACTION, ACTION_DELETE);
return PendingIntent.getService(App.getApp(), ACTION_DELETE, 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();
break;
case ACTION_START_OR_PAUSE:
startOrPauseRecord();
break;
case ACTION_DELETE:
stopRecordIfNeeded();
}
collapseNotificationBar();
}
private void collapseNotificationBar() {
sendBroadcast(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS));
}
private void stopRecordIfNeeded() {
if (AccessibilityRecorderDelegate.getInstance().getState() != STOPPED) {
stopRecord();
}
}
private void startOrPauseRecord() {
int state = AccessibilityRecorderDelegate.getInstance().getState();
if (state == PAUSED) {
AccessibilityRecorderDelegate.getInstance().resumeRecord();
ActionRecordSwitchView.getInstance().setState(RECORDING);
} else if (state == RECORDING) {
AccessibilityRecorderDelegate.getInstance().pauseRecord();
ActionRecordSwitchView.getInstance().setState(PAUSED);
} 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();
}
}
private void stopRecord() {
if (AccessibilityRecorderDelegate.getInstance().getState() != STOPPED) {
String script = AccessibilityRecorderDelegate.getInstance().stopRecord();
ActionRecordSwitchView.getInstance().setState(STOPPED);
MainActivity.onActionRecordStopped(this, script);
} else {
Toast.makeText(App.getApp(), R.string.text_not_recording, Toast.LENGTH_SHORT).show();
}
}
@Nullable
@Override
public IBinder onBind(Intent intent) {
return null;
}
}

View File

@@ -0,0 +1,36 @@
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;
/**
* 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(ActionRecordNotificationHandleService.getDeleteIntent())
.setCustomContentView(ActionRecordSwitchView.getInstance());
}
showNotification(builder.build());
}
private static void showNotification(Notification notification) {
NotificationManager notificationManager = (NotificationManager) App.getApp().getSystemService(Context.NOTIFICATION_SERVICE);
notificationManager.notify(NOTIFY_ID, notification);
}
}

View File

@@ -0,0 +1,54 @@
package com.stardust.scriptdroid.external.notification.record;
import android.widget.RemoteViews;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.R;
/**
* Created by Stardust on 2017/2/14.
*/
public class ActionRecordSwitchView extends RemoteViews {
private static ActionRecordSwitchView instance;
public static final int STOPPED = 0;
public static final int PAUSED = 1;
public static final int RECORDING = 2;
public static ActionRecordSwitchView getInstance() {
if (instance == null) {
instance = new ActionRecordSwitchView();
}
return instance;
}
private ActionRecordSwitchView() {
super(App.getApp().getPackageName(), R.layout.remote_views_record_switch);
setUpOnClick();
}
public void setState(int state) {
switch (state) {
case 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:
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:
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();
}
private void setUpOnClick() {
setOnClickPendingIntent(R.id.stop, ActionRecordNotificationHandleService.getStopIntent());
setOnClickPendingIntent(R.id.start_or_pause, ActionRecordNotificationHandleService.getStartOrPauseIntent());
}
}

View File

@@ -0,0 +1,114 @@
package com.stardust.scriptdroid.external.shortcut;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
/**
* Created by Stardust on 2017/1/20.
*/
public class Shortcut {
private Context mContext;
private String mName;
private String mTargetClass;
private String mTargetPackage;
private Intent.ShortcutIconResource mIcon;
private boolean mDuplicate = false;
private Intent mLaunchIntent = new Intent();
public Shortcut(Context context) {
mContext = context;
mTargetPackage = mContext.getPackageName();
}
public Shortcut(String name, Context context) {
this(context);
mName = name;
}
public Shortcut name(String name) {
mName = name;
return this;
}
public Shortcut targetPackage(String targetPackage) {
mTargetPackage = targetPackage;
return this;
}
public Shortcut targetClass(String targetClass) {
mTargetClass = targetClass;
return this;
}
public Shortcut targetClass(Class<?> targetClass) {
mTargetClass = targetClass.getName();
return this;
}
public Shortcut icon(Intent.ShortcutIconResource icon) {
mIcon = icon;
return this;
}
public Shortcut icon(int resId) {
mIcon = Intent.ShortcutIconResource.fromContext(mContext, resId);
return this;
}
public Shortcut duplicate(boolean duplicate) {
mDuplicate = duplicate;
return this;
}
public String getName() {
return mName;
}
public Intent.ShortcutIconResource getIcon() {
return mIcon;
}
public boolean isDuplicate() {
return mDuplicate;
}
private String getClassName() {
return mTargetClass;
}
private String getPackageName() {
return mTargetPackage;
}
public Intent getCreateIntent() {
return new Intent(Intent.ACTION_CREATE_SHORTCUT)
.putExtra(Intent.EXTRA_SHORTCUT_NAME, getName())
.putExtra(Intent.EXTRA_SHORTCUT_ICON_RESOURCE, getIcon())
.putExtra(Intent.EXTRA_SHORTCUT_INTENT, getLaunchIntent())
.putExtra("duplicate", isDuplicate())
.setAction("com.android.launcher.action.INSTALL_SHORTCUT");
}
public Intent getLaunchIntent() {
mLaunchIntent.setClassName(getPackageName(), getClassName());
return mLaunchIntent;
}
public void send() {
mContext.sendBroadcast(getCreateIntent());
}
public Shortcut extras(Bundle bundle) {
mLaunchIntent.putExtras(bundle);
return this;
}
public Shortcut extras(Intent intent) {
mLaunchIntent.putExtras(intent);
return this;
}
}

View File

@@ -0,0 +1,125 @@
package com.stardust.scriptdroid.external.shortcut;
import android.app.Activity;
import android.os.Build;
import android.os.Bundle;
import android.text.TextUtils;
import android.widget.Toast;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.droid.Droid;
import java.io.File;
import java.util.LinkedList;
import java.util.List;
import static android.Manifest.permission.READ_EXTERNAL_STORAGE;
import static android.content.pm.PackageManager.PERMISSION_GRANTED;
/**
* Created by Stardust on 2017/1/23.
*/
public class ShortcutActivity extends Activity {
interface BooleanSupplier {
boolean getAsBoolean();
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
final String path = getIntent().getStringExtra("path");
if (!ensure(new BooleanSupplier() {
@Override
public boolean getAsBoolean() {
return !TextUtils.isEmpty(path);
}
}, R.string.text_path_is_empty))
return;
final File scriptFile = new File(path);
new Domino()
.then(new Tile() {
@Override
public boolean fall() {
return ShortcutActivity.this.ensure(new BooleanSupplier() {
@Override
public boolean getAsBoolean() {
return scriptFile.exists();
}
}, R.string.text_file_not_exists);
}
})
.then(new Tile() {
@Override
public boolean fall() {
return ShortcutActivity.this.ensure(new BooleanSupplier() {
@Override
public boolean getAsBoolean() {
return ShortcutActivity.this.hasStorageReadPermission();
}
}, R.string.text_no_file_rw_permission);
}
})
.then(new Tile() {
@Override
public boolean fall() {
ShortcutActivity.this.runScriptFile(path);
return true;
}
})
.fall();
}
public void onStart() {
super.onStart();
finish();
}
private void runScriptFile(String path) {
try {
Droid.getInstance().runScriptFile(path);
} catch (Exception e) {
e.printStackTrace();
Toast.makeText(this, e.getMessage(), Toast.LENGTH_SHORT).show();
}
}
private boolean ensure(BooleanSupplier bool, String message) {
boolean b = bool.getAsBoolean();
if (!b) {
Toast.makeText(this, message, Toast.LENGTH_SHORT).show();
}
return b;
}
private boolean ensure(BooleanSupplier bool, int resId) {
return ensure(bool, getString(resId));
}
private boolean hasStorageReadPermission() {
return Build.VERSION.SDK_INT >= Build.VERSION_CODES.M &&
checkSelfPermission(READ_EXTERNAL_STORAGE) == PERMISSION_GRANTED;
}
interface Tile {
boolean fall();
}
public static class Domino {
private List<Tile> mTiles = new LinkedList<>();
Domino then(Tile next) {
mTiles.add(next);
return this;
}
void fall() {
for (Tile tile : mTiles) {
if (!tile.fall())
break;
}
}
}
}

View File

@@ -0,0 +1,32 @@
package com.stardust.scriptdroid.external.tile;
import android.os.Build;
import android.service.quicksettings.Tile;
import android.service.quicksettings.TileService;
import android.support.annotation.RequiresApi;
import com.stardust.scriptdroid.bounds_assist.BoundsAssistant;
/**
* Created by Stardust on 2017/1/26.
*/
@RequiresApi(api = Build.VERSION_CODES.N)
public class BoundsAssistEnableTileService extends TileService {
public void onClick() {
BoundsAssistant.setAssistModeEnable(!BoundsAssistant.isAssistModeEnable());
updateTile();
}
private void updateTile() {
getQsTile().setState(BoundsAssistant.isAssistModeEnable() ? Tile.STATE_ACTIVE : Tile.STATE_INACTIVE);
getQsTile().updateTile();
}
@Override
public void onStartListening() {
updateTile();
}
}