fix(ui): some bugs on back pressed

This commit is contained in:
hyb1996
2018-09-19 16:41:01 +08:00
parent 62aafaf06b
commit 723c7a7ff0
10 changed files with 63 additions and 56 deletions

View File

@@ -3,6 +3,7 @@ package org.autojs.autojs.external.foreground;
import android.app.Notification; import android.app.Notification;
import android.app.NotificationChannel; import android.app.NotificationChannel;
import android.app.NotificationManager; import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service; import android.app.Service;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
@@ -12,11 +13,34 @@ import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi; import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat; import android.support.v4.app.NotificationCompat;
import com.stardust.app.GlobalAppContext;
import org.autojs.autojs.R; import org.autojs.autojs.R;
import org.autojs.autojs.ui.main.MainActivity_;
public class ForegroundService extends Service { public class ForegroundService extends Service {
private static final int NOTIFICATION_ID = 117;
private static final String CHANEL_ID = "foreground";
private static final int NOTIFICATION_ID = 1;
private static final String CHANEL_ID = ForegroundService.class.getName() + ".foreground";
public static void start(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
context.startForegroundService(new Intent(context, ForegroundService.class));
} else {
context.startService(new Intent(context, ForegroundService.class));
}
}
public static void stop(Context context){
context.stopService(new Intent(context, ForegroundService.class));
}
@Override
public void onCreate() {
super.onCreate();
startForeground();
}
@Nullable @Nullable
@Override @Override
@@ -24,13 +48,7 @@ public class ForegroundService extends Service {
return null; return null;
} }
@Override private void startForeground() {
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground();
return START_STICKY;
}
private void startForeground(){
startForeground(NOTIFICATION_ID, buildNotification()); startForeground(NOTIFICATION_ID, buildNotification());
} }
@@ -38,21 +56,33 @@ public class ForegroundService extends Service {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel(); createNotificationChannel();
} }
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, MainActivity_.intent(this).get(), 0);
return new NotificationCompat.Builder(this, CHANEL_ID) return new NotificationCompat.Builder(this, CHANEL_ID)
.setContentTitle(getString(R.string.foreground_notification_title)) .setContentTitle(getString(R.string.foreground_notification_title))
.setContentText(getString(R.string.foreground_notification_text)) .setContentText(getString(R.string.foreground_notification_text))
.setSmallIcon(R.drawable.autojs_material) .setSmallIcon(R.drawable.autojs_material)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.setChannelId(CHANEL_ID)
.build(); .build();
} }
@RequiresApi(api = Build.VERSION_CODES.O) @RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel(){ private void createNotificationChannel() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null; assert manager != null;
CharSequence name = getString(R.string.foreground_notification_channel_name); CharSequence name = getString(R.string.foreground_notification_channel_name);
String description = getString(R.string.foreground_notification_channel_name); String description = getString(R.string.foreground_notification_channel_name);
NotificationChannel channel = new NotificationChannel(CHANEL_ID, name, NotificationManager.IMPORTANCE_HIGH); NotificationChannel channel = new NotificationChannel(CHANEL_ID, name, NotificationManager.IMPORTANCE_DEFAULT);
channel.setDescription(description); channel.setDescription(description);
channel.enableLights(false);
manager.createNotificationChannel(channel); manager.createNotificationChannel(channel);
} }
@Override
public void onDestroy() {
stopForeground(true);
super.onDestroy();
}
} }

View File

@@ -74,23 +74,12 @@ public class DocsFragment extends ViewPagerFragment implements BackPressedHandle
} }
@Override
public void onResume() {
super.onResume();
((BackPressedHandler.HostActivity) getActivity())
.getBackPressedObserver()
.registerHandlerAtFront(this);
}
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
Bundle savedWebViewState = new Bundle(); Bundle savedWebViewState = new Bundle();
mWebView.saveState(savedWebViewState); mWebView.saveState(savedWebViewState);
getArguments().putBundle("savedWebViewState", savedWebViewState); getArguments().putBundle("savedWebViewState", savedWebViewState);
((BackPressedHandler.HostActivity) getActivity())
.getBackPressedObserver()
.unregisterHandler(this);
} }
@Override @Override

