Adjust StageActivity Intent logic

This commit is contained in:
seven332 committed 2016-03-06 18:43:00 +08:00
1 parent 1f05057ddd
commit 1905fa526e
5 files changed
+187 -82

No files matched your search

@@ -49,6 +49,7 @@ public class EhUrlOpener {
args.putString(GalleryListScene.KEY_ACTION, GalleryListScene.ACTION_LIST_URL_BUILDER);
args.putParcelable(GalleryListScene.KEY_LIST_URL_BUILDER, listUrlBuilder);
intent = new Intent(activity, MainActivity.class);
intent.setAction(StageActivity.ACTION_START_SCENE);
intent.putExtra(StageActivity.KEY_SCENE_NAME, GalleryListScene.class.getName());
intent.putExtra(StageActivity.KEY_SCENE_ARGS, args);
activity.startActivity(intent);
@@ -62,6 +63,7 @@ public class EhUrlOpener {
args.putLong(GalleryDetailScene.KEY_GID, result1.gid);
args.putString(GalleryDetailScene.KEY_TOKEN, result1.token);
intent = new Intent(activity, MainActivity.class);
intent.setAction(StageActivity.ACTION_START_SCENE);
intent.putExtra(StageActivity.KEY_SCENE_NAME, GalleryDetailScene.class.getName());
intent.putExtra(StageActivity.KEY_SCENE_ARGS, args);
activity.startActivity(intent);
@@ -76,6 +78,7 @@ public class EhUrlOpener {
args.putString(ProgressScene.KEY_PTOKEN, result2.pToken);
args.putLong(ProgressScene.KEY_PAGE, result2.page);
intent = new Intent(activity, MainActivity.class);
intent.setAction(StageActivity.ACTION_START_SCENE);
intent.putExtra(StageActivity.KEY_SCENE_NAME, ProgressScene.class.getName());
intent.putExtra(StageActivity.KEY_SCENE_ARGS, args);
activity.startActivity(intent);
@@ -33,6 +33,9 @@ import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.ehviewer.dao.DownloadInfo;
import com.hippo.ehviewer.ui.MainActivity;
import com.hippo.ehviewer.ui.scene.DownloadsScene;
import com.hippo.scene.StageActivity;
import com.hippo.util.ReadableTime;
import com.hippo.yorozuya.FileUtils;
import com.hippo.yorozuya.LongList;
@@ -222,13 +225,22 @@ public class DownloadService extends Service implements DownloadManager.Download
Intent stopAllIntent = new Intent(this, DownloadService.class);
stopAllIntent.setAction(ACTION_STOP_ALL);
PendingIntent piStopAll = PendingIntent.getService(this, 0, stopAllIntent, 0);
Intent activityIntent = new Intent(this, MainActivity.class);
activityIntent.setAction(StageActivity.ACTION_START_SCENE);
activityIntent.putExtra(StageActivity.KEY_SCENE_NAME, DownloadsScene.class.getName());
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)
.setAutoCancel(false)
.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);
.addAction(R.drawable.ic_pause_x24, getString(R.string.stat_download_action_stop_all), piStopAll)
.setShowWhen(false)
.setContentIntent(piActivity);
mDownloadingDelay = new NotificationDelay(this, mNotifyManager, mDownloadingBuilder, ID_DOWNLOADING);
}
@@ -241,13 +253,21 @@ public class DownloadService extends Service implements DownloadManager.Download
Intent clearIntent = new Intent(this, DownloadService.class);
clearIntent.setAction(ACTION_CLEAR);
PendingIntent piClear = PendingIntent.getService(this, 0, clearIntent, 0);
Intent activityIntent = new Intent(this, MainActivity.class);
activityIntent.setAction(StageActivity.ACTION_START_SCENE);
activityIntent.putExtra(StageActivity.KEY_SCENE_NAME, DownloadsScene.class.getName());
PendingIntent piActivity = PendingIntent.getActivity(DownloadService.this, 0,
activityIntent, PendingIntent.FLAG_UPDATE_CURRENT);
mDownloadedBuilder = new NotificationCompat.Builder(getApplicationContext())
.setSmallIcon(android.R.drawable.stat_sys_download_done)
.setLargeIcon(BitmapFactory.decodeResource(getResources(), R.mipmap.ic_launcher))
.setContentTitle(getString(R.string.stat_download_done_title))
.setDeleteIntent(piClear)
.setOngoing(false)
.setAutoCancel(true);
.setAutoCancel(true)
.setContentIntent(piActivity);
mDownloadedDelay = new NotificationDelay(this, mNotifyManager, mDownloadedBuilder, ID_DOWNLOADED);
}
@@ -291,8 +311,7 @@ public class DownloadService extends Service implements DownloadManager.Download
mDownloadingBuilder.setContentTitle(info.title)
.setContentText(null)
.setContentInfo(null)
.setProgress(0, 0, true)
.setWhen(System.currentTimeMillis());
.setProgress(0, 0, true);
mDownloadingDelay.startForeground();
}
@@ -36,6 +36,7 @@ import com.hippo.ehviewer.R;
import com.hippo.ehviewer.Settings;
import com.hippo.ehviewer.client.EhUrlOpener;
import com.hippo.ehviewer.client.EhUtils;
import com.hippo.ehviewer.ui.annotation.WholeLifeCircle;
import com.hippo.ehviewer.ui.scene.BaseScene;
import com.hippo.ehviewer.ui.scene.DownloadLabelScene;
import com.hippo.ehviewer.ui.scene.DownloadsScene;
@@ -52,6 +53,7 @@ import com.hippo.scene.Announcer;
import com.hippo.scene.SceneFragment;
import com.hippo.scene.StageActivity;
import com.hippo.util.PermissionRequester;
import com.hippo.yorozuya.ViewUtils;
public final class MainActivity extends StageActivity
implements NavigationView.OnNavigationItemSelectedListener {
@@ -61,11 +63,15 @@ public final class MainActivity extends StageActivity
private static final String KEY_NAV_CHECKED_ITEM = "nav_checked_item";
@Nullable
@WholeLifeCircle
private DrawerLayout mDrawerLayout;
@Nullable
@WholeLifeCircle
private NavigationView mNavView;
@Nullable
@WholeLifeCircle
private FrameLayout mRightDrawer;
private int mNavCheckedItem = 0;
static {
@@ -87,35 +93,23 @@ public final class MainActivity extends StageActivity
return R.id.fragment_container;
}
@Nullable
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) findViewById(R.id.draw_view);
mNavView = (NavigationView) findViewById(R.id.nav_view);
mRightDrawer = (FrameLayout) findViewById(R.id.right_drawer);
if (mNavView != null) {
mNavView.setNavigationItemSelectedListener(this);
}
if (savedInstanceState == null) {
onInit();
protected Announcer getLaunchAnnouncer() {
if (Settings.getShowWarning()) {
//setDrawerLayoutEnable(false);
return new Announcer(WarningScene.class);
} else if (!EhUtils.hasSignedIn(this)) {
//setDrawerLayoutEnable(false);
return new Announcer(LoginScene.class);
} else {
onRestore(savedInstanceState);
//setDrawerLayoutEnable(true);
Bundle args = new Bundle();
args.putString(GalleryListScene.KEY_ACTION, GalleryListScene.ACTION_HOMEPAGE);
return new Announcer(GalleryListScene.class).setArgs(args);
}
}
@Override
protected void onDestroy() {
super.onDestroy();
mDrawerLayout = null;
mNavView = null;
mRightDrawer = null;
}
private boolean handleIntent(Intent intent) {
if (intent == null) {
return false;
@@ -130,36 +124,44 @@ public final class MainActivity extends StageActivity
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (!handleIntent(intent) && intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
Toast.makeText(this, R.string.error_cannot_parse_the_url, Toast.LENGTH_SHORT).show();
protected void onUnrecognizedIntent(@Nullable Intent intent) {
if (!handleIntent(intent)) {
if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
Toast.makeText(this, R.string.error_cannot_parse_the_url, Toast.LENGTH_SHORT).show();
}
if (0 == getSceneCount()) {
finish();
}
}
}
@Nullable
@Override
protected Announcer onStartSceneFromIntent(@NonNull Class<?> clazz, @Nullable Bundle args) {
// TODO start login scene or warning scene
return super.onStartSceneFromIntent(clazz, args);
}
@Override
protected void onCreate2(@Nullable Bundle savedInstanceState) {
setContentView(R.layout.activity_main);
mDrawerLayout = (DrawerLayout) ViewUtils.$$(this, R.id.draw_view);
mNavView = (NavigationView) ViewUtils.$$(this, R.id.nav_view);
mRightDrawer = (FrameLayout) ViewUtils.$$(this, R.id.right_drawer);
if (mNavView != null) {
mNavView.setNavigationItemSelectedListener(this);
}
if (savedInstanceState == null) {
onInit();
} else {
onRestore(savedInstanceState);
}
}
private void onInit() {
Intent intent = getIntent();
if (!handleIntent(intent)) {
if (intent != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
Toast.makeText(this, R.string.error_cannot_parse_the_url, Toast.LENGTH_SHORT).show();
finish();
return;
} else {
if (Settings.getShowWarning()) {
setDrawerLayoutEnable(false);
startScene(new Announcer(WarningScene.class));
} else if (!EhUtils.hasSignedIn(this)) {
setDrawerLayoutEnable(false);
startScene(new Announcer(LoginScene.class));
} else {
setDrawerLayoutEnable(true);
Bundle args = new Bundle();
args.putString(GalleryListScene.KEY_ACTION, GalleryListScene.ACTION_HOMEPAGE);
startScene(new Announcer(GalleryListScene.class).setArgs(args));
}
}
}
// Check permission
PermissionRequester.request(this, Manifest.permission.WRITE_EXTERNAL_STORAGE,
getString(R.string.write_rationale), PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
@@ -175,6 +177,15 @@ public final class MainActivity extends StageActivity
outState.putInt(KEY_NAV_CHECKED_ITEM, mNavCheckedItem);
}
@Override
protected void onDestroy() {
super.onDestroy();
mDrawerLayout = null;
mNavView = null;
mRightDrawer = null;
}
@Override
protected void onResume() {
super.onResume();
@@ -18,12 +18,12 @@ package com.hippo.scene;
import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.support.v7.app.AppCompatActivity;
import android.text.TextUtils;
import android.util.Log;
import com.hippo.ehviewer.R;
@@ -39,6 +39,8 @@ public abstract class StageActivity extends AppCompatActivity {
private static final String TAG = StageActivity.class.getSimpleName();
public static final String ACTION_START_SCENE = "start_scene";
public static final String KEY_SCENE_NAME = "stage_activity_scene_name";
public static final String KEY_SCENE_ARGS = "stage_activity_scene_args";
@@ -64,32 +66,74 @@ public abstract class StageActivity extends AppCompatActivity {
public abstract int getContainerViewId();
/**
* @return {@code true} for start scene
*/
private boolean startSceneFromIntent(Intent intent) {
String clazzStr = intent.getStringExtra(KEY_SCENE_NAME);
if (null == clazzStr) {
return false;
}
Class clazz;
try {
clazz = Class.forName(clazzStr);
} catch (ClassNotFoundException e) {
Log.e(TAG, "Can't find class " + clazzStr, e);
return false;
}
Bundle args = intent.getBundleExtra(KEY_SCENE_ARGS);
Announcer announcer = onStartSceneFromIntent(clazz, args);
if (announcer == null) {
return false;
}
startScene(announcer);
return true;
}
/**
* Start scene from {@code Intent}, it might be not safe,
* Correct it here.
*
* @return {@code null} for do not start scene
*/
@Nullable
protected Announcer onStartSceneFromIntent(@NonNull Class<?> clazz, @Nullable Bundle args) {
return new Announcer(clazz).setArgs(args);
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
if (intent != null) {
String clazzStr = intent.getStringExtra(KEY_SCENE_NAME);
if (TextUtils.isEmpty(clazzStr)) {
return;
}
Class clazz;
try {
clazz = Class.forName(clazzStr);
} catch (ClassNotFoundException e) {
Log.e(TAG, "Can't find class " + clazzStr, e);
return;
}
Bundle args = intent.getBundleExtra(KEY_SCENE_ARGS);
startScene(new Announcer(clazz).setArgs(args));
if (intent == null || !ACTION_START_SCENE.equals(intent.getAction()) ||
!startSceneFromIntent(intent)) {
onUnrecognizedIntent(intent);
}
}
/**
* Called when launch with action {@code android.intent.action.MAIN}
*/
@Nullable
protected abstract Announcer getLaunchAnnouncer();
/**
* Can't recognize intent in first time {@code onCreate} and {@code onNewIntent},
* null included.
*/
protected void onUnrecognizedIntent(@Nullable Intent intent) {}
/**
* Call {@code setContentView} here. Do <b>NOT</b> call {@code startScene} here
*/
protected abstract void onCreate2(@Nullable Bundle savedInstanceState);
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
protected final void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState != null) {
@@ -103,6 +147,38 @@ public abstract class StageActivity extends AppCompatActivity {
} else {
((SceneApplication) getApplicationContext()).registerStageActivity(this, mStageId);
}
// Create layout
onCreate2(savedInstanceState);
Intent intent = getIntent();
if (savedInstanceState == null) {
if (intent != null) {
String action = intent.getAction();
if (Intent.ACTION_MAIN.equals(action)) {
Announcer announcer = getLaunchAnnouncer();
if (announcer != null) {
startScene(announcer);
return;
}
} else if (ACTION_START_SCENE.equals(action)) {
if (startSceneFromIntent(intent)) {
return;
}
}
}
// Can't recognize intent
onUnrecognizedIntent(intent);
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_STAGE_ID, mStageId);
outState.putStringArrayList(KEY_SCENE_TAG_LIST, mSceneTagList);
outState.putInt(KEY_NEXT_ID, mIdGenerator.getAndIncrement());
}
@Override
@@ -128,6 +204,10 @@ public abstract class StageActivity extends AppCompatActivity {
return mStageId;
}
public int getSceneCount() {
return mSceneTagList.size();
}
public int getSceneLaunchMode(Class<?> clazz) {
Integer integer = sLaunchModeMap.get(clazz);
if (integer == null) {
@@ -466,12 +546,4 @@ public abstract class StageActivity extends AppCompatActivity {
return null;
}
}
@Override
protected void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putInt(KEY_STAGE_ID, mStageId);
outState.putStringArrayList(KEY_SCENE_TAG_LIST, mSceneTagList);
outState.putInt(KEY_NEXT_ID, mIdGenerator.getAndIncrement());
}
}