reformat code & optimize import & reorder entries

This commit is contained in:
CsHeng
2017-04-27 14:27:11 +08:00
parent 153f33f44b
commit 9432e34571
671 changed files with 33616 additions and 34861 deletions

View File

@ -1,32 +1,26 @@
package com.gh.gamecenter;
import android.app.Dialog;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.*;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.os.*;
import android.text.TextUtils;
import android.util.TypedValue;
import android.view.View;
import android.view.ViewGroup.LayoutParams;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;
import android.widget.*;
import butterknife.BindView;
import com.gh.base.BaseActivity;
import com.gh.common.constant.Config;
import com.gh.common.util.BitmapUtils;
import com.gh.common.util.DialogUtils;
import com.gh.common.util.DisplayUtils;
import com.gh.common.util.FileUtils;
import com.gh.common.util.TokenUtils;
import com.gh.common.util.*;
import com.gh.common.view.CropImageCustom;
import org.json.JSONException;
import org.json.JSONObject;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
import java.io.File;
import java.lang.ref.SoftReference;
@ -34,190 +28,184 @@ import java.net.HttpURLConnection;
import java.text.SimpleDateFormat;
import java.util.Date;
import butterknife.BindView;
import rx.Observable;
import rx.Subscriber;
import rx.functions.Action1;
import rx.functions.Func1;
import rx.schedulers.Schedulers;
public class CropImageActivity extends BaseActivity {
@BindView(R.id.cropimage_custom) CropImageCustom cropimage_custom;
@BindView(R.id.cropimage_custom)
CropImageCustom cropimage_custom;
private SoftReference<Bitmap> reference;
private SoftReference<Bitmap> reference;
private SharedPreferences sp;
private SharedPreferences sp;
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
toast("上传成功");
} else if (msg.what == 1) {
toast("上传失败");
} else if (msg.what == 2) {
toast("修改太频繁,请稍后再试");
}
}
};
private Handler handler = new Handler() {
@Override
public void handleMessage(Message msg) {
if (msg.what == 0) {
toast("上传成功");
} else if (msg.what == 1) {
toast("上传失败");
} else if (msg.what == 2) {
toast("修改太频繁,请稍后再试");
}
}
};
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
View contentView = View.inflate(this, R.layout.activity_cropimage, null);
View contentView = View.inflate(this, R.layout.activity_cropimage, null);
init(contentView, "图片裁剪");
init(contentView, "图片裁剪");
sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE);
RelativeLayout reuse_actionbar = (RelativeLayout) findViewById(R.id.reuse_actionbar);
RelativeLayout reuse_actionbar = (RelativeLayout) findViewById(R.id.reuse_actionbar);
TextView confirm = new TextView(this);
confirm.setText("确定");
confirm.setTextColor(0xffffffff);
confirm.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
int padding = DisplayUtils.dip2px(getApplicationContext(), 10);
confirm.setPadding(padding, padding, padding, padding);
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = DialogUtils.showWaitDialog(CropImageActivity.this, "上传中...");
final String path = getCacheDir() + File.separator + System.currentTimeMillis() + ".jpg";
Observable.create(new Observable.OnSubscribe<Boolean>() {
@Override
public void call(Subscriber<? super Boolean> subscriber) {
subscriber.onNext(cropimage_custom.savePicture(path));
subscriber.onCompleted();
}
}).flatMap(new Func1<Boolean, Observable<String>>() {
@Override
public Observable<String> call(Boolean status) {
if (status == null || !status) {
dialog.dismiss();
handler.sendEmptyMessage(1);
return null;
}
return TokenUtils.getToken(CropImageActivity.this, true);
}
}).flatMap(new Func1<String, Observable<JSONObject>>() {
@Override
public Observable<JSONObject> call(String token) {
if (token != null) {
return Observable.just(FileUtils.uploadFile(Config.USER_HOST + "icon", path, token));
}
dialog.dismiss();
handler.sendEmptyMessage(1);
return null;
}
}).flatMap(new Func1<JSONObject, Observable<JSONObject>>() {
@Override
public Observable<JSONObject> call(JSONObject result) {
if (result != null) {
try {
if (result.getInt("statusCode") == 401) {
return TokenUtils.getToken(CropImageActivity.this, false)
.flatMap(new Func1<String, Observable<JSONObject>>() {
@Override
public Observable<JSONObject> call(String token) {
return Observable.just(FileUtils.uploadFile(Config.USER_HOST + "icon", path, token));
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
return Observable.just(result);
}
dialog.dismiss();
handler.sendEmptyMessage(1);
return null;
}
})
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(new Action1<JSONObject>() {
@Override
public void call(JSONObject result) {
if (result != null) {
try {
int statusCode = result.getInt("statusCode");
if (statusCode == HttpURLConnection.HTTP_OK) {
handler.sendEmptyMessage(0);
TextView confirm = new TextView(this);
confirm.setText("确定");
confirm.setTextColor(0xffffffff);
confirm.setTextSize(TypedValue.COMPLEX_UNIT_DIP, 16);
int padding = DisplayUtils.dip2px(getApplicationContext(), 10);
confirm.setPadding(padding, padding, padding, padding);
confirm.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
final Dialog dialog = DialogUtils.showWaitDialog(CropImageActivity.this, "上传中...");
final String path = getCacheDir() + File.separator + System.currentTimeMillis() + ".jpg";
Observable.create(new Observable.OnSubscribe<Boolean>() {
@Override
public void call(Subscriber<? super Boolean> subscriber) {
subscriber.onNext(cropimage_custom.savePicture(path));
subscriber.onCompleted();
}
}).flatMap(new Func1<Boolean, Observable<String>>() {
@Override
public Observable<String> call(Boolean status) {
if (status == null || !status) {
dialog.dismiss();
handler.sendEmptyMessage(1);
return null;
}
return TokenUtils.getToken(CropImageActivity.this, true);
}
}).flatMap(new Func1<String, Observable<JSONObject>>() {
@Override
public Observable<JSONObject> call(String token) {
if (token != null) {
return Observable.just(FileUtils.uploadFile(Config.USER_HOST + "icon", path, token));
}
dialog.dismiss();
handler.sendEmptyMessage(1);
return null;
}
}).flatMap(new Func1<JSONObject, Observable<JSONObject>>() {
@Override
public Observable<JSONObject> call(JSONObject result) {
if (result != null) {
try {
if (result.getInt("statusCode") == 401) {
return TokenUtils.getToken(CropImageActivity.this, false)
.flatMap(new Func1<String, Observable<JSONObject>>() {
@Override
public Observable<JSONObject> call(String token) {
return Observable.just(FileUtils.uploadFile(Config.USER_HOST + "icon", path, token));
}
});
}
} catch (JSONException e) {
e.printStackTrace();
}
return Observable.just(result);
}
dialog.dismiss();
handler.sendEmptyMessage(1);
return null;
}
})
.subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(new Action1<JSONObject>() {
@Override
public void call(JSONObject result) {
if (result != null) {
try {
int statusCode = result.getInt("statusCode");
if (statusCode == HttpURLConnection.HTTP_OK) {
handler.sendEmptyMessage(0);
String iconCount = sp.getString("updateIconCount", null);
String iconCount = sp.getString("updateIconCount", null);
long l = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String time = format.format(new Date(l));
long l = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
String time = format.format(new Date(l));
JSONObject jsonObject = new JSONObject();
jsonObject.put("time", time);
JSONObject jsonObject = new JSONObject();
jsonObject.put("time", time);
if (TextUtils.isEmpty(iconCount)) {
jsonObject.put("count", 1);
} else {
JSONObject json = new JSONObject(iconCount);
String lastTime = json.getString("time");
if (lastTime.equals(time)) {
jsonObject.put("count", json.getInt("count") + 1);
} else {
jsonObject.put("count", 1);
}
}
if (TextUtils.isEmpty(iconCount)) {
jsonObject.put("count", 1);
} else {
JSONObject json = new JSONObject(iconCount);
String lastTime = json.getString("time");
if (lastTime.equals(time)) {
jsonObject.put("count", json.getInt("count") + 1);
} else {
jsonObject.put("count", 1);
}
}
sp.edit().putString("updateIconCount", jsonObject.toString()).apply();
sp.edit().putString("updateIconCount", jsonObject.toString()).apply();
Intent data = new Intent();
data.putExtra("url", result.getString("icon"));
setResult(RESULT_OK, data);
finish();
Intent data = new Intent();
data.putExtra("url", result.getString("icon"));
setResult(RESULT_OK, data);
finish();
} else if (statusCode == HttpURLConnection.HTTP_FORBIDDEN
&& "too frequent".equals(result.getString("detail"))) {
handler.sendEmptyMessage(2);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
handler.sendEmptyMessage(1);
}
dialog.dismiss();
}
});
}
});
RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rparams.addRule(RelativeLayout.CENTER_VERTICAL);
confirm.setLayoutParams(rparams);
reuse_actionbar.addView(confirm);
}
} else if (statusCode == HttpURLConnection.HTTP_FORBIDDEN
&& "too frequent".equals(result.getString("detail"))) {
handler.sendEmptyMessage(2);
}
} catch (JSONException e) {
e.printStackTrace();
}
} else {
handler.sendEmptyMessage(1);
}
dialog.dismiss();
}
});
}
});
RelativeLayout.LayoutParams rparams = new RelativeLayout.LayoutParams(
LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
rparams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT);
rparams.addRule(RelativeLayout.CENTER_VERTICAL);
confirm.setLayoutParams(rparams);
reuse_actionbar.addView(confirm);
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && (reference == null || reference.get() == null)) {
ImageView imageView = cropimage_custom.getCropImageZoomView();
Bitmap bitmap = BitmapUtils.getBitmapByFile(getIntent().getStringExtra("path"),
imageView.getWidth(), imageView.getHeight());
if (bitmap != null) {
reference = new SoftReference<>(bitmap);
imageView.setImageBitmap(reference.get());
}
}
}
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (hasFocus && (reference == null || reference.get() == null)) {
ImageView imageView = cropimage_custom.getCropImageZoomView();
Bitmap bitmap = BitmapUtils.getBitmapByFile(getIntent().getStringExtra("path"),
imageView.getWidth(), imageView.getHeight());
if (bitmap != null) {
reference = new SoftReference<>(bitmap);
imageView.setImageBitmap(reference.get());
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (reference != null) {
reference.get().recycle();
}
@Override
protected void onDestroy() {
super.onDestroy();
if (reference != null) {
reference.get().recycle();
}
handler.removeCallbacksAndMessages(null);
}
handler.removeCallbacksAndMessages(null);
}
}