From f744f112edec27f0ee2f95625dd6c589240fca3f Mon Sep 17 00:00:00 2001 From: iamwent Date: Tue, 7 Mar 2017 10:13:52 +0800 Subject: [PATCH] config leakcanary & fix bugs --- app/build.gradle | 6 +- app/src/main/AndroidManifest.xml | 3 +- .../com/iamwent/gank/app/GankApplication.java | 38 ++++++++++++ .../java/com/iamwent/gank/app/GankConfig.java | 15 ----- .../iamwent/gank/ui/base/BaseActivity.java | 8 +++ .../iamwent/gank/ui/base/BaseFragment.java | 9 +++ .../com/iamwent/gank/ui/base/WebActivity.java | 4 +- .../gank/ui/category/CategoryAdapter.java | 8 +-- .../iamwent/gank/ui/image/ImageActivity.java | 8 +-- .../com/iamwent/gank/util/BitmapUtil.java | 51 +++++++-------- .../java/com/iamwent/gank/util/Toasts.java | 62 +++++++++++++++++++ app/src/main/res/values-zh/strings.xml | 1 + app/src/main/res/values/strings.xml | 4 ++ 13 files changed, 156 insertions(+), 61 deletions(-) create mode 100644 app/src/main/java/com/iamwent/gank/util/Toasts.java diff --git a/app/build.gradle b/app/build.gradle index 09e0bcc..16902a6 100644 --- a/app/build.gradle +++ b/app/build.gradle @@ -166,9 +166,9 @@ dependencies { // leakcanary -// debugCompile "com.squareup.leakcanary:leakcanary-android:${rootProject.ext.leakcanaryVersion}" -// releaseCompile "com.squareup.leakcanary:leakcanary-android-no-op:${rootProject.ext.leakcanaryVersion}" -// testCompile "com.squareup.leakcanary:leakcanary-android-no-op:${rootProject.ext.leakcanaryVersion}" + debugCompile "com.squareup.leakcanary:leakcanary-android:${rootProject.ext.leakcanaryVersion}" + releaseCompile "com.squareup.leakcanary:leakcanary-android-no-op:${rootProject.ext.leakcanaryVersion}" + testCompile "com.squareup.leakcanary:leakcanary-android-no-op:${rootProject.ext.leakcanaryVersion}" compile 'com.tencent.bugly:crashreport:latest.release' // unit test diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index da283e9..c072a71 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -8,6 +8,7 @@ + - + diff --git a/app/src/main/java/com/iamwent/gank/app/GankApplication.java b/app/src/main/java/com/iamwent/gank/app/GankApplication.java index 620dd75..648292d 100644 --- a/app/src/main/java/com/iamwent/gank/app/GankApplication.java +++ b/app/src/main/java/com/iamwent/gank/app/GankApplication.java @@ -1,8 +1,12 @@ package com.iamwent.gank.app; import android.app.Application; +import android.content.Context; +import android.os.StrictMode; import com.iamwent.gank.BuildConfig; +import com.squareup.leakcanary.LeakCanary; +import com.squareup.leakcanary.RefWatcher; import com.tencent.bugly.crashreport.CrashReport; /** @@ -14,11 +18,45 @@ public class GankApplication extends Application { + private RefWatcher refWatcher; + + public static RefWatcher getRefWatcher(Context context) { + GankApplication application = (GankApplication) context.getApplicationContext(); + return application.refWatcher; + } + @Override public void onCreate() { super.onCreate(); + toggleStrictMode(BuildConfig.DEBUG); + // init bugly CrashReport.initCrashReport(getApplicationContext(), "094e5020cb", BuildConfig.DEBUG); + + if (LeakCanary.isInAnalyzerProcess(this)) { + return; + } + refWatcher = LeakCanary.install(this); + } + + private void toggleStrictMode(boolean isDebug) { + if (!isDebug) { + return; + } + + StrictMode.ThreadPolicy threadPolicy = new StrictMode.ThreadPolicy + .Builder() + .detectAll() + .penaltyLog() + .build(); + StrictMode.setThreadPolicy(threadPolicy); + + StrictMode.VmPolicy vmPolicy = new StrictMode.VmPolicy + .Builder() + .detectAll() + .penaltyLog() + .build(); + StrictMode.setVmPolicy(vmPolicy); } } diff --git a/app/src/main/java/com/iamwent/gank/app/GankConfig.java b/app/src/main/java/com/iamwent/gank/app/GankConfig.java index fea272c..c0d0fab 100644 --- a/app/src/main/java/com/iamwent/gank/app/GankConfig.java +++ b/app/src/main/java/com/iamwent/gank/app/GankConfig.java @@ -13,21 +13,6 @@ public final class GankConfig { public static final int MAX_COUNT = 10; - /** - * 干货类型:all | Android | iOS | 休息视频 | 福利 | 拓展资源 | 前端 | 瞎推荐 | App - */ - public enum GankType { - ALL, - ANDROID, - IOS, - VIDEO, - GIRL, - EXPANSION, - WEB, - FUNNY, - APP - } - private GankConfig() { throw new AssertionError("NO INSTANCE!"); } diff --git a/app/src/main/java/com/iamwent/gank/ui/base/BaseActivity.java b/app/src/main/java/com/iamwent/gank/ui/base/BaseActivity.java index a4accac..df8f128 100644 --- a/app/src/main/java/com/iamwent/gank/ui/base/BaseActivity.java +++ b/app/src/main/java/com/iamwent/gank/ui/base/BaseActivity.java @@ -9,6 +9,7 @@ import android.view.MenuItem; import com.iamwent.gank.R; +import com.iamwent.gank.app.GankApplication; import butterknife.BindView; import butterknife.ButterKnife; @@ -41,6 +42,13 @@ protected void onCreate(@Nullable Bundle savedInstanceState) { Timber.tag(getClass().getSimpleName()); } + @Override + protected void onDestroy() { + super.onDestroy(); + + GankApplication.getRefWatcher(this).watch(this); + } + protected void setDisplayHomeAsUpEnabled(boolean showHomeAsUp) { ActionBar actionBar = getSupportActionBar(); if (actionBar != null) { diff --git a/app/src/main/java/com/iamwent/gank/ui/base/BaseFragment.java b/app/src/main/java/com/iamwent/gank/ui/base/BaseFragment.java index a08cb73..e45cd8b 100644 --- a/app/src/main/java/com/iamwent/gank/ui/base/BaseFragment.java +++ b/app/src/main/java/com/iamwent/gank/ui/base/BaseFragment.java @@ -8,6 +8,8 @@ import android.view.View; import android.view.ViewGroup; +import com.iamwent.gank.app.GankApplication; + import butterknife.ButterKnife; import timber.log.Timber; @@ -37,4 +39,11 @@ public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, ButterKnife.bind(this, view); return view; } + + @Override + public void onDestroyView() { + super.onDestroyView(); + + GankApplication.getRefWatcher(getContext()).watch(this); + } } diff --git a/app/src/main/java/com/iamwent/gank/ui/base/WebActivity.java b/app/src/main/java/com/iamwent/gank/ui/base/WebActivity.java index 90fab95..3e6f1dd 100644 --- a/app/src/main/java/com/iamwent/gank/ui/base/WebActivity.java +++ b/app/src/main/java/com/iamwent/gank/ui/base/WebActivity.java @@ -1,6 +1,5 @@ package com.iamwent.gank.ui.base; -import android.app.Activity; import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; @@ -20,6 +19,7 @@ import android.widget.Toast; import com.iamwent.gank.R; +import com.iamwent.gank.util.Toasts; import butterknife.BindView; @@ -154,7 +154,7 @@ private void openTheUrlInBrowser() { if (intent.resolveActivity(getPackageManager()) != null) { startActivity(intent); } else { -// Toasts.showLong(R.string.tip_open_fail); + Toasts.showLong(R.string.alert_open_url_failed); } } } diff --git a/app/src/main/java/com/iamwent/gank/ui/category/CategoryAdapter.java b/app/src/main/java/com/iamwent/gank/ui/category/CategoryAdapter.java index e2397aa..295138d 100644 --- a/app/src/main/java/com/iamwent/gank/ui/category/CategoryAdapter.java +++ b/app/src/main/java/com/iamwent/gank/ui/category/CategoryAdapter.java @@ -94,9 +94,7 @@ void bind(Gank newGank) { .load(gank.url) .into(beauty); - beauty.setOnClickListener(v -> { - ImageActivity.start(itemView.getContext(), gank.url); - }); + beauty.setOnClickListener(v -> ImageActivity.start(itemView.getContext(), gank.url)); } } @@ -124,9 +122,7 @@ void bind(Gank newGank) { String time = gank.publishedAt.substring(0, 10); header.setText(String.format("%s\t\t%s", who, time)); - itemView.setOnClickListener(v -> { - WebActivity.start(itemView.getContext(), gank.desc, gank.url); - }); + itemView.setOnClickListener(v -> WebActivity.start(itemView.getContext(), gank.desc, gank.url)); } } } diff --git a/app/src/main/java/com/iamwent/gank/ui/image/ImageActivity.java b/app/src/main/java/com/iamwent/gank/ui/image/ImageActivity.java index f56004f..3bb00e8 100644 --- a/app/src/main/java/com/iamwent/gank/ui/image/ImageActivity.java +++ b/app/src/main/java/com/iamwent/gank/ui/image/ImageActivity.java @@ -35,8 +35,6 @@ public class ImageActivity extends BaseActivity { - private static final int REQUEST_WRITE_EXTERNAL_STORAGE = 110; - @BindView(R.id.iv_img) ImageView imageView; @@ -106,9 +104,7 @@ private void showDialog() { private void saveToLocal() { saveImageToGallery() - .subscribe(uri -> { - Toast.makeText(ImageActivity.this, "saved " + uri.getPath(), Toast.LENGTH_SHORT).show(); - }); + .subscribe(uri -> Toast.makeText(ImageActivity.this, "saved " + uri.getPath(), Toast.LENGTH_SHORT).show()); } private Observable saveImageToGallery() { @@ -119,7 +115,7 @@ private Observable saveImageToGallery() { .observeOn(AndroidSchedulers.mainThread()) .flatMap(new Function>() { @Override - public ObservableSource apply(Boolean aBoolean) throws Exception { + public ObservableSource apply(Boolean granted) throws Exception { Bitmap bitmap = imageView.getDrawingCache(); int start = url.lastIndexOf('/') + 1; int end = url.length(); diff --git a/app/src/main/java/com/iamwent/gank/util/BitmapUtil.java b/app/src/main/java/com/iamwent/gank/util/BitmapUtil.java index f2695bc..673f3da 100644 --- a/app/src/main/java/com/iamwent/gank/util/BitmapUtil.java +++ b/app/src/main/java/com/iamwent/gank/util/BitmapUtil.java @@ -12,8 +12,6 @@ import java.io.IOException; import io.reactivex.Observable; -import io.reactivex.ObservableEmitter; -import io.reactivex.ObservableOnSubscribe; /** * Created by iamwent on 25/02/2017. @@ -31,37 +29,34 @@ public static Observable saveImageToGallery(@NonNull Bitmap bitmap, @NonNul private static Observable saveImage(@NonNull Bitmap bitmap, @NonNull String fileName) { - return Observable.create(new ObservableOnSubscribe() { - @Override - public void subscribe(ObservableEmitter emitter) throws Exception { - File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); - File file = new File(dir, fileName); + return Observable.create(emitter -> { + File dir = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_PICTURES); + File file = new File(dir, fileName); - String path = file.getAbsolutePath(); + String path = file.getAbsolutePath(); - FileOutputStream out = null; - try { - if (!file.exists()) { - file.createNewFile(); - } + FileOutputStream out = null; + try { + if (!file.exists()) { + file.createNewFile(); + } - out = new FileOutputStream(file); - bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); - out.flush(); - out.close(); + out = new FileOutputStream(file); + bitmap.compress(Bitmap.CompressFormat.JPEG, 100, out); + out.flush(); + out.close(); - emitter.onNext(path); - emitter.onComplete(); - } catch (IOException e) { - emitter.onError(e); - } finally { - try { - if (out != null) { - out.close(); - } - } catch (IOException e) { - e.printStackTrace(); + emitter.onNext(path); + emitter.onComplete(); + } catch (IOException e) { + emitter.onError(e); + } finally { + try { + if (out != null) { + out.close(); } + } catch (IOException e) { + e.printStackTrace(); } } }); diff --git a/app/src/main/java/com/iamwent/gank/util/Toasts.java b/app/src/main/java/com/iamwent/gank/util/Toasts.java new file mode 100644 index 0000000..21a0ae6 --- /dev/null +++ b/app/src/main/java/com/iamwent/gank/util/Toasts.java @@ -0,0 +1,62 @@ +package com.iamwent.gank.util; + +import android.annotation.SuppressLint; +import android.app.Application; +import android.content.Context; +import android.support.annotation.NonNull; +import android.support.annotation.StringRes; +import android.widget.Toast; + +/** + * Created by iamwent on 07/03/2017. + * + * @author iamwent + * @since 07/03/2017 + */ + +public final class Toasts { + + @SuppressLint("StaticFieldLeak") + private static Context ctx; + + + public static void install(Context ctx) { + Toasts.ctx = ctx.getApplicationContext(); + } + + public static void showShort(@NonNull String msg) { + show(msg, Toast.LENGTH_SHORT); + } + + public static void showShort(@StringRes int msgId) { + showShort(ctx.getString(msgId)); + } + + public static void showLong(@NonNull String msg) { + show(msg, Toast.LENGTH_LONG); + } + + public static void showLong(@StringRes int msgId) { + showLong(ctx.getString(msgId)); + } + + private static void show(@StringRes int msgId, int duration) { + show(ctx.getString(msgId), duration); + } + + private static void show(@NonNull String msg, int duration) { + check(); + + Toast.makeText(ctx, msg, duration).show(); + } + + private static void check() { + if (ctx == null) { + throw new IllegalArgumentException("You should install it before using"); + } + } + + private Toasts() { + throw new AssertionError("NO INSTANCE!"); + } +} diff --git a/app/src/main/res/values-zh/strings.xml b/app/src/main/res/values-zh/strings.xml index 245872c..b1de8b9 100644 --- a/app/src/main/res/values-zh/strings.xml +++ b/app/src/main/res/values-zh/strings.xml @@ -45,5 +45,6 @@ 干货类型: 干货司机: 壮士,你叫什么名字? + 大爷,打不开啊~ \ No newline at end of file diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index a0d72fa..d0c6b19 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -56,4 +56,8 @@ Submitter: Your name, or github id + No application hold this action + + Gank Leaks +