From d33281f0c2654767a37bc4be142a1d5fb16e26cd Mon Sep 17 00:00:00 2001 From: zfdang Date: Fri, 29 Apr 2022 15:39:01 +0800 Subject: [PATCH] refactor --- app/build.gradle | 6 + app/src/main/AndroidManifest.xml | 6 - .../main/java/com/zfdang/SMTHApplication.java | 7 +- .../zfdang/zsmth_android/MainActivity.java | 31 +++-- .../zsmth_android/newsmth/UserStatus.java | 1 + .../services/AlarmBroadcastReceiver.java | 52 -------- ...ice.java => MaintainUserStatusWorker.java} | 119 +++++++++++------- .../services/UpdateUserStatusService.java | 81 ------------ 8 files changed, 108 insertions(+), 195 deletions(-) delete mode 100644 app/src/main/java/com/zfdang/zsmth_android/services/AlarmBroadcastReceiver.java rename app/src/main/java/com/zfdang/zsmth_android/services/{MaintainUserStatusService.java => MaintainUserStatusWorker.java} (81%) delete mode 100644 app/src/main/java/com/zfdang/zsmth_android/services/UpdateUserStatusService.java diff --git a/app/build.gradle b/app/build.gradle index 7815f083..87c24ed0 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -106,6 +106,12 @@ dependencies { implementation "io.reactivex.rxjava2:rxjava:$rootProject.RXJAVA2_VERSION" implementation "io.reactivex.rxjava2:rxandroid:$rootProject.RXANDROID2_VERSION" + def work_version = "2.7.1" + // (Java only) + implementation "androidx.work:work-runtime:$work_version" + // optional - RxJava2 support + implementation "androidx.work:work-rxjava2:$work_version" + // OkHttp implementation "com.squareup.okhttp3:okhttp:$OKHTTP_VERSION" implementation "com.squareup.okhttp3:logging-interceptor:$OKHTTP_VERSION" diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 54fa3a65..7a6aec97 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -81,12 +81,6 @@ android:configChanges="orientation|screenSize" /> - - its = helper.wService.login(username, password, "7").map(new Function() { - @Override public Integer apply(@NonNull AjaxResponse response) throws Exception { + @Override + public Integer apply(@NonNull AjaxResponse response) throws Exception { if (response.getAjax_st() == 1) { // {"ajax_st":1,"ajax_code":"0005","ajax_msg":"操作成功"} return AjaxResponse.AJAX_RESULT_OK; @@ -150,10 +147,10 @@ public UserStatus apply(UserStatus userStatus) throws Exception { //Log.d(TAG, "call: " + "2.2.5.1 try to get userstatus again after login action"); UserStatus stat = SMTHHelper.queryActiveUserStatus().blockingFirst(); //Log.d(TAG, "call: " + stats.size()); - if(stat == null) { + if (stat == null) { stat = new UserStatus(); } - if(stat.getId() == null) { + if (stat.getId() == null) { stat.setId("guest"); } return stat; @@ -202,23 +199,24 @@ public void onNext(UserStatus userStatus) { SMTHApplication.activeUser = userStatus; String message = ""; - if(SMTHApplication.isValidUser()) { + if (SMTHApplication.isValidUser()) { // get message if user is valid user message = getNotificationMessage(SMTHApplication.activeUser); - } else if(updateUserIcon == true) { + } else if (updateUserIcon == true) { // not a valid user, but updateUserIcon is true, means login status has lost message = SMTHApplication.NOTIFICATION_LOGIN_LOST; } + UserStatusReceiver receiver = SMTHApplication.mUserStatusReceiver; // send notification: 1. new message 2. new activeUser to update Sidebar status - if ((updateUserIcon || message.length() > 0) && mUserStatusReceiver != null) { + if ((updateUserIcon || message.length() > 0) && receiver != null) { //Log.d(TAG, "4.2 cached user, valid message, valid receiver, send message"); Bundle bundle = new Bundle(); - if(message.length() > 0) { + if (message.length() > 0) { bundle.putString(SMTHApplication.SERVICE_NOTIFICATION_MESSAGE, message); } // Here we call send passing a resultCode and the bundle of extras - mUserStatusReceiver.send(Activity.RESULT_OK, bundle); + receiver.send(Activity.RESULT_OK, bundle); } } @@ -231,8 +229,35 @@ public void onError(Throwable e) { public void onComplete() { } }); + } + + public String getNotificationMessage(UserStatus userStatus) { + String message = ""; + Settings settings = Settings.getInstance(); + if (userStatus.hasNewMail() && settings.isNotificationMail()) { + message = SMTHApplication.NOTIFICATION_NEW_MAIL + " "; + } + if (userStatus.hasNewLike() && settings.isNotificationLike()) { + message += SMTHApplication.NOTIFICATION_NEW_LIKE + " "; + } + if (userStatus.hasNewAt() && settings.isNotificationAt()) { + message += SMTHApplication.NOTIFICATION_NEW_AT + " "; + } + if (userStatus.hasNewReply() && settings.isNotificationReply()) { + message += SMTHApplication.NOTIFICATION_NEW_REPLY + " "; + } + //Log.d(TAG, message); + return message; + } - // stop the service - stopSelf(); + private void enqueueNextWorker() { + Data.Builder inputData = new Data.Builder(); + inputData.putBoolean(MaintainUserStatusWorker.REPEAT, true); + WorkRequest userStatusWorkRequest = + new OneTimeWorkRequest.Builder(MaintainUserStatusWorker.class) + .setInitialDelay(SMTHApplication.INTERVAL_TO_CHECK_MESSAGE, TimeUnit.MINUTES) + .setInputData(inputData.build()) + .build(); + WorkManager.getInstance(getApplicationContext()).enqueue(userStatusWorkRequest); } } diff --git a/app/src/main/java/com/zfdang/zsmth_android/services/UpdateUserStatusService.java b/app/src/main/java/com/zfdang/zsmth_android/services/UpdateUserStatusService.java deleted file mode 100644 index 9b046a40..00000000 --- a/app/src/main/java/com/zfdang/zsmth_android/services/UpdateUserStatusService.java +++ /dev/null @@ -1,81 +0,0 @@ -package com.zfdang.zsmth_android.services; - -import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.app.Service; -import android.content.Intent; -import android.os.Build; -import android.os.IBinder; -import android.os.SystemClock; -import android.util.Log; - -import androidx.annotation.Nullable; -import androidx.core.app.NotificationCompat; - -import com.zfdang.zsmth_android.MainActivity; -import com.zfdang.zsmth_android.R; - -public class UpdateUserStatusService extends Service { - - private final String TAG = this.getClass().getSimpleName(); - private static final int ONGOING_NOTIFICATION_ID = 108; - private boolean running = true; - - @Override - public void onCreate() { - super.onCreate(); - - String NOTIFICATION_CHANNEL_ID = "com.zfdang.zsmth.userstatus"; - - try { - Intent notificationIntent = new Intent(this, MainActivity.class); - PendingIntent pendingIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); - - Notification.Builder mBuilder = new Notification.Builder(this) - .setOngoing(true) - .setSmallIcon(R.drawable.ic_launcher) - .setContentTitle("zSMTH后台检查服务") - .setContentText("后台程序会在程序运行期间,持续检查各种通知并保持登录状态...") - .setContentIntent(pendingIntent); - - NotificationManager mNotifyMgr = (NotificationManager) getSystemService(NOTIFICATION_SERVICE); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - NotificationChannel channel = new NotificationChannel( - NOTIFICATION_CHANNEL_ID, - "zSMTH后台检查服务", - NotificationManager.IMPORTANCE_LOW - ); - mNotifyMgr.createNotificationChannel(channel); - mBuilder.setChannelId(NOTIFICATION_CHANNEL_ID); //必须添加(Android 8.0) 【唯一标识】 - } - - Notification notification = mBuilder.build(); - startForeground(ONGOING_NOTIFICATION_ID, notification); - } catch (Exception se) { - Log.e(TAG, "showNotification: " + se.toString()); - } - } - - @Override - public int onStartCommand(Intent intent, int flags, int startId) { - Log.d(TAG, "onStartCommand"); - - // If we get killed, after returning from here, restart - return START_STICKY; - } - - - @Override - public void onDestroy() { - running = false; - super.onDestroy(); - } - - @Nullable - @Override - public IBinder onBind(Intent intent) { - return null; - } -}