View File

@@ -37,6 +37,7 @@ import org.autojs.autojs.BuildConfig;
import org.autojs.autojs.Pref; import org.autojs.autojs.Pref;
import org.autojs.autojs.R; import org.autojs.autojs.R;
import org.autojs.autojs.autojs.AutoJs; import org.autojs.autojs.autojs.AutoJs;
import org.autojs.autojs.external.foreground.ForegroundService;
import org.autojs.autojs.model.explorer.Explorers; import org.autojs.autojs.model.explorer.Explorers;
import org.autojs.autojs.tool.AccessibilityServiceTool; import org.autojs.autojs.tool.AccessibilityServiceTool;
import org.autojs.autojs.ui.BaseActivity; import org.autojs.autojs.ui.BaseActivity;
@@ -208,6 +209,7 @@ public class MainActivity extends BaseActivity implements OnActivityResultDelega
public void exitCompletely() { public void exitCompletely() {
finish(); finish();
FloatyWindowManger.hideCircularMenu(); FloatyWindowManger.hideCircularMenu();
ForegroundService.stop(this);
stopService(new Intent(this, FloatyService.class)); stopService(new Intent(this, FloatyService.class));
AutoJs.getInstance().getScriptEngineService().stopAll(); AutoJs.getInstance().getScriptEngineService().stopAll();
} }
@@ -264,6 +266,12 @@ public class MainActivity extends BaseActivity implements OnActivityResultDelega
@Override @Override
public void onBackPressed() { public void onBackPressed() {
Fragment fragment = mPagerAdapter.getStoredFragment(mViewPager.getCurrentItem());
if (fragment instanceof BackPressedHandler) {
if (((BackPressedHandler) fragment).onBackPressed(this)) {
return;
}
}
if (!mBackPressObserver.onBackPressed(this)) { if (!mBackPressObserver.onBackPressed(this)) {
super.onBackPressed(); super.onBackPressed();
} }

View File

@@ -3,14 +3,17 @@ package org.autojs.autojs.ui.main;
import android.support.annotation.CallSuper; import android.support.annotation.CallSuper;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.animation.FastOutSlowInInterpolator; import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.view.View; import android.view.View;
import com.stardust.util.BackPressedHandler;
/** /**
* Created by Stardust on 2017/8/22. * Created by Stardust on 2017/8/22.
*/ */
public abstract class ViewPagerFragment extends Fragment { public abstract class ViewPagerFragment extends Fragment implements BackPressedHandler {
protected static final int ROTATION_GONE = -1; protected static final int ROTATION_GONE = -1;

View File

@@ -72,23 +72,12 @@ public class CommunityFragment extends ViewPagerFragment implements BackPressedH
} }
} }
@Override
public void onResume() {
super.onResume();
((BackPressedHandler.HostActivity) getActivity())
.getBackPressedObserver()
.registerHandlerAtFront(this);
}
@Override @Override
public void onPause() { public void onPause() {
super.onPause(); super.onPause();
Bundle savedWebViewState = new Bundle(); Bundle savedWebViewState = new Bundle();
mWebView.saveState(savedWebViewState); mWebView.saveState(savedWebViewState);
getArguments().putBundle("savedWebViewState", savedWebViewState); getArguments().putBundle("savedWebViewState", savedWebViewState);
((BackPressedHandler.HostActivity) getActivity())
.getBackPressedObserver()
.unregisterHandler(this);
} }
@Override @Override

View File

