fix: switch state sync problem

This commit is contained in:
hyb1996
2017-02-04 10:14:58 +08:00
parent 5640b47663
commit 3fb16518e4
11 changed files with 205 additions and 106 deletions

View File

@@ -1,8 +1,10 @@
package com.stardust.scriptdroid;
import android.app.Application;
import android.preference.PreferenceManager;
import com.stardust.util.CrashHandler;
import com.stardust.util.StateObserver;
/**
* Created by Stardust on 2017/1/27.
@@ -11,14 +13,21 @@ import com.stardust.util.CrashHandler;
public class App extends Application {
private static App instance;
private static StateObserver stateObserver;
public static App getApp() {
return instance;
}
public static StateObserver getStateObserver() {
return stateObserver;
}
public void onCreate() {
super.onCreate();
Thread.setDefaultUncaughtExceptionHandler(new CrashHandler(ErrorReportActivity.class));
instance = this;
stateObserver = new StateObserver(PreferenceManager.getDefaultSharedPreferences(this));
}
}

View File

@@ -7,7 +7,8 @@ import android.os.IBinder;
import android.support.annotation.Nullable;
import com.stardust.scriptdroid.droid.runtime.action.ActionPerformService;
import com.stardust.scriptdroid.ui.AssistModeSwitchNotification;
import static com.stardust.scriptdroid.ui.AssistModeSwitchNotification.KEY_ASSIST_MODE_NOTIFICATION;
/**
* Created by Stardust on 2017/2/2.
@@ -18,19 +19,19 @@ public class AssistModeSwitchService extends Service {
Intent intent = new Intent(App.getApp(), AssistModeSwitchService.class)
.putExtra("intentValid", true)
.putExtra("switch", "assistMode");
return PendingIntent.getService(App.getApp(), 0, intent, 0);
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("intentValid", false);
if (intentValid) {
String action = intent.getStringExtra("action");
String action = intent.getStringExtra("switch");
if (action != null) {
if (action.equals("assistMode")) {
ActionPerformService.setAssistModeEnable(!ActionPerformService.isAssistModeEnable());
} else {
AssistModeSwitchNotification.setEnable(false);
App.getStateObserver().setState(KEY_ASSIST_MODE_NOTIFICATION, false);
}
}
}
@@ -48,6 +49,6 @@ public class AssistModeSwitchService extends Service {
Intent deleteIntent = new Intent(App.getApp(), AssistModeSwitchService.class)
.putExtra("intentValid", true)
.putExtra("switch", "assistModeNotification");
return PendingIntent.getService(App.getApp(), 0, deleteIntent, 0);
return PendingIntent.getService(App.getApp(), 0, deleteIntent, PendingIntent.FLAG_ONE_SHOT);
}
}

View File

@@ -0,0 +1,29 @@
package com.stardust.scriptdroid;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.stardust.scriptdroid.BaseActivity;
import com.stardust.scriptdroid.EditActivity;
/**
* Created by Stardust on 2017/2/2.
*/
public class EditAndRunIntentActivity extends BaseActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
handleIntent();
}
private void handleIntent() {
Intent intent = getIntent();
String path = intent.getData().getPath();
if (!TextUtils.isEmpty(path))
EditActivity.editFile(this, path);
}
}

View File

