Files
EhViewer/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java
T

323 lines
11 KiB
Java

/*
* Copyright 2016 Hippo Seven
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.hippo.ehviewer.ui;
import android.Manifest;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import android.os.PersistableBundle;
import android.support.annotation.IdRes;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.MenuItem;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.Toast;
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;
import com.hippo.ehviewer.ui.scene.FavoritesScene;
import com.hippo.ehviewer.ui.scene.GalleryCommentsScene;
import com.hippo.ehviewer.ui.scene.GalleryDetailScene;
import com.hippo.ehviewer.ui.scene.GalleryInfoScene;
import com.hippo.ehviewer.ui.scene.GalleryListScene;
import com.hippo.ehviewer.ui.scene.GalleryPreviewsScene;
import com.hippo.ehviewer.ui.scene.LoginScene;
import com.hippo.ehviewer.ui.scene.ProgressScene;
import com.hippo.ehviewer.ui.scene.WarningScene;
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 {
private static final int PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE = 0;
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 {
registerLaunchMode(WarningScene.class, SceneFragment.LAUNCH_MODE_SINGLE_TASK);
registerLaunchMode(LoginScene.class, SceneFragment.LAUNCH_MODE_SINGLE_TASK);
registerLaunchMode(GalleryListScene.class, SceneFragment.LAUNCH_MODE_SINGLE_TOP);
registerLaunchMode(GalleryDetailScene.class, SceneFragment.LAUNCH_MODE_STANDARD);
registerLaunchMode(GalleryInfoScene.class, SceneFragment.LAUNCH_MODE_STANDARD);
registerLaunchMode(GalleryCommentsScene.class, SceneFragment.LAUNCH_MODE_STANDARD);
registerLaunchMode(GalleryPreviewsScene.class, SceneFragment.LAUNCH_MODE_STANDARD);
registerLaunchMode(DownloadsScene.class, SceneFragment.LAUNCH_MODE_SINGLE_TASK);
registerLaunchMode(DownloadLabelScene.class, SceneFragment.LAUNCH_MODE_SINGLE_TASK);
registerLaunchMode(FavoritesScene.class, SceneFragment.LAUNCH_MODE_SINGLE_TASK);
registerLaunchMode(ProgressScene.class, SceneFragment.LAUNCH_MODE_STANDARD);
}
@Override
public int getContainerViewId() {
return R.id.fragment_container;
}
@Nullable
@Override
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 {
//setDrawerLayoutEnable(true);
Bundle args = new Bundle();
args.putString(GalleryListScene.KEY_ACTION, GalleryListScene.ACTION_HOMEPAGE);
return new Announcer(GalleryListScene.class).setArgs(args);
}
}
private boolean handleIntent(Intent intent) {
if (intent == null) {
return false;
}
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
return EhUrlOpener.openUrl(this, intent.getData().toString());
}
return false;
}
@Override
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() {
// Check permission
PermissionRequester.request(this, Manifest.permission.WRITE_EXTERNAL_STORAGE,
getString(R.string.write_rationale), PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE);
}
private void onRestore(Bundle savedInstanceState) {
mNavCheckedItem = savedInstanceState.getInt(KEY_NAV_CHECKED_ITEM);
}
@Override
public void onSaveInstanceState(Bundle outState, PersistableBundle outPersistentState) {
super.onSaveInstanceState(outState, outPersistentState);
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();
setNavCheckedItem(mNavCheckedItem);
}
@Override
public void onRequestPermissionsResult(int requestCode,
@NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == PERMISSION_REQUEST_WRITE_EXTERNAL_STORAGE) {
if (grantResults.length == 1 && grantResults[0] != PackageManager.PERMISSION_GRANTED) {
Toast.makeText(this, R.string.you_rejected_me, Toast.LENGTH_SHORT).show();
}
} else {
super.onRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
@Override
public void onSceneViewCreated(SceneFragment scene, Bundle savedInstanceState) {
super.onSceneViewCreated(scene, savedInstanceState);
if (scene instanceof BaseScene && mRightDrawer != null && mDrawerLayout != null) {
BaseScene baseScene = (BaseScene) scene;
mRightDrawer.removeAllViews();
View drawerView = baseScene.onCreateDrawerView(
getLayoutInflater(), mRightDrawer, savedInstanceState);
if (drawerView != null) {
mRightDrawer.addView(drawerView);
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.RIGHT);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED, Gravity.RIGHT);
}
}
}
@Override
public void onSceneViewDestroyed(SceneFragment scene) {
super.onSceneViewDestroyed(scene);
if (scene instanceof BaseScene) {
BaseScene baseScene = (BaseScene) scene;
baseScene.onDestroyDrawerView();
}
}
public void setDrawerLayoutEnable(boolean enable) {
if (mDrawerLayout != null) {
if (enable) {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED);
} else {
mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED);
}
}
}
public void openDrawer(int drawerGravity) {
if (mDrawerLayout != null) {
mDrawerLayout.openDrawer(drawerGravity);
}
}
public void closeDrawer(int drawerGravity) {
if (mDrawerLayout != null) {
mDrawerLayout.closeDrawer(drawerGravity);
}
}
public void toggleDrawer(int drawerGravity) {
if (mDrawerLayout != null) {
if (mDrawerLayout.isDrawerOpen(drawerGravity)) {
mDrawerLayout.closeDrawer(drawerGravity);
} else {
mDrawerLayout.openDrawer(drawerGravity);
}
}
}
public void setNavCheckedItem(@IdRes int resId) {
mNavCheckedItem = resId;
if (mNavView != null) {
if (resId == 0) {
mNavView.setCheckedItem(R.id.nav_stub);
} else {
mNavView.setCheckedItem(resId);
}
}
}
@Override
public void onBackPressed() {
if (mDrawerLayout != null && (mDrawerLayout.isDrawerOpen(Gravity.LEFT) ||
mDrawerLayout.isDrawerOpen(Gravity.RIGHT))) {
mDrawerLayout.closeDrawers();
} else {
super.onBackPressed();
}
}
@Override
public boolean onNavigationItemSelected(MenuItem item) {
// Don't select twice
if (item.isChecked()) {
return false;
}
int id = item.getItemId();
if (id == R.id.nav_homepage) {
Bundle args = new Bundle();
args.putString(GalleryListScene.KEY_ACTION, GalleryListScene.ACTION_HOMEPAGE);
startSceneFirstly(new Announcer(GalleryListScene.class)
.setArgs(args));
} else if (id == R.id.nav_whats_hot) {
} else if (id == R.id.nav_favourite) {
startScene(new Announcer(FavoritesScene.class));
} else if (id == R.id.nav_history) {
} else if (id == R.id.nav_downloads) {
startScene(new Announcer(DownloadsScene.class));
} else if (id == R.id.nav_settings) {
Intent intent = new Intent(this, SettingsActivity.class);
startActivity(intent);
}
if (id != R.id.nav_stub && mDrawerLayout != null) {
mDrawerLayout.closeDrawers();
}
return true;
}
}