From 3ef767791d8bce59be6af78e727cae41c85f8605 Mon Sep 17 00:00:00 2001 From: seven332 Date: Sat, 6 Feb 2016 20:26:01 +0800 Subject: [PATCH] Add open eh url --- app/src/main/AndroidManifest.xml | 7 + .../com/hippo/ehviewer/OpenUrlHelper.java | 120 --------- .../java/com/hippo/ehviewer/UrlOpener.java | 73 ++++++ .../com/hippo/ehviewer/client/EhClient.java | 14 ++ .../com/hippo/ehviewer/client/EhEngine.java | 46 +++- .../hippo/ehviewer/client/EhUrlOpener.java | 84 +++++++ .../client/parser/GalleryListUrlParser.java | 4 +- .../client/parser/GalleryTokenApiParser.java | 43 ++++ .../client/parser/GalleryUrlParser.java | 8 +- .../com/hippo/ehviewer/ui/MainActivity.java | 53 +++- .../hippo/ehviewer/ui/scene/EhCallback.java | 53 ++++ .../ui/scene/GalleryCommentsScene.java | 4 +- .../ehviewer/ui/scene/GalleryDetailScene.java | 4 +- .../ehviewer/ui/scene/ProgressScene.java | 237 ++++++++++++++++++ app/src/main/res/layout/scene_progress.xml | 55 ++++ app/src/main/res/values/strings.xml | 5 + 16 files changed, 668 insertions(+), 142 deletions(-) delete mode 100644 app/src/main/java/com/hippo/ehviewer/OpenUrlHelper.java create mode 100644 app/src/main/java/com/hippo/ehviewer/UrlOpener.java create mode 100644 app/src/main/java/com/hippo/ehviewer/client/EhUrlOpener.java create mode 100644 app/src/main/java/com/hippo/ehviewer/client/parser/GalleryTokenApiParser.java create mode 100644 app/src/main/java/com/hippo/ehviewer/ui/scene/EhCallback.java create mode 100644 app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.java create mode 100644 app/src/main/res/layout/scene_progress.xml diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index ff40cc8ec..c459fd589 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -37,9 +37,16 @@ + + + + + + + diff --git a/app/src/main/java/com/hippo/ehviewer/OpenUrlHelper.java b/app/src/main/java/com/hippo/ehviewer/OpenUrlHelper.java deleted file mode 100644 index 4c8631529..000000000 --- a/app/src/main/java/com/hippo/ehviewer/OpenUrlHelper.java +++ /dev/null @@ -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(); - } -} diff --git a/app/src/main/java/com/hippo/ehviewer/UrlOpener.java b/app/src/main/java/com/hippo/ehviewer/UrlOpener.java new file mode 100644 index 000000000..bc1e09d73 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/UrlOpener.java @@ -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(); + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java index 30e92eeda..8f7159ab6 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java @@ -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); } diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java index dd83e570b..8c6437e75 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhEngine.java @@ -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 } diff --git a/app/src/main/java/com/hippo/ehviewer/client/EhUrlOpener.java b/app/src/main/java/com/hippo/ehviewer/client/EhUrlOpener.java new file mode 100644 index 000000000..50d552be1 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/client/EhUrlOpener.java @@ -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; + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListUrlParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListUrlParser.java index 2bae677cd..1d1ba7b42 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListUrlParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryListUrlParser.java @@ -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; } diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryTokenApiParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryTokenApiParser.java new file mode 100644 index 000000000..a0589825f --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryTokenApiParser.java @@ -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")); + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryUrlParser.java b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryUrlParser.java index 074cf8133..a5aa9c6c4 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryUrlParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/parser/GalleryUrlParser.java @@ -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; } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java index cf638aefe..507f7a762 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java @@ -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)); + } + } } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/EhCallback.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/EhCallback.java new file mode 100644 index 000000000..f0b17fe1f --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/EhCallback.java @@ -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 implements EhClient.Callback { + + 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; + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.java index 4fd1548a7..62554270e 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryCommentsScene.java @@ -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; } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java index 340c875d3..eb8996ab8 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryDetailScene.java @@ -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: diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.java new file mode 100644 index 000000000..bc380cf79 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.java @@ -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 { + + 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; + } + } +} diff --git a/app/src/main/res/layout/scene_progress.xml b/app/src/main/res/layout/scene_progress.xml new file mode 100644 index 000000000..39329731d --- /dev/null +++ b/app/src/main/res/layout/scene_progress.xml @@ -0,0 +1,55 @@ + + + + + + + + + + + + + + + + diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 8c568a52f..0f0ab6a62 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -93,6 +93,8 @@ Click to retry + Can\'t parse the url + Search ExHentai The World is Big and Pandas sit alone @@ -207,6 +209,9 @@ Show Japanese title Try to show Japanese title. It takes more time to get gallery list Show default title. It is usually English title + + + Something wrong happened From the future