Files
assistant-android/app/src/main/java/com/gh/gamecenter/CropImageActivity.java

214 lines
8.6 KiB
Java

package com.gh.gamecenter;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Message;
import android.preference.PreferenceManager;
import androidx.annotation.NonNull;
import android.text.TextUtils;
import android.view.MenuItem;
import android.widget.ImageView;
import com.gh.base.BaseActivity;
import com.gh.base.fragment.WaitingDialogFragment;
import com.gh.common.util.BitmapUtils;
import com.gh.common.util.EntranceUtils;
import com.gh.common.util.UploadImageUtils;
import com.gh.common.view.CropImageCustom;
import com.gh.gamecenter.retrofit.Response;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
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 java.util.Locale;
import butterknife.BindView;
import io.reactivex.Observable;
import io.reactivex.ObservableOnSubscribe;
import io.reactivex.schedulers.Schedulers;
import retrofit2.HttpException;
public class CropImageActivity extends BaseActivity {
@BindView(R.id.cropimage_custom)
CropImageCustom mCropimageCustom;
private SoftReference<Bitmap> reference;
private SharedPreferences sp;
@Override
protected void handleMessage(Message msg) {
switch (msg.what) {
case 0:
toast("上传成功");
break;
case 1:
toast("上传失败");
break;
case 2:
toast("修改太频繁,请稍后再试");
break;
case 3:
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));
setToolbarMenu(R.menu.menu_positive);
sp = PreferenceManager.getDefaultSharedPreferences(this);
}
@Override
public boolean onMenuItemClick(MenuItem item) {
if (item.getItemId() == R.id.layout_menu_positive) {
final WaitingDialogFragment postDialog = WaitingDialogFragment.newInstance(getString(R.string.post_img));
postDialog.show(getSupportFragmentManager(), null);
final String path = getCacheDir() + File.separator + System.currentTimeMillis() + ".jpg";
Observable.create((ObservableOnSubscribe<String>) emitter -> {
boolean isSuccess = mCropimageCustom.savePicture(path);
if (isSuccess) {
UploadImageUtils.INSTANCE.uploadImage(UploadImageUtils.UploadType.icon, path, new UploadImageUtils.OnUploadImageListener() {
@Override
public void onSuccess(@NotNull String imageUrl) {
emitter.onNext(imageUrl);
emitter.onComplete();
}
@Override
public void onError(@Nullable Throwable e) {
if (e != null) {
emitter.onError(e);
} else {
emitter.onError(new IllegalStateException("upload image error"));
}
}
@Override
public void onProgress(long total, long progress) {
int percent = (int) (100 * (progress / (float) total));
if (percent >= 100) percent = 99;
if (postDialog != null) {
postDialog.uploadWaitingHint("图片上传中 " + percent + "%");
}
}
});
}
}).subscribeOn(Schedulers.io())
.observeOn(Schedulers.io())
.subscribe(new Response<String>() {
@Override
public void onResponse(String url) {
try {
if (postDialog != null) postDialog.dismissAllowingStateLoss();
mBaseHandler.sendEmptyMessage(0);
String iconCount = sp.getString("updateIconCount", null);
long l = System.currentTimeMillis();
SimpleDateFormat format = new SimpleDateFormat("yyyyMMdd", Locale.CHINA);
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, url);
setResult(RESULT_OK, data);
finish();
} catch (Exception e) {
e.printStackTrace();
}
}
@Override
public void onFailure(HttpException e) {
if (postDialog != null) postDialog.dismissAllowingStateLoss();
try {
if (e != null && e.code() == HttpURLConnection.HTTP_FORBIDDEN && e.response().errorBody() != null) {
JSONObject object = new JSONObject(e.response().errorBody().string());
String detail = object.getString("detail");
if ("too frequent".equals(detail)) {
mBaseHandler.sendEmptyMessage(2);
} else if ("INVALID PICTURE".equals(detail)) {
mBaseHandler.sendEmptyMessage(3);
} else {
mBaseHandler.sendEmptyMessage(1);
}
} else {
mBaseHandler.sendEmptyMessage(1);
}
} catch (Exception e1) {
e1.printStackTrace();
mBaseHandler.sendEmptyMessage(1);
}
}
});
}
return super.onMenuItemClick(item);
}
@Override
protected void onDestroy() {
super.onDestroy();
if (reference != null && reference.get() != null) {
reference.get().recycle();
}
}
@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());
}
}
}
}