diff --git a/app/src/main/java/com/hippo/ehviewer/UrlOpener.java b/app/src/main/java/com/hippo/ehviewer/UrlOpener.java index 6dcbde6ff..360fe6916 100644 --- a/app/src/main/java/com/hippo/ehviewer/UrlOpener.java +++ b/app/src/main/java/com/hippo/ehviewer/UrlOpener.java @@ -17,10 +17,12 @@ package com.hippo.ehviewer; import android.app.Activity; +import android.content.ActivityNotFoundException; +import android.content.Context; import android.content.Intent; -import android.content.pm.PackageManager; -import android.content.pm.ResolveInfo; import android.net.Uri; +import android.provider.Browser; +import android.support.annotation.NonNull; import android.support.customtabs.CustomTabsIntent; import android.text.TextUtils; import android.widget.Toast; @@ -36,7 +38,7 @@ public final class UrlOpener { private UrlOpener() { } - public static void openUrl(Activity activity, String url, boolean ehUrl, boolean customTabs) { + public static void openUrl(@NonNull Context context, String url, boolean ehUrl, boolean customTabs) { if (TextUtils.isEmpty(url)) { return; } @@ -47,17 +49,19 @@ public final class UrlOpener { if (ehUrl) { Announcer announcer = EhUrlOpener.parseUrl(url); if (null != announcer) { - intent = new Intent(activity, MainActivity.class); + intent = new Intent(context, MainActivity.class); intent.setAction(StageActivity.ACTION_START_SCENE); intent.putExtra(StageActivity.KEY_SCENE_NAME, announcer.getClazz().getName()); intent.putExtra(StageActivity.KEY_SCENE_ARGS, announcer.getArgs()); - activity.startActivity(intent); + intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + context.startActivity(intent); return; } } // CustomTabs - if (customTabs) { + if (customTabs && context instanceof Activity) { + Activity activity = (Activity) context; String packageName = CustomTabsHelper.getPackageNameToUseFixed(activity); if (packageName != null) { new CustomTabsIntent.Builder() @@ -70,15 +74,12 @@ public final class UrlOpener { } // 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; + intent = new Intent(Intent.ACTION_VIEW, uri); + intent.putExtra(Browser.EXTRA_APPLICATION_ID, context.getPackageName()); + try { + context.startActivity(intent); + } catch (ActivityNotFoundException e) { + Toast.makeText(context, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show(); } - - Toast.makeText(activity, R.string.error_cant_find_activity, Toast.LENGTH_SHORT).show(); } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java index 92cdfe52c..0f82d1918 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/CommonOperations.java @@ -217,6 +217,7 @@ public final class CommonOperations { client.execute(request); } + // TODO Add context if activity and context are different style public static void startDownload(final MainActivity activity, final GalleryInfo galleryInfo, boolean forceDefault) { final DownloadManager dm = EhApplication.getDownloadManager(activity); 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 7ca617b8d..6beb1ae7f 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/MainActivity.java @@ -339,7 +339,7 @@ public final class MainActivity extends StageActivity BaseScene baseScene = (BaseScene) scene; mRightDrawer.removeAllViews(); View drawerView = baseScene.onCreateDrawerView( - getLayoutInflater(), mRightDrawer, savedInstanceState); + baseScene.getLayoutInflater2(), mRightDrawer, savedInstanceState); if (drawerView != null) { mRightDrawer.addView(drawerView); mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED, Gravity.RIGHT); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/AnalyticsScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/AnalyticsScene.java index 2fa408d97..b74aa7881 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/AnalyticsScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/AnalyticsScene.java @@ -16,6 +16,7 @@ package com.hippo.ehviewer.ui.scene; +import android.content.Context; import android.os.Bundle; import android.support.annotation.Nullable; import android.view.LayoutInflater; @@ -58,7 +59,7 @@ public class AnalyticsScene extends BaseScene implements View.OnClickListener { TextView text = (TextView) ViewUtils.$$(view, R.id.text); text.setText(Html.fromHtml(getString(R.string.analytics_explain))); - text.setMovementMethod(new LinkMovementMethod2(getActivity())); + text.setMovementMethod(LinkMovementMethod2.getInstance()); mCancel.setOnClickListener(this); mOk.setOnClickListener(this); @@ -79,18 +80,25 @@ public class AnalyticsScene extends BaseScene implements View.OnClickListener { @Override public void onClick(View v) { + Context context = getContext2(); + if (null == context) { + return; + } + if (mCancel == v) { Settings.putEnableAnalytics(false); } else if (mOk == v) { Settings.putEnableAnalytics(true); // Start Analytics - Analytics.start(getContext()); + Analytics.start(context); } Settings.putAskAnalytics(false); // Start new scene and finish it self - ((MainActivity) getActivity()).startSceneForCheckStep( - MainActivity.CHECK_STEP_ANALYTICS, getArguments()); + MainActivity activity = getActivity2(); + if (null != activity) { + activity.startSceneForCheckStep(MainActivity.CHECK_STEP_ANALYTICS, getArguments()); + } finish(); } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/BaseScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/BaseScene.java index 57135e43b..6cf9c03c4 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/BaseScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/BaseScene.java @@ -16,6 +16,8 @@ package com.hippo.ehviewer.ui.scene; +import android.content.Context; +import android.content.res.Resources; import android.os.Bundle; import android.support.annotation.IdRes; import android.support.annotation.Nullable; @@ -151,14 +153,52 @@ public abstract class BaseScene extends SceneFragment { return mViewCreated; } - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - if (savedInstanceState != null) { - onRestoreInstanceState(savedInstanceState); + @Nullable + public Context getContext2() { + return super.getContext(); + } + + @Nullable + public Resources getResources2() { + Context context = getContext2(); + if (null != context) { + return context.getResources(); + } else { + return null; } } - protected void onRestoreInstanceState(Bundle savedInstanceState) { + @Nullable + public MainActivity getActivity2() { + FragmentActivity activity = getActivity(); + if (activity instanceof MainActivity) { + return (MainActivity) activity; + } else { + return null; + } + } + + @Nullable + public LayoutInflater getLayoutInflater2() { + FragmentActivity activity = getActivity(); + if (null != activity) { + return activity.getLayoutInflater(); + } else { + return null; + } + } + + public void hideSoftInput() { + FragmentActivity activity = getActivity(); + if (null != activity) { + ActivityHelper.hideSoftInput(activity); + } + } + + public void showSoftInput(@Nullable View view) { + FragmentActivity activity = getActivity(); + if (null != activity && null != view) { + ActivityHelper.showSoftInput(activity, view); + } } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/CookieSignInScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/CookieSignInScene.java index 0c0ab708c..5a286fccd 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/CookieSignInScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/CookieSignInScene.java @@ -36,7 +36,6 @@ import com.hippo.ehviewer.client.EhCookieStore; import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.EhUtils; import com.hippo.rippleold.RippleSalon; -import com.hippo.util.ActivityHelper; import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.ViewUtils; @@ -90,7 +89,7 @@ public class CookieSignInScene extends BaseScene implements EditText.OnEditorAct public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - ActivityHelper.showSoftInput(getActivity(), mIpbMemberId); + showSoftInput(mIpbMemberId); } @Override @@ -144,7 +143,8 @@ public class CookieSignInScene extends BaseScene implements EditText.OnEditorAct } public void enter() { - if (null == mIpbMemberIdLayout || null == mIpbPassHashLayout || + Context context = getContext2(); + if (null == context || null == mIpbMemberIdLayout || null == mIpbPassHashLayout || null == mIpbMemberId || null == mIpbPassHash) { return; } @@ -165,10 +165,10 @@ public class CookieSignInScene extends BaseScene implements EditText.OnEditorAct mIpbPassHashLayout.setError(null); } - ActivityHelper.hideSoftInput(getActivity()); + hideSoftInput(); if (!checkIpbMemberId(ipbMemberId) || !(checkIpbPassHash(ipbPassHash))) { - new AlertDialog.Builder(getContext()).setTitle(R.string.waring) + new AlertDialog.Builder(context).setTitle(R.string.waring) .setMessage(R.string.wrong_cookie_warning) .setNegativeButton(R.string.i_dont_think_so, null) .setPositiveButton(R.string.i_will_check_it, new DialogInterface.OnClickListener() { @@ -192,12 +192,12 @@ public class CookieSignInScene extends BaseScene implements EditText.OnEditorAct } private void storeCookie(String id, String hash) { - Context context = getContext(); + Context context = getContext2(); if (null == context) { return; } - EhUtils.signOut(getContext()); + EhUtils.signOut(context); EhCookieStore store = EhApplication.getEhCookieStore(context); store.add(newCookie(EhCookieStore.KEY_IPD_MEMBER_ID, id, EhUrl.DOMAIN_E)); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/CrashScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/CrashScene.java index 6631d1b87..28d57cda2 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/CrashScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/CrashScene.java @@ -78,15 +78,18 @@ public class CrashScene extends BaseScene implements View.OnClickListener { @Override public void onClick(View v) { - if (mSend == v && null != mCrash) { - ActivityHelper.sendEmail(getActivity(), EhApplication.getDeveloperEmail(), + MainActivity activity = getActivity2(); + + if (mSend == v && null != mCrash && null != activity) { + ActivityHelper.sendEmail(activity, EhApplication.getDeveloperEmail(), "I found a bug in EhViewer !", mCrash); } Crash.resetCrashFile(); // Start new scene and finish it self - ((MainActivity) getActivity()).startSceneForCheckStep( - MainActivity.CHECK_STEP_CRASH, getArguments()); + if (null != activity) { + activity.startSceneForCheckStep(MainActivity.CHECK_STEP_CRASH, getArguments()); + } finish(); } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelsScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelsScene.java index 89e0ec8ef..3d4dd05b8 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelsScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadLabelsScene.java @@ -16,6 +16,7 @@ package com.hippo.ehviewer.ui.scene; +import android.content.Context; import android.content.DialogInterface; import android.graphics.drawable.Drawable; import android.graphics.drawable.NinePatchDrawable; @@ -52,6 +53,7 @@ import com.hippo.ehviewer.R; import com.hippo.ehviewer.dao.DownloadLabel; import com.hippo.util.DrawableManager; import com.hippo.view.ViewTransition; +import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.ViewUtils; import java.util.List; @@ -77,7 +79,7 @@ public class DownloadLabelsScene extends ToolbarScene { @Override public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mList = EhApplication.getDownloadManager(getContext()).getLabelList(); + mList = EhApplication.getDownloadManager(getContext2()).getLabelList(); } @Override @@ -97,7 +99,9 @@ public class DownloadLabelsScene extends ToolbarScene { TextView tip = (TextView) ViewUtils.$$(view, R.id.tip); mViewTransition = new ViewTransition(mRecyclerView, tip); - Drawable drawable = DrawableManager.getDrawable(getContext(), R.drawable.big_label); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + Drawable drawable = DrawableManager.getDrawable(context, R.drawable.big_label); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); tip.setCompoundDrawables(null, drawable, null, null); tip.setText(R.string.no_download_label); @@ -109,7 +113,7 @@ public class DownloadLabelsScene extends ToolbarScene { // drag & drop manager RecyclerViewDragDropManager dragDropManager = new RecyclerViewDragDropManager(); dragDropManager.setDraggingItemShadowDrawable( - (NinePatchDrawable) getResources().getDrawable(R.drawable.shadow_8dp)); + (NinePatchDrawable) context.getResources().getDrawable(R.drawable.shadow_8dp)); // swipe manager RecyclerViewSwipeManager swipeManager = new RecyclerViewSwipeManager(); RecyclerView.Adapter adapter = new LabelAdapter(); @@ -120,7 +124,7 @@ public class DownloadLabelsScene extends ToolbarScene { final GeneralItemAnimator animator = new SwipeDismissItemAnimator(); animator.setSupportsChangeAnimations(false); mRecyclerView.hasFixedSize(); - mRecyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + mRecyclerView.setLayoutManager(new LinearLayoutManager(context)); mRecyclerView.setAdapter(adapter); mRecyclerView.setItemAnimator(animator); guardManager.attachRecyclerView(mRecyclerView); @@ -160,10 +164,15 @@ public class DownloadLabelsScene extends ToolbarScene { @Override public boolean onMenuItemClick(MenuItem item) { + Context context = getContext2(); + if (null == context) { + return false; + } + int id = item.getItemId(); switch (id) { case R.id.action_add: { - EditTextDialogBuilder builder = new EditTextDialogBuilder(getContext(), null, getString(R.string.download_labels)); + EditTextDialogBuilder builder = new EditTextDialogBuilder(context, null, getString(R.string.download_labels)); builder.setTitle(R.string.new_label_title); builder.setPositiveButton(android.R.string.ok, null); AlertDialog dialog = builder.show(); @@ -199,17 +208,22 @@ public class DownloadLabelsScene extends ToolbarScene { @Override public void onClick(View v) { + Context context = getContext2(); + if (null == context) { + return; + } + String text = mBuilder.getText(); if (TextUtils.isEmpty(text)) { mBuilder.setError(getString(R.string.label_text_is_empty)); } else if (getString(R.string.default_download_label_name).equals(text)) { mBuilder.setError(getString(R.string.label_text_is_invalid)); - } else if (EhApplication.getDownloadManager(getContext()).containLabel(text)) { + } else if (EhApplication.getDownloadManager(context).containLabel(text)) { mBuilder.setError(getString(R.string.label_text_exist)); } else { mBuilder.setError(null); mDialog.dismiss(); - EhApplication.getDownloadManager(getContext()).addLabel(text); + EhApplication.getDownloadManager(context).addLabel(text); if (mAdapter != null && mList != null) { mAdapter.notifyItemInserted(mList.size() - 1); } @@ -245,17 +259,22 @@ public class DownloadLabelsScene extends ToolbarScene { @Override public void onClick(View v) { + Context context = getContext2(); + if (null == context) { + return; + } + String text = mBuilder.getText(); if (TextUtils.isEmpty(text)) { mBuilder.setError(getString(R.string.label_text_is_empty)); } else if (getString(R.string.default_download_label_name).equals(text)) { mBuilder.setError(getString(R.string.label_text_is_invalid)); - } else if (EhApplication.getDownloadManager(getContext()).containLabel(text)) { + } else if (EhApplication.getDownloadManager(context).containLabel(text)) { mBuilder.setError(getString(R.string.label_text_exist)); } else { mBuilder.setError(null); mDialog.dismiss(); - EhApplication.getDownloadManager(getContext()).renameLabel(mOriginalLabel, text); + EhApplication.getDownloadManager(context).renameLabel(mOriginalLabel, text); if (mAdapter != null) { mAdapter.notifyItemChanged(mPosition); } @@ -282,7 +301,8 @@ public class DownloadLabelsScene extends ToolbarScene { @Override public void onClick(View v) { - if (mList == null || mRecyclerView == null) { + Context context = getContext2(); + if (null == context || null == mList || null == mRecyclerView) { return; } @@ -294,7 +314,7 @@ public class DownloadLabelsScene extends ToolbarScene { if (label == v) { DownloadLabel raw = mList.get(index); EditTextDialogBuilder builder = new EditTextDialogBuilder( - getContext(), raw.getLabel(), getString(R.string.download_labels)); + context, raw.getLabel(), getString(R.string.download_labels)); builder.setTitle(R.string.rename_label_title); builder.setPositiveButton(android.R.string.ok, null); AlertDialog dialog = builder.show(); @@ -312,10 +332,16 @@ public class DownloadLabelsScene extends ToolbarScene { implements DraggableItemAdapter, SwipeableItemAdapter { + private final LayoutInflater mInflater; + + public LabelAdapter() { + mInflater = getLayoutInflater2(); + AssertUtils.assertNotNull(mInflater); + } + @Override public LabelHolder onCreateViewHolder(ViewGroup parent, int viewType) { - return new LabelHolder(getActivity().getLayoutInflater() - .inflate(R.layout.item_label_list, parent, false)); + return new LabelHolder(mInflater.inflate(R.layout.item_label_list, parent, false)); } @Override @@ -348,11 +374,12 @@ public class DownloadLabelsScene extends ToolbarScene { @Override public void onMoveItem(int fromPosition, int toPosition) { - if (fromPosition == toPosition) { + Context context = getContext2(); + if (null == context || fromPosition == toPosition) { return; } - EhApplication.getDownloadManager(getContext()).moveLabel(fromPosition, toPosition); + EhApplication.getDownloadManager(context).moveLabel(fromPosition, toPosition); if (mAdapter != null && mList != null) { notifyItemMoved(fromPosition, toPosition); } @@ -397,19 +424,20 @@ public class DownloadLabelsScene extends ToolbarScene { protected void onPerformAction() { super.onPerformAction(); + final Context context = getContext2(); final List list = mList; - if (list == null || mPosition < 0 || mPosition >= list.size()) { + if (null == context || null == list || mPosition < 0 || mPosition >= list.size()) { return; } final DownloadLabel label = list.get(mPosition); - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setTitle(R.string.delete_label_title) .setMessage(getString(R.string.delete_label_message, label.getLabel())) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { - EhApplication.getDownloadManager(getContext()).deleteLabel(label.getLabel()); + EhApplication.getDownloadManager(context).deleteLabel(label.getLabel()); } }) .setOnDismissListener(new DialogInterface.OnDismissListener() { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java index 366b45c29..5419bf17b 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/DownloadsScene.java @@ -18,8 +18,11 @@ package com.hippo.ehviewer.ui.scene; import android.annotation.SuppressLint; import android.annotation.TargetApi; +import android.app.Activity; +import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.res.Resources; import android.graphics.Point; import android.graphics.drawable.Drawable; import android.os.AsyncTask; @@ -34,6 +37,7 @@ import android.support.v7.widget.LinearLayoutManager; import android.support.v7.widget.RecyclerView; import android.support.v7.widget.SimpleItemAnimator; import android.support.v7.widget.Toolbar; +import android.util.Log; import android.util.SparseBooleanArray; import android.view.Display; import android.view.Gravity; @@ -68,6 +72,7 @@ import com.hippo.ehviewer.download.DownloadManager; import com.hippo.ehviewer.download.DownloadService; import com.hippo.ehviewer.spider.SpiderDen; import com.hippo.ehviewer.ui.GalleryActivity; +import com.hippo.ehviewer.ui.MainActivity; import com.hippo.ehviewer.widget.SimpleRatingView; import com.hippo.rippleold.RippleSalon; import com.hippo.scene.Announcer; @@ -77,6 +82,7 @@ import com.hippo.util.DrawableManager; import com.hippo.view.ViewTransition; import com.hippo.widget.FabLayout; import com.hippo.widget.LoadImageView; +import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.FileUtils; import com.hippo.yorozuya.LongList; import com.hippo.yorozuya.ObjectUtils; @@ -94,6 +100,8 @@ public class DownloadsScene extends ToolbarScene EasyRecyclerView.OnItemLongClickListener, FabLayout.OnClickFabListener, FastScroller.OnDragHandlerListener { + private static final String TAG = DownloadsScene.class.getSimpleName(); + public static final String KEY_ACTION = "action"; private static final String KEY_LABEL = "label"; @@ -145,7 +153,9 @@ public class DownloadsScene extends ToolbarScene handleArguments(getArguments()); - mDownloadManager = EhApplication.getDownloadManager(getContext()); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + mDownloadManager = EhApplication.getDownloadManager(context); mDownloadManager.addDownloadInfoListener(this); if (savedInstanceState == null) { @@ -162,11 +172,19 @@ public class DownloadsScene extends ToolbarScene DownloadManager manager = mDownloadManager; if (null == manager) { - manager = EhApplication.getDownloadManager(getContext()); + Context context = getContext2(); + if (null != context) { + manager = EhApplication.getDownloadManager(context); + } } else { mDownloadManager = null; } - manager.removeDownloadInfoListener(this); + + if (null != manager) { + manager.removeDownloadInfoListener(this); + } else { + Log.e(TAG, "Can't removeDownloadInfoListener"); + } } private void updateForLabel() { @@ -226,14 +244,17 @@ public class DownloadsScene extends ToolbarScene TextView tip = (TextView) ViewUtils.$$(view, R.id.tip); mViewTransition = new ViewTransition(content, tip); - Drawable drawable = DrawableManager.getDrawable(getContext(), R.drawable.big_download); + Context context = getContext2(); + AssertUtils.assertNotNull(content); + + Drawable drawable = DrawableManager.getDrawable(context, R.drawable.big_download); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); tip.setCompoundDrawables(null, drawable, null, null); mAdapter = new DownloadAdapter(); mAdapter.setHasStableIds(true); mRecyclerView.setAdapter(mAdapter); - mLayoutManager = new LinearLayoutManager(getContext()); + mLayoutManager = new LinearLayoutManager(context); mRecyclerView.setLayoutManager(mLayoutManager); mRecyclerView.setSelector(RippleSalon.generateRippleDrawable(false)); mRecyclerView.setDrawSelectorOnTop(true); @@ -248,13 +269,13 @@ public class DownloadsScene extends ToolbarScene if (itemAnimator instanceof SimpleItemAnimator) { ((SimpleItemAnimator) itemAnimator).setSupportsChangeAnimations(false); } - int paddingH = getResources().getDimensionPixelOffset(R.dimen.list_content_margin_h); - int paddingV = getResources().getDimensionPixelOffset(R.dimen.list_content_margin_v); + int paddingH = context.getResources().getDimensionPixelOffset(R.dimen.list_content_margin_h); + int paddingV = context.getResources().getDimensionPixelOffset(R.dimen.list_content_margin_v); mRecyclerView.setPadding(paddingV, paddingH, paddingV, paddingH); fastScroller.attachToRecyclerView(mRecyclerView); HandlerDrawable handlerDrawable = new HandlerDrawable(); - handlerDrawable.setColor(ResourcesUtils.getAttrColor(getContext(), R.attr.colorAccent)); + handlerDrawable.setColor(ResourcesUtils.getAttrColor(context, R.attr.colorAccent)); fastScroller.setHandlerDrawable(handlerDrawable); fastScroller.setOnDragHandlerListener(this); @@ -284,7 +305,8 @@ public class DownloadsScene extends ToolbarScene } private void guideDownloadThumb() { - if (!Settings.getGuideDownloadThumb() || null == mLayoutManager || null == mRecyclerView) { + MainActivity activity = getActivity2(); + if (null == activity || !Settings.getGuideDownloadThumb() || null == mLayoutManager || null == mRecyclerView) { guideDownloadLabels(); return; } @@ -299,7 +321,7 @@ public class DownloadsScene extends ToolbarScene return; } - new ShowcaseView.Builder(getActivity()) + new ShowcaseView.Builder(activity) .withMaterialShowcase() .setStyle(R.style.Guide) .setTarget(new ViewTarget(((DownloadHolder) holder).thumb)) @@ -318,15 +340,16 @@ public class DownloadsScene extends ToolbarScene } private void guideDownloadLabels() { - if (!Settings.getGuideDownloadLabels()) { + MainActivity activity = getActivity2(); + if (null == activity || !Settings.getGuideDownloadLabels()) { return; } - Display display = getActivity().getWindowManager().getDefaultDisplay(); + Display display = activity.getWindowManager().getDefaultDisplay(); Point point = new Point(); display.getSize(point); - new ShowcaseView.Builder(getActivity()) + new ShowcaseView.Builder(activity) .withMaterialShowcase() .setStyle(R.style.Guide) .setTarget(new PointTarget(point.x, point.y / 3)) @@ -375,16 +398,17 @@ public class DownloadsScene extends ToolbarScene @Override public boolean onMenuItemClick(MenuItem item) { // Skip when in choice mode - if (mRecyclerView == null || mRecyclerView.isInCustomChoice()) { + Activity activity = getActivity2(); + if (null == activity || null == mRecyclerView || mRecyclerView.isInCustomChoice()) { return false; } int id = item.getItemId(); switch (id) { case R.id.action_start_all: { - Intent intent = new Intent(getActivity(), DownloadService.class); + Intent intent = new Intent(activity, DownloadService.class); intent.setAction(DownloadService.ACTION_START_ALL); - getActivity().startService(intent); + activity.startService(intent); return true; } case R.id.action_stop_all: { @@ -412,6 +436,9 @@ public class DownloadsScene extends ToolbarScene @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.drawer_list, container, false); + final Context context = getContext2(); + AssertUtils.assertNotNull(context); + Toolbar toolbar = (Toolbar) view.findViewById(R.id.toolbar); toolbar.setTitle(R.string.download_labels); toolbar.inflateMenu(R.menu.drawer_download); @@ -436,7 +463,7 @@ public class DownloadsScene extends ToolbarScene for (int i = 0, n = list.size(); i < n; i++) { items[i + 2] = list.get(i).getLabel(); } - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setTitle(R.string.default_download_label) .setItems(items, new DialogInterface.OnClickListener() { @Override @@ -461,7 +488,7 @@ public class DownloadsScene extends ToolbarScene } }); - List list = EhApplication.getDownloadManager(getContext()).getLabelList(); + List list = EhApplication.getDownloadManager(context).getLabelList(); final List labels = new ArrayList<>(list.size() + 1); // Add default label name labels.add(getString(R.string.default_download_label_name)); @@ -471,7 +498,7 @@ public class DownloadsScene extends ToolbarScene // TODO handle download label items update ListView listView = (ListView) view.findViewById(R.id.list_view); - listView.setAdapter(new ArrayAdapter<>(getContext(), R.layout.item_simple_list, labels)); + listView.setAdapter(new ArrayAdapter<>(context, R.layout.item_simple_list, labels)); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override public void onItemClick(AdapterView parent, View view, int position, long id) { @@ -518,8 +545,9 @@ public class DownloadsScene extends ToolbarScene @Override public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) { + Activity activity = getActivity2(); EasyRecyclerView recyclerView = mRecyclerView; - if (recyclerView == null) { + if (null == activity || null == recyclerView) { return false; } @@ -535,7 +563,7 @@ public class DownloadsScene extends ToolbarScene return false; } - Intent intent = new Intent(getActivity(), GalleryActivity.class); + Intent intent = new Intent(activity, GalleryActivity.class); intent.setAction(GalleryActivity.ACTION_EH); intent.putExtra(GalleryActivity.KEY_GALLERY_INFO, list.get(position)); startActivity(intent); @@ -567,8 +595,10 @@ public class DownloadsScene extends ToolbarScene @Override public void onClickSecondaryFab(FabLayout view, FloatingActionButton fab, int position) { + Context context = getContext2(); + Activity activity = getActivity2(); EasyRecyclerView recyclerView = mRecyclerView; - if (recyclerView == null) { + if (null == context || null == activity || null == recyclerView) { return; } @@ -606,10 +636,10 @@ public class DownloadsScene extends ToolbarScene switch (position) { case 1: { // Start - Intent intent = new Intent(getActivity(), DownloadService.class); + Intent intent = new Intent(activity, DownloadService.class); intent.setAction(DownloadService.ACTION_START_RANGE); intent.putExtra(DownloadService.KEY_GID_LIST, gidList); - getActivity().startService(intent); + activity.startService(intent); // Cancel check mode recyclerView.outOfCustomChoiceMode(); break; @@ -623,7 +653,7 @@ public class DownloadsScene extends ToolbarScene break; } case 3: { // Delete - CheckBoxDialogBuilder builder = new CheckBoxDialogBuilder(getContext(), + CheckBoxDialogBuilder builder = new CheckBoxDialogBuilder(context, getString(R.string.download_remove_dialog_message_2, gidList.size()), getString(R.string.download_remove_dialog_check_text), Settings.getRemoveImageFiles()); @@ -635,7 +665,7 @@ public class DownloadsScene extends ToolbarScene break; } case 4: {// Move - List labelRawList = EhApplication.getDownloadManager(getContext()).getLabelList(); + List labelRawList = EhApplication.getDownloadManager(context).getLabelList(); List labelList = new ArrayList<>(labelRawList.size() + 1); labelList.add(getString(R.string.default_download_label_name)); for (int i = 0, n = labelRawList.size(); i < n; i++) { @@ -645,7 +675,7 @@ public class DownloadsScene extends ToolbarScene MoveDialogHelper helper = new MoveDialogHelper(labels, downloadInfoList); - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setTitle(R.string.download_move_dialog_title) .setItems(labels, helper) .show(); @@ -728,12 +758,17 @@ public class DownloadsScene extends ToolbarScene } private void bindForState(DownloadHolder holder, DownloadInfo info) { + Resources resources = getResources2(); + if (null == resources) { + return; + } + switch (info.state) { case DownloadInfo.STATE_NONE: - bindState(holder, info, getContext().getString(R.string.download_state_none)); + bindState(holder, info, resources.getString(R.string.download_state_none)); break; case DownloadInfo.STATE_WAIT: - bindState(holder, info, getContext().getString(R.string.download_state_wait)); + bindState(holder, info, resources.getString(R.string.download_state_wait)); break; case DownloadInfo.STATE_DOWNLOAD: bindProgress(holder, info); @@ -741,14 +776,14 @@ public class DownloadsScene extends ToolbarScene case DownloadInfo.STATE_FAILED: String text; if (info.legacy <= 0) { - text = getContext().getString(R.string.download_state_failed); + text = resources.getString(R.string.download_state_failed); } else { - text = getContext().getString(R.string.download_state_failed_2, info.legacy); + text = resources.getString(R.string.download_state_failed_2, info.legacy); } bindState(holder, info, text); break; case DownloadInfo.STATE_FINISH: - bindState(holder, info, getContext().getString(R.string.download_state_finish)); + bindState(holder, info, resources.getString(R.string.download_state_finish)); break; } } @@ -912,7 +947,11 @@ public class DownloadsScene extends ToolbarScene @Override public void onClick(DialogInterface dialog, int which) { // Cancel check mode - if (mRecyclerView != null) { + Context context = getContext2(); + if (null == context) { + return; + } + if (null != mRecyclerView) { mRecyclerView.outOfCustomChoiceMode(); } @@ -922,7 +961,7 @@ public class DownloadsScene extends ToolbarScene } else { label = mLabels[which]; } - EhApplication.getDownloadManager(getContext()).changeLabel(mDownloadInfoList, label); + EhApplication.getDownloadManager(context).changeLabel(mDownloadInfoList, label); } } @@ -969,8 +1008,10 @@ public class DownloadsScene extends ToolbarScene @Override public void onClick(View v) { + Context context = getContext2(); + Activity activity = getActivity2(); EasyRecyclerView recyclerView = mRecyclerView; - if (recyclerView == null || recyclerView.isInCustomChoice()) { + if (null == context || null == activity || null == recyclerView || recyclerView.isInCustomChoice()) { return; } List list = mList; @@ -993,17 +1034,17 @@ public class DownloadsScene extends ToolbarScene } startScene(announcer); } else if (start == v) { - Intent intent = new Intent(getActivity(), DownloadService.class); + Intent intent = new Intent(activity, DownloadService.class); intent.setAction(DownloadService.ACTION_START); intent.putExtra(DownloadService.KEY_GALLERY_INFO, list.get(index)); - getActivity().startService(intent); + activity.startService(intent); } else if (stop == v) { if (null != mDownloadManager) { mDownloadManager.stopDownload(list.get(index).gid); } } else if (delete == v) { GalleryInfo galleryInfo = list.get(index); - CheckBoxDialogBuilder builder = new CheckBoxDialogBuilder(getContext(), + CheckBoxDialogBuilder builder = new CheckBoxDialogBuilder(context, getString(R.string.download_remove_dialog_message, EhUtils.getSuitableTitle(galleryInfo)), getString(R.string.download_remove_dialog_check_text), Settings.getRemoveImageFiles()); @@ -1017,6 +1058,13 @@ public class DownloadsScene extends ToolbarScene private class DownloadAdapter extends RecyclerView.Adapter { + private final LayoutInflater mInflater; + + public DownloadAdapter() { + mInflater = getLayoutInflater2(); + AssertUtils.assertNotNull(mInflater); + } + @Override public long getItemId(int position) { if (mList == null || position < 0 || position >= mList.size()) { @@ -1027,7 +1075,7 @@ public class DownloadsScene extends ToolbarScene @Override public DownloadHolder onCreateViewHolder(ViewGroup parent, int viewType) { - return new DownloadHolder(getActivity().getLayoutInflater().inflate(R.layout.item_download, parent, false)); + return new DownloadHolder(mInflater.inflate(R.layout.item_download, parent, false)); } @TargetApi(Build.VERSION_CODES.LOLLIPOP) diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java index 3c017bcd4..b52a97af2 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/FavoritesScene.java @@ -16,6 +16,7 @@ package com.hippo.ehviewer.ui.scene; +import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.res.Resources; @@ -61,6 +62,7 @@ import com.hippo.ehviewer.client.data.FavListUrlBuilder; import com.hippo.ehviewer.client.data.GalleryInfo; import com.hippo.ehviewer.client.exception.NotFoundException; import com.hippo.ehviewer.client.parser.FavoritesParser; +import com.hippo.ehviewer.ui.MainActivity; import com.hippo.ehviewer.ui.annotation.DrawerLifeCircle; import com.hippo.ehviewer.ui.annotation.ViewLifeCircle; import com.hippo.ehviewer.ui.annotation.WholeLifeCircle; @@ -68,7 +70,6 @@ import com.hippo.ehviewer.widget.SearchBar; import com.hippo.rippleold.RippleSalon; import com.hippo.scene.Announcer; import com.hippo.scene.SceneFragment; -import com.hippo.scene.StageActivity; import com.hippo.util.ApiHelper; import com.hippo.util.DrawableManager; import com.hippo.widget.ContentLayout; @@ -76,6 +77,7 @@ import com.hippo.widget.FabLayout; import com.hippo.widget.SearchBarMover; import com.hippo.widget.recyclerview.GridAutoSpanLayoutManager; import com.hippo.widget.refreshlayout.RefreshLayout; +import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.ObjectUtils; import com.hippo.yorozuya.SimpleHandler; import com.hippo.yorozuya.ViewUtils; @@ -181,7 +183,9 @@ public class FavoritesScene extends BaseScene implements public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mClient = EhApplication.getEhClient(getContext()); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + mClient = EhApplication.getEhClient(context); mFavCatArray = Settings.getFavCat(); if (savedInstanceState == null) { @@ -248,14 +252,16 @@ public class FavoritesScene extends BaseScene implements mSearchBar = (SearchBar) ViewUtils.$$(view, R.id.search_bar); mFabLayout = (FabLayout) ViewUtils.$$(view, R.id.fab_layout); - Resources resources = getResources(); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + Resources resources = context.getResources(); int paddingTopSB = resources.getDimensionPixelOffset(R.dimen.list_padding_top_search_bar); mHelper = new FavoritesHelper(); contentLayout.setHelper(mHelper); contentLayout.getFastScroller().setOnDragHandlerListener(this); - GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(getContext(), 0); + GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(context, 0); mRecyclerView.setLayoutManager(layoutManager); mRecyclerView.setSelector(RippleSalon.generateRippleDrawable(false)); mRecyclerView.setDrawSelectorOnTop(true); @@ -265,8 +271,8 @@ public class FavoritesScene extends BaseScene implements mRecyclerView.setOnItemLongClickListener(this); mRecyclerView.setChoiceMode(EasyRecyclerView.CHOICE_MODE_MULTIPLE_CUSTOM); mRecyclerView.setCustomCheckedListener(this); - mAdapter = new FavoritesAdapter(LayoutInflater.from(getContext()), - getContext(), mRecyclerView, layoutManager, Settings.getListMode()); + mAdapter = new FavoritesAdapter(getLayoutInflater2(), + getResources2(), mRecyclerView, layoutManager, Settings.getListMode()); mRecyclerView.setAdapter(mAdapter); mAdapter.register(); mRecyclerView.setPadding(mRecyclerView.getPaddingLeft(), mRecyclerView.getPaddingTop() + paddingTopSB, @@ -277,9 +283,9 @@ public class FavoritesScene extends BaseScene implements refreshLayout.setHeaderTranslationY(paddingTopSB); - mLeftDrawable = new DrawerArrowDrawable(getContext()); + mLeftDrawable = new DrawerArrowDrawable(context); mSearchBar.setLeftDrawable(mLeftDrawable); - mSearchBar.setRightDrawable(DrawableManager.getDrawable(getContext(), R.drawable.v_magnify_x24)); + mSearchBar.setRightDrawable(DrawableManager.getDrawable(context, R.drawable.v_magnify_x24)); mSearchBar.setHelper(this); mSearchBar.setAllowEmptySearch(false); updateSearchBar(); @@ -308,15 +314,16 @@ public class FavoritesScene extends BaseScene implements } private void guideCollections() { - if (!Settings.getGuideCollections()) { + Activity activity = getActivity2(); + if (null == activity || !Settings.getGuideCollections()) { return; } - Display display = getActivity().getWindowManager().getDefaultDisplay(); + Display display = activity.getWindowManager().getDefaultDisplay(); Point point = new Point(); display.getSize(point); - new ShowcaseView.Builder(getActivity()) + new ShowcaseView.Builder(activity) .withMaterialShowcase() .setStyle(R.style.Guide) .setTarget(new PointTarget(point.x, point.y / 3)) @@ -337,7 +344,8 @@ public class FavoritesScene extends BaseScene implements // keyword of mUrlBuilder, fav cat of mUrlBuilder, mFavCatArray. // They changed, call it private void updateSearchBar() { - if (mUrlBuilder == null || mSearchBar == null || mFavCatArray == null) { + Context context = getContext2(); + if (null == context || null == mUrlBuilder || null == mSearchBar || null == mFavCatArray) { return; } @@ -364,7 +372,7 @@ public class FavoritesScene extends BaseScene implements // Update hint if (!ObjectUtils.equal(favCatName, mOldFavCat)) { - Drawable searchImage = DrawableManager.getDrawable(getContext(), R.drawable.v_magnify_x24); + Drawable searchImage = DrawableManager.getDrawable(context, R.drawable.v_magnify_x24); SpannableStringBuilder ssb = new SpannableStringBuilder(" "); ssb.append(getString(R.string.favorites_search_bar_hint, favCatName)); int textSize = (int) (mSearchBar.getEditTextTextSize() * 1.25); @@ -409,7 +417,8 @@ public class FavoritesScene extends BaseScene implements } private void showFavoritesInfoDialog() { - if (mFavCatArray == null || mFavCountArray == null) { + Context context = getContext2(); + if (null == context || null == mFavCatArray || null == mFavCountArray) { return; } @@ -419,7 +428,7 @@ public class FavoritesScene extends BaseScene implements sb.append(mFavCatArray[i]).append(": ").append(mFavCountArray[i]).append("\n"); } - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setTitle(R.string.favorites_info) .setMessage(sb.toString()) .show(); @@ -432,6 +441,9 @@ public class FavoritesScene extends BaseScene implements Toolbar toolbar = (Toolbar) ViewUtils.$$(view, R.id.toolbar); ListView listView = (ListView) view.findViewById(R.id.list_view); + final Context context = getContext2(); + AssertUtils.assertNotNull(context); + toolbar.setTitle(R.string.collections); toolbar.inflateMenu(R.menu.drawer_favorites); toolbar.setOnMenuItemClickListener(new Toolbar.OnMenuItemClickListener() { @@ -448,7 +460,7 @@ public class FavoritesScene extends BaseScene implements items[1] = getString(R.string.local_favorites); String[] favCat = Settings.getFavCat(); System.arraycopy(favCat, 0, items, 2, 10); - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setTitle(R.string.default_favorites_collection) .setItems(items, new DialogInterface.OnClickListener() { @Override @@ -468,7 +480,7 @@ public class FavoritesScene extends BaseScene implements if (mFavCatArray != null) { Collections.addAll(mDrawerList, mFavCatArray); } - mDrawerAdapter = new ArrayAdapter<>(getContext(), R.layout.item_simple_list, mDrawerList); + mDrawerAdapter = new ArrayAdapter<>(context, R.layout.item_simple_list, mDrawerList); listView.setAdapter(mDrawerAdapter); listView.setOnItemClickListener(this); @@ -656,7 +668,8 @@ public class FavoritesScene extends BaseScene implements @Override @Implemented(FabLayout.OnClickFabListener.class) public void onClickSecondaryFab(FabLayout view, FloatingActionButton fab, int position) { - if (mRecyclerView == null || mHelper == null || !mRecyclerView.isInCustomChoice()) { + Context context = getContext2(); + if (null == context || null == mRecyclerView || null == mHelper || !mRecyclerView.isInCustomChoice()) { return; } @@ -671,7 +684,7 @@ public class FavoritesScene extends BaseScene implements switch (position) { case 0: { // Delete DeleteDialogHelper helper = new DeleteDialogHelper(); - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setTitle(R.string.delete_favorites_dialog_title) .setMessage(getString(R.string.delete_favorites_dialog_message, mModifyGiList.size())) .setPositiveButton(android.R.string.ok, helper) @@ -685,7 +698,7 @@ public class FavoritesScene extends BaseScene implements String[] array = new String[11]; array[0] = getString(R.string.local_favorites); System.arraycopy(Settings.getFavCat(), 0, array, 1, 10); - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setTitle(R.string.move_favorites_dialog_title) .setItems(array, helper) .setOnCancelListener(helper) @@ -930,9 +943,9 @@ public class FavoritesScene extends BaseScene implements private class FavoritesAdapter extends GalleryAdapter { - public FavoritesAdapter(LayoutInflater inflater, Context context, + public FavoritesAdapter(LayoutInflater inflater, Resources resources, RecyclerView recyclerView, GridAutoSpanLayoutManager layoutManager, int type) { - super(inflater, context, recyclerView, layoutManager, type); + super(inflater, resources, recyclerView, layoutManager, type); } @Override @@ -951,7 +964,8 @@ public class FavoritesScene extends BaseScene implements @Override protected void getPageData(final int taskId, int type, int page) { - if (mUrlBuilder == null || mClient == null) { + MainActivity activity = getActivity2(); + if (null == activity || null == mUrlBuilder || null == mClient ) { return; } @@ -973,7 +987,7 @@ public class FavoritesScene extends BaseScene implements EhRequest request = new EhRequest(); request.setMethod(EhClient.METHOD_ADD_FAVORITES_RANGE); request.setCallback(new AddFavoritesListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag(), + activity.getStageId(), getTag(), taskId, mUrlBuilder.getKeyword())); request.setArgs(gidArray, tokenArray, mModifyFavCat); mClient.execute(request); @@ -996,7 +1010,7 @@ public class FavoritesScene extends BaseScene implements EhRequest request = new EhRequest(); request.setMethod(EhClient.METHOD_MODIFY_FAVORITES); request.setCallback(new GetFavoritesListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag(), + activity.getStageId(), getTag(), taskId, local, mUrlBuilder.getKeyword())); request.setArgs(url, gidArray, mModifyFavCat, Settings.getShowJpnTitle()); mClient.execute(request); @@ -1015,7 +1029,7 @@ public class FavoritesScene extends BaseScene implements EhRequest request = new EhRequest(); request.setMethod(EhClient.METHOD_GET_FAVORITES); request.setCallback(new GetFavoritesListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag(), + activity.getStageId(), getTag(), taskId, false, mUrlBuilder.getKeyword())); request.setArgs(url, Settings.getShowJpnTitle()); mClient.execute(request); @@ -1032,7 +1046,7 @@ public class FavoritesScene extends BaseScene implements @Override protected Context getContext() { - return FavoritesScene.this.getContext(); + return FavoritesScene.this.getContext2(); } @Override diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java index e64fea1f6..2fca5d772 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryAdapter.java @@ -17,11 +17,11 @@ package com.hippo.ehviewer.ui.scene; import android.annotation.TargetApi; -import android.content.Context; import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Build; import android.support.annotation.IntDef; +import android.support.annotation.NonNull; import android.support.annotation.Nullable; import android.support.v7.widget.RecyclerView; import android.view.LayoutInflater; @@ -55,18 +55,16 @@ abstract class GalleryAdapter extends RecyclerView.Adapter implem public static final int TYPE_GRID = 1; private final LayoutInflater mInflater; - private final Context mContext; private final Resources mResources; private final RecyclerView mRecyclerView; private final GridAutoSpanLayoutManager mLayoutManager; private RecyclerView.ItemDecoration mGirdDecoration; private int mType = TYPE_INVALID; - public GalleryAdapter(LayoutInflater inflater, Context context, - RecyclerView recyclerView, GridAutoSpanLayoutManager layoutManager, int type) { + public GalleryAdapter(@NonNull LayoutInflater inflater, @NonNull Resources resources, + @NonNull RecyclerView recyclerView, @NonNull GridAutoSpanLayoutManager layoutManager, int type) { mInflater = inflater; - mContext = context; - mResources = context.getResources(); + mResources = resources; mRecyclerView = recyclerView; mLayoutManager = layoutManager; 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 d770cd8cb..093f087d6 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 @@ -18,7 +18,9 @@ package com.hippo.ehviewer.ui.scene; import android.animation.Animator; import android.annotation.TargetApi; +import android.app.Activity; import android.content.Context; +import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Build; import android.os.Bundle; @@ -48,18 +50,18 @@ import com.hippo.ehviewer.client.EhClient; import com.hippo.ehviewer.client.EhRequest; import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.data.GalleryComment; +import com.hippo.ehviewer.ui.MainActivity; import com.hippo.rippleold.RippleSalon; import com.hippo.scene.SceneFragment; -import com.hippo.scene.StageActivity; import com.hippo.text.Html; import com.hippo.text.URLImageGetter; -import com.hippo.util.ActivityHelper; import com.hippo.util.DrawableManager; import com.hippo.util.ReadableTime; import com.hippo.util.TextUrl; import com.hippo.view.ViewTransition; import com.hippo.widget.LinkifyTextView; import com.hippo.yorozuya.AnimationUtils; +import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.LayoutUtils; import com.hippo.yorozuya.SimpleAnimatorListener; import com.hippo.yorozuya.ViewUtils; @@ -143,19 +145,22 @@ public final class GalleryCommentsScene extends ToolbarScene mEditText = (EditText) ViewUtils.$$(mEditPanel, R.id.edit_text); mFab = (FloatingActionButton) ViewUtils.$$(view, R.id.fab); - int paddingBottomFab = getResources().getDimensionPixelOffset(R.dimen.list_padding_bottom_fab); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + Resources resources = context.getResources(); + int paddingBottomFab = resources.getDimensionPixelOffset(R.dimen.list_padding_bottom_fab); - Drawable drawable = DrawableManager.getDrawable(getContext(), R.drawable.big_weird_face); + Drawable drawable = DrawableManager.getDrawable(context, R.drawable.big_weird_face); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); tip.setCompoundDrawables(null, drawable, null, null); mAdapter = new CommentAdapter(); recyclerView.setAdapter(mAdapter); - recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), + recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)); LinearDividerItemDecoration decoration = new LinearDividerItemDecoration( - LinearDividerItemDecoration.VERTICAL, getResources().getColor(R.color.divider), - LayoutUtils.dp2pix(getContext(), 1)); + LinearDividerItemDecoration.VERTICAL, context.getResources().getColor(R.color.divider), + LayoutUtils.dp2pix(context, 1)); decoration.setShowLastDivider(true); recyclerView.addItemDecoration(decoration); recyclerView.setSelector(RippleSalon.generateRippleDrawable(false)); @@ -200,6 +205,11 @@ public final class GalleryCommentsScene extends ToolbarScene @Override public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) { + Activity activity = getActivity2(); + if (null == activity) { + return false; + } + RecyclerView.ViewHolder holder = parent.getChildViewHolder(view); if (holder instanceof CommentHolder) { CommentHolder commentHolder = (CommentHolder) holder; @@ -207,7 +217,7 @@ public final class GalleryCommentsScene extends ToolbarScene commentHolder.comment.clearCurrentSpan(); if (span instanceof URLSpan) { - UrlOpener.openUrl(getActivity(), ((URLSpan) span).getURL(), true, true); + UrlOpener.openUrl(activity, ((URLSpan) span).getURL(), true, true); return true; } } @@ -322,6 +332,12 @@ public final class GalleryCommentsScene extends ToolbarScene @Override public void onClick(View v) { + Context context = getContext2(); + MainActivity activity = getActivity2(); + if (null == context || null == activity) { + return; + } + if (mFab == v) { if (!mInAnimation) { showEditPanel(true); @@ -341,10 +357,10 @@ public final class GalleryCommentsScene extends ToolbarScene EhRequest request = new EhRequest() .setMethod(EhClient.METHOD_GET_COMMENT_GALLERY) .setArgs(url, comment) - .setCallback(new CommentGalleryListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag())); - EhApplication.getEhClient(getContext()).execute(request); - ActivityHelper.hideSoftInput(getActivity()); + .setCallback(new CommentGalleryListener(context, + activity.getStageId(), getTag())); + EhApplication.getEhClient(context).execute(request); + hideSoftInput(); hideEditPanel(true); } } @@ -378,19 +394,30 @@ public final class GalleryCommentsScene extends ToolbarScene private class CommentAdapter extends RecyclerView.Adapter { + private final LayoutInflater mInflater; + + public CommentAdapter() { + mInflater = getLayoutInflater2(); + AssertUtils.assertNotNull(mInflater); + } + @Override public CommentHolder onCreateViewHolder(ViewGroup parent, int viewType) { - return new CommentHolder(getActivity().getLayoutInflater() - .inflate(R.layout.item_gallery_comment, parent, false)); + return new CommentHolder(mInflater.inflate(R.layout.item_gallery_comment, parent, false)); } @Override public void onBindViewHolder(CommentHolder holder, int position) { + Context context = getContext2(); + if (null == context) { + return; + } + GalleryComment comment = mComments[position]; holder.user.setText(comment.user); holder.time.setText(ReadableTime.getTimeAgo(comment.time)); holder.comment.setText(TextUrl.handleTextUrl(Html.fromHtml(comment.comment, - new URLImageGetter(holder.comment, EhApplication.getConaco(getContext())), null))); + new URLImageGetter(holder.comment, EhApplication.getConaco(context)), null))); } @Override 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 84a17d479..344e38607 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 @@ -16,8 +16,8 @@ package com.hippo.ehviewer.ui.scene; -import android.annotation.SuppressLint; import android.annotation.TargetApi; +import android.app.Activity; import android.app.Dialog; import android.app.DownloadManager; import android.content.Context; @@ -81,7 +81,6 @@ import com.hippo.ehviewer.ui.annotation.WholeLifeCircle; import com.hippo.rippleold.RippleSalon; import com.hippo.scene.Announcer; import com.hippo.scene.SceneFragment; -import com.hippo.scene.StageActivity; import com.hippo.scene.TransitionHelper; import com.hippo.text.Html; import com.hippo.text.URLImageGetter; @@ -96,6 +95,7 @@ import com.hippo.widget.LoadImageView; import com.hippo.widget.ProgressView; import com.hippo.widget.ProgressiveRatingBar; import com.hippo.widget.recyclerview.SimpleGridAutoSpanLayout; +import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.SimpleHandler; import com.hippo.yorozuya.ViewUtils; @@ -347,7 +347,9 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene long gid = getGid(); if (gid != -1) { - mDownloadState = EhApplication.getDownloadManager(getContext()).getDownloadState(gid); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + mDownloadState = EhApplication.getDownloadManager(context).getDownloadState(gid); } else { mDownloadState = DownloadInfo.STATE_INVALID; } @@ -398,7 +400,10 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene mTip = (TextView) ViewUtils.$$(main, R.id.tip); mViewTransition = new ViewTransition(mainView, progressView, mTip); - Drawable drawable = DrawableManager.getDrawable(getContext(), R.drawable.big_weird_face); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + + Drawable drawable = DrawableManager.getDrawable(context, R.drawable.big_weird_face); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); mTip.setCompoundDrawables(null, drawable, null, null); mTip.setOnClickListener(this); @@ -491,7 +496,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene adjustViewVisibility(STATE_FAILED, false); } - EhApplication.getDownloadManager(getContext()).addDownloadInfoListener(this); + EhApplication.getDownloadManager(context).addDownloadInfoListener(this); return view; } @@ -505,7 +510,9 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene public void onDestroyView() { super.onDestroyView(); - EhApplication.getDownloadManager(getContext()).removeDownloadInfoListener(this); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + EhApplication.getDownloadManager(context).removeDownloadInfoListener(this); mTip = null; mViewTransition = null; @@ -557,6 +564,9 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } private boolean prepareData() { + Context context = getContext2(); + AssertUtils.assertNotNull(context); + if (mGalleryDetail != null) { return true; } @@ -567,12 +577,12 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } // Get from cache - mGalleryDetail = EhApplication.getGalleryDetailCache(getContext()).get(gid); + mGalleryDetail = EhApplication.getGalleryDetailCache(context).get(gid); if (mGalleryDetail != null) { return true; } - EhApplication application = (EhApplication) getContext().getApplicationContext(); + EhApplication application = (EhApplication) context.getApplicationContext(); if (application.containGlobalStuff(mRequestId)) { // request exist return true; @@ -583,19 +593,21 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } private boolean request() { + Context context = getContext2(); + MainActivity activity = getActivity2(); String url = getGalleryDetailUrl(true); - if (url == null) { + if (null == context || null == activity || null == url) { return false; } - EhClient.Callback callback = new GetGalleryDetailListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag()); - mRequestId = ((EhApplication) getContext().getApplicationContext()).putGlobalStuff(callback); + EhClient.Callback callback = new GetGalleryDetailListener(context, + activity.getStageId(), getTag()); + mRequestId = ((EhApplication) context.getApplicationContext()).putGlobalStuff(callback); EhRequest request = new EhRequest() .setMethod(EhClient.METHOD_GET_GALLERY_DETAIL) .setArgs(url) .setCallback(callback); - EhApplication.getEhClient(getContext()).execute(request); + EhApplication.getEhClient(context).execute(request); return true; } @@ -606,15 +618,17 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } private void ensureActionDrawable() { - Drawable heart = DrawableManager.getDrawable(getContext(), R.drawable.v_heart_primary_x48); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + Drawable heart = DrawableManager.getDrawable(context, R.drawable.v_heart_primary_x48); setActionDrawable(mHeart, heart); - Drawable heartOutline = DrawableManager.getDrawable(getContext(), R.drawable.v_heart_outline_primary_x48); + Drawable heartOutline = DrawableManager.getDrawable(context, R.drawable.v_heart_outline_primary_x48); setActionDrawable(mHeartOutline, heartOutline); - Drawable torrent = DrawableManager.getDrawable(getContext(), R.drawable.v_utorrent_primary_x48); + Drawable torrent = DrawableManager.getDrawable(context, R.drawable.v_utorrent_primary_x48); setActionDrawable(mTorrent, torrent); - Drawable share = DrawableManager.getDrawable(getContext(), R.drawable.v_share_primary_x48); + Drawable share = DrawableManager.getDrawable(context, R.drawable.v_share_primary_x48); setActionDrawable(mShare, share); - Drawable rate = DrawableManager.getDrawable(getContext(), R.drawable.v_thumb_up_primary_x48); + Drawable rate = DrawableManager.getDrawable(context, R.drawable.v_thumb_up_primary_x48); setActionDrawable(mRate, rate); } @@ -733,7 +747,8 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene return; } - Resources resources = getContext().getResources(); + Resources resources = getResources2(); + AssertUtils.assertNotNull(resources); mThumb.load(EhCacheKeyFactory.getThumbKey(gd.gid), gd.thumb, true); mTitle.setText(EhUtils.getSuitableTitle(gd)); @@ -763,7 +778,10 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene @SuppressWarnings("deprecation") private void bindTags(GalleryTagGroup[] tagGroups) { - if (mTags == null || mNoTags == null) { + Context context = getContext2(); + LayoutInflater inflater = getLayoutInflater2(); + Resources resources = getResources2(); + if (null == context || null == inflater || null == resources || null == mTags || null == mNoTags) { return; } @@ -775,9 +793,8 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene mNoTags.setVisibility(View.GONE); } - LayoutInflater inflater = getActivity().getLayoutInflater(); - int colorTag = getResources().getColor(R.color.colorPrimary); - int colorName = getResources().getColor(R.color.purple_a400); + int colorTag = resources.getColor(R.color.colorPrimary); + int colorName = resources.getColor(R.color.purple_a400); for (GalleryTagGroup tg : tagGroups) { LinearLayout ll = (LinearLayout) inflater.inflate(R.layout.gallery_tag_group, mTags, false); ll.setOrientation(LinearLayout.HORIZONTAL); @@ -788,7 +805,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene tgName.setText(tg.groupName); tgName.setBackgroundDrawable(new RoundSideRectDrawable(colorName)); - AutoWrapLayout awl = new AutoWrapLayout(getContext()); + AutoWrapLayout awl = new AutoWrapLayout(context); ll.addView(awl, ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); for (int j = 0, z = tg.size(); j < z; j++) { TextView tag = (TextView) inflater.inflate(R.layout.item_gallery_tag, awl, false); @@ -804,7 +821,9 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } private void bindComments(GalleryComment[] comments) { - if (mComments == null || mCommentsText == null) { + Context context = getContext2(); + LayoutInflater inflater = getLayoutInflater2(); + if (null == context || null == inflater || null == mComments || null == mCommentsText) { return; } @@ -820,7 +839,6 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene mCommentsText.setText(R.string.more_comment); } - LayoutInflater inflater = getActivity().getLayoutInflater(); int length = Math.min(maxShowCount, comments.length); for (int i = 0; i < length; i++) { GalleryComment comment = comments[i]; @@ -833,12 +851,14 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene TextView c = (TextView) v.findViewById(R.id.comment); c.setMaxLines(5); c.setText(Html.fromHtml(comment.comment, - new URLImageGetter(c, EhApplication.getConaco(getContext())), null)); + new URLImageGetter(c, EhApplication.getConaco(context)), null)); } } private void bindPreviews(GalleryDetail gd) { - if (mGridLayout == null || mPreviewText == null) { + LayoutInflater inflater = getLayoutInflater2(); + Resources resources = getResources2(); + if (null == inflater || null == resources || null == mGridLayout || null == mPreviewText) { return; } @@ -853,10 +873,9 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene mPreviewText.setText(R.string.more_previews); } - int columnWidth = getResources().getDimensionPixelOffset(R.dimen.preview_grid_column_width); + int columnWidth = resources.getDimensionPixelOffset(R.dimen.preview_grid_column_width); mGridLayout.setColumnSize(columnWidth); mGridLayout.setStrategy(SimpleGridAutoSpanLayout.STRATEGY_SUITABLE_SIZE); - LayoutInflater inflater = getActivity().getLayoutInflater(); for (int i = 0, size = previewSet.size(); i < size; i++) { View view = inflater.inflate(R.layout.item_gallery_preview, mGridLayout, false); mGridLayout.addView(view); @@ -912,7 +931,8 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } private String getAllRatingText(float rating, int ratedTimes) { - Resources resources = getResources(); + Resources resources = getResources2(); + AssertUtils.assertNotNull(resources); return resources.getString(R.string.rating_text, getRatingText(rating, resources), rating, ratedTimes); } @@ -934,7 +954,9 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene return; } - PopupMenu popup = new PopupMenu(getContext(), mOtherActions, Gravity.TOP); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + PopupMenu popup = new PopupMenu(context, mOtherActions, Gravity.TOP); mPopupMenu = popup; popup.getMenuInflater().inflate(R.menu.scene_gallery_detail, popup.getMenu()); popup.setOnMenuItemClickListener(new PopupMenu.OnMenuItemClickListener() { @@ -943,8 +965,9 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene switch (item.getItemId()) { case R.id.action_open_in_other_app: String url = getGalleryDetailUrl(false); - if (url != null) { - UrlOpener.openUrl(getActivity(), url, false, false); + Activity activity = getActivity2(); + if (null != url && null != activity) { + UrlOpener.openUrl(activity, url, false, false); } break; case R.id.action_refresh: @@ -961,6 +984,12 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene @Override public void onClick(View v) { + Context context = getContext2(); + MainActivity activity = getActivity2(); + if (null == context || null == activity) { + return; + } + if (mTip == v) { if (request()) { adjustViewVisibility(STATE_REFRESH, true); @@ -990,7 +1019,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } else if (mDownload == v) { GalleryInfo galleryInfo = getGalleryInfo(); if (galleryInfo != null) { - CommonOperations.startDownload((MainActivity) getActivity(), galleryInfo, false); + CommonOperations.startDownload(activity, galleryInfo, false); } } else if (mRead == v) { GalleryInfo galleryInfo = null; @@ -1000,7 +1029,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene galleryInfo = mGalleryDetail; } if (galleryInfo != null) { - Intent intent = new Intent(getActivity(), GalleryActivity.class); + Intent intent = new Intent(activity, GalleryActivity.class); intent.setAction(GalleryActivity.ACTION_EH); intent.putExtra(GalleryActivity.KEY_GALLERY_INFO, galleryInfo); startActivity(intent); @@ -1018,16 +1047,16 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } if (mGalleryDetail.isFavored) { mModifingFavorites = true; - CommonOperations.removeFromFavorites(getActivity(), mGalleryDetail, - new ModifyFavoritesListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag(), true)); + CommonOperations.removeFromFavorites(activity, mGalleryDetail, + new ModifyFavoritesListener(context, + activity.getStageId(), getTag(), true)); remove = true; } if (!remove) { mModifingFavorites = true; - CommonOperations.addToFavorites(getActivity(), mGalleryDetail, - new ModifyFavoritesListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag(), false)); + CommonOperations.addToFavorites(activity, mGalleryDetail, + new ModifyFavoritesListener(context, + activity.getStageId(), getTag(), false)); } // Update UI updateFavoriteDrawable(); @@ -1035,29 +1064,30 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } else if (mShare == v) { String url = getGalleryDetailUrl(false); if (url != null) { - ActivityHelper.share(getActivity(), url); + ActivityHelper.share(activity, url); } } else if (mTorrent == v) { if (mGalleryDetail != null) { - TorrentListDialogHelper helper = new TorrentListDialogHelper(mGalleryDetail.torrentUrl); - Dialog dialog = new AlertDialog.Builder(getContext()) + TorrentListDialogHelper helper = new TorrentListDialogHelper(); + Dialog dialog = new AlertDialog.Builder(context) .setTitle(R.string.torrents) - .setView(helper.getView()) + .setView(R.layout.dialog_torrent_list) .setOnDismissListener(helper) .show(); - helper.setDialog(dialog); + helper.setDialog(dialog, mGalleryDetail.torrentUrl); } } else if (mRate == v) { if (mGalleryDetail == null) { return; } - RateDialogHelper helper = new RateDialogHelper(mGalleryDetail.rating); - new AlertDialog.Builder(getContext()) + RateDialogHelper helper = new RateDialogHelper(); + Dialog dialog = new AlertDialog.Builder(context) .setTitle(R.string.rate) - .setView(helper.view) + .setView(R.layout.dialog_rate) .setNegativeButton(android.R.string.cancel, null) .setPositiveButton(android.R.string.ok, helper) .show(); + helper.setDialog(dialog, mGalleryDetail.rating); } else if (mComments == v) { if (mGalleryDetail == null) { return; @@ -1086,10 +1116,9 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene return; } - Context context = getContext(); GalleryInfo galleryInfo = getGalleryInfo(); o = v.getTag(R.id.index); - if (null != context && null != galleryInfo && o instanceof Integer) { + if (null != galleryInfo && o instanceof Integer) { int index = (Integer) o; Intent intent = new Intent(context, GalleryActivity.class); intent.setAction(GalleryActivity.ACTION_EH); @@ -1102,12 +1131,16 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } private void showBlockUploaderDialog() { + Context context = getContext2(); + if (null == context) { + return; + } final String uploader = getUploader(); if (null == uploader) { return; } - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setMessage(getString(R.string.block_the_uploader, uploader)) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override @@ -1127,7 +1160,12 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } private void showBlockTagDialog(final String tag) { - new AlertDialog.Builder(getContext()) + Context context = getContext2(); + if (null == context) { + return; + } + + new AlertDialog.Builder(context) .setMessage(getString(R.string.block_the_tag, tag)) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { @Override @@ -1148,12 +1186,17 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene @Override public boolean onLongClick(View v) { + MainActivity activity = getActivity2(); + if (null == activity) { + return false; + } + if (mUploader == v) { showBlockUploaderDialog(); } else if (mDownload == v) { GalleryInfo galleryInfo = getGalleryInfo(); if (galleryInfo != null) { - CommonOperations.startDownload((MainActivity) getActivity(), galleryInfo, true); + CommonOperations.startDownload(activity, galleryInfo, true); } return true; } else { @@ -1230,12 +1273,13 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } private void updateDownloadState() { + Context context = getContext2(); long gid = getGid(); - if (-1L == gid) { + if (null == context || -1L == gid) { return; } - int downloadState = EhApplication.getDownloadManager(getContext()).getDownloadState(gid); + int downloadState = EhApplication.getDownloadManager(context).getDownloadState(gid); if (downloadState == mDownloadState) { return; } @@ -1320,12 +1364,11 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene private void onGetGalleryDetailFailure(Exception e) { e.printStackTrace(); - if (isViewCreated()) { - String error = ExceptionUtils.getReadableString(getContext(), e); - if (mTip != null) { - mTip.setText(error); - adjustViewVisibility(STATE_FAILED, true); - } + Context context = getContext2(); + if (null != context && null != mTip) { + String error = ExceptionUtils.getReadableString(context, e); + mTip.setText(error); + adjustViewVisibility(STATE_FAILED, true); } } @@ -1390,42 +1433,41 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene private class TorrentListDialogHelper implements AdapterView.OnItemClickListener, DialogInterface.OnDismissListener, EhClient.Callback[]> { - private final View mView; - private final ProgressView mProgressView; - private final TextView mErrorText; - private final ListView mListView; - + @Nullable + private ProgressView mProgressView; + @Nullable + private TextView mErrorText; + @Nullable + private ListView mListView; @Nullable private EhRequest mRequest; @Nullable private Dialog mDialog; - @SuppressLint("InflateParams") - public TorrentListDialogHelper(String url) { - mView = getActivity().getLayoutInflater().inflate(R.layout.dialog_torrent_list, null); - mProgressView = (ProgressView) ViewUtils.$$(mView, R.id.progress); - mErrorText = (TextView) ViewUtils.$$(mView, R.id.text); - mListView = (ListView) ViewUtils.$$(mView, R.id.list_view); + public void setDialog(@Nullable Dialog dialog, String url) { + mDialog = dialog; + mProgressView = (ProgressView) ViewUtils.$$(dialog, R.id.progress); + mErrorText = (TextView) ViewUtils.$$(dialog, R.id.text); + mListView = (ListView) ViewUtils.$$(dialog, R.id.list_view); mListView.setOnItemClickListener(this); - if (mTorrentList == null) { - mErrorText.setVisibility(View.GONE); - mListView.setVisibility(View.GONE); - mRequest = new EhRequest().setMethod(EhClient.METHOD_GET_TORRENT_LIST) - .setArgs(url) - .setCallback(this); - EhApplication.getEhClient(getContext()).execute(mRequest); - } else { - bind(mTorrentList); + Context context = getContext2(); + if (context != null) { + if (mTorrentList == null) { + mErrorText.setVisibility(View.GONE); + mListView.setVisibility(View.GONE); + mRequest = new EhRequest().setMethod(EhClient.METHOD_GET_TORRENT_LIST) + .setArgs(url) + .setCallback(this); + EhApplication.getEhClient(context).execute(mRequest); + } else { + bind(mTorrentList); + } } } - public void setDialog(@Nullable Dialog dialog) { - mDialog = dialog; - } - private void bind(Pair[] data) { - if (null == mDialog) { + if (null == mDialog || null == mProgressView || null == mErrorText || null == mListView) { return; } @@ -1446,13 +1488,10 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene } } - public View getView() { - return mView; - } - @Override public void onItemClick(AdapterView parent, View view, int position, long id) { - if (mTorrentList != null && position < mTorrentList.length) { + Context context = getContext2(); + if (null != context && null != mTorrentList && position < mTorrentList.length) { String url = mTorrentList[position].first; String name = mTorrentList[position].second; // Use system download service @@ -1460,7 +1499,7 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene r.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS, name + ".torrent"); r.allowScanningByMediaScanner(); r.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED); - DownloadManager dm = (DownloadManager) getContext().getSystemService(Context.DOWNLOAD_SERVICE); + DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); dm.enqueue(r); } @@ -1477,6 +1516,9 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene mRequest = null; } mDialog = null; + mProgressView = null; + mErrorText = null; + mListView = null; } @Override @@ -1490,12 +1532,13 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene @Override public void onFailure(Exception e) { - if (mRequest != null) { - mRequest = null; + mRequest = null; + Context context = getContext2(); + if (null != context && null != mProgressView && null != mErrorText && null != mListView) { mProgressView.setVisibility(View.GONE); mErrorText.setVisibility(View.VISIBLE); mListView.setVisibility(View.GONE); - mErrorText.setText(ExceptionUtils.getReadableString(getContext(), e)); + mErrorText.setText(ExceptionUtils.getReadableString(context, e)); } } @@ -1554,37 +1597,41 @@ public class GalleryDetailScene extends BaseScene implements View.OnClickListene private class RateDialogHelper implements ProgressiveRatingBar.OnUserRateListener, DialogInterface.OnClickListener { - public View view; - private final ProgressiveRatingBar mRatingBar; - private final TextView mRatingText; + @Nullable + private ProgressiveRatingBar mRatingBar; + @Nullable + private TextView mRatingText; - @SuppressLint("InflateParams") - private RateDialogHelper(float rating) { - view = getActivity().getLayoutInflater().inflate(R.layout.dialog_rate, null); - mRatingText = (TextView) view.findViewById(R.id.rating_text); - mRatingBar = (ProgressiveRatingBar) view.findViewById(R.id.rating_view); - mRatingText.setText(getRatingText(rating, getResources())); + public void setDialog(Dialog dialog, float rating) { + mRatingText = (TextView) ViewUtils.$$(dialog, R.id.rating_text); + mRatingBar = (ProgressiveRatingBar) ViewUtils.$$(dialog, R.id.rating_view); + mRatingText.setText(getRatingText(rating, getResources2())); mRatingBar.setRating(rating); mRatingBar.setOnUserRateListener(this); } @Override public void onUserRate(float rating) { - mRatingText.setText(getRatingText(rating, getResources())); + if (null != mRatingText) { + mRatingText.setText(getRatingText(rating, getResources2())); + } } @Override public void onClick(DialogInterface dialog, int which) { - if (which != DialogInterface.BUTTON_POSITIVE || mGalleryDetail == null) { + Context context = getContext2(); + MainActivity activity = getActivity2(); + if (null == context || null == activity || which != DialogInterface.BUTTON_POSITIVE || + null == mGalleryDetail || null == mRatingBar) { return; } EhRequest request = new EhRequest() .setMethod(EhClient.METHOD_GET_RATE_GALLERY) .setArgs(mGalleryDetail.gid, mGalleryDetail.token, mRatingBar.getRating()) - .setCallback(new RateGalleryListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag(), mGalleryDetail.gid)); - EhApplication.getEhClient(getContext()).execute(request); + .setCallback(new RateGalleryListener(context, + activity.getStageId(), getTag(), mGalleryDetail.gid)); + EhApplication.getEhClient(context).execute(request); } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryInfoScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryInfoScene.java index c92a3944e..2f176218a 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryInfoScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryInfoScene.java @@ -37,6 +37,7 @@ import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.EhUtils; import com.hippo.ehviewer.client.data.GalleryDetail; import com.hippo.rippleold.RippleSalon; +import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.LayoutUtils; import com.hippo.yorozuya.ViewUtils; @@ -79,7 +80,8 @@ public final class GalleryInfoScene extends ToolbarScene implements EasyRecycler return; } - Resources resources = getResources(); + Resources resources = getResources2(); + AssertUtils.assertNotNull(resources); mKeys.add(resources.getString(R.string.header_key)); mValues.add(resources.getString(R.string.header_value)); mKeys.add(resources.getString(R.string.key_gid)); @@ -151,14 +153,17 @@ public final class GalleryInfoScene extends ToolbarScene implements EasyRecycler @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.scene_gallery_info, container, false); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + EasyRecyclerView recyclerView = (EasyRecyclerView) ViewUtils.$$(view, R.id.recycler_view); InfoAdapter adapter = new InfoAdapter(); recyclerView.setAdapter(adapter); - recyclerView.setLayoutManager(new LinearLayoutManager(getContext(), LinearLayoutManager.VERTICAL, false)); + recyclerView.setLayoutManager(new LinearLayoutManager(context, LinearLayoutManager.VERTICAL, false)); LinearDividerItemDecoration decoration = new LinearDividerItemDecoration( - LinearDividerItemDecoration.VERTICAL, getResources().getColor(R.color.divider), - LayoutUtils.dp2pix(getContext(), 1)); - decoration.setPadding(getResources().getDimensionPixelOffset(R.dimen.keyline_margin)); + LinearDividerItemDecoration.VERTICAL, context.getResources().getColor(R.color.divider), + LayoutUtils.dp2pix(context, 1)); + decoration.setPadding(context.getResources().getDimensionPixelOffset(R.dimen.keyline_margin)); recyclerView.addItemDecoration(decoration); recyclerView.setSelector(RippleSalon.generateRippleDrawable(false)); recyclerView.setClipToPadding(false); @@ -176,8 +181,9 @@ public final class GalleryInfoScene extends ToolbarScene implements EasyRecycler @Override public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) { - if (position != 0 && mValues != null) { - ClipboardManager cmb = (ClipboardManager) getContext().getSystemService(Context.CLIPBOARD_SERVICE); + Context context = getContext2(); + if (null != context && 0 != position && null != mValues) { + ClipboardManager cmb = (ClipboardManager) context.getSystemService(Context.CLIPBOARD_SERVICE); cmb.setPrimaryClip(ClipData.newPlainText(null, mValues.get(position))); showTip(R.string.copied_to_clipboard, LENGTH_SHORT); return true; @@ -209,6 +215,13 @@ public final class GalleryInfoScene extends ToolbarScene implements EasyRecycler private static final int TYPE_HEADER = 0; private static final int TYPE_DATA = 1; + private final LayoutInflater mInflater; + + public InfoAdapter() { + mInflater = getLayoutInflater2(); + AssertUtils.assertNotNull(mInflater); + } + @Override public int getItemViewType(int position) { if (position == 0) { @@ -220,7 +233,7 @@ public final class GalleryInfoScene extends ToolbarScene implements EasyRecycler @Override public InfoHolder onCreateViewHolder(ViewGroup parent, int viewType) { - return new InfoHolder(getActivity().getLayoutInflater().inflate(viewType == TYPE_HEADER ? + return new InfoHolder(mInflater.inflate(viewType == TYPE_HEADER ? R.layout.item_gallery_info_header : R.layout.item_gallery_info_data, parent, false)); } 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 4f6ff05b8..c6b12dbd8 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 @@ -17,6 +17,7 @@ package com.hippo.ehviewer.ui.scene; import android.animation.Animator; +import android.app.Activity; import android.content.Context; import android.content.DialogInterface; import android.content.res.Resources; @@ -72,7 +73,6 @@ import com.hippo.ehviewer.widget.SearchLayout; import com.hippo.rippleold.RippleSalon; import com.hippo.scene.Announcer; import com.hippo.scene.SceneFragment; -import com.hippo.scene.StageActivity; import com.hippo.util.ApiHelper; import com.hippo.util.DrawableManager; import com.hippo.view.ViewTransition; @@ -81,6 +81,7 @@ import com.hippo.widget.SearchBarMover; import com.hippo.widget.recyclerview.GridAutoSpanLayoutManager; import com.hippo.widget.refreshlayout.RefreshLayout; import com.hippo.yorozuya.AnimationUtils; +import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.MathUtils; import com.hippo.yorozuya.SimpleAnimatorListener; import com.hippo.yorozuya.SimpleHandler; @@ -210,7 +211,9 @@ public final class GalleryListScene extends BaseScene public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mClient = EhApplication.getEhClient(getContext()); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + mClient = EhApplication.getEhClient(context); if (savedInstanceState == null) { onInit(); @@ -291,11 +294,11 @@ public final class GalleryListScene extends BaseScene // Update search bar title, drawer checked item private void onUpdateUrlBuilder() { ListUrlBuilder builder = mUrlBuilder; - if (null == builder) { + Resources resources = getResources2(); + if (null == resources || null == builder) { return; } - Resources resources = getResources(); String keyword = builder.getKeyword(); int category = builder.getCategory(); @@ -305,7 +308,7 @@ public final class GalleryListScene extends BaseScene } // Update title - String title = getSuitableTitleForUrlBuilder(getResources(), builder, true); + String title = getSuitableTitleForUrlBuilder(resources, builder, true); if (null == title) { title = resources.getString(R.string.search); } @@ -334,7 +337,9 @@ public final class GalleryListScene extends BaseScene @Nullable Bundle savedInstanceState) { View view = inflater.inflate(R.layout.scene_gallery_list, container, false); - Resources resources = getContext().getResources(); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + Resources resources = context.getResources(); View mainLayout = ViewUtils.$$(view, R.id.main_layout); ContentLayout contentLayout = (ContentLayout) ViewUtils.$$(mainLayout, R.id.content_layout); @@ -355,7 +360,7 @@ public final class GalleryListScene extends BaseScene contentLayout.setHelper(mHelper); contentLayout.getFastScroller().setOnDragHandlerListener(this); - GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(getContext(), 0); + GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(context, 0); mRecyclerView.setLayoutManager(layoutManager); mRecyclerView.setSelector(RippleSalon.generateRippleDrawable(false)); mRecyclerView.setDrawSelectorOnTop(true); @@ -363,8 +368,8 @@ public final class GalleryListScene extends BaseScene mRecyclerView.setClipToPadding(false); mRecyclerView.setOnItemClickListener(this); mRecyclerView.setOnItemLongClickListener(this); - mAdapter = new GalleryListAdapter(LayoutInflater.from(getContext()), - getContext(), mRecyclerView, layoutManager, Settings.getListMode()); + mAdapter = new GalleryListAdapter(getLayoutInflater2(), getResources2(), + mRecyclerView, layoutManager, Settings.getListMode()); mRecyclerView.setAdapter(mAdapter); mAdapter.register(); mRecyclerView.setPadding(mRecyclerView.getPaddingLeft(), mRecyclerView.getPaddingTop() + paddingTopSB, @@ -375,13 +380,13 @@ public final class GalleryListScene extends BaseScene refreshLayout.setHeaderTranslationY(paddingTopSB); - mLeftDrawable = new DrawerArrowDrawable(getContext()); - mRightDrawable = new AddDeleteDrawable(getContext()); + mLeftDrawable = new DrawerArrowDrawable(context); + mRightDrawable = new AddDeleteDrawable(context); mSearchBar.setLeftDrawable(mLeftDrawable); mSearchBar.setRightDrawable(mRightDrawable); mSearchBar.setHelper(this); mSearchBar.setOnStateChangeListener(this); - setSearchBarHint(getContext(), mSearchBar); + setSearchBarHint(context, mSearchBar); mSearchLayout.setHelper(this); mSearchLayout.setPadding(mSearchLayout.getPaddingLeft(), mSearchLayout.getPaddingTop() + paddingTopSB, @@ -411,15 +416,16 @@ public final class GalleryListScene extends BaseScene } private void guideQuickSearch() { - if (!Settings.getGuideQuickSearch()) { + Activity activity = getActivity2(); + if (null == activity || !Settings.getGuideQuickSearch()) { return; } - Display display = getActivity().getWindowManager().getDefaultDisplay(); + Display display = activity.getWindowManager().getDefaultDisplay(); Point point = new Point(); display.getSize(point); - new ShowcaseView.Builder(getActivity()) + new ShowcaseView.Builder(activity) .withMaterialShowcase() .setStyle(R.style.Guide) .setTarget(new PointTarget(point.x, point.y / 3)) @@ -468,12 +474,13 @@ public final class GalleryListScene extends BaseScene private void showAddQuickSearchDialog(final List list, final ArrayAdapter adapter, final ListView listView, final TextView tip) { - if (null == mUrlBuilder) { + Context context = getContext2(); + if (null == context || null == mUrlBuilder) { return; } - final EditTextDialogBuilder builder = new EditTextDialogBuilder(getContext(), - getSuitableTitleForUrlBuilder(getResources(), mUrlBuilder, false), getString(R.string.quick_search)); + final EditTextDialogBuilder builder = new EditTextDialogBuilder(context, + getSuitableTitleForUrlBuilder(context.getResources(), mUrlBuilder, false), getString(R.string.quick_search)); builder.setTitle(R.string.add_quick_search_dialog_title); builder.setPositiveButton(android.R.string.ok, null); final AlertDialog dialog = builder.show(); @@ -512,8 +519,11 @@ public final class GalleryListScene extends BaseScene final TextView tip = (TextView) ViewUtils.$$(view, R.id.tip); final ListView listView = (ListView) ViewUtils.$$(view, R.id.list_view); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + final List list = EhDB.getAllQuickSearch(); - final ArrayAdapter adapter = new ArrayAdapter<>(getContext(), R.layout.item_simple_list, list); + final ArrayAdapter adapter = new ArrayAdapter<>(context, R.layout.item_simple_list, list); listView.setAdapter(adapter); listView.setOnItemClickListener(new AdapterView.OnItemClickListener() { @Override @@ -634,24 +644,26 @@ public final class GalleryListScene extends BaseScene @Override public boolean onItemLongClick(EasyRecyclerView parent, View view, int position, long id) { - if (null == mHelper) { + final Context context = getContext2(); + final MainActivity activity = getActivity2(); + if (null == context || null == activity || null == mHelper) { return false; } final GalleryInfo gi = mHelper.getDataAt(position); - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setTitle(EhUtils.getSuitableTitle(gi)) .setItems(R.array.gallery_list_menu_entries, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: // Download - CommonOperations.startDownload((MainActivity) getActivity(), gi, false); + CommonOperations.startDownload(activity, gi, false); break; case 1: // Favorites - CommonOperations.addToFavorites(getActivity(), gi, - new addToFavoriteListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag())); + CommonOperations.addToFavorites(activity, gi, + new addToFavoriteListener(context, + activity.getStageId(), getTag())); break; } } @@ -945,9 +957,9 @@ public final class GalleryListScene extends BaseScene private class GalleryListAdapter extends GalleryAdapter { - public GalleryListAdapter(LayoutInflater inflater, Context context, + public GalleryListAdapter(LayoutInflater inflater, Resources resources, RecyclerView recyclerView, GridAutoSpanLayoutManager layoutManager, int type) { - super(inflater, context, recyclerView, layoutManager, type); + super(inflater, resources, recyclerView, layoutManager, type); } @Override @@ -966,7 +978,8 @@ public final class GalleryListScene extends BaseScene @Override protected void getPageData(int taskId, int type, int page) { - if (null == mClient || null == mUrlBuilder) { + MainActivity activity = getActivity2(); + if (null == activity || null == mClient || null == mUrlBuilder) { return; } @@ -975,7 +988,7 @@ public final class GalleryListScene extends BaseScene EhRequest request = new EhRequest(); request.setMethod(EhClient.METHOD_GET_WHATS_HOT); request.setCallback(new GetWhatsHotListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag(), taskId)); + activity.getStageId(), getTag(), taskId)); request.setArgs(); mClient.execute(request); } else { @@ -983,7 +996,7 @@ public final class GalleryListScene extends BaseScene EhRequest request = new EhRequest(); request.setMethod(EhClient.METHOD_GET_GALLERY_LIST); request.setCallback(new GetGalleryListListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag(), taskId)); + activity.getStageId(), getTag(), taskId)); request.setArgs(url); mClient.execute(request); } @@ -991,7 +1004,7 @@ public final class GalleryListScene extends BaseScene @Override protected Context getContext() { - return GalleryListScene.this.getContext(); + return GalleryListScene.this.getContext2(); } @Override diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryPreviewsScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryPreviewsScene.java index 44b88d58a..d3901fae3 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryPreviewsScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/GalleryPreviewsScene.java @@ -16,11 +16,11 @@ package com.hippo.ehviewer.ui.scene; -import android.annotation.SuppressLint; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.content.Intent; +import android.content.res.Resources; import android.os.Bundle; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -46,14 +46,16 @@ import com.hippo.ehviewer.client.data.GalleryPreview; import com.hippo.ehviewer.client.data.LargePreviewSet; import com.hippo.ehviewer.client.exception.EhException; import com.hippo.ehviewer.ui.GalleryActivity; +import com.hippo.ehviewer.ui.MainActivity; import com.hippo.scene.SceneFragment; -import com.hippo.scene.StageActivity; import com.hippo.widget.ContentLayout; import com.hippo.widget.LoadImageView; import com.hippo.widget.Slider; import com.hippo.widget.recyclerview.GridAutoSpanLayoutManager; +import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.LayoutUtils; import com.hippo.yorozuya.SimpleHandler; +import com.hippo.yorozuya.ViewUtils; import java.util.ArrayList; import java.util.Locale; @@ -85,7 +87,9 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi public void onCreate(@Nullable Bundle savedInstanceState) { super.onCreate(savedInstanceState); - mClient = EhApplication.getEhClient(getContext()); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + mClient = EhApplication.getEhClient(context); if (savedInstanceState == null) { onInit(); } else { @@ -130,13 +134,17 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi contentLayout.hideFastScroll(); EasyRecyclerView recyclerView = contentLayout.getRecyclerView(); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + Resources resources = context.getResources(); + mAdapter = new GalleryPreviewAdapter(); recyclerView.setAdapter(mAdapter); - int columnWidth = getResources().getDimensionPixelOffset(R.dimen.preview_grid_column_width); - GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(getContext(), columnWidth); + int columnWidth = resources.getDimensionPixelOffset(R.dimen.preview_grid_column_width); + GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(context, columnWidth); layoutManager.setStrategy(GridAutoSpanLayoutManager.STRATEGY_SUITABLE_SIZE); recyclerView.setLayoutManager(layoutManager); - int padding = LayoutUtils.dp2pix(getContext(), 4); + int padding = LayoutUtils.dp2pix(context, 4); recyclerView.setPadding(padding, padding, padding, padding); recyclerView.setClipToPadding(false); recyclerView.addItemDecoration(new MarginItemDecoration(padding)); @@ -182,6 +190,11 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi @Override public boolean onMenuItemClick(MenuItem item) { + Context context = getContext2(); + if (null == context) { + return false; + } + int id = item.getItemId(); switch (id) { case R.id.action_go_to: @@ -191,12 +204,12 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi int pages = mHelper.getPages(); if (pages > 0 && mHelper.canGoTo()) { GoToDialogHelper helper = new GoToDialogHelper(pages, mHelper.getPageForTop()); - AlertDialog dialog = new AlertDialog.Builder(getContext()).setTitle(R.string.go_to) - .setView(helper.getView()) + AlertDialog dialog = new AlertDialog.Builder(context).setTitle(R.string.go_to) + .setView(R.layout.dialog_go_to) .setPositiveButton(android.R.string.ok, null) .create(); dialog.show(); - helper.setPositiveButtonClickListener(dialog); + helper.setDialog(dialog); } return true; } @@ -210,7 +223,7 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi @Override public boolean onItemClick(EasyRecyclerView parent, View view, int position, long id) { - Context context = getContext(); + Context context = getContext2(); if (null != context && null != mHelper && null != mGalleryInfo) { GalleryPreview p = mHelper.getDataAt(position); Intent intent = new Intent(context, GalleryActivity.class); @@ -237,9 +250,16 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi private class GalleryPreviewAdapter extends RecyclerView.Adapter { + private final LayoutInflater mInflater; + + public GalleryPreviewAdapter() { + mInflater = getLayoutInflater2(); + AssertUtils.assertNotNull(mInflater); + } + @Override public GalleryPreviewHolder onCreateViewHolder(ViewGroup parent, int viewType) { - return new GalleryPreviewHolder(getActivity().getLayoutInflater().inflate(R.layout.item_gallery_preview, parent, false)); + return new GalleryPreviewHolder(mInflater.inflate(R.layout.item_gallery_preview, parent, false)); } @Override @@ -261,7 +281,8 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi @Override protected void getPageData(final int taskId, int type, int page) { - if (null == mClient || null == mGalleryInfo) { + MainActivity activity = getActivity2(); + if (null == activity || null == mClient || null == mGalleryInfo) { onGetException(taskId, new EhException(getString(R.string.error_cannot_find_gallery))); return; } @@ -283,14 +304,14 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi EhRequest request = new EhRequest(); request.setMethod(EhClient.METHOD_GET_LARGE_PREVIEW_SET); request.setCallback(new GetLargePreviewSetListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag(), taskId, mGalleryInfo.gid, page)); + activity.getStageId(), getTag(), taskId, mGalleryInfo.gid, page)); request.setArgs(url); mClient.execute(request); } @Override protected Context getContext() { - return GalleryPreviewsScene.this.getContext(); + return GalleryPreviewsScene.this.getContext2(); } @Override @@ -387,35 +408,37 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi DialogInterface.OnDismissListener { private final int mPages; + private final int mCurrentPage; - private final View mView; - private final Slider mSlider; - + @Nullable + private Slider mSlider; + @Nullable private Dialog mDialog; - @SuppressLint("InflateParams") private GoToDialogHelper(int pages, int currentPage) { mPages = pages; - mView = getActivity().getLayoutInflater().inflate(R.layout.dialog_go_to, null); - ((TextView) mView.findViewById(R.id.start)).setText("1"); - ((TextView) mView.findViewById(R.id.end)).setText(String.format(Locale.US, "%d", pages)); - mSlider = (Slider) mView.findViewById(R.id.slider); - mSlider.setRange(1, pages); - mSlider.setProgress(currentPage + 1); + mCurrentPage = currentPage; } - public View getView() { - return mView; - } - - public void setPositiveButtonClickListener(AlertDialog dialog) { + public void setDialog(@NonNull AlertDialog dialog) { mDialog = dialog; + + ((TextView) ViewUtils.$$(dialog, R.id.start)).setText(String.format(Locale.US, "%d", 1)); + ((TextView) ViewUtils.$$(dialog, R.id.end)).setText(String.format(Locale.US, "%d", mPages)); + mSlider = (Slider) ViewUtils.$$(dialog, R.id.slider); + mSlider.setRange(1, mPages); + mSlider.setProgress(mCurrentPage + 1); + dialog.getButton(AlertDialog.BUTTON_POSITIVE).setOnClickListener(this); dialog.setOnDismissListener(this); } @Override public void onClick(View v) { + if (null == mSlider) { + return; + } + int page = mSlider.getProgress() - 1; if (page >= 0 && page < mPages && mHelper != null) { mHelper.goTo(page); @@ -431,6 +454,7 @@ public class GalleryPreviewsScene extends ToolbarScene implements EasyRecyclerVi @Override public void onDismiss(DialogInterface dialog) { mDialog = null; + mSlider = null; } } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.java index d5a373818..f7ce76db1 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/HistoryScene.java @@ -18,6 +18,7 @@ package com.hippo.ehviewer.ui.scene; import android.content.Context; import android.content.DialogInterface; +import android.content.res.Resources; import android.graphics.drawable.Drawable; import android.os.Bundle; import android.support.annotation.Nullable; @@ -42,11 +43,11 @@ import com.hippo.ehviewer.ui.MainActivity; import com.hippo.rippleold.RippleSalon; import com.hippo.scene.Announcer; import com.hippo.scene.SceneFragment; -import com.hippo.scene.StageActivity; import com.hippo.util.ApiHelper; import com.hippo.util.DrawableManager; import com.hippo.view.ViewTransition; import com.hippo.widget.recyclerview.GridAutoSpanLayoutManager; +import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.ResourcesUtils; import com.hippo.yorozuya.ViewUtils; @@ -82,12 +83,15 @@ public class HistoryScene extends ToolbarScene TextView tip = (TextView) ViewUtils.$$(view, R.id.tip); mViewTransition = new ViewTransition(content, tip); - Drawable drawable = DrawableManager.getDrawable(getContext(), R.drawable.big_history); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + + Drawable drawable = DrawableManager.getDrawable(context, R.drawable.big_history); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); tip.setCompoundDrawables(null, drawable, null, null); mList = EhDB.getHistoryLazyList(); - GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(getContext(), 0); + GridAutoSpanLayoutManager layoutManager = new GridAutoSpanLayoutManager(context, 0); recyclerView.setLayoutManager(layoutManager); recyclerView.setSelector(RippleSalon.generateRippleDrawable(false)); recyclerView.setDrawSelectorOnTop(true); @@ -95,14 +99,14 @@ public class HistoryScene extends ToolbarScene recyclerView.setClipToPadding(false); recyclerView.setOnItemClickListener(this); recyclerView.setOnItemLongClickListener(this); - mAdapter = new HistoryAdapter(LayoutInflater.from(getContext()), - getContext(), recyclerView, layoutManager, Settings.getListMode()); + mAdapter = new HistoryAdapter(getLayoutInflater2(), getResources2(), + recyclerView, layoutManager, Settings.getListMode()); recyclerView.setAdapter(mAdapter); mAdapter.register(); fastScroller.attachToRecyclerView(recyclerView); HandlerDrawable handlerDrawable = new HandlerDrawable(); - handlerDrawable.setColor(ResourcesUtils.getAttrColor(getContext(), R.attr.colorAccent)); + handlerDrawable.setColor(ResourcesUtils.getAttrColor(context, R.attr.colorAccent)); fastScroller.setHandlerDrawable(handlerDrawable); if (null != mList && mList.size() > 0) { @@ -163,24 +167,26 @@ public class HistoryScene extends ToolbarScene @Override public boolean onItemLongClick(EasyRecyclerView parent, View view, int position, long id) { - if (null == mList) { + final Context context = getContext2(); + final MainActivity activity = getActivity2(); + if (null == context || null == activity || null == mList) { return false; } final GalleryInfo gi = mList.get(position); - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setTitle(EhUtils.getSuitableTitle(gi)) .setItems(R.array.gallery_list_menu_entries, new DialogInterface.OnClickListener() { @Override public void onClick(DialogInterface dialog, int which) { switch (which) { case 0: // Download - CommonOperations.startDownload((MainActivity) getActivity(), gi, false); + CommonOperations.startDownload(activity, gi, false); break; case 1: // Favorites - CommonOperations.addToFavorites(getActivity(), gi, - new addToFavoriteListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag())); + CommonOperations.addToFavorites(activity, gi, + new addToFavoriteListener(context, + activity.getStageId(), getTag())); break; } } @@ -190,9 +196,9 @@ public class HistoryScene extends ToolbarScene private class HistoryAdapter extends GalleryAdapter { - public HistoryAdapter(LayoutInflater inflater, Context context, + public HistoryAdapter(LayoutInflater inflater, Resources resources, RecyclerView recyclerView, GridAutoSpanLayoutManager layoutManager, int type) { - super(inflater, context, recyclerView, layoutManager, type); + super(inflater, resources, recyclerView, layoutManager, type); } @Nullable 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 index f7d38d57c..f62ae7c55 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/ProgressScene.java @@ -30,12 +30,14 @@ 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.ehviewer.ui.MainActivity; import com.hippo.scene.Announcer; import com.hippo.scene.SceneFragment; -import com.hippo.scene.StageActivity; import com.hippo.util.DrawableManager; import com.hippo.util.ExceptionUtils; import com.hippo.view.ViewTransition; +import com.hippo.yorozuya.AssertUtils; +import com.hippo.yorozuya.ViewUtils; /** * Only show a progress with jobs in background @@ -61,7 +63,9 @@ public final class ProgressScene extends BaseScene implements View.OnClickListen private String mPToken; private int mPage; + @Nullable private TextView mTip; + @Nullable private ViewTransition mViewTransition; @Override @@ -81,6 +85,12 @@ public final class ProgressScene extends BaseScene implements View.OnClickListen } private boolean doJobs() { + Context context = getContext2(); + MainActivity activity = getActivity2(); + if (null == context || null == activity) { + return false; + } + if (ACTION_GALLERY_TOKEN.equals(mAction)) { if (mGid == -1 || mPToken == null || mPage == -1) { return false; @@ -89,9 +99,9 @@ public final class ProgressScene extends BaseScene implements View.OnClickListen 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); + .setCallback(new GetGalleryTokenListener(context, + activity.getStageId(), getTag())); + EhApplication.getEhClient(context).execute(request); return true; } return false; @@ -155,10 +165,13 @@ public final class ProgressScene extends BaseScene implements View.OnClickListen 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 = (TextView) view.findViewById(R.id.tip); + View progress = ViewUtils.$$(view, R.id.progress); + mTip = (TextView) ViewUtils.$$(view, R.id.tip); - Drawable drawable = DrawableManager.getDrawable(getContext(), R.drawable.big_weird_face); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + + Drawable drawable = DrawableManager.getDrawable(context, R.drawable.big_weird_face); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); mTip.setCompoundDrawables(null, drawable, null, null); mTip.setOnClickListener(this); @@ -175,13 +188,23 @@ public final class ProgressScene extends BaseScene implements View.OnClickListen return view; } + @Override + public void onDestroyView() { + super.onDestroyView(); + + mTip = null; + mViewTransition = null; + } + @Override public void onClick(View v) { if (mTip == v) { if (doJobs()) { mValid = true; // Show progress - mViewTransition.showView(0, true); + if (null != mViewTransition) { + mViewTransition.showView(0, true); + } } } } @@ -198,10 +221,12 @@ public final class ProgressScene extends BaseScene implements View.OnClickListen private void onGetGalleryTokenFailure(Exception e) { mValid = false; - mError = ExceptionUtils.getReadableString(getContext(), e); - if (isViewCreated()) { + Context context = getContext2(); + + if (null != context && null != mViewTransition && null != mTip) { // Show tip + mError = ExceptionUtils.getReadableString(context, e); mViewTransition.showView(1); mTip.setText(mError); } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/QuickSearchScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/QuickSearchScene.java index 304f24290..23db3b5f3 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/QuickSearchScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/QuickSearchScene.java @@ -16,6 +16,7 @@ package com.hippo.ehviewer.ui.scene; +import android.content.Context; import android.content.DialogInterface; import android.graphics.drawable.Drawable; import android.graphics.drawable.NinePatchDrawable; @@ -48,6 +49,7 @@ import com.hippo.ehviewer.R; import com.hippo.ehviewer.dao.QuickSearch; import com.hippo.util.DrawableManager; import com.hippo.view.ViewTransition; +import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.ViewUtils; import java.util.List; @@ -91,7 +93,10 @@ public final class QuickSearchScene extends ToolbarScene { TextView tip = (TextView) ViewUtils.$$(view, R.id.tip); mViewTransition = new ViewTransition(recyclerView, tip); - Drawable drawable = DrawableManager.getDrawable(getContext(), R.drawable.big_search); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + + Drawable drawable = DrawableManager.getDrawable(context, R.drawable.big_search); drawable.setBounds(0, 0, drawable.getIntrinsicWidth(), drawable.getIntrinsicHeight()); tip.setCompoundDrawables(null, drawable, null, null); tip.setText(R.string.no_quick_search); @@ -103,7 +108,7 @@ public final class QuickSearchScene extends ToolbarScene { // drag & drop manager RecyclerViewDragDropManager dragDropManager = new RecyclerViewDragDropManager(); dragDropManager.setDraggingItemShadowDrawable( - (NinePatchDrawable) getResources().getDrawable(R.drawable.shadow_8dp)); + (NinePatchDrawable) context.getResources().getDrawable(R.drawable.shadow_8dp)); // swipe manager RecyclerViewSwipeManager swipeManager = new RecyclerViewSwipeManager(); RecyclerView.Adapter adapter = new QuickSearchAdapter(); @@ -114,7 +119,7 @@ public final class QuickSearchScene extends ToolbarScene { final GeneralItemAnimator animator = new SwipeDismissItemAnimator(); animator.setSupportsChangeAnimations(false); recyclerView.hasFixedSize(); - recyclerView.setLayoutManager(new LinearLayoutManager(getContext())); + recyclerView.setLayoutManager(new LinearLayoutManager(context)); recyclerView.setAdapter(adapter); recyclerView.setItemAnimator(animator); guardManager.attachRecyclerView(recyclerView); @@ -181,10 +186,16 @@ public final class QuickSearchScene extends ToolbarScene { implements DraggableItemAdapter, SwipeableItemAdapter { + private final LayoutInflater mInflater; + + public QuickSearchAdapter() { + mInflater = getLayoutInflater2(); + AssertUtils.assertNotNull(mInflater); + } + @Override public QuickSearchHolder onCreateViewHolder(ViewGroup parent, int viewType) { - return new QuickSearchHolder(getActivity().getLayoutInflater() - .inflate(R.layout.item_label_list, parent, false)); + return new QuickSearchHolder(mInflater.inflate(R.layout.item_label_list, parent, false)); } @Override @@ -269,14 +280,15 @@ public final class QuickSearchScene extends ToolbarScene { protected void onPerformAction() { super.onPerformAction(); + Context context = getContext2(); final List quickSearchList = mQuickSearchList; - if (quickSearchList == null || mPosition < 0 || mPosition >= quickSearchList.size()) { + if (null == context || null == quickSearchList || mPosition < 0 || mPosition >= quickSearchList.size()) { return; } final QuickSearch quickSearch = quickSearchList.get(mPosition); - new AlertDialog.Builder(getContext()) + new AlertDialog.Builder(context) .setTitle(R.string.delete_quick_search_title) .setMessage(getString(R.string.delete_quick_search_message, quickSearch.name)) .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() { diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.java index f3200447c..430922c34 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/SignInScene.java @@ -44,8 +44,6 @@ import com.hippo.ehviewer.ui.MainActivity; import com.hippo.rippleold.RippleSalon; import com.hippo.scene.Announcer; import com.hippo.scene.SceneFragment; -import com.hippo.scene.StageActivity; -import com.hippo.util.ActivityHelper; import com.hippo.util.ExceptionUtils; import com.hippo.yorozuya.AssertUtils; import com.hippo.yorozuya.ViewUtils; @@ -148,7 +146,9 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct RippleSalon.addRipple(mRegister, true); RippleSalon.addRipple(mSignIn, true); - EhApplication application = (EhApplication) getContext().getApplicationContext(); + Context context = getContext2(); + AssertUtils.assertNotNull(container); + EhApplication application = (EhApplication) context.getApplicationContext(); if (application.containGlobalStuff(mRequestId)) { // request exist showProgress(false); @@ -163,7 +163,7 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct // Show IME if (mProgress != null && View.INVISIBLE != mProgress.getVisibility()) { - ActivityHelper.showSoftInput(getActivity(), mUsername); + showSoftInput(mUsername); } } @@ -215,8 +215,13 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct @Override public void onClick(View v) { + MainActivity activity = getActivity2(); + if (null == activity) { + return; + } + if (mRegister == v) { - UrlOpener.openUrl(getActivity(), EhUrl.URL_REGISTER, false, true); + UrlOpener.openUrl(activity, EhUrl.URL_REGISTER, false, true); } else if (mSignIn == v) { signIn(); } else if (mSignInViaWebView == v) { @@ -243,7 +248,9 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct } private void signIn() { - if (null == mUsername || null == mPassword || null == mUsernameLayout || + Context context = getContext2(); + MainActivity activity = getActivity2(); + if (null == context || null == activity || null == mUsername || null == mPassword || null == mUsernameLayout || null == mPasswordLayout) { return; } @@ -265,44 +272,57 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct mPasswordLayout.setError(null); } - ActivityHelper.hideSoftInput(getActivity()); + hideSoftInput(); showProgress(true); // Clean up for sign in - EhUtils.signOut(getContext()); + EhUtils.signOut(context); - EhCallback callback = new SignInListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag()); - mRequestId = ((EhApplication) getContext().getApplicationContext()).putGlobalStuff(callback); + EhCallback callback = new SignInListener(context, + activity.getStageId(), getTag()); + mRequestId = ((EhApplication) context.getApplicationContext()).putGlobalStuff(callback); EhRequest request = new EhRequest() .setMethod(EhClient.METHOD_SIGN_IN) .setArgs(username, password) .setCallback(callback); - EhApplication.getEhClient(getContext()).execute(request); + EhApplication.getEhClient(context).execute(request); } private void getProfile() { - ActivityHelper.hideSoftInput(getActivity()); + Context context = getContext2(); + MainActivity activity = getActivity2(); + if (null == context || null == activity) { + return; + } + + hideSoftInput(); showProgress(true); - EhCallback callback = new GetProfileListener(getContext(), - ((StageActivity) getActivity()).getStageId(), getTag()); - mRequestId = ((EhApplication) getContext().getApplicationContext()).putGlobalStuff(callback); + EhCallback callback = new GetProfileListener(context, + activity.getStageId(), getTag()); + mRequestId = ((EhApplication) context.getApplicationContext()).putGlobalStuff(callback); EhRequest request = new EhRequest() .setMethod(EhClient.METHOD_GET_PROFILE) .setCallback(callback); - EhApplication.getEhClient(getContext()).execute(request); + EhApplication.getEhClient(context).execute(request); } private void redirectTo() { Settings.putNeedSignIn(false); - ((MainActivity) getActivity()).startSceneForCheckStep( - MainActivity.CHECK_STEP_SIGN_IN, getArguments()); + MainActivity activity = getActivity2(); + if (null != activity) { + activity.startSceneForCheckStep(MainActivity.CHECK_STEP_SIGN_IN, getArguments()); + } finish(); } private void whetherToSkip() { - new AlertDialog.Builder(getContext()) + Context context = getContext2(); + if (null == context) { + return; + } + + new AlertDialog.Builder(context) .setTitle(R.string.sign_in_failed) .setMessage(R.string.sign_in_failed_plain) .setNegativeButton(android.R.string.cancel, null) @@ -311,7 +331,12 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct } public void onSignInSuccess() { - if (EhApplication.getEhCookieStore(getContext()).hasSignedIn()) { + Context context = getContext2(); + if (null == context) { + return; + } + + if (EhApplication.getEhCookieStore(context).hasSignedIn()) { getProfile(); } else { hideProgress(); @@ -320,7 +345,12 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct } public void onSignInFailure(Exception e) { - Toast.makeText(getContext(), ExceptionUtils.getReadableString(getContext(), e), + Context context = getContext2(); + if (null == context) { + return; + } + + Toast.makeText(context, ExceptionUtils.getReadableString(context, e), Toast.LENGTH_SHORT).show(); hideProgress(); whetherToSkip(); @@ -332,7 +362,12 @@ public final class SignInScene extends BaseScene implements EditText.OnEditorAct } public void onGetProfileFailure(Exception e) { - Toast.makeText(getContext(), ExceptionUtils.getReadableString(getContext(), e), + Context context = getContext2(); + if (null == context) { + return; + } + + Toast.makeText(context, ExceptionUtils.getReadableString(context, e), Toast.LENGTH_SHORT).show(); hideProgress(); whetherToSkip(); diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/WarningScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/WarningScene.java index 9efe5f89a..466ca8aa9 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/WarningScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/WarningScene.java @@ -80,8 +80,10 @@ public final class WarningScene extends BaseScene implements View.OnClickListene Settings.putShowWarning(false); // Start new scene and finish it self - ((MainActivity) getActivity()).startSceneForCheckStep( - MainActivity.CHECK_STEP_WARNING, getArguments()); + MainActivity activity = getActivity2(); + if (null != activity) { + activity.startSceneForCheckStep(MainActivity.CHECK_STEP_WARNING, getArguments()); + } finish(); } } diff --git a/app/src/main/java/com/hippo/ehviewer/ui/scene/WebViewSignInScene.java b/app/src/main/java/com/hippo/ehviewer/ui/scene/WebViewSignInScene.java index 877db8e3b..dc6d7c141 100644 --- a/app/src/main/java/com/hippo/ehviewer/ui/scene/WebViewSignInScene.java +++ b/app/src/main/java/com/hippo/ehviewer/ui/scene/WebViewSignInScene.java @@ -31,6 +31,7 @@ import com.hippo.ehviewer.EhApplication; import com.hippo.ehviewer.client.EhCookieStore; import com.hippo.ehviewer.client.EhUrl; import com.hippo.ehviewer.client.EhUtils; +import com.hippo.yorozuya.AssertUtils; import java.util.ArrayList; import java.util.Collections; @@ -58,9 +59,12 @@ public class WebViewSignInScene extends BaseScene { @SuppressLint("SetJavaScriptEnabled") public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { - EhUtils.signOut(getContext()); + Context context = getContext2(); + AssertUtils.assertNotNull(context); + + EhUtils.signOut(context); CookieManager.getInstance().removeAllCookie(); - mWebView = new WebView(getContext()); + mWebView = new WebView(context); mWebView.getSettings().setJavaScriptEnabled(true); mWebView.setWebViewClient(new LoginWebViewClient()); mWebView.loadUrl(EhUrl.URL_SIGN_IN); @@ -106,7 +110,7 @@ public class WebViewSignInScene extends BaseScene { @Override public void onPageFinished(WebView view, String url) { - Context context = getContext(); + Context context = getContext2(); if (context == null) { return; } diff --git a/app/src/main/java/com/hippo/scene/SceneFragment.java b/app/src/main/java/com/hippo/scene/SceneFragment.java index a281cf929..3075c2527 100644 --- a/app/src/main/java/com/hippo/scene/SceneFragment.java +++ b/app/src/main/java/com/hippo/scene/SceneFragment.java @@ -97,14 +97,18 @@ public class SceneFragment extends Fragment { } @Override + @SuppressWarnings("deprecation") public void onViewCreated(View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); view.setTag(R.id.fragment_tag, getTag()); - view.setBackgroundColor(getContext().getResources().getColor(R.color.background_light)); + view.setBackgroundColor(getResources().getColor(R.color.background_light)); // Notify - ((StageActivity) getActivity()).onSceneViewCreated(this, savedInstanceState); + FragmentActivity activity = getActivity(); + if (activity instanceof StageActivity) { + ((StageActivity) activity).onSceneViewCreated(this, savedInstanceState); + } } @Override @@ -112,14 +116,20 @@ public class SceneFragment extends Fragment { super.onDestroyView(); // Notify - ((StageActivity) getActivity()).onSceneViewDestroyed(this); + FragmentActivity activity = getActivity(); + if (activity instanceof StageActivity) { + ((StageActivity) activity).onSceneViewDestroyed(this); + } } @Override public void onDestroy() { super.onDestroy(); - ((StageActivity) getActivity()).onSceneDestroyed(this); + FragmentActivity activity = getActivity(); + if (activity instanceof StageActivity) { + ((StageActivity) activity).onSceneDestroyed(this); + } } void addRequest(String requestSceneTag, int requestCode) { diff --git a/app/src/main/java/com/hippo/text/LinkMovementMethod2.java b/app/src/main/java/com/hippo/text/LinkMovementMethod2.java index 498172914..afbba0672 100644 --- a/app/src/main/java/com/hippo/text/LinkMovementMethod2.java +++ b/app/src/main/java/com/hippo/text/LinkMovementMethod2.java @@ -16,11 +16,11 @@ package com.hippo.text; -import android.app.Activity; import android.text.Layout; import android.text.NoCopySpan; import android.text.Selection; import android.text.Spannable; +import android.text.method.MovementMethod; import android.text.method.ScrollingMovementMethod; import android.text.style.ClickableSpan; import android.text.style.URLSpan; @@ -40,11 +40,7 @@ public class LinkMovementMethod2 extends ScrollingMovementMethod { private static final int UP = 2; private static final int DOWN = 3; - private final Activity mActivity; - - public LinkMovementMethod2(Activity activity) { - mActivity = activity; - } + private LinkMovementMethod2() {} @Override public boolean canSelectArbitrarily() { @@ -227,7 +223,7 @@ public class LinkMovementMethod2 extends ScrollingMovementMethod { if (action == MotionEvent.ACTION_UP) { ClickableSpan span = link[0]; if (span instanceof URLSpan) { - UrlOpener.openUrl(mActivity, ((URLSpan) span).getURL(), true, true); + UrlOpener.openUrl(widget.getContext(), ((URLSpan) span).getURL(), true, true); } else { span.onClick(widget); } @@ -263,5 +259,13 @@ public class LinkMovementMethod2 extends ScrollingMovementMethod { } } + public static MovementMethod getInstance() { + if (sInstance == null) + sInstance = new LinkMovementMethod2(); + + return sInstance; + } + + private static LinkMovementMethod2 sInstance; private static final Object FROM_BELOW = new NoCopySpan.Concrete(); }