@@ -147,7 +147,7 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
} }
setChecked(mConnectionItem, DevPluginService.getInstance().isConnected()); setChecked(mConnectionItem, DevPluginService.getInstance().isConnected());
if(Pref.isForegroundServiceEnabled()){ if(Pref.isForegroundServiceEnabled()){
GlobalAppContext.get().startService(new Intent(getContext(), ForegroundService.class)); ForegroundService.start(GlobalAppContext.get());
setChecked(mForegroundServiceItem, true); setChecked(mForegroundServiceItem, true);
} }
} }
@@ -265,9 +265,9 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
private void toggleForegroundService(DrawerMenuItemViewHolder holder) { private void toggleForegroundService(DrawerMenuItemViewHolder holder) {
boolean checked = holder.getSwitchCompat().isChecked(); boolean checked = holder.getSwitchCompat().isChecked();
if(checked){ if(checked){
GlobalAppContext.get().startService(new Intent(getContext(), ForegroundService.class)); ForegroundService.start(GlobalAppContext.get());
}else { }else {
GlobalAppContext.get().stopService(new Intent(getContext(), ForegroundService.class)); ForegroundService.stop(GlobalAppContext.get());
} }
} }

View File

@@ -62,7 +62,6 @@ public class MarketFragment extends ViewPagerFragment implements BackPressedHand
@Override @Override
public boolean onBackPressed(Activity activity) { public boolean onBackPressed(Activity activity) {
return false; return false;
} }

View File

@@ -35,7 +35,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
* Created by Stardust on 2017/3/13. * Created by Stardust on 2017/3/13.
*/ */
@EFragment(R.layout.fragment_my_script_list) @EFragment(R.layout.fragment_my_script_list)
public class MyScriptListFragment extends ViewPagerFragment implements BackPressedHandler, FloatingActionMenu.OnFloatingActionButtonClickListener { public class MyScriptListFragment extends ViewPagerFragment implements FloatingActionMenu.OnFloatingActionButtonClickListener {
private static final String TAG = "MyScriptListFragment"; private static final String TAG = "MyScriptListFragment";
@@ -68,22 +68,6 @@ public class MyScriptListFragment extends ViewPagerFragment implements BackPress
}); });
} }
@Override
public void onResume() {
super.onResume();
((BackPressedHandler.HostActivity) getActivity())
.getBackPressedObserver()
.registerHandlerAtFront(this);
}
@Override
public void onPause() {
super.onPause();
((BackPressedHandler.HostActivity) getActivity())
.getBackPressedObserver()
.unregisterHandler(this);
}
@Override @Override
protected void onFabClick(FloatingActionButton fab) { protected void onFabClick(FloatingActionButton fab) {
initFloatingActionMenuIfNeeded(fab); initFloatingActionMenuIfNeeded(fab);

View File

@@ -1,5 +1,6 @@
package org.autojs.autojs.ui.main.task; package org.autojs.autojs.ui.main.task;
import android.app.Activity;
import android.os.Bundle; import android.os.Bundle;
import android.support.design.widget.FloatingActionButton; import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout; import android.support.v4.widget.SwipeRefreshLayout;
@@ -70,4 +71,8 @@ public class TaskManagerFragment extends ViewPagerFragment {
AutoJs.getInstance().getScriptEngineService().stopAll(); AutoJs.getInstance().getScriptEngineService().stopAll();
} }
@Override
public boolean onBackPressed(Activity activity) {
return false;
}
} }

View File

@@ -136,7 +136,7 @@ public class BlockedMaterialDialog extends MaterialDialog {
} }
public MaterialDialog.Builder itemsCallbackMultiChoice(@Nullable Integer[] selectedIndices) { public MaterialDialog.Builder itemsCallbackMultiChoice(@Nullable Integer[] selectedIndices) {
dismissListener(dialog -> setAndNotify(new Integer[0])); dismissListener(dialog -> setAndNotify(new int[0]));
super.itemsCallbackMultiChoice(selectedIndices, (dialog, which, text) -> { super.itemsCallbackMultiChoice(selectedIndices, (dialog, which, text) -> {
setAndNotify(ArrayUtils.unbox(which)); setAndNotify(ArrayUtils.unbox(which));
return true; return true;