Skip to content

Commit

Permalink
config leakcanary & fix bugs
Browse files Browse the repository at this point in the history
  • Loading branch information
iamwent committed Mar 7, 2017
1 parent 2662c53 commit f744f11
Show file tree
Hide file tree
Showing 13 changed files with 156 additions and 61 deletions.
6 changes: 3 additions & 3 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
3 changes: 2 additions & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE" />
<uses-permission android:name="android.permission.ACCESS_WIFI_STATE" />
<uses-permission android:name="android.permission.READ_LOGS" />
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE" />

<application
android:name=".app.GankApplication"
Expand Down Expand Up @@ -67,7 +68,7 @@
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain"/>
<data android:mimeType="text/plain" />
</intent-filter>

</activity>
Expand Down
38 changes: 38 additions & 0 deletions app/src/main/java/com/iamwent/gank/app/GankApplication.java
Original file line number Diff line number Diff line change
@@ -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;

/**
Expand All @@ -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);
}
}
15 changes: 0 additions & 15 deletions app/src/main/java/com/iamwent/gank/app/GankConfig.java
Original file line number Diff line number Diff line change
Expand Up @@ -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!");
}
Expand Down
8 changes: 8 additions & 0 deletions app/src/main/java/com/iamwent/gank/ui/base/BaseActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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) {
Expand Down
9 changes: 9 additions & 0 deletions app/src/main/java/com/iamwent/gank/ui/base/BaseFragment.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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);
}
}
4 changes: 2 additions & 2 deletions app/src/main/java/com/iamwent/gank/ui/base/WebActivity.java
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -20,6 +19,7 @@
import android.widget.Toast;

import com.iamwent.gank.R;
import com.iamwent.gank.util.Toasts;

import butterknife.BindView;

Expand Down Expand Up @@ -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);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
}

Expand Down Expand Up @@ -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));
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down Expand Up @@ -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<Uri> saveImageToGallery() {
Expand All @@ -119,7 +115,7 @@ private Observable<Uri> saveImageToGallery() {
.observeOn(AndroidSchedulers.mainThread())
.flatMap(new Function<Boolean, ObservableSource<Uri>>() {
@Override
public ObservableSource<Uri> apply(Boolean aBoolean) throws Exception {
public ObservableSource<Uri> apply(Boolean granted) throws Exception {
Bitmap bitmap = imageView.getDrawingCache();
int start = url.lastIndexOf('/') + 1;
int end = url.length();
Expand Down
51 changes: 23 additions & 28 deletions app/src/main/java/com/iamwent/gank/util/BitmapUtil.java
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -31,37 +29,34 @@ public static Observable<Uri> saveImageToGallery(@NonNull Bitmap bitmap, @NonNul

private static Observable<String> saveImage(@NonNull Bitmap bitmap, @NonNull String fileName) {

return Observable.create(new ObservableOnSubscribe<String>() {
@Override
public void subscribe(ObservableEmitter<String> 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();
}
}
});
Expand Down
62 changes: 62 additions & 0 deletions app/src/main/java/com/iamwent/gank/util/Toasts.java
Original file line number Diff line number Diff line change
@@ -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!");
}
}
1 change: 1 addition & 0 deletions app/src/main/res/values-zh/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,6 @@
<string name="label_submit_type">干货类型:</string>
<string name="label_submit_who">干货司机:</string>
<string name="hint_submit_who">壮士,你叫什么名字?</string>
<string name="alert_open_url_failed">大爷,打不开啊~</string>

</resources>
4 changes: 4 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -56,4 +56,8 @@
<string name="label_submit_who">Submitter:</string>
<string name="hint_submit_who">Your name, or github id</string>

<string name="alert_open_url_failed">No application hold this action</string>

<string name="leak_canary_display_activity_label" translatable="false">Gank Leaks</string>

</resources>

0 comments on commit f744f11

Please sign in to comment.