diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 9b62f8e0d0..c31c4fa763 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -196,10 +196,17 @@ - + + + + + + diff --git a/app/src/main/java/com/gh/common/util/NotificationUtils.java b/app/src/main/java/com/gh/common/util/NotificationUtils.java new file mode 100644 index 0000000000..1e3c94ad6c --- /dev/null +++ b/app/src/main/java/com/gh/common/util/NotificationUtils.java @@ -0,0 +1,82 @@ +package com.gh.common.util; + +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import android.support.v4.app.NotificationCompat; + +import com.gh.download.DownloadEntity; +import com.gh.download.DownloadManager; +import com.gh.download.DownloadStatus; +import com.gh.gamecenter.R; + +/** + * Created by LGT on 2016/10/10. + */ +public class NotificationUtils { + + public static void showDownloadDoneNotification(Context context, DownloadEntity downloadEntity, int flag) { + NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + Intent intent = new Intent(); + intent.putExtra("path", downloadEntity.getPath()); + intent.setAction("com.gh.gamecenter.INSTALL"); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, flag, + intent, PendingIntent.FLAG_UPDATE_CURRENT); + String text; + String title; + if (downloadEntity.isPluggable()) { + text = "下载完成,点击继续插件化。"; + title = downloadEntity.getName() + "-" + + PlatformUtils.getInstance(context).getPlatformName(downloadEntity.getPlatform()); + } else { + if (downloadEntity.isPlugin()) { + text = downloadEntity.getName() + "-" + + PlatformUtils.getInstance(context).getPlatformName(downloadEntity.getPlatform()) + " 下载完成"; + } else { + text = downloadEntity.getName() + " 下载完成"; + } + title = "点击查看详情"; + } + Notification notification = new NotificationCompat.Builder(context) + .setSmallIcon(R.drawable.logo) + .setTicker(text) + .setContentTitle(title) + .setContentText(text) + .setContentIntent(pendingIntent).build(); + notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音 + notification.flags |= Notification.FLAG_AUTO_CANCEL; // // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。 + nManager.notify(flag, notification); + } + + public static void showDownloadingNotification(Context context) { + int downloadingSize = 0; + for (DownloadEntity entity : DownloadManager.getInstance(context).getAll()) { + if (entity.getStatus().equals(DownloadStatus.downloading) + || entity.getStatus().equals(DownloadStatus.waiting)) { + downloadingSize++; + } + } + if (downloadingSize == 0) { + NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + nManager.cancel(0x123); + } else { + NotificationManager nManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE); + Intent intent = new Intent(); + intent.setAction("com.gh.gamecenter.DOWNLOAD"); + PendingIntent pendingIntent = PendingIntent.getBroadcast(context, 0x123, + intent, PendingIntent.FLAG_UPDATE_CURRENT); + Notification notification = new NotificationCompat.Builder(context) + .setSmallIcon(R.drawable.logo) + .setTicker("你有" + downloadingSize + "个下载正在进行中") + .setContentTitle("点击查看详情") + .setContentText("你有" + downloadingSize + "个下载正在进行中") + .setContentIntent(pendingIntent).build(); + notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音 + notification.flags |= Notification.FLAG_NO_CLEAR; // 通知无法手动清除 + nManager.notify(0x123, notification); + } + } + +} diff --git a/app/src/main/java/com/gh/download/DownloadManager.java b/app/src/main/java/com/gh/download/DownloadManager.java index 34e5c806ff..c6f20b11b9 100644 --- a/app/src/main/java/com/gh/download/DownloadManager.java +++ b/app/src/main/java/com/gh/download/DownloadManager.java @@ -66,7 +66,6 @@ public class DownloadManager { } } }; - context.startService(new Intent(context, DownloadService.class)); } public void put(String url, long time) { diff --git a/app/src/main/java/com/gh/download/DownloadService.java b/app/src/main/java/com/gh/download/DownloadService.java index 1988237b40..b6e95daf43 100644 --- a/app/src/main/java/com/gh/download/DownloadService.java +++ b/app/src/main/java/com/gh/download/DownloadService.java @@ -1,21 +1,30 @@ package com.gh.download; import android.annotation.SuppressLint; +import android.app.NotificationManager; import android.app.Service; +import android.content.Context; import android.content.Intent; import android.os.Handler; import android.os.IBinder; +import android.support.v4.util.ArrayMap; import com.gh.common.constant.Constants; +import com.gh.common.util.NotificationUtils; import com.gh.common.util.Utils; import com.gh.gamecenter.eventbus.EBDownloadStatus; +import java.util.ArrayList; import java.util.Map.Entry; import de.greenrobot.event.EventBus; public class DownloadService extends Service { + private int mFlag = (int) System.currentTimeMillis() / 1000; + + private ArrayMap flagMap = new ArrayMap<>(); + @Override public IBinder onBind(Intent intent) { return null; @@ -24,16 +33,29 @@ public class DownloadService extends Service { @Override public void onCreate() { super.onCreate(); + Utils.log(DownloadService.class.getSimpleName(), "onCreate"); + NotificationUtils.showDownloadingNotification(this); + ArrayList doneList = new ArrayList<>(); + for (DownloadEntity downloadEntity : DownloadManager.getInstance(this).getAll()) { + if (downloadEntity.getStatus().equals(DownloadStatus.done)) { + doneList.add(downloadEntity); + } + } + if (doneList.size() != 0) { + for (DownloadEntity downloadEntity : doneList) { + mFlag++; + flagMap.put(downloadEntity.getUrl(), mFlag); + NotificationUtils.showDownloadDoneNotification(this, downloadEntity, mFlag); + } + } } @Override public int onStartCommand(Intent intent, int flags, int startId) { Utils.log(DownloadService.class.getSimpleName(), "onStartCommand"); if (intent != null && intent.getExtras() != null) { - DownloadStatus status = DownloadStatus.valueOf(intent - .getStringExtra(Constants.KEY_DOWNLOAD_ACTION)); - DownloadEntity entry = (DownloadEntity) intent - .getSerializableExtra(Constants.KEY_DOWNLOAD_ENTRY); + DownloadStatus status = DownloadStatus.valueOf(intent.getStringExtra(Constants.KEY_DOWNLOAD_ACTION)); + DownloadEntity entry = (DownloadEntity) intent.getSerializableExtra(Constants.KEY_DOWNLOAD_ENTRY); switch (status) { case add: addDownload(entry); @@ -49,16 +71,13 @@ public class DownloadService extends Service { case cancel: cancelDownload(entry); break; - default: - break; } } return super.onStartCommand(intent, flags, startId); } private synchronized void addDownload(DownloadEntity entry) { - DownloadEntity downloadEntity = DownloadDao.getInstance(this).get( - entry.getUrl()); + DownloadEntity downloadEntity = DownloadDao.getInstance(this).get(entry.getUrl()); // 数据库下载历史中有这个任务的下载记录 if (downloadEntity != null) { entry = downloadEntity; @@ -79,6 +98,8 @@ public class DownloadService extends Service { } Utils.log(DownloadService.class.getSimpleName(), "addDownload==>" + entry); EventBus.getDefault().post(new EBDownloadStatus("download")); + + NotificationUtils.showDownloadingNotification(this); } private synchronized void startDownload(DownloadEntity entry) { @@ -103,8 +124,7 @@ public class DownloadService extends Service { private synchronized void pauseDownload(DownloadEntity entry) { if (entry != null) { - DownloadTask task = DataChanger.getInstance().getDownloadingTasks() - .get(entry.getUrl()); + DownloadTask task = DataChanger.getInstance().getDownloadingTasks().get(entry.getUrl()); if (task != null) { // 1.改任务队列的状态 entry.setStatus(DownloadStatus.pause); @@ -117,6 +137,8 @@ public class DownloadService extends Service { DataChanger.getInstance().notifyDataChanged(entry); } Utils.log(DownloadService.class.getSimpleName(), "pauseDownload==>" + entry); + + NotificationUtils.showDownloadingNotification(this); } private synchronized void cancelDownload(DownloadEntity downloadEntity) { @@ -137,6 +159,12 @@ public class DownloadService extends Service { downloadEntity.getPlatform(), downloadEntity.getUrl()); status.setPluggable(downloadEntity.isPluggable()); EventBus.getDefault().post(status); + + Integer flag = flagMap.get(downloadEntity.getUrl()); + if (flag != null) { + NotificationManager nManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + nManager.cancel(flag); + } } @SuppressLint("HandlerLeak") @@ -150,10 +178,19 @@ public class DownloadService extends Service { case downloading: break; case done: + NotificationUtils.showDownloadingNotification(DownloadService.this); + mFlag++; + flagMap.put(entry.getUrl(), mFlag); + NotificationUtils.showDownloadDoneNotification(DownloadService.this, entry, mFlag); + removeAndCheckNext(entry); + break; case pause: case cancel: + removeAndCheckNext(entry); + break; case timeout: case neterror: + NotificationUtils.showDownloadingNotification(DownloadService.this); removeAndCheckNext(entry); break; default: diff --git a/app/src/main/java/com/gh/gamecenter/DownloadManagerActivity.java b/app/src/main/java/com/gh/gamecenter/DownloadManagerActivity.java index 1f40b43cec..ecc82f3386 100644 --- a/app/src/main/java/com/gh/gamecenter/DownloadManagerActivity.java +++ b/app/src/main/java/com/gh/gamecenter/DownloadManagerActivity.java @@ -1,6 +1,7 @@ package com.gh.gamecenter; import android.os.Bundle; +import android.os.Handler; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentPagerAdapter; @@ -19,7 +20,7 @@ import com.gh.gamecenter.download.GameDownLoadFragment; import com.gh.gamecenter.download.GameUpdateFragment; import com.gh.gamecenter.eventbus.EBDownloadChanged; import com.gh.gamecenter.eventbus.EBMiPush; -import com.gh.gamecenter.eventbus.EBPerformClick; +import com.gh.gamecenter.eventbus.EBSkip; import com.gh.gamecenter.eventbus.EBUISwitch; import com.gh.gamecenter.manager.PackageManager; @@ -46,6 +47,8 @@ public class DownloadManagerActivity extends BaseFragmentActivity implements private int width; + private Handler handler = new Handler(); + @Override protected void onSaveInstanceState(Bundle outState) { super.onSaveInstanceState(outState); @@ -238,11 +241,20 @@ public class DownloadManagerActivity extends BaseFragmentActivity implements } } } - - public void onEventMainThread(EBPerformClick click) { - if (click.getFrom().equals("update")) { - downloadmanager_ll_download.performClick(); + + public void onEventMainThread(EBSkip skip) { + if ("DownloadManagerActivity".equals(skip.getType())) { + if (skip.getCurrentItem() == 0) { + handler.postDelayed(runnable, 300); + } } } + Runnable runnable = new Runnable() { + @Override + public void run() { + downloadmanager_ll_download.performClick(); + } + }; + } \ No newline at end of file diff --git a/app/src/main/java/com/gh/gamecenter/MainActivity.java b/app/src/main/java/com/gh/gamecenter/MainActivity.java index b8f8d56d64..87f21cafa2 100644 --- a/app/src/main/java/com/gh/gamecenter/MainActivity.java +++ b/app/src/main/java/com/gh/gamecenter/MainActivity.java @@ -1,9 +1,6 @@ package com.gh.gamecenter; import android.app.Dialog; -import android.app.Notification; -import android.app.NotificationManager; -import android.app.PendingIntent; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; @@ -17,7 +14,6 @@ import android.os.SystemClock; import android.provider.Settings; import android.support.v4.app.Fragment; import android.support.v4.app.FragmentTransaction; -import android.support.v4.app.NotificationCompat; import android.text.Html; import android.text.Spanned; import android.text.TextUtils; @@ -47,7 +43,6 @@ import com.gh.common.util.NetworkUtils; import com.gh.common.util.PackageUtils; import com.gh.common.util.PlatformUtils; import com.gh.common.util.RandomUtils; -import com.gh.common.util.RunningUtils; import com.gh.common.util.SpeedUtils; import com.gh.common.util.TokenUtils; import com.gh.common.util.TrafficUtils; @@ -154,6 +149,7 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene return; } else if (DownloadStatus.neterror.equals(downloadEntity.getStatus()) || DownloadStatus.timeout.equals(downloadEntity.getStatus())) { + toast("网络不稳定,下载任务已暂停"); uploadNeterrorLog(downloadEntity); } if (downloadEntity.getName().contains("光环助手") && isShowDownload) { @@ -169,60 +165,32 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene } else { statDoneEvent(downloadEntity); - if (RunningUtils.isApplicationBroughtToBackground(getApplicationContext())) { - // 应用程序在后台,如果是插件化下载,则弹出notification提示用户 + String platform = PlatformUtils.getInstance(getApplicationContext()) + .getPlatformName(downloadEntity.getPlatform()); + if (platform != null) { if (downloadEntity.isPluggable()) { - String path = downloadEntity.getPath(); - String platform = PlatformUtils.getInstance(getApplicationContext()) - .getPlatformName(downloadEntity.getPlatform()); - String title = downloadEntity.getName() + " - " + platform; - - Intent intent = new Intent(); - intent.putExtra("path", path); - intent.setAction("com.gh.gamecenter.UNINSTALL"); - PendingIntent pendingIntent = PendingIntent.getBroadcast( - MainActivity.this, 0, intent, PendingIntent.FLAG_ONE_SHOT); - Notification notification = new NotificationCompat.Builder(MainActivity.this) - .setSmallIcon(R.drawable.logo) - .setTicker(title) - .setContentTitle(title) - .setContentText("下载完成,点击继续插件化。") - .setContentIntent(pendingIntent) - .build(); - notification.defaults = Notification.DEFAULT_SOUND;// 添加系统默认声音 - notification.flags |= Notification.FLAG_AUTO_CANCEL; // FLAG_AUTO_CANCEL表明当通知被用户点击时,通知将被清除。 - NotificationManager notificationManager = (NotificationManager) getSystemService( - Context.NOTIFICATION_SERVICE); - notificationManager.notify(((int) System.currentTimeMillis() / 1000), notification); - } - } else { - String platform = PlatformUtils.getInstance(getApplicationContext()) - .getPlatformName(downloadEntity.getPlatform()); - if (platform != null) { - if (downloadEntity.isPluggable()) { - // 弹出插件化提示框 - EventBus.getDefault().post(new EBShowDialog("plugin", downloadEntity.getPath())); - } else if (platform.equals("官方版")) { - toast(downloadEntity.getName() + " - 下载完成"); - } else { - toast(downloadEntity.getName() + " - " + platform + " - 下载完成"); - } + // 弹出插件化提示框 + EventBus.getDefault().post(new EBShowDialog("plugin", downloadEntity.getPath())); + } else if (downloadEntity.isPlugin()) { + toast(downloadEntity.getName() + " - " + platform + " - 下载完成"); } else { toast(downloadEntity.getName() + " - 下载完成"); } - if (!downloadEntity.isPluggable()) { - // 是否是自动安装 - if (sp.getBoolean("autoinstall", true)) { - if (FileUtils.isEmptyFile(downloadEntity.getPath())) { - toast("解析包出错(可能被误删了),请重新下载"); - DownloadManager.getInstance(MainActivity.this).cancel(downloadEntity.getUrl()); + } else { + toast(downloadEntity.getName() + " - 下载完成"); + } + if (!downloadEntity.isPluggable()) { + // 是否是自动安装 + if (sp.getBoolean("autoinstall", true)) { + if (FileUtils.isEmptyFile(downloadEntity.getPath())) { + toast("解析包出错(可能被误删了),请重新下载"); + DownloadManager.getInstance(MainActivity.this).cancel(downloadEntity.getUrl()); + } else { + if (PackageUtils.isCanLaunchSetup(getApplicationContext(), downloadEntity.getPath())) { + startActivity(PackageUtils.getInstallIntent(downloadEntity.getPath())); } else { - if (PackageUtils.isCanLaunchSetup(getApplicationContext(), downloadEntity.getPath())) { - startActivity(PackageUtils.getInstallIntent(downloadEntity.getPath())); - } else { - // 弹出卸载提示框 - EventBus.getDefault().post(new EBShowDialog("plugin", downloadEntity.getPath())); - } + // 弹出卸载提示框 + EventBus.getDefault().post(new EBShowDialog("plugin", downloadEntity.getPath())); } } } @@ -462,9 +430,6 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene // 初始化PlatformUtils PlatformUtils.getInstance(getApplicationContext()); - // 解决助手奔溃后导致的下载状态保留问题 - DownloadManager.getInstance(this).checkAll(); - // 添加观察者 DownloadManager.getInstance(this).addObserver(dataWatcher); @@ -1019,8 +984,11 @@ public class MainActivity extends BaseFragmentActivity implements OnClickListene toIntent.putExtra("newsId", getIntent().getExtras().getString("newsId")); toIntent.putExtra("entrance", getIntent().getExtras().getString("entrance")); } else if("DownloadManagerActivity".equals(to)) { - toIntent.putExtra("packageName" , getIntent().getExtras().getString("packageName")); - toIntent.putExtra("currentItem" , 1); + String packageName = getIntent().getExtras().getString("packageName"); + if (packageName != null) { + toIntent.putExtra("packageName" , getIntent().getExtras().getString("packageName")); + toIntent.putExtra("currentItem" , 1); + } } else if ("GameDetailsActivity".equals(to) || "GameDetailActivity".equals(to)) { toIntent.putExtra("gameId", getIntent().getExtras().getString("gameId")); toIntent.putExtra("entrance", getIntent().getExtras().getString("entrance")); diff --git a/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java b/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java index 040d54d29f..6e284ae8b0 100644 --- a/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java +++ b/app/src/main/java/com/gh/gamecenter/SplashScreenActivity.java @@ -26,6 +26,8 @@ import com.gh.common.constant.Config; import com.gh.common.util.FileUtils; import com.gh.common.util.PackageUtils; import com.gh.common.util.Utils; +import com.gh.download.DownloadManager; +import com.gh.download.DownloadService; import com.gh.gamecenter.db.info.FilterInfo; import com.gh.gamecenter.eventbus.EBReuse; import com.gh.gamecenter.manager.DataCollectionManager; @@ -226,6 +228,12 @@ public class SplashScreenActivity extends BaseActivity { */ DataCollectionManager.getInstance(getApplicationContext()).upload(); + // 解决助手奔溃后导致的下载状态保留问题 + DownloadManager.getInstance(this).checkAll(); + + // 开启下载服务 + startService(new Intent(this, DownloadService.class)); + // 不是第一次启动 if (!sp.getBoolean("isNewFirstLaunch", true)) { int height = sp.getInt("actionbar_height", 0); diff --git a/app/src/main/java/com/gh/gamecenter/download/GameUpdateAdapter.java b/app/src/main/java/com/gh/gamecenter/download/GameUpdateAdapter.java index baf30e6402..e9b96fc411 100644 --- a/app/src/main/java/com/gh/gamecenter/download/GameUpdateAdapter.java +++ b/app/src/main/java/com/gh/gamecenter/download/GameUpdateAdapter.java @@ -2,7 +2,6 @@ package com.gh.gamecenter.download; import android.content.Context; import android.graphics.Color; -import android.os.Handler; import android.support.v4.util.ArrayMap; import android.support.v7.widget.RecyclerView; import android.text.TextUtils; @@ -34,7 +33,7 @@ import com.gh.gamecenter.R; import com.gh.gamecenter.db.info.ConcernInfo; import com.gh.gamecenter.entity.GameUpdateEntity; import com.gh.gamecenter.eventbus.EBDownloadChanged; -import com.gh.gamecenter.eventbus.EBPerformClick; +import com.gh.gamecenter.eventbus.EBSkip; import com.gh.gamecenter.manager.ConcernManager; import com.gh.gamecenter.manager.DataCollectionManager; import com.gh.gamecenter.manager.PackageManager; @@ -73,8 +72,6 @@ public class GameUpdateAdapter extends RecyclerView.Adapter getUpdateList() { return updateList; } diff --git a/app/src/main/java/com/gh/gamecenter/eventbus/EBPerformClick.java b/app/src/main/java/com/gh/gamecenter/eventbus/EBPerformClick.java deleted file mode 100644 index 6a117da8b4..0000000000 --- a/app/src/main/java/com/gh/gamecenter/eventbus/EBPerformClick.java +++ /dev/null @@ -1,20 +0,0 @@ -package com.gh.gamecenter.eventbus; - -public class EBPerformClick { - - private String from; - - public EBPerformClick(String from) { - super(); - this.from = from; - } - - public String getFrom() { - return from; - } - - public void setFrom(String from) { - this.from = from; - } - -} diff --git a/app/src/main/java/com/gh/gamecenter/receiver/DownloadReceiver.java b/app/src/main/java/com/gh/gamecenter/receiver/DownloadReceiver.java new file mode 100644 index 0000000000..5daaea87da --- /dev/null +++ b/app/src/main/java/com/gh/gamecenter/receiver/DownloadReceiver.java @@ -0,0 +1,56 @@ +package com.gh.gamecenter.receiver; + +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; + +import com.gh.common.util.RunningUtils; +import com.gh.gamecenter.DownloadManagerActivity; +import com.gh.gamecenter.SplashScreenActivity; +import com.gh.gamecenter.eventbus.EBSkip; + +import de.greenrobot.event.EventBus; + +/** + * Created by LGT on 2016/10/10. + */ +public class DownloadReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + + if (RunningUtils.isRunning(context)) { + // 助手正在运行 + if (RunningUtils.isEqualsTop(context, DownloadManagerActivity.class.getName())) { + if (RunningUtils.isApplicationBroughtToBackground(context)) { + // 这里是指从后台返回到前台 前两个的是关键 + Intent intent2 = new Intent(); + intent2.setAction(Intent.ACTION_MAIN); + intent2.addCategory(Intent.CATEGORY_LAUNCHER); + intent2.setComponent(new ComponentName(context, DownloadManagerActivity.class)); + intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT + | Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); + context.startActivity(intent2); + } + // 切换到游戏下载fragment + EventBus.getDefault().post(new EBSkip("DownloadManagerActivity", 0)); + } else { + Intent intent2 = new Intent(context, DownloadManagerActivity.class); + intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent2); + } + } else { + // 助手未在运行 + Intent intent2 = new Intent(context, SplashScreenActivity.class); + intent2.setAction(Intent.ACTION_MAIN); + intent2.addCategory(Intent.CATEGORY_LAUNCHER); + intent2.putExtra("to", "DownloadManagerActivity"); + intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent2); + } + + } + +} diff --git a/app/src/main/java/com/gh/gamecenter/receiver/InstallReceiver.java b/app/src/main/java/com/gh/gamecenter/receiver/InstallReceiver.java new file mode 100644 index 0000000000..c487fba67d --- /dev/null +++ b/app/src/main/java/com/gh/gamecenter/receiver/InstallReceiver.java @@ -0,0 +1,60 @@ +package com.gh.gamecenter.receiver; + +import android.content.BroadcastReceiver; +import android.content.ComponentName; +import android.content.Context; +import android.content.Intent; + +import com.gh.common.util.PackageUtils; +import com.gh.common.util.RunningUtils; +import com.gh.gamecenter.DownloadManagerActivity; +import com.gh.gamecenter.SplashScreenActivity; +import com.gh.gamecenter.eventbus.EBMiPush; + +import de.greenrobot.event.EventBus; + +/** + * Created by LGT on 2016/10/10. + */ +public class InstallReceiver extends BroadcastReceiver { + + @Override + public void onReceive(Context context, Intent intent) { + String path = intent.getStringExtra("path"); + if (PackageUtils.isCanLaunchSetup(context, path)) { + context.startActivity(PackageUtils.getInstallIntent(path)); + } else { + if (RunningUtils.isRunning(context)) { + if (RunningUtils.isEqualsTop(context, DownloadManagerActivity.class.getName())) { + // 这里是指从后台返回到前台 前两个的是关键 + Intent intent2 = new Intent(); + intent2.setAction(Intent.ACTION_MAIN); + intent2.addCategory(Intent.CATEGORY_LAUNCHER); + intent2.setComponent(new ComponentName(context, DownloadManagerActivity.class)); + intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT + | Intent.FLAG_ACTIVITY_NEW_TASK + | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); + context.startActivity(intent2); + EventBus.getDefault().post(new EBMiPush("plugin_install", path)); + } else { + Intent intent2 = new Intent(context, + DownloadManagerActivity.class); + intent2.putExtra("currentItem", 0); + intent2.putExtra("path", path); + intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent2); + } + } else { + // 应用未在运行 + Intent intent2 = new Intent(context, SplashScreenActivity.class); + intent2.setAction(Intent.ACTION_MAIN); + intent2.addCategory(Intent.CATEGORY_LAUNCHER); + intent2.putExtra("from", "plugin_install"); + intent2.putExtra("path", path); + intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent2); + } + } + } + +} diff --git a/app/src/main/java/com/gh/gamecenter/receiver/UninstallReceiver.java b/app/src/main/java/com/gh/gamecenter/receiver/UninstallReceiver.java deleted file mode 100644 index 689f7a8143..0000000000 --- a/app/src/main/java/com/gh/gamecenter/receiver/UninstallReceiver.java +++ /dev/null @@ -1,77 +0,0 @@ -package com.gh.gamecenter.receiver; - -import android.app.ActivityManager; -import android.app.ActivityManager.RunningTaskInfo; -import android.content.BroadcastReceiver; -import android.content.ComponentName; -import android.content.Context; -import android.content.Intent; - -import com.gh.common.util.RunningUtils; -import com.gh.gamecenter.DownloadManagerActivity; -import com.gh.gamecenter.SplashScreenActivity; -import com.gh.gamecenter.eventbus.EBMiPush; - -import java.util.List; - -import de.greenrobot.event.EventBus; - -public class UninstallReceiver extends BroadcastReceiver { - - @Override - public void onReceive(Context context, Intent intent) { - - String path = intent.getStringExtra("path"); - if (RunningUtils.isRunning(context)) { - if (RunningUtils.isEqualsTop(context, - DownloadManagerActivity.class.getName())) { - ActivityManager manager = (ActivityManager) context - .getSystemService(Context.ACTIVITY_SERVICE); - List task_info = manager.getRunningTasks(20); - String packageName = context.getPackageName(); - for (int i = 0; i < task_info.size(); i++) { - if (packageName.equals(task_info.get(i).topActivity - .getPackageName())) { - String className = task_info.get(i).topActivity - .getClassName(); - // 这里是指从后台返回到前台 前两个的是关键 - Intent intent2 = new Intent(); - intent2.setAction(Intent.ACTION_MAIN); - intent2.addCategory(Intent.CATEGORY_LAUNCHER); - try { - intent2.setComponent(new ComponentName(context, - Class.forName(className))); - } catch (ClassNotFoundException e) { - - e.printStackTrace(); - } - // intent2.setClass(context, Class.forName(className)); - intent2.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT - | Intent.FLAG_ACTIVITY_NEW_TASK - | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED); - context.startActivity(intent2); - } - } - EventBus.getDefault().post(new EBMiPush("plugin_install", path)); - } else { - Intent intent2 = new Intent(context, - DownloadManagerActivity.class); - intent2.putExtra("currentItem", 0); - intent2.putExtra("path", path); - intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - context.startActivity(intent2); - } - - } else { - // 应用未在运行 - Intent intent2 = new Intent(context, SplashScreenActivity.class); - intent2.setAction(Intent.ACTION_MAIN); - intent2.addCategory(Intent.CATEGORY_LAUNCHER); - intent2.putExtra("from", "plugin_install"); - intent2.putExtra("path", path); - intent2.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - context.startActivity(intent2); - } - } - -}