diff --git a/app/src/main/java/com/hippo/ehviewer/Constants.java b/app/src/main/java/com/hippo/ehviewer/Constants.java index e8aabf056..50225142c 100644 --- a/app/src/main/java/com/hippo/ehviewer/Constants.java +++ b/app/src/main/java/com/hippo/ehviewer/Constants.java @@ -21,10 +21,14 @@ public final class Constants { public static final int MESSENGER_ID_EH_SOURCE; public static final int MESSENGER_ID_SHOW_APP_STATUS; + public static final int MESSENGER_ID_SIGN_IN_OR_OUT; + public static final int MESSENGER_ID_USER_AVATAR; static { Messenger messenger = Messenger.getInstance(); MESSENGER_ID_EH_SOURCE = messenger.newId(); MESSENGER_ID_SHOW_APP_STATUS = messenger.newId(); + MESSENGER_ID_SIGN_IN_OR_OUT = messenger.newId(); + MESSENGER_ID_USER_AVATAR = messenger.newId(); } } 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 ac83497e1..cd65830e3 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/EhClient.java +++ b/app/src/main/java/com/hippo/ehviewer/client/EhClient.java @@ -23,6 +23,8 @@ import com.hippo.ehviewer.network.EhHttpHelper; import com.hippo.network.ResponseCodeException; import com.hippo.util.PriorityThreadFactory; +import org.json.JSONObject; + import java.util.List; import java.util.concurrent.BlockingQueue; import java.util.concurrent.LinkedBlockingQueue; @@ -30,6 +32,7 @@ import java.util.concurrent.ThreadFactory; import java.util.concurrent.ThreadPoolExecutor; import java.util.concurrent.TimeUnit; +@SuppressWarnings("unchecked") public final class EhClient { @SuppressWarnings("unused") @@ -107,10 +110,6 @@ public final class EhClient { void onFailure(Exception e); } - public abstract static class OnGetGalleryListListener implements EhClientListener { - public abstract void onSuccess(List glList, int pageNum); - } - private void doBgJob(BgJobHelper bjh) { new AsyncTask() { @Override @@ -165,7 +164,6 @@ public final class EhClient { public abstract void doBgJob() throws Exception; public abstract void doSuccessCallback(); - } private void checkRequest(EhHttpHelper ehh) throws ResponseCodeException { @@ -176,16 +174,21 @@ public final class EhClient { } private Object[] doGetGalleryList(int source, String url) throws Exception { - EhHttpHelper ehh = new EhHttpHelper(); + EhHttpHelper ehh = EhHttpHelper.obtain(); String body = ehh.get(url); checkRequest(ehh); + EhHttpHelper.recycle(ehh); - ListParser parser = new ListParser(); + GalleryListParser parser = new GalleryListParser(); parser.parse(body, source); return new Object[]{parser.giList, parser.pageNum}; } + public abstract static class OnGetGalleryListListener implements EhClientListener { + public abstract void onSuccess(List glList, int pageNum); + } + private final class GetGalleryListHelper extends SimpleBgJobHelper { private int mSource; @@ -227,4 +230,57 @@ public final class EhClient { doBgJob(new GetGalleryListHelper(source, url, listener)); } + private Object[] doGetPopular() throws Exception { + final JSONObject json = new JSONObject(); + json.put("method", "popular"); + + EhHttpHelper ehh = EhHttpHelper.obtain(); + String body = ehh.postJson(API_EHVIEWER, json); + + checkRequest(ehh); + EhHttpHelper.recycle(ehh); + + PopularParser parser = new PopularParser(); + parser.parse(body); + return new Object[]{parser.galleryInfoList, parser.timeStamp}; + } + + public abstract static class OnGetPopularListener implements EhClientListener { + public abstract void onSuccess(List giList, long timeStamp); + } + + + private final class GetPopularHelper extends SimpleBgJobHelper { + + private OnGetPopularListener mListener; + + private List mGiList; + private long mTimeStamp; + + public GetPopularHelper(OnGetPopularListener listener) { + super(listener); + mListener = listener; + } + + @Override + public void doBgJob() throws Exception { + Object[] objs = doGetPopular(); + mGiList = (List) objs[0]; + mTimeStamp = (Long) objs[1]; + } + + @Override + public void doSuccessCallback() { + mListener.onSuccess(mGiList, mTimeStamp); + } + } + + /** + * Get popular gallery list + * + * @param listener the listener for callback + */ + public void getPopular(OnGetPopularListener listener) { + doBgJob(new GetPopularHelper(listener)); + } } diff --git a/app/src/main/java/com/hippo/ehviewer/client/ListParser.java b/app/src/main/java/com/hippo/ehviewer/client/GalleryListParser.java similarity index 99% rename from app/src/main/java/com/hippo/ehviewer/client/ListParser.java rename to app/src/main/java/com/hippo/ehviewer/client/GalleryListParser.java index 3743a5f70..f233974a0 100644 --- a/app/src/main/java/com/hippo/ehviewer/client/ListParser.java +++ b/app/src/main/java/com/hippo/ehviewer/client/GalleryListParser.java @@ -27,7 +27,7 @@ import java.util.List; import java.util.regex.Matcher; import java.util.regex.Pattern; -public class ListParser { +public class GalleryListParser { private static final String PARSE_MESSAGE = "Gallery list parser error"; private static final int DEFAULT_LIST_SIZE = 25; diff --git a/app/src/main/java/com/hippo/ehviewer/client/PopularParser.java b/app/src/main/java/com/hippo/ehviewer/client/PopularParser.java new file mode 100644 index 000000000..97ba63402 --- /dev/null +++ b/app/src/main/java/com/hippo/ehviewer/client/PopularParser.java @@ -0,0 +1,68 @@ +/* + * Copyright (C) 2015 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 + * 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 com.hippo.ehviewer.data.GalleryInfo; +import com.hippo.ehviewer.util.EhUtils; +import com.hippo.util.Utils; + +import org.json.JSONArray; +import org.json.JSONObject; + +import java.util.ArrayList; +import java.util.List; + +public class PopularParser { + + public List galleryInfoList; + public long timeStamp; + + public void parse(String body) throws Exception { + galleryInfoList = new ArrayList<>(); + JSONObject js = new JSONObject(body); + js = js.getJSONObject("popular"); + + if (!js.has("galleries")) { + if (js.has("error")) { + throw new EhException(js.getString("error")); + } else { + throw new EhException("Parser error"); // TODO + } + } + + JSONArray ja = js.getJSONArray("galleries"); + for (int i = 0; i < ja.length(); i++) { + JSONObject j = ja.getJSONObject(i); + GalleryInfo gi = new GalleryInfo(); + gi.gid = j.getInt("gid"); + gi.token = j.getString("token"); + gi.title = Utils.unescapeXml(j.getString("title")); + gi.posted = EhUtils.formatPostDate(Long.parseLong(j.getString("posted")) * 1000); + gi.thumb = j.getString("thumb"); + gi.category = EhUtils.getCategory(j.getString("category")); + gi.uploader = j.getString("uploader"); + gi.rating = Float.parseFloat(j.getString("rating")); + gi.generateSLang(); + galleryInfoList.add(gi); + } + + if (js.has("time")) { + timeStamp = js.getLong("time"); + } else { + timeStamp = -1; + } + } +} diff --git a/app/src/main/java/com/hippo/ehviewer/ui/ContentActivity.java b/app/src/main/java/com/hippo/ehviewer/ui/ContentActivity.java index d89af7189..429e87b99 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/ContentActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/ContentActivity.java @@ -23,6 +23,8 @@ import android.os.Bundle; import android.support.annotation.NonNull; import android.support.v4.widget.DrawerLayout; import android.view.Gravity; +import android.view.View; +import android.widget.AdapterView; import com.hippo.content.VectorContext; import com.hippo.ehviewer.R; @@ -30,6 +32,7 @@ import com.hippo.ehviewer.ui.scene.GalleryListScene; import com.hippo.ehviewer.util.Config; import com.hippo.ehviewer.widget.DrawerLeftPanel; import com.hippo.ehviewer.widget.StatusBarLayout; +import com.hippo.scene.Announcer; import com.hippo.scene.StageActivity; import com.hippo.scene.StageLayout; import com.hippo.widget.DrawerListView; @@ -37,6 +40,14 @@ import com.hippo.widget.DrawerListView; public class ContentActivity extends StageActivity implements StatusBarLayout.OnGetFitPaddingBottomListener { + public static final int DRAWER_LIST_NONE = -1; + public static final int DRAWER_LIST_HOMEPAGE = 0; + public static final int DRAWER_LIST_WHATS_HOT = 1; + public static final int DRAWER_LIST_HISTORY = 2; + public static final int DRAWER_LIST_FAVORITE = 3; + public static final int DRAWER_LIST_DOWNLOAD = 4; + public static final int DRAWER_LIST_SETTINGS = 5; + private Resources mResources; private DrawerLayout mDrawerLayout; @@ -45,11 +56,43 @@ public class ContentActivity extends StageActivity private DrawerLeftPanel mDrawerLeftPanel; private DrawerListView mDrawerListView; + private AdapterView.OnItemClickListener mDrawerListListener = new AdapterView.OnItemClickListener() { + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + if (mDrawerListView.getActivatedPosition() != position) { + Announcer announcer; + switch (position) { + case DRAWER_LIST_HOMEPAGE: + announcer = new Announcer(); + announcer.putExtra(GalleryListScene.KEY_MODE, GalleryListScene.MODE_HOMEPAGE); + startScene(GalleryListScene.class, announcer); + break; + case DRAWER_LIST_WHATS_HOT: + announcer = new Announcer(); + announcer.putExtra(GalleryListScene.KEY_MODE, GalleryListScene.MODE_POPULAR); + startScene(GalleryListScene.class, announcer); + break; + case DRAWER_LIST_HISTORY: + break; + case DRAWER_LIST_FAVORITE: + break; + case DRAWER_LIST_DOWNLOAD: + break; + case DRAWER_LIST_SETTINGS: + break; + } + + mDrawerLayout.closeDrawers(); + } + } + }; + @Override protected boolean isShowStats() { return Config.getShowApplicationStats(); } + @SuppressWarnings("deprecation") @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -83,11 +126,11 @@ public class ContentActivity extends StageActivity mResources.getString(R.string.settings) }; mDrawerListView.setData(drawerListDrawables, drawerListStrings); + mDrawerListView.setOnItemClickListener(mDrawerListListener); // First time if (savedInstanceState == null) { startScene(GalleryListScene.class, null); - mDrawerListView.setActivatedPosition(0); } } @@ -101,6 +144,20 @@ public class ContentActivity extends StageActivity super.attachBaseContext(VectorContext.wrapContext(newBase)); } + @SuppressLint("RtlHardcoded") + @Override + public void onBackPressed() { + if (mDrawerLayout.isDrawerOpen(Gravity.LEFT) || mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) { + mDrawerLayout.closeDrawers(); + } else { + super.onBackPressed(); + } + } + + public void setDrawerListActivatedPosition(int position) { + mDrawerListView.setActivatedPosition(position); + } + @SuppressLint("RtlHardcoded") public void toggleDrawer() { if (mDrawerLayout.isDrawerOpen(Gravity.LEFT) || mDrawerLayout.isDrawerOpen(Gravity.RIGHT)) { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java index 9d567e374..dbe2793db 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryListScene.java @@ -47,7 +47,7 @@ import com.hippo.drawable.AddDeleteDrawable; import com.hippo.effect.ViewTransition; import com.hippo.ehviewer.R; import com.hippo.ehviewer.client.EhClient; -import com.hippo.ehviewer.client.ListParser; +import com.hippo.ehviewer.client.GalleryListParser; import com.hippo.ehviewer.data.GalleryInfo; import com.hippo.ehviewer.data.ListUrlBuilder; import com.hippo.ehviewer.data.UnsupportedSearchException; @@ -60,6 +60,7 @@ import com.hippo.ehviewer.widget.RatingView; import com.hippo.ehviewer.widget.SearchBar; import com.hippo.ehviewer.widget.SearchDatabase; import com.hippo.ehviewer.widget.SearchLayout; +import com.hippo.scene.Announcer; import com.hippo.scene.Scene; import com.hippo.scene.SimpleDialog; import com.hippo.util.AnimationUtils; @@ -79,6 +80,11 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, View.OnClickListener, FabLayout.OnCancelListener, SearchLayout.SearhLayoutHelper { + public static final String KEY_MODE = "mode"; + + public static final int MODE_HOMEPAGE = 0; + public static final int MODE_POPULAR = 1; + private static final long ANIMATE_TIME = 300l; private final static int STATE_NORMAL = 0; @@ -179,6 +185,11 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, } }; + @Override + public int getLaunchMode() { + return LAUNCH_MODE_SINGLE_TOP; + } + @Override protected void onReplace(@NonNull Scene oldScene) { super.onReplace(oldScene); @@ -188,6 +199,39 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, .newInstance(getStageActivity()); } + private void handleAnnouncer(Announcer announcer) { + int mode = MODE_HOMEPAGE; + if (announcer != null) { + mode = announcer.getIntExtra(KEY_MODE, MODE_HOMEPAGE); + } + + switch (mode) { + default: + case MODE_HOMEPAGE: + mActivity.setDrawerListActivatedPosition(ContentActivity.DRAWER_LIST_HOMEPAGE); + mListUrlBuilder.setMode(ListUrlBuilder.MODE_NORMAL); + break; + case MODE_POPULAR: + mActivity.setDrawerListActivatedPosition(ContentActivity.DRAWER_LIST_WHATS_HOT); + mListUrlBuilder.setMode(ListUrlBuilder.MODE_POPULAR); + break; + } + + if (announcer == null) { + mGalleryListHelper.firstRefresh(); + } else { + showSearchBar(true); + mGalleryListHelper.refresh(); + } + } + + @Override + protected void onNewAnnouncer(Announcer announcer) { + super.onNewAnnouncer(announcer); + + handleAnnouncer(announcer); + } + @SuppressWarnings("deprecation") @Override protected void onCreate(@Nullable Bundle savedInstanceState) { @@ -223,8 +267,7 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, ViewUtils.removeOnGlobalLayoutListener(mSearchBar.getViewTreeObserver(), this); mSearchBarOriginalTop = mSearchBar.getTop(); mSearchBarOriginalBottom = mSearchBar.getBottom(); - int fitPaddingTop = mSearchBar.getHeight() + - mResources.getDimensionPixelOffset(R.dimen.search_bar_padding_vertical); + int fitPaddingTop = mSearchBar.getHeight() + mResources.getDimensionPixelOffset(R.dimen.search_bar_padding_vertical); mContentLayout.setFitPaddingTop(fitPaddingTop); mSearchLayout.setFitPaddingTop(fitPaddingTop); } @@ -255,7 +298,7 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, // When scene start if (savedInstanceState == null) { - mGalleryListHelper.firstRefresh(); + handleAnnouncer(getAnnouncer()); } } @@ -447,6 +490,8 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, return; } + mActivity.setDrawerListActivatedPosition(ContentActivity.DRAWER_LIST_NONE); + mSearchDatabase.addQuery(query); if (mViewTransition.getShownViewIndex() == 0) { @@ -463,7 +508,7 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, mState = STATE_NORMAL; mViewTransition.showView(0); mSearchBar.setState(SearchBar.STATE_NORMAL); - returnSearchBarPosition(); + showSearchBar(true); setFabState(FAB_STATE_NORMAL); mGalleryListHelper.refresh(); @@ -486,17 +531,19 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, int offset = mSearchBarOriginalTop - mSearchBar.getTop(); if (offset != 0) { if (animation) { + final boolean needRequestLayout = mSearchBar.getBottom() <= 0 && offset > 0; final ValueAnimator va = ValueAnimator.ofInt(0, offset); va.setDuration(ANIMATE_TIME); va.addListener(new SimpleAnimatorListener() { @Override - public void onAnimationEnd(Animator animation) { + public void onAnimationEnd(Animator animation, boolean canceled) { + super.onAnimationEnd(animation, canceled); mSearchBarMoveAnimator = null; } }); va.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { int lastValue; - + boolean hasRequestLayout; @Override public void onAnimationUpdate(ValueAnimator animation) { int value = (Integer) animation.getAnimatedValue(); @@ -504,8 +551,16 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, lastValue = value; mSearchBar.offsetTopAndBottom(offsetStep); ((OffsetLayout.LayoutParams) mSearchBar.getLayoutParams()).offsetY += offsetStep; + + if (!hasRequestLayout && needRequestLayout && mSearchBar.getBottom() > 0) { + hasRequestLayout = true; + mSearchBar.requestLayout(); + } } }); + if (mSearchBarMoveAnimator != null) { + mSearchBarMoveAnimator.cancel(); + } mSearchBarMoveAnimator = va; va.start(); } else { @@ -646,7 +701,7 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, private LayoutInflater mInflater; - private GalleryListListener mLastGalleryListListener; + private GalleryListHelperSettable mListener; private Runnable mSearchBarPositionTask = new Runnable() { @Override @@ -663,9 +718,9 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, private GalleryListHelper(Context context, GalleryListHelper oldContentHelper) { super(context, oldContentHelper); // Update gallery listener - mLastGalleryListListener = oldContentHelper.mLastGalleryListListener; - if (mLastGalleryListListener != null) { - mLastGalleryListListener.setGalleryListHelper(this); + mListener = oldContentHelper.mListener; + if (mListener != null) { + mListener.setGalleryListHelper(this); } init(); } @@ -678,9 +733,9 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, mInflater = mActivity.getLayoutInflater(); } - private void clearLastGalleryListListener(GalleryListListener listener) { - if (mLastGalleryListListener == listener) { - mLastGalleryListListener = null; + private void clearLastGalleryListHelperSettable(GalleryListHelperSettable listener) { + if (mListener == listener) { + mListener = null; } } @@ -708,17 +763,25 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, @Override protected void getPageData(int taskId, int type, int page) { - try { - int source = Config.getEhSource(); - mListUrlBuilder.setPageIndex(page); - String url = mListUrlBuilder.build(source); - GalleryListListener listener = new GalleryListListener(taskId, type, - page, source); + int mode = mListUrlBuilder.getMode(); + if (mode == ListUrlBuilder.MODE_POPULAR) { + PopularListener listener = new PopularListener(taskId); listener.setGalleryListHelper(this); - mLastGalleryListListener = listener; - EhClient.getInstance().getGalleryList(source, url, listener); - } catch (UnsupportedSearchException e) { - onGetPageData(taskId, e); + mListener = listener; + EhClient.getInstance().getPopular(listener); + } else { + try { + int source = Config.getEhSource(); + mListUrlBuilder.setPageIndex(page); + String url = mListUrlBuilder.build(source); + GalleryListListener listener = new GalleryListListener(taskId, + type, page, source); + listener.setGalleryListHelper(this); + mListener = listener; + EhClient.getInstance().getGalleryList(source, url, listener); + } catch (UnsupportedSearchException e) { + onGetPageData(taskId, e); + } } } @@ -746,7 +809,8 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, } } - private static class GalleryListListener extends EhClient.OnGetGalleryListListener { + private static class GalleryListListener extends EhClient.OnGetGalleryListListener + implements GalleryListHelperSettable { private int mTaskId; private int mTaskType; @@ -762,6 +826,7 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, mSource = source; } + @Override public void setGalleryListHelper(GalleryListHelper helper) { mHelper = helper; } @@ -770,7 +835,7 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, public void onSuccess(List glList, int pageNum) { if (mHelper != null) { if (mSource == EhClient.SOURCE_LOFI) { - if (pageNum == ListParser.CURRENT_PAGE_IS_LAST) { + if (pageNum == GalleryListParser.CURRENT_PAGE_IS_LAST) { mHelper.setPageSize(mTargetPage); } else if (mTaskType == ContentLayout.ContentHelper.TYPE_REFRESH) { mHelper.setPageSize(Integer.MAX_VALUE); @@ -780,7 +845,7 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, } mHelper.onGetPageData(mTaskId, glList); - mHelper.clearLastGalleryListListener(this); + mHelper.clearLastGalleryListHelperSettable(this); } } @@ -789,8 +854,47 @@ public class GalleryListScene extends Scene implements SearchBar.Helper, if (mHelper != null) { mHelper.onGetPageData(mTaskId, e); - mHelper.clearLastGalleryListListener(this); + mHelper.clearLastGalleryListHelperSettable(this); } } } + + private static class PopularListener extends EhClient.OnGetPopularListener + implements GalleryListHelperSettable { + + private int mTaskId; + private GalleryListHelper mHelper; + + public PopularListener(int taskId) { + mTaskId = taskId; + } + + @Override + public void setGalleryListHelper(GalleryListHelper helper) { + mHelper = helper; + } + + @Override + public void onSuccess(List glList, long timeStamp) { + if (mHelper != null) { + mHelper.setPageSize(1); + mHelper.onGetPageData(mTaskId, glList); + + mHelper.clearLastGalleryListHelperSettable(this); + } + } + + @Override + public void onFailure(Exception e) { + if (mHelper != null) { + mHelper.onGetPageData(mTaskId, e); + + mHelper.clearLastGalleryListHelperSettable(this); + } + } + } + + private interface GalleryListHelperSettable { + void setGalleryListHelper(GalleryListHelper helper); + } } diff --git a/app/src/main/java/com/hippo/ehviewer/util/EhUtils.java b/app/src/main/java/com/hippo/ehviewer/util/EhUtils.java index b0a8aeb1b..450bdf25e 100644 --- a/app/src/main/java/com/hippo/ehviewer/util/EhUtils.java +++ b/app/src/main/java/com/hippo/ehviewer/util/EhUtils.java @@ -17,8 +17,14 @@ package com.hippo.ehviewer.util; import android.graphics.Color; +import java.text.DateFormat; +import java.text.SimpleDateFormat; +import java.util.Locale; + public final class EhUtils { + public static final DateFormat POST_DATE_FORMAT = new SimpleDateFormat("yyyy-MM-dd HH:mm", Locale.getDefault()); + public static final String EH_DOWNLOAD_FILENAME = ".ehviewer"; public static final int NONE = -1; // Use it for homepage @@ -119,4 +125,8 @@ public final class EhUtils { return BG_COLOR_UNKNOWN; } } + + public static String formatPostDate(long date) { + return POST_DATE_FORMAT.format(date); + } } diff --git a/app/src/main/java/com/hippo/ehviewer/widget/DrawerLeftPanel.java b/app/src/main/java/com/hippo/ehviewer/widget/DrawerLeftPanel.java index f8951195f..18c1d504f 100644 --- a/app/src/main/java/com/hippo/ehviewer/widget/DrawerLeftPanel.java +++ b/app/src/main/java/com/hippo/ehviewer/widget/DrawerLeftPanel.java @@ -18,6 +18,7 @@ package com.hippo.ehviewer.widget; import android.content.Context; import android.content.res.TypedArray; import android.graphics.Rect; +import android.net.Uri; import android.os.Bundle; import android.os.Parcelable; import android.support.annotation.NonNull; @@ -30,7 +31,10 @@ import android.widget.TextView; import com.facebook.drawee.view.SimpleDraweeView; import com.hippo.effect.ripple.RippleSalon; +import com.hippo.ehviewer.Constants; import com.hippo.ehviewer.R; +import com.hippo.util.FrescoUtils; +import com.hippo.util.Messenger; import com.hippo.widget.DrawerListView; public class DrawerLeftPanel extends LinearLayout { @@ -40,6 +44,8 @@ public class DrawerLeftPanel extends LinearLayout { private static final int[] MAX_ATTRS = {android.R.attr.maxWidth}; + private static Uri DEFAULT_AVATAR_URI = FrescoUtils.getResourcesDrawableUri(R.drawable.default_avatar); + private int mMaxWidth = -1; private ViewGroup mSuperUserPanel; @@ -57,6 +63,32 @@ public class DrawerLeftPanel extends LinearLayout { private int mUserPanelOriginalPaddingTop; private int mDrawerListViewOriginalPaddingBottom; + private boolean mSignIn; + + private Messenger.Receiver mMessengerReceiver = new Messenger.Receiver() { + @Override + public void onReceive(int id, Object obj) { + if (id == Constants.MESSENGER_ID_SIGN_IN_OR_OUT) { + if (obj == null) { + // Sign out + mSignIn = false; + mAvatar.setImageURI(DEFAULT_AVATAR_URI); + mUsename.setText(""); + mAction.setText(mContext.getString(R.string.signin)); + } else if (obj instanceof String) { + // Sign in + mSignIn = true; + mUsename.setText((String) obj); + mAction.setText(mContext.getString(R.string.signout)); + } + } else if (id == Constants.MESSENGER_ID_USER_AVATAR) { + if (mSignIn && obj instanceof Uri) { + mAvatar.setImageURI((Uri) obj); + } + } + } + }; + public DrawerLeftPanel(Context context, AttributeSet attrs) { super(context, attrs); init(context, attrs); @@ -86,9 +118,9 @@ public class DrawerLeftPanel extends LinearLayout { RippleSalon.addRipple(mAction, false); - mAvatar.getHierarchy().setPlaceholderImage(R.drawable.theme_primary); - mUsename.setText("速度速度加快"); - mAction.setText("退出"); + mAvatar.setImageURI(DEFAULT_AVATAR_URI); + mUsename.setText(""); + mAction.setText(mContext.getString(R.string.signin)); mSuperUserPanelHeight = mSuperUserPanel.getLayoutParams().height; mUserPanelOriginalPaddingTop = mUserPanel.getPaddingTop(); @@ -99,6 +131,24 @@ public class DrawerLeftPanel extends LinearLayout { return mDrawerListView; } + @Override + protected void onAttachedToWindow() { + super.onAttachedToWindow(); + + Messenger.getInstance().register(Constants.MESSENGER_ID_SIGN_IN_OR_OUT, mMessengerReceiver); + Messenger.getInstance().register(Constants.MESSENGER_ID_USER_AVATAR, mMessengerReceiver); + } + + @Override + protected void onDetachedFromWindow() { + super.onDetachedFromWindow(); + + Messenger.getInstance().unregister(Constants.MESSENGER_ID_SIGN_IN_OR_OUT, mMessengerReceiver); + Messenger.getInstance().register(Constants.MESSENGER_ID_USER_AVATAR, mMessengerReceiver); + } + + + @Override public Parcelable onSaveInstanceState() { final Bundle state = new Bundle(); diff --git a/app/src/main/java/com/hippo/scene/Scene.java b/app/src/main/java/com/hippo/scene/Scene.java index 5fdfdda6d..623f8166d 100644 --- a/app/src/main/java/com/hippo/scene/Scene.java +++ b/app/src/main/java/com/hippo/scene/Scene.java @@ -40,6 +40,9 @@ import com.hippo.util.ViewUtils; */ public abstract class Scene { + public static final int LAUNCH_MODE_STANDARD = 0; + public static final int LAUNCH_MODE_SINGLE_TOP = 1; + static final int SCENE_STATE_CREATE = 0; static final int SCENE_STATE_RUN = 1; static final int SCENE_STATE_DESTROY = 2; @@ -86,7 +89,7 @@ public abstract class Scene { return mCurtain; } - @Nullable Announcer getAnnouncer() { + public @Nullable Announcer getAnnouncer() { return mAnnouncer; } @@ -97,6 +100,10 @@ public abstract class Scene { return stageActivity; } + public int getLaunchMode() { + return LAUNCH_MODE_STANDARD; + } + /** * You will get null before onCreate and after onDestroy mostly * @@ -257,6 +264,9 @@ public abstract class Scene { mCurtain = oldScene.mCurtain; } + protected void onNewAnnouncer(Announcer announcer) { + } + /** * It will be called if nav bar is transparent. * @param b the nav bar's height diff --git a/app/src/main/java/com/hippo/scene/SceneManager.java b/app/src/main/java/com/hippo/scene/SceneManager.java index 3692b1701..1c6e232ce 100644 --- a/app/src/main/java/com/hippo/scene/SceneManager.java +++ b/app/src/main/java/com/hippo/scene/SceneManager.java @@ -76,6 +76,13 @@ class SceneManager { return; } + Scene topScene = getTopState(); + if (sceneClass.isInstance(topScene) && + topScene.getLaunchMode() == Scene.LAUNCH_MODE_SINGLE_TOP) { + topScene.onNewAnnouncer(announcer); + return; + } + Scene scene = createSceneByClass(sceneClass); scene.setAnnouncer(announcer); scene.setCurtain(curtain); diff --git a/app/src/main/java/com/hippo/util/FrescoUtils.java b/app/src/main/java/com/hippo/util/FrescoUtils.java new file mode 100644 index 000000000..29339c1ba --- /dev/null +++ b/app/src/main/java/com/hippo/util/FrescoUtils.java @@ -0,0 +1,29 @@ +/* + * Copyright (C) 2015 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 + * 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.util; + +import android.annotation.IdRes; +import android.net.Uri; + +import com.facebook.common.util.UriUtil; + +public class FrescoUtils { + + public static Uri getResourcesDrawableUri(@IdRes int resId) { + return new Uri.Builder().scheme(UriUtil.LOCAL_RESOURCE_SCHEME) + .path(String.valueOf(resId)).build(); + } +} diff --git a/app/src/main/res/drawable-xhdpi/default_avatar.png b/app/src/main/res/drawable-xhdpi/default_avatar.png new file mode 100644 index 000000000..144578112 Binary files /dev/null and b/app/src/main/res/drawable-xhdpi/default_avatar.png differ diff --git a/app/src/main/res/drawable-xxhdpi/default_avatar.png b/app/src/main/res/drawable-xxhdpi/default_avatar.png new file mode 100644 index 000000000..55448328f Binary files /dev/null and b/app/src/main/res/drawable-xxhdpi/default_avatar.png differ diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 3b55720f0..fe93c06f1 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -100,5 +100,7 @@ Favorite Download Settings + Sign in + Sign out