Add open eh url

This commit is contained in:
seven332 committed 2016-02-06 20:26:01 +08:00
1 parent 67e3c8dc60
commit 3ef767791d
16 files changed
+668 -142

No files matched your search

@@ -1,120 +0,0 @@
/*
* 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;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.os.Bundle;
import android.support.customtabs.CustomTabsIntent;
import android.text.TextUtils;
import android.widget.Toast;
import com.hippo.ehviewer.client.data.ListUrlBuilder;
import com.hippo.ehviewer.client.parser.GalleryDetailUrlParser;
import com.hippo.ehviewer.client.parser.GalleryListUrlParser;
import com.hippo.ehviewer.ui.MainActivity;
import com.hippo.ehviewer.ui.scene.GalleryDetailScene;
import com.hippo.ehviewer.ui.scene.GalleryListScene;
import com.hippo.scene.StageActivity;
import com.hippo.util.CustomTabsHelper;
public final class OpenUrlHelper {
private OpenUrlHelper() {
}
public static void openUrl(Activity activity, String url, boolean inApp, boolean customTabs) {
if (TextUtils.isEmpty(url)) {
return;
}
Intent intent;
Uri uri = Uri.parse(url);
if (inApp) {
ListUrlBuilder listUrlBuilder = GalleryListUrlParser.parse(url);
if (listUrlBuilder != null) {
Bundle args = new Bundle();
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.putExtra(StageActivity.KEY_SCENE_NAME, GalleryListScene.class.getName());
intent.putExtra(StageActivity.KEY_SCENE_ARGS, args);
activity.startActivity(intent);
return;
}
int gid = -1;
String token = null;
int page = -1;
GalleryDetailUrlParser.Result result1 = GalleryDetailUrlParser.parse(url);
if (result1 != null) {
gid = result1.gid;
token = result1.token;
} else {
/* TODO
GalleryUrlParser.Result result2 = GalleryUrlParser.parse(url);
if (result2 != null) {
gid = result2.gid;
token = result2.token;
page = result2.index;
}
*/
}
if (gid != -1 && token != null) {
Bundle args = new Bundle();
args.putString(GalleryDetailScene.KEY_ACTION, GalleryDetailScene.ACTION_GID_TOKEN);
args.putInt(GalleryDetailScene.KEY_GID, gid);
args.putString(GalleryDetailScene.KEY_TOKEN, token);
args.putInt(GalleryDetailScene.KEY_PAGE, page);
intent = new Intent(activity, MainActivity.class);
intent.putExtra(StageActivity.KEY_SCENE_NAME, GalleryDetailScene.class.getName());
intent.putExtra(StageActivity.KEY_SCENE_ARGS, args);
activity.startActivity(intent);
return;
}
}
// CustomTabs
if (customTabs) {
String packageName = CustomTabsHelper.getPackageNameToUseFixed(activity);
if (packageName != null) {
new CustomTabsIntent.Builder()
.setToolbarColor(activity.getResources().getColor(R.color.colorPrimary))
.setShowTitle(true)
.build()
.launchUrl(activity, uri);
return;
}
}
// Intent.ACTION_VIEW
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
PackageManager pm = activity.getPackageManager();
ResolveInfo ri = pm.resolveActivity(intent, 0);
if (ri != null) {
activity.startActivity(intent);
return;
}
Toast.makeText(activity, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
}
}
@@ -0,0 +1,73 @@
/*
* 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;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.net.Uri;
import android.support.customtabs.CustomTabsIntent;
import android.text.TextUtils;
import android.widget.Toast;
import com.hippo.ehviewer.client.EhUrlOpener;
import com.hippo.util.CustomTabsHelper;
public final class UrlOpener {
private UrlOpener() {
}
public static void openUrl(Activity activity, String url, boolean ehUrl, boolean customTabs) {
if (TextUtils.isEmpty(url)) {
return;
}
Intent intent;
Uri uri = Uri.parse(url);
if (ehUrl && EhUrlOpener.openUrl(activity, url)) {
return;
}
// CustomTabs
if (customTabs) {
String packageName = CustomTabsHelper.getPackageNameToUseFixed(activity);
if (packageName != null) {
new CustomTabsIntent.Builder()
.setToolbarColor(activity.getResources().getColor(R.color.colorPrimary))
.setShowTitle(true)
.build()
.launchUrl(activity, uri);
return;
}
}
// Intent.ACTION_VIEW
intent = new Intent(Intent.ACTION_VIEW);
intent.setData(uri);
PackageManager pm = activity.getPackageManager();
ResolveInfo ri = pm.resolveActivity(intent, 0);
if (ri != null) {
activity.startActivity(intent);
return;
}
Toast.makeText(activity, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show();
}
}
@@ -43,6 +43,7 @@ public class EhClient {
public static final int METHOD_GET_LARGE_PREVIEW_SET = 3;
public static final int METHOD_GET_RATE_GALLERY = 4;
public static final int METHOD_GET_COMMENT_GALLERY = 5;
public static final int METHOD_GET_GALLERY_TOKEN = 6;
private final ThreadPoolExecutor mRequestThreadPool;
private final OkHttpClient mOkHttpClient;
@@ -173,6 +174,17 @@ public class EhClient {
}
}
private Object getGalleryToken(EhConfig ehConfig, Object... params) throws Exception {
Call call = EhEngine.prepareGetGalleryToken(mOkHttpClient, ehConfig,
(Integer) params[0], (String) params[1], (Integer) params[2]);
if (!mStop) {
mCall = call;
return EhEngine.doGetGalleryToken(call);
} else {
throw new CancelledException();
}
}
@Override
protected Object doInBackground(Object... params) {
try {
@@ -189,6 +201,8 @@ public class EhClient {
return rateGallery(mEhConfig, params);
case METHOD_GET_COMMENT_GALLERY:
return commentGallery(mEhConfig, params);
case METHOD_GET_GALLERY_TOKEN:
return getGalleryToken(mEhConfig, params);
default:
return new IllegalStateException("Can't detect method " + mMethod);
}
@@ -26,8 +26,10 @@ import com.hippo.ehviewer.client.data.GalleryInfo;
import com.hippo.ehviewer.client.data.LargePreviewSet;
import com.hippo.ehviewer.client.exception.CancelledException;
import com.hippo.ehviewer.client.exception.EhException;
import com.hippo.ehviewer.client.exception.ParseException;
import com.hippo.ehviewer.client.parser.GalleryDetailParser;
import com.hippo.ehviewer.client.parser.GalleryListParser;
import com.hippo.ehviewer.client.parser.GalleryTokenApiParser;
import com.hippo.ehviewer.client.parser.ParserUtils;
import com.hippo.ehviewer.client.parser.RateGalleryParser;
import com.hippo.ehviewer.client.parser.SignInParser;
@@ -75,7 +77,7 @@ public class EhEngine {
throw new EhException("Sad Panda");
}
if (body != null && !body.contains("<")) {
if (e instanceof ParseException && body != null && !body.contains("<")) {
throw new EhException(body);
}
@@ -342,5 +344,47 @@ public class EhEngine {
}
}
/**
*
* {
* "method": "gtoken",
* "pagelist": [
* [902127,"5a4ee2caff",3]
* ]
* }
*/
public static Call prepareGetGalleryToken(OkHttpClient okHttpClient,
EhConfig ehConfig, int gid, String gtoken, int page) throws JSONException {
JSONObject json = new JSONObject()
.put("method", "gtoken")
.put("pagelist", new JSONArray().put(
new JSONArray().put(gid).put(gtoken).put(page + 1)));
final RequestBody body = RequestBody.create(JSON, json.toString());
String url = EhUrl.API_EX;
Log.d(TAG, url);
Request request = new EhRequestBuilder(url, ehConfig)
.post(body)
.build();
return okHttpClient.newCall(request);
}
public static String doGetGalleryToken(Call call) throws Exception {
String body = null;
Headers headers = null;
int code = -1;
try {
Response response = call.execute();
code = response.code();
headers = response.headers();
body = response.body().string();
return GalleryTokenApiParser.parse(body);
} catch (Exception e) {
throwException(call, code, headers, body, e);
throw e;
}
}
// TODO Add get gallery api info
}
@@ -0,0 +1,84 @@
/*
* 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.client;
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.text.TextUtils;
import com.hippo.ehviewer.client.data.ListUrlBuilder;
import com.hippo.ehviewer.client.parser.GalleryDetailUrlParser;
import com.hippo.ehviewer.client.parser.GalleryListUrlParser;
import com.hippo.ehviewer.client.parser.GalleryUrlParser;
import com.hippo.ehviewer.ui.MainActivity;
import com.hippo.ehviewer.ui.scene.GalleryDetailScene;
import com.hippo.ehviewer.ui.scene.GalleryListScene;
import com.hippo.ehviewer.ui.scene.ProgressScene;
import com.hippo.scene.StageActivity;
public class EhUrlOpener {
public static boolean openUrl(Activity activity, String url) {
if (TextUtils.isEmpty(url)) {
return false;
}
Intent intent;
ListUrlBuilder listUrlBuilder = GalleryListUrlParser.parse(url);
if (listUrlBuilder != null) {
Bundle args = new Bundle();
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.putExtra(StageActivity.KEY_SCENE_NAME, GalleryListScene.class.getName());
intent.putExtra(StageActivity.KEY_SCENE_ARGS, args);
activity.startActivity(intent);
return true;
}
GalleryDetailUrlParser.Result result1 = GalleryDetailUrlParser.parse(url);
if (result1 != null) {
Bundle args = new Bundle();
args.putString(GalleryDetailScene.KEY_ACTION, GalleryDetailScene.ACTION_GID_TOKEN);
args.putInt(GalleryDetailScene.KEY_GID, result1.gid);
args.putString(GalleryDetailScene.KEY_TOKEN, result1.token);
intent = new Intent(activity, MainActivity.class);
intent.putExtra(StageActivity.KEY_SCENE_NAME, GalleryDetailScene.class.getName());
intent.putExtra(StageActivity.KEY_SCENE_ARGS, args);
activity.startActivity(intent);
return true;
}
GalleryUrlParser.Result result2 = GalleryUrlParser.parse(url);
if (result2 != null) {
Bundle args = new Bundle();
args.putString(ProgressScene.KEY_ACTION, ProgressScene.ACTION_GALLERY_TOKEN);
args.putInt(ProgressScene.KEY_GID, result2.gid);
args.putString(ProgressScene.KEY_PTOKEN, result2.pToken);
args.putInt(ProgressScene.KEY_PAGE, result2.page);
intent = new Intent(activity, MainActivity.class);
intent.putExtra(StageActivity.KEY_SCENE_NAME, ProgressScene.class.getName());
intent.putExtra(StageActivity.KEY_SCENE_ARGS, args);
activity.startActivity(intent);
return true;
}
return false;
}
}
@@ -30,7 +30,7 @@ import java.net.URLDecoder;
public final class GalleryListUrlParser {
private static final String[] INVALID_HOSTS = {EhUrl.DOMAIN_EX, EhUrl.DOMAIN_G, EhUrl.DOMAIN_LOFI};
private static final String[] VALID_HOSTS = {EhUrl.DOMAIN_EX, EhUrl.DOMAIN_G, EhUrl.DOMAIN_LOFI};
private static final String PATH_NORMAL = "/";
private static final String PATH_UPLOADER = "/uploader/";
@@ -44,7 +44,7 @@ public final class GalleryListUrlParser {
return null;
}
if (!Utilities.contain(INVALID_HOSTS, url.getHost())) {
if (!Utilities.contain(VALID_HOSTS, url.getHost())) {
return null;
}
@@ -0,0 +1,43 @@
/*
* 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.client.parser;
import com.hippo.ehviewer.client.exception.EhException;
import org.json.JSONObject;
public class GalleryTokenApiParser {
/**
* {
* "tokenlist": [
* {
* "gid":618395,
* "token":"0439fa3666"
* }
* ]
* }
*/
public static String parse(String body) throws Exception {
JSONObject jo = new JSONObject(body).getJSONArray("tokenlist").getJSONObject(0);
try {
return jo.getString("token");
} catch (Exception e) {
throw new EhException(jo.getString("error"));
}
}
}
@@ -38,8 +38,8 @@ public final class GalleryUrlParser {
if (m.find()) {
Result result = new Result();
result.gid = Integer.parseInt(m.group(2));
result.token = m.group(1);
result.index = Integer.parseInt(m.group(3)) - 1;
result.pToken = m.group(1);
result.page = Integer.parseInt(m.group(3)) - 1;
return result;
} else {
return null;
@@ -48,7 +48,7 @@ public final class GalleryUrlParser {
public static class Result {
public int gid;
public String token;
public int index;
public String pToken;
public int page;
}
}
@@ -25,9 +25,11 @@ import android.support.design.widget.NavigationView;
import android.support.v4.widget.DrawerLayout;
import android.view.Gravity;
import android.view.MenuItem;
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.scene.GalleryListScene;
import com.hippo.ehviewer.ui.scene.LoginScene;
@@ -67,18 +69,47 @@ public final class MainActivity extends StageActivity
}
}
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 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();
}
}
private void onInit() {
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));
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();
} 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));
}
}
}
}
@@ -0,0 +1,53 @@
/*
* 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.scene;
import android.content.Context;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.client.EhClient;
import com.hippo.scene.SceneFragment;
import com.hippo.scene.StageActivity;
public abstract class EhCallback<E extends SceneFragment, T> implements EhClient.Callback<T> {
private EhApplication mApplication;
private int mStageId;
private String mSceneTag;
public EhCallback(Context context, int stageId, String sceneTag) {
mApplication = (EhApplication) context.getApplicationContext();
mStageId = stageId;
mSceneTag = sceneTag;
}
public abstract boolean isInstance(SceneFragment scene);
@SuppressWarnings("unchecked")
public E getScene() {
StageActivity stage = mApplication.findStageActivityById(mStageId);
if (stage == null) {
return null;
}
SceneFragment scene = stage.findSceneByTag(mSceneTag);
if (isInstance(scene)) {
return (E) scene;
} else {
return null;
}
}
}
@@ -43,7 +43,7 @@ import com.hippo.anani.SimpleAnimatorListener;
import com.hippo.easyrecyclerview.EasyRecyclerView;
import com.hippo.easyrecyclerview.LinearDividerItemDecoration;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.OpenUrlHelper;
import com.hippo.ehviewer.UrlOpener;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.client.EhClient;
import com.hippo.ehviewer.client.EhRequest;
@@ -207,7 +207,7 @@ public final class GalleryCommentsScene extends ToolbarScene
commentHolder.comment.clearCurrentSpan();
if (span instanceof URLSpan) {
OpenUrlHelper.openUrl(getActivity(), ((URLSpan) span).getURL(), true, true);
UrlOpener.openUrl(getActivity(), ((URLSpan) span).getURL(), true, true);
return true;
}
}
@@ -44,7 +44,7 @@ import android.widget.TextView;
import android.widget.Toast;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.OpenUrlHelper;
import com.hippo.ehviewer.UrlOpener;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.client.EhCacheKeyFactory;
import com.hippo.ehviewer.client.EhClient;
@@ -817,7 +817,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene
case R.id.action_open_in_other_app:
String url = getGalleryDetailUrl(false);
if (url != null) {
OpenUrlHelper.openUrl(getActivity(), url, false, false);
UrlOpener.openUrl(getActivity(), url, false, false);
}
break;
case R.id.action_refresh:
@@ -0,0 +1,237 @@
/*
* 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.scene;
import android.content.Context;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;
import com.hippo.ehviewer.EhApplication;
import com.hippo.ehviewer.R;
import com.hippo.ehviewer.client.EhClient;
import com.hippo.ehviewer.client.EhRequest;
import com.hippo.scene.Announcer;
import com.hippo.scene.SceneFragment;
import com.hippo.scene.StageActivity;
import com.hippo.util.ExceptionUtils;
import com.hippo.vector.VectorDrawable;
import com.hippo.view.ViewTransition;
import com.hippo.widget.SimpleImageView;
/**
* Only show a progress with jobs in background
*/
public final class ProgressScene extends BaseScene implements View.OnClickListener {
public static final String KEY_ACTION = "action";
public static final String ACTION_GALLERY_TOKEN = "gallery_token";
private static final String KEY_VALID = "valid";
private static final String KEY_ERROR = "error";
public static final String KEY_GID = "gid";
public static final String KEY_PTOKEN = "ptoken";
public static final String KEY_PAGE = "page";
private boolean mValid;
private String mError;
private String mAction;
private int mGid;
private String mPToken;
private int mPage;
private View mTip;
private TextView mTipText;
private ViewTransition mViewTransition;
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (savedInstanceState == null) {
onInit();
} else {
onRestore(savedInstanceState);
}
}
private boolean doJobs() {
if (ACTION_GALLERY_TOKEN.equals(mAction)) {
if (mGid == -1 || mPToken == null || mPage == -1) {
return false;
}
EhRequest request = new EhRequest()
.setMethod(EhClient.METHOD_GET_GALLERY_TOKEN)
.setArgs(mGid, mPToken, mPage)
.setCallback(new GetGalleryTokenListener(getContext(),
((StageActivity) getActivity()).getStageId(), getTag()));
EhApplication.getEhClient(getContext()).execute(request);
return true;
}
return false;
}
private boolean handleArgs(Bundle args) {
if (args == null) {
return false;
}
mAction = args.getString(KEY_ACTION);
if (ACTION_GALLERY_TOKEN.equals(mAction)) {
mGid = args.getInt(KEY_GID, -1);
mPToken = args.getString(KEY_PTOKEN, null);
mPage = args.getInt(KEY_PAGE, -1);
if (mGid == -1 || mPToken == null || mPage == -1) {
return false;
}
return true;
}
return false;
}
private void onInit() {
mValid = handleArgs(getArguments());
if (mValid) {
mValid = doJobs();
}
if (!mValid) {
mError = getString(R.string.error_something_wrong_happened);
}
}
private void onRestore(@NonNull Bundle savedInstanceState) {
mValid = savedInstanceState.getBoolean(KEY_VALID);
mError = savedInstanceState.getString(KEY_ERROR);
mAction = savedInstanceState.getString(KEY_ACTION);
mGid = savedInstanceState.getInt(KEY_GID, -1);
mPToken = savedInstanceState.getString(KEY_PTOKEN, null);
mPage = savedInstanceState.getInt(KEY_PAGE, -1);
}
@Override
public void onSaveInstanceState(Bundle outState) {
super.onSaveInstanceState(outState);
outState.putBoolean(KEY_VALID, mValid);
outState.putString(KEY_ERROR, mError);
outState.putString(KEY_ACTION, mAction);
outState.putInt(KEY_GID, mGid);
outState.putString(KEY_PTOKEN, mPToken);
outState.putInt(KEY_PAGE, mPage);
}
@Nullable
@Override
public View onCreateView(LayoutInflater inflater,
@Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.scene_progress, container, false);
View progress = view.findViewById(R.id.progress);
mTip = view.findViewById(R.id.tip);
SimpleImageView tipImage = (SimpleImageView) mTip.findViewById(R.id.tip_image);
mTipText = (TextView) mTip.findViewById(R.id.tip_text);
mTip.setOnClickListener(this);
tipImage.setDrawable(VectorDrawable.create(getContext(), R.drawable.sadpanda_head));
mTipText.setText(mError);
mViewTransition = new ViewTransition(progress, mTip);
if (mValid) {
mViewTransition.showView(0, false);
} else {
mViewTransition.showView(1, false);
}
return view;
}
@Override
public void onClick(View v) {
if (mTip == v) {
if (doJobs()) {
mValid = true;
// Show progress
mViewTransition.showView(0, true);
}
}
}
private void onGetGalleryTokenSuccess(String result) {
Bundle arg = new Bundle();
arg.putString(GalleryDetailScene.KEY_ACTION, GalleryDetailScene.ACTION_GID_TOKEN);
arg.putInt(GalleryDetailScene.KEY_GID, mGid);
arg.putString(GalleryDetailScene.KEY_TOKEN, result);
arg.putInt(GalleryDetailScene.KEY_PAGE, mPage);
startScene(new Announcer(GalleryDetailScene.class).setArgs(arg));
finish();
}
private void onGetGalleryTokenFailure(Exception e) {
mValid = false;
mError = ExceptionUtils.getReadableString(getContext(), e);
if (isViewCreated()) {
// Show tip
mViewTransition.showView(1);
mTipText.setText(mError);
}
}
private static class GetGalleryTokenListener extends EhCallback<ProgressScene, String> {
public GetGalleryTokenListener(Context context, int stageId, String sceneTag) {
super(context, stageId, sceneTag);
}
@Override
public void onSuccess(String result) {
ProgressScene scene = getScene();
if (scene != null) {
scene.onGetGalleryTokenSuccess(result);
}
}
@Override
public void onFailure(Exception e) {
ProgressScene scene = getScene();
if (scene != null) {
scene.onGetGalleryTokenFailure(e);
}
}
@Override
public void onCancel() {
}
@Override
public boolean isInstance(SceneFragment scene) {
return scene instanceof ProgressScene;
}
}
}