This commit is contained in:
陈君陶
2024-07-25 17:34:03 +08:00
parent 7cf2adaa37
commit 3c207244e6
101 changed files with 4708 additions and 267 deletions

View File

@ -1,5 +1,6 @@
package com.gh.ndownload;
import android.app.Activity;
import android.app.Notification;
import android.app.NotificationChannel;
import android.app.NotificationManager;
@ -14,6 +15,11 @@ import androidx.annotation.Nullable;
import androidx.core.app.NotificationCompat;
import com.gh.download.DownloadManager;
import com.gh.gamecenter.SplashScreenActivity;
import com.gh.gamecenter.common.base.GlobalActivityManager;
import com.gh.ndownload.suspendwindow.NDownloadDrawOverlayPermissionWindowController;
import com.gh.ndownload.suspendwindow.NDownloadSuspendWindowController;
import com.gh.ndownload.suspendwindow.utils.NDownloadSuspendWindowHelper;
import com.lightgame.BuildConfig;
import com.lightgame.download.DownloadConfig;
import com.lightgame.download.DownloadDao;
@ -29,12 +35,17 @@ public class NDownloadService extends Service {
public static final String KEY_SERVICE_ACTION = "service_action";
public static final String START_FOREGROUND = "start_foreground";
private static final String SERVICE_CHANNEL_ID = "Halo_Download";
private static NDownloadService sService = null;
private ForegroundNotificationManager mForegroundNotificationManager;
private NDownloadSuspendWindowController mDownloadSuspendWindowController;
private NDownloadDrawOverlayPermissionWindowController mDrawOverlayPermissionWindowController;
@Nullable
public static NDownloadService getService() {
return sService;
@ -47,6 +58,7 @@ public class NDownloadService extends Service {
sService = this;
mForegroundNotificationManager = new ForegroundNotificationManager(this, this.getApplication());
Utils.log(NDownloadService.class.getSimpleName(), "onCreate");
}
@ -129,8 +141,15 @@ public class NDownloadService extends Service {
@Override
public void onDestroy() {
super.onDestroy();
sService = null;
if (mDownloadSuspendWindowController != null) {
mDownloadSuspendWindowController.dismiss();
}
if (mDrawOverlayPermissionWindowController != null) {
mDrawOverlayPermissionWindowController.dismiss();
}
}
@Override
@ -188,6 +207,9 @@ public class NDownloadService extends Service {
synchronized void startDownload(DownloadEntity entry) {
if (!NDataChanger.INSTANCE.getDownloadingTaskUrlSet().contains(entry.getUrl())) {
// 显示下载悬浮窗
showDownloadSuspendWindowIfNeeded(entry);
// 刷新初始状态
NDataChanger.INSTANCE.notifyDataChanged(entry);
@ -253,6 +275,20 @@ public class NDownloadService extends Service {
}
}
private void showDownloadSuspendWindowIfNeeded(DownloadEntity entry) {
if (!NDownloadSuspendWindowHelper.shouldSkipDownloadEntity(getApplicationContext(), entry)) {
if (NDownloadSuspendWindowHelper.canDrawOverLayer(getApplicationContext())) {// 已开启悬浮窗权限,直接显示悬浮窗
showDownloadSuspendWindow();
} else {// 未开启悬浮窗权限,显示开启悬浮窗权限引导窗口
Activity topActivity = GlobalActivityManager.INSTANCE.getCurrentActivity();
if (!NDownloadSuspendWindowHelper.isDrawOverlayPermissionWindowShown()
&& topActivity != null && !(topActivity instanceof SplashScreenActivity)) {
showDownloadDrawOverlayPermissionWindow(topActivity, entry);
}
}
}
}
/**
* 获取 0 以外的随机 int 数值
*/
@ -265,4 +301,21 @@ public class NDownloadService extends Service {
}
}
private void showDownloadSuspendWindow() {
if (mDownloadSuspendWindowController == null) {
mDownloadSuspendWindowController = new NDownloadSuspendWindowController(getApplication());
}
mDownloadSuspendWindowController.show();
}
private void showDownloadDrawOverlayPermissionWindow(Activity activity, DownloadEntity downloadEntity) {
if (mDrawOverlayPermissionWindowController == null) {
mDrawOverlayPermissionWindowController = new NDownloadDrawOverlayPermissionWindowController(getApplication());
mDrawOverlayPermissionWindowController.setOnGrantDrawOverlayPermissionListener(this::showDownloadSuspendWindow);
}
mDrawOverlayPermissionWindowController.setCurrentDownloadEntity(downloadEntity);
mDrawOverlayPermissionWindowController.show(activity);
NDownloadSuspendWindowHelper.markDrawOverlayPermissionWindowShown();
}
}