242 lines
10 KiB
Java
242 lines
10 KiB
Java
package com.gh.gamecenter;
|
|
|
|
import android.app.Dialog;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.SharedPreferences;
|
|
import android.graphics.Bitmap;
|
|
import android.os.Bundle;
|
|
import android.os.Handler;
|
|
import android.os.Message;
|
|
import android.preference.PreferenceManager;
|
|
import android.support.annotation.NonNull;
|
|
import android.support.v4.content.ContextCompat;
|
|
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 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.EntranceUtils;
|
|
import com.gh.common.view.CropImageCustom;
|
|
import com.gh.gamecenter.manager.UserManager;
|
|
import com.lightgame.download.FileUtils;
|
|
|
|
import org.json.JSONException;
|
|
import org.json.JSONObject;
|
|
|
|
import java.io.File;
|
|
import java.lang.ref.SoftReference;
|
|
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 mCropimageCustom;
|
|
|
|
private SoftReference<Bitmap> reference;
|
|
|
|
private SharedPreferences sp;
|
|
|
|
private UserManager mUserManager;
|
|
|
|
private Handler handler = new Handler() {
|
|
@Override
|
|
public void handleMessage(Message msg) {
|
|
switch (msg.what) {
|
|
case 0:
|
|
toast("上传成功");
|
|
break;
|
|
case 1:
|
|
toast("上传失败");
|
|
break;
|
|
case 2:
|
|
toast("修改太频繁,请稍后再试");
|
|
break;
|
|
}
|
|
}
|
|
};
|
|
|
|
@NonNull
|
|
public static Intent getIntent(Context context, String picturePath, String entrance) {
|
|
Intent intent = new Intent(context, CropImageActivity.class);
|
|
intent.putExtra(EntranceUtils.KEY_PATH, picturePath);
|
|
intent.putExtra(EntranceUtils.KEY_ENTRANCE, entrance);
|
|
return intent;
|
|
}
|
|
|
|
@Override
|
|
protected int getLayoutId() {
|
|
return R.layout.activity_cropimage;
|
|
}
|
|
|
|
@Override
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
super.onCreate(savedInstanceState);
|
|
|
|
setNavigationTitle(getString(R.string.title_crop_image));
|
|
|
|
sp = PreferenceManager.getDefaultSharedPreferences(this);
|
|
|
|
mUserManager = UserManager.getInstance();
|
|
|
|
RelativeLayout reuse_actionbar = (RelativeLayout) findViewById(R.id.reuse_actionbar);
|
|
|
|
TextView confirm = new TextView(this);
|
|
confirm.setText("确定");
|
|
confirm.setTextColor(ContextCompat.getColor(this, R.color.title));
|
|
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(mCropimageCustom.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 Observable.just(mUserManager.getToken());
|
|
}
|
|
}).flatMap(new Func1<String, Observable<JSONObject>>() {
|
|
@Override
|
|
public Observable<JSONObject> call(String token) {
|
|
if (token != null) {
|
|
return Observable.just(FileUtils.uploadFile(Config.API_HOST + "support/upload/img?type=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 Observable.just(FileUtils.uploadFile(Config.API_HOST + "support/upload/img?type=icon", path,mUserManager.getToken()));
|
|
}
|
|
} 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);
|
|
|
|
long l = System.currentTimeMillis();
|
|
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd");
|
|
String time = format.format(new Date(l));
|
|
|
|
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);
|
|
}
|
|
}
|
|
|
|
sp.edit().putString("updateIconCount", jsonObject.toString()).apply();
|
|
|
|
Intent data = new Intent();
|
|
data.putExtra(EntranceUtils.KEY_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);
|
|
}
|
|
|
|
@Override
|
|
protected void onDestroy() {
|
|
super.onDestroy();
|
|
if (reference != null && reference.get() != null) {
|
|
reference.get().recycle();
|
|
}
|
|
|
|
handler.removeCallbacksAndMessages(null);
|
|
}
|
|
|
|
@Override
|
|
public void onWindowFocusChanged(boolean hasFocus) {
|
|
super.onWindowFocusChanged(hasFocus);
|
|
if (hasFocus && (reference == null || reference.get() == null)) {
|
|
ImageView imageView = mCropimageCustom.getCropImageZoomView();
|
|
Bitmap bitmap = BitmapUtils.getBitmapByFile(getIntent().getStringExtra(EntranceUtils.KEY_PATH),
|
|
imageView.getWidth(), imageView.getHeight());
|
|
if (bitmap != null) {
|
|
reference = new SoftReference<>(bitmap);
|
|
imageView.setImageBitmap(reference.get());
|
|
}
|
|
}
|
|
}
|
|
}
|