From 44c2d252180cc4892c88aa14809e19e22c2bb521 Mon Sep 17 00:00:00 2001 From: seven332 Date: Tue, 22 Mar 2016 19:44:50 +0800 Subject: [PATCH] Click downloading notification to go to the downloading item --- .../ehviewer/download/DownloadManager.java | 5 ++ .../ehviewer/download/DownloadService.java | 24 +++++----- .../ehviewer/ui/scene/DownloadsScene.java | 46 ++++++++++++++++--- 3 files changed, 57 insertions(+), 18 deletions(-) diff --git a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java index 03ce2a70c..88d706945 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadManager.java @@ -168,6 +168,11 @@ public class DownloadManager implements SpiderQueen.OnSpiderListener { return mMap.get(label); } + @Nullable + public DownloadInfo getDownloadInfo(long gid) { + return mAllInfoMap.get(gid); + } + public int getDownloadState(long gid) { DownloadInfo info = mAllInfoMap.get(gid); if (null != info) { diff --git a/app/src/main/java/com/hippo/ehviewer/download/DownloadService.java b/app/src/main/java/com/hippo/ehviewer/download/DownloadService.java index 356127488..68b70c891 100644 --- a/app/src/main/java/com/hippo/ehviewer/download/DownloadService.java +++ b/app/src/main/java/com/hippo/ehviewer/download/DownloadService.java @@ -209,15 +209,6 @@ public class DownloadService extends Service implements DownloadManager.Download stopAllIntent.setAction(ACTION_STOP_ALL); PendingIntent piStopAll = PendingIntent.getService(this, 0, stopAllIntent, 0); - Bundle bundle = new Bundle(); - bundle.putString(DownloadsScene.KEY_ACTION, DownloadsScene.ACTION_CLEAR_DOWNLOAD_SERVICE); - Intent activityIntent = new Intent(this, MainActivity.class); - activityIntent.setAction(StageActivity.ACTION_START_SCENE); - activityIntent.putExtra(StageActivity.KEY_SCENE_NAME, DownloadsScene.class.getName()); - activityIntent.putExtra(StageActivity.KEY_SCENE_ARGS, bundle); - PendingIntent piActivity = PendingIntent.getActivity(DownloadService.this, 0, - activityIntent, PendingIntent.FLAG_UPDATE_CURRENT); - mDownloadingBuilder = new NotificationCompat.Builder(getApplicationContext()) .setSmallIcon(android.R.drawable.stat_sys_download) .setOngoing(true) @@ -225,8 +216,7 @@ public class DownloadService extends Service implements DownloadManager.Download .setCategory(NotificationCompat.CATEGORY_PROGRESS) .setColor(getResources().getColor(R.color.colorPrimary)) .addAction(R.drawable.ic_pause_x24, getString(R.string.stat_download_action_stop_all), piStopAll) - .setShowWhen(false) - .setContentIntent(piActivity); + .setShowWhen(false); mDownloadingDelay = new NotificationDelay(this, mNotifyManager, mDownloadingBuilder, ID_DOWNLOADING); } @@ -297,10 +287,20 @@ public class DownloadService extends Service implements DownloadManager.Download ensureDownloadingBuilder(); + Bundle bundle = new Bundle(); + bundle.putLong(DownloadsScene.KEY_GID, info.gid); + Intent activityIntent = new Intent(this, MainActivity.class); + activityIntent.setAction(StageActivity.ACTION_START_SCENE); + activityIntent.putExtra(StageActivity.KEY_SCENE_NAME, DownloadsScene.class.getName()); + activityIntent.putExtra(StageActivity.KEY_SCENE_ARGS, bundle); + PendingIntent piActivity = PendingIntent.getActivity(DownloadService.this, 0, + activityIntent, PendingIntent.FLAG_UPDATE_CURRENT); + mDownloadingBuilder.setContentTitle(EhUtils.getSuitableTitle(info)) .setContentText(null) .setContentInfo(null) - .setProgress(0, 0, true); + .setProgress(0, 0, true) + .setContentIntent(piActivity); mDownloadingDelay.startForeground(); } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java index 47ffc7982..f8e80d530 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java @@ -102,6 +102,8 @@ public class DownloadsScene extends ToolbarScene private static final String TAG = DownloadsScene.class.getSimpleName(); + public static final String KEY_GID = "gid"; + public static final String KEY_ACTION = "action"; private static final String KEY_LABEL = "label"; @@ -133,15 +135,43 @@ public class DownloadsScene extends ToolbarScene private ShowcaseView mShowcaseView; + private int mInitPosition = -1; + @Override public int getNavCheckedItem() { return R.id.nav_downloads; } - private void handleArguments(Bundle args) { - if (args != null && ACTION_CLEAR_DOWNLOAD_SERVICE.equals(args.getString(KEY_ACTION))) { + private boolean handleArguments(Bundle args) { + if (null == args) { + return false; + } + + if (ACTION_CLEAR_DOWNLOAD_SERVICE.equals(args.getString(KEY_ACTION))) { DownloadService.clear(); } + + long gid; + if (null != mDownloadManager && -1L != (gid = args.getLong(KEY_GID, -1L))) { + DownloadInfo info = mDownloadManager.getDownloadInfo(gid); + if (null != info) { + mLabel = info.getLabel(); + updateForLabel(); + updateView(); + + // Get position + if (null != mList) { + int position = mList.indexOf(info); + if (position >= 0 && null != mRecyclerView) { + mRecyclerView.scrollToPosition(position); + } else { + mInitPosition = position; + } + } + return true; + } + } + return false; } @Override @@ -153,8 +183,6 @@ public class DownloadsScene extends ToolbarScene public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - handleArguments(getArguments()); - Context context = getContext2(); AssertUtils.assertNotNull(context); mDownloadManager = EhApplication.getDownloadManager(context); @@ -218,8 +246,10 @@ public class DownloadsScene extends ToolbarScene } private void onInit() { - mLabel = Settings.getRecentDownloadLabel(); - updateForLabel(); + if (!handleArguments(getArguments())) { + mLabel = Settings.getRecentDownloadLabel(); + updateForLabel(); + } } private void onRestore(@NonNull Bundle savedInstanceState) { @@ -274,6 +304,10 @@ public class DownloadsScene extends ToolbarScene int paddingH = context.getResources().getDimensionPixelOffset(R.dimen.list_content_margin_h); int paddingV = context.getResources().getDimensionPixelOffset(R.dimen.list_content_margin_v); mRecyclerView.setPadding(paddingV, paddingH, paddingV, paddingH); + if (mInitPosition >= 0) { + mRecyclerView.scrollToPosition(mInitPosition); + mInitPosition = -1; + } fastScroller.attachToRecyclerView(mRecyclerView); HandlerDrawable handlerDrawable = new HandlerDrawable();