@@ -3,7 +3,6 @@ package com.stardust.scriptdroid.droid.runtime.action;
import android.accessibilityservice.AccessibilityService;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Intent;
import android.graphics.Rect;
import android.os.Build;
import android.preference.PreferenceManager;
@@ -28,12 +27,8 @@ import java.util.List;
public class ActionPerformService extends AccessibilityService {
public static final String ACTION_CHANGE_ASSIST_MODE = App.getApp().getPackageName() + ".ACTION_CHANGE_ASSIST_MODE";
private static final String TAG = "ActionPerformService";
private static final String KEY_ASSIST_MODE_ENABLE = "ASSIST_MODE_ENABLE";
public static final String KEY_ASSIST_MODE_ENABLE = "ASSIST_MODE_ENABLE";
private static ActionPerformService instance;
@SuppressWarnings("unchecked")
@@ -69,8 +64,7 @@ public class ActionPerformService extends AccessibilityService {
public static void setAssistModeEnable(boolean assistModeEnable) {
ActionPerformService.assistModeEnable = assistModeEnable;
PreferenceManager.getDefaultSharedPreferences(App.getApp()).edit().putBoolean(KEY_ASSIST_MODE_ENABLE, assistModeEnable).apply();
App.getApp().sendBroadcast(new Intent(ACTION_CHANGE_ASSIST_MODE).putExtra("enable", assistModeEnable));
App.getStateObserver().setState(KEY_ASSIST_MODE_ENABLE, assistModeEnable);
}
@Override

View File

@@ -10,6 +10,9 @@ import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.AssistModeSwitchService;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.droid.runtime.action.ActionPerformService;
import com.stardust.util.StateObserver;
import static com.stardust.scriptdroid.droid.runtime.action.ActionPerformService.KEY_ASSIST_MODE_ENABLE;
/**
* Created by Stardust on 2017/2/2.
@@ -18,19 +21,46 @@ import com.stardust.scriptdroid.droid.runtime.action.ActionPerformService;
public class AssistModeSwitchNotification {
private static final int NOTIFY_ID = 11126;
private static final String KEY_ASSIST_MODE_NOTIFICATION = "KEY_ASSIST_MODE_NOTIFICATION";
public static final String KEY_ASSIST_MODE_NOTIFICATION = "KEY_ASSIST_MODE_NOTIFICATION";
private static boolean enable = false;
private static Notification notification;
static {
readPreference();
}
public static boolean isEnable() {
return enable;
}
static {
App.getStateObserver().register(KEY_ASSIST_MODE_NOTIFICATION, new StateObserver.OnBooleanStateChangedListener() {
@Override
public void onStateChanged(Boolean newState) {
setEnable(newState);
}
@Override
public void initState(Boolean state) {
enable = state;
if (enable) {
showNotification();
}
}
});
App.getStateObserver().register(KEY_ASSIST_MODE_ENABLE, new StateObserver.OnBooleanStateChangedListener() {
@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 (AssistModeSwitchNotification.enable == enable)
return;
@@ -43,13 +73,6 @@ public class AssistModeSwitchNotification {
}
}
public static void notifyAssistModeChanged() {
if (enable) {
setEnable(false);
setEnable(true);
}
}
private static void showNotification() {
notification = new NotificationCompat.Builder(App.getApp())
@@ -74,10 +97,4 @@ public class AssistModeSwitchNotification {
notificationManager.cancel(NOTIFY_ID);
}
private static void readPreference() {
enable = PreferenceManager.getDefaultSharedPreferences(App.getApp()).getBoolean(KEY_ASSIST_MODE_NOTIFICATION, false);
setEnable(enable);
}
}

View File

@@ -0,0 +1,20 @@
package com.stardust.scriptdroid.ui;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
/**
* Created by Stardust on 2017/2/4.
*/
public class EditSideMenuFragment extends com.stardust.app.Fragment {
@Nullable
@Override
public View createView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
return null;
}
}

View File

@@ -1,9 +1,6 @@
package com.stardust.scriptdroid.ui;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.design.widget.Snackbar;
@@ -14,6 +11,7 @@ import android.view.View;
import android.view.ViewGroup;
import com.stardust.app.Fragment;
import com.stardust.scriptdroid.App;
import com.stardust.scriptdroid.DocumentActivity;
import com.stardust.scriptdroid.R;
import com.stardust.scriptdroid.droid.Droid;
@@ -22,7 +20,8 @@ import com.stardust.view.ViewBinder;
import com.stardust.view.ViewBinding;
import com.stardust.view.accessibility.AccessibilityServiceUtils;
import static com.stardust.scriptdroid.droid.runtime.action.ActionPerformService.ACTION_CHANGE_ASSIST_MODE;
import static com.stardust.scriptdroid.droid.runtime.action.ActionPerformService.KEY_ASSIST_MODE_ENABLE;
import static com.stardust.scriptdroid.ui.AssistModeSwitchNotification.KEY_ASSIST_MODE_NOTIFICATION;
/**
* Created by Stardust on 2017/1/30.
@@ -37,7 +36,6 @@ public class SlideMenuFragment extends Fragment {
}
private SwitchCompat mAutoOperateServiceSwitch, mAssistServiceSwitch, mAssistServiceNotificationSwitch;
private Receiver mReceiver = new Receiver();
@Nullable
@Override
@@ -51,9 +49,6 @@ public class SlideMenuFragment extends Fragment {
if (mAutoOperateServiceSwitch == null) {
setUpSwitchCompat();
ViewBinder.bind(this);
IntentFilter filter = new IntentFilter();
filter.addAction(ACTION_CHANGE_ASSIST_MODE);
getContext().registerReceiver(mReceiver, filter);
}
syncSwitchState();
}
@@ -73,6 +68,8 @@ public class SlideMenuFragment extends Fragment {
mAutoOperateServiceSwitch = $(R.id.sw_auto_operate_service);
mAssistServiceSwitch = $(R.id.sw_assist_service);
mAssistServiceNotificationSwitch = $(R.id.sw_assist_service_notification);
App.getStateObserver().register(KEY_ASSIST_MODE_ENABLE, mAssistServiceSwitch);
App.getStateObserver().register(KEY_ASSIST_MODE_NOTIFICATION, mAssistServiceNotificationSwitch);
}
@ViewBinding.Click(R.id.syntax_and_api)
@@ -97,7 +94,6 @@ public class SlideMenuFragment extends Fragment {
@ViewBinding.Check(R.id.sw_assist_service)
private void setAssistServiceEnable(boolean enable) {
ActionPerformService.setAssistModeEnable(enable);
AssistModeSwitchNotification.notifyAssistModeChanged();
}
@ViewBinding.Click(R.id.assist_service)
@@ -126,21 +122,7 @@ public class SlideMenuFragment extends Fragment {
@Override
public void onDestroy() {
getContext().unregisterReceiver(mReceiver);
super.onDestroy();
}
private class Receiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(ACTION_CHANGE_ASSIST_MODE)) {
AssistModeSwitchNotification.notifyAssistModeChanged();
mAssistServiceSwitch.setChecked(intent.getBooleanExtra("enable", false));
}
}
}
}