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.NotificationChannel;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
@ -12,11 +13,34 @@ import android.support.annotation.Nullable;
import android.support.annotation.RequiresApi;
import android.support.v4.app.NotificationCompat;
import com.stardust.app.GlobalAppContext;
import org.autojs.autojs.R;
import org.autojs.autojs.ui.main.MainActivity_;
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
@Override
@ -24,13 +48,7 @@ public class ForegroundService extends Service {
return null;
}
@Override
public int onStartCommand(Intent intent, int flags, int startId) {
startForeground();
return START_STICKY;
}
private void startForeground(){
private void startForeground() {
startForeground(NOTIFICATION_ID, buildNotification());
}
@ -38,21 +56,33 @@ public class ForegroundService extends Service {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
createNotificationChannel();
}
PendingIntent contentIntent = PendingIntent.getActivity(this, 0, MainActivity_.intent(this).get(), 0);
return new NotificationCompat.Builder(this, CHANEL_ID)
.setContentTitle(getString(R.string.foreground_notification_title))
.setContentText(getString(R.string.foreground_notification_text))
.setSmallIcon(R.drawable.autojs_material)
.setWhen(System.currentTimeMillis())
.setContentIntent(contentIntent)
.setChannelId(CHANEL_ID)
.build();
}
@RequiresApi(api = Build.VERSION_CODES.O)
private void createNotificationChannel(){
private void createNotificationChannel() {
NotificationManager manager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
assert manager != null;
CharSequence name = 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.enableLights(false);
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
public void onPause() {
super.onPause();
Bundle savedWebViewState = new Bundle();
mWebView.saveState(savedWebViewState);
getArguments().putBundle("savedWebViewState", savedWebViewState);
((BackPressedHandler.HostActivity) getActivity())
.getBackPressedObserver()
.unregisterHandler(this);
}
@Override

View File

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

View File

@ -3,14 +3,17 @@ package org.autojs.autojs.ui.main;
import android.support.annotation.CallSuper;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentActivity;
import android.support.v4.view.animation.FastOutSlowInInterpolator;
import android.view.View;
import com.stardust.util.BackPressedHandler;
/**
* 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;

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
public void onPause() {
super.onPause();
Bundle savedWebViewState = new Bundle();
mWebView.saveState(savedWebViewState);
getArguments().putBundle("savedWebViewState", savedWebViewState);
((BackPressedHandler.HostActivity) getActivity())
.getBackPressedObserver()
.unregisterHandler(this);
}
@Override

View File

@ -147,7 +147,7 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
}
setChecked(mConnectionItem, DevPluginService.getInstance().isConnected());
if(Pref.isForegroundServiceEnabled()){
GlobalAppContext.get().startService(new Intent(getContext(), ForegroundService.class));
ForegroundService.start(GlobalAppContext.get());
setChecked(mForegroundServiceItem, true);
}
}
@ -265,9 +265,9 @@ public class DrawerFragment extends android.support.v4.app.Fragment {
private void toggleForegroundService(DrawerMenuItemViewHolder holder) {
boolean checked = holder.getSwitchCompat().isChecked();
if(checked){
GlobalAppContext.get().startService(new Intent(getContext(), ForegroundService.class));
ForegroundService.start(GlobalAppContext.get());
}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
public boolean onBackPressed(Activity activity) {
return false;
}

View File

@ -35,7 +35,7 @@ import io.reactivex.android.schedulers.AndroidSchedulers;
* Created by Stardust on 2017/3/13.
*/
@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";
@ -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
protected void onFabClick(FloatingActionButton fab) {
initFloatingActionMenuIfNeeded(fab);

View File

@ -1,5 +1,6 @@
package org.autojs.autojs.ui.main.task;
import android.app.Activity;
import android.os.Bundle;
import android.support.design.widget.FloatingActionButton;
import android.support.v4.widget.SwipeRefreshLayout;
@ -70,4 +71,8 @@ public class TaskManagerFragment extends ViewPagerFragment {
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) {
dismissListener(dialog -> setAndNotify(new Integer[0]));
dismissListener(dialog -> setAndNotify(new int[0]));
super.itemsCallbackMultiChoice(selectedIndices, (dialog, which, text) -> {
setAndNotify(ArrayUtils.unbox(which));
return true;