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.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.FileUtils; import com.gh.common.util.TokenUtils; import com.gh.common.view.CropImageCustom; 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 cropimage_custom; private SoftReference reference; 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("修改太频繁,请稍后再试"); } } }; @Override protected int getLayoutId() { return R.layout.activity_cropimage; } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); initTitle(getString(R.string.title_crop_image)); sp = getSharedPreferences(Config.PREFERENCE, Context.MODE_PRIVATE); 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() { @Override public void call(Subscriber subscriber) { subscriber.onNext(cropimage_custom.savePicture(path)); subscriber.onCompleted(); } }).flatMap(new Func1>() { @Override public Observable call(Boolean status) { if (status == null || !status) { dialog.dismiss(); handler.sendEmptyMessage(1); return null; } return TokenUtils.getToken(CropImageActivity.this, true); } }).flatMap(new Func1>() { @Override public Observable 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>() { @Override public Observable call(JSONObject result) { if (result != null) { try { if (result.getInt("statusCode") == 401) { return TokenUtils.getToken(CropImageActivity.this, false) .flatMap(new Func1>() { @Override public Observable 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() { @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("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 = 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()); } } } }