diff --git a/app/src/main/java/com/gh/common/util/DialogUtils.java b/app/src/main/java/com/gh/common/util/DialogUtils.java index 5498ed6247..42de2fc456 100644 --- a/app/src/main/java/com/gh/common/util/DialogUtils.java +++ b/app/src/main/java/com/gh/common/util/DialogUtils.java @@ -354,14 +354,15 @@ public class DialogUtils { } public static void checkDownload(Context context, String size, CheckDownloadCallBack callBack) { - if (NetworkUtils.isWifiConnected(context) || filter4GorSize(context, size)) { + if (!NetworkUtils.isNetworkConnected(context)) { + showNoConnectionDownloadDialog(context, null, + () -> callBack.onResponse(true)); + } else if (NetworkUtils.isWifiConnected(context) || filter4GorSize(context, size)) { callBack.onResponse(false); } else { - showDownloadDialog(context, () -> { - callBack.onResponse(false); - }, () -> { - callBack.onResponse(true); - }); + showDownloadDialog(context, + () -> callBack.onResponse(false), + () -> callBack.onResponse(true)); } } @@ -394,6 +395,9 @@ public class DialogUtils { } } + public static void showNoConnectionDownloadDialog(Context context, ConfirmListener listener, CancelListener cancelListener) { + showWarningDialog(context, "下载提示", "网络异常,请检查手机网络状态", "连上WiFi后自动下载", "关闭", listener, cancelListener); + } public static void showDownloadDialog(Context context, ConfirmListener listener, CancelListener cancelListener) { showWarningDialog(context, "下载提示", "当前正在使用移动网络,立即下载会消耗手机流量", "连上WiFi后自动下载", "立即下载", listener, cancelListener); diff --git a/app/src/main/java/com/gh/download/DownloadNotification.java b/app/src/main/java/com/gh/download/DownloadNotification.java index dc895ced6a..17b51ca1b5 100644 --- a/app/src/main/java/com/gh/download/DownloadNotification.java +++ b/app/src/main/java/com/gh/download/DownloadNotification.java @@ -1,10 +1,12 @@ package com.gh.download; import android.app.Notification; +import android.app.NotificationChannel; import android.app.NotificationManager; import android.app.PendingIntent; import android.content.Context; import android.content.Intent; +import android.os.Build; import android.support.v4.app.NotificationCompat; import com.gh.common.util.AppDebugConfig; @@ -28,8 +30,9 @@ public class DownloadNotification { public static final String ACTION_INSTALL = "com.gh.gamecenter.INSTALL"; public static final String ACTION_DOWNLOAD = "com.gh.gamecenter.DOWNLOAD"; public static final int ICON = R.drawable.logo; - private static final int NOTIFY_ID = 0x123; - private static final int WAIT_NOTIFY_ID = 0x124; + public static final int NOTIFY_ID = 0x123; + public static final int WAIT_NOTIFY_ID = 0x124; + private static final String CHANNEL_ID = "Halo_Download"; private static NotificationManager getNotificationManager(Context context) { return (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); @@ -38,6 +41,12 @@ public class DownloadNotification { //TODO 下载重构 public static void showDownloadDoneNotification(Context context, DownloadEntity downloadEntity) { final NotificationManager manager = getNotificationManager(context); + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW); + manager.createNotificationChannel(channel); + } + Intent intent = new Intent(); intent.putExtra(EntranceUtils.KEY_PATH, downloadEntity.getPath()); intent.setAction(ACTION_INSTALL); @@ -58,7 +67,8 @@ public class DownloadNotification { } title = "下载完成,点击立即安装"; } - final Notification notification = new NotificationCompat.Builder(context) + + final Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(R.drawable.logo) .setTicker(title) .setContentTitle(text) @@ -85,7 +95,11 @@ public class DownloadNotification { downloadingSize++; } } - NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager nManager = getNotificationManager(context); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW); + nManager.createNotificationChannel(channel); + } if (downloadingSize == 0) { nManager.cancel(NOTIFY_ID); } else { @@ -93,7 +107,7 @@ public class DownloadNotification { intent.setAction(ACTION_DOWNLOAD); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); - Notification notification = new NotificationCompat.Builder(context) + Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(ICON) .setTicker("点击查看") .setContentTitle("正在下载" + downloadingSize + "个游戏") @@ -110,12 +124,16 @@ public class DownloadNotification { AppDebugConfig.logMethodWithParams(DownloadNotification.class, context); } - NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + NotificationManager nManager = getNotificationManager(context); + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + NotificationChannel channel = new NotificationChannel(CHANNEL_ID, CHANNEL_ID, NotificationManager.IMPORTANCE_LOW); + nManager.createNotificationChannel(channel); + } Intent intent = new Intent(); intent.setAction(ACTION_DOWNLOAD); PendingIntent pendingIntent = PendingIntent.getBroadcast(context, NOTIFY_ID, intent, PendingIntent.FLAG_UPDATE_CURRENT); - Notification notification = new NotificationCompat.Builder(context) + Notification notification = new NotificationCompat.Builder(context, CHANNEL_ID) .setSmallIcon(ICON) .setTicker("连接WiFi后自动恢复下载") .setContentTitle("下载已暂停") diff --git a/app/src/main/java/com/gh/gamecenter/MainActivity.java b/app/src/main/java/com/gh/gamecenter/MainActivity.java index a0cda9015e..fad27271f6 100644 --- a/app/src/main/java/com/gh/gamecenter/MainActivity.java +++ b/app/src/main/java/com/gh/gamecenter/MainActivity.java @@ -1081,6 +1081,7 @@ public class MainActivity extends BaseActivity { if (sp.getBoolean("concerngame", true)) { //设置页面控制是否安装后自动关注 // 安装后关注游戏 + DownloadEntity finalDownloadEntity = mDownloadEntity; RetrofitManager.getInstance(this).getApi().getGameDigestByPackageName(UrlFilterUtils.getFilterQuery("package", packageName)) .subscribeOn(Schedulers.io()) .observeOn(AndroidSchedulers.mainThread()) @@ -1091,7 +1092,7 @@ public class MainActivity extends BaseActivity { concernGame(gameDigestEntity.getId(), packageName); if (!TextUtils.isEmpty(gameDigestEntity.getId())) { // 关注游戏 - if (gameInfo == null || gameInfo.getId().equals(gameDigestEntity.getId())) { + if (finalDownloadEntity != null && gameDigestEntity.getId().equals(finalDownloadEntity.getGameId())) { ConcernUtils.INSTANCE.postConcernGameId(MainActivity.this, gameDigestEntity.getId(), null); } } diff --git a/app/src/main/java/com/gh/gamecenter/download/GameUpdateFragmentAdapter.java b/app/src/main/java/com/gh/gamecenter/download/GameUpdateFragmentAdapter.java index 9aa0ff1b27..c6d7725559 100644 --- a/app/src/main/java/com/gh/gamecenter/download/GameUpdateFragmentAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/download/GameUpdateFragmentAdapter.java @@ -521,7 +521,6 @@ class GameUpdateFragmentAdapter extends BaseRecyclerAdapter { return size + unit; } - // 添加下载 private void addUpdateDownload(int position, boolean isSubscribe) { GameUpdateEntity updateEntity = updateList.get(position); @@ -577,6 +576,7 @@ class GameUpdateFragmentAdapter extends BaseRecyclerAdapter { notifyItemChanged(0); EventBus.getDefault().post(new EBSkip(DownloadManagerActivity.TAG, DownloadManagerActivity.INDEX_UPDATE)); } + // 添加下载 public List getUpdateList() { return updateList; diff --git a/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java b/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java index 4955ed11a2..482750703d 100644 --- a/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java +++ b/app/src/main/java/com/gh/gamecenter/receiver/NetworkStateReceiver.java @@ -1,5 +1,6 @@ package com.gh.gamecenter.receiver; +import android.app.NotificationManager; import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; @@ -7,6 +8,7 @@ import android.content.Intent; import com.gh.common.exposure.meta.MetaUtil; import com.gh.common.util.CommunityHelper; import com.gh.common.util.NetworkUtils; +import com.gh.download.DownloadNotification; import com.gh.gamecenter.eventbus.EBNetworkState; import org.greenrobot.eventbus.EventBus; @@ -22,6 +24,11 @@ public class NetworkStateReceiver extends BroadcastReceiver { // 初始化开启的社区列表 if (NetworkUtils.isNetworkConnected(context)) { CommunityHelper.getAvailableCommunityList(); + + NotificationManager manager = (NotificationManager) context.getApplicationContext().getSystemService(Context.NOTIFICATION_SERVICE); + if (manager != null) { + manager.cancel(DownloadNotification.WAIT_NOTIFY_ID); + } } }