package cn.flightfeather.thirdapp.module.inspectioninfo;
import android.app.Dialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.os.Build;
import android.os.Bundle;
import android.os.Environment;
import android.support.annotation.RequiresApi;
import android.support.constraint.ConstraintLayout;
import android.support.constraint.Group;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatActivity;
import android.transition.Explode;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import com.amap.api.maps.AMap;
import com.amap.api.maps.CameraUpdateFactory;
import com.amap.api.maps.TextureMapView;
import com.amap.api.maps.model.BitmapDescriptorFactory;
import com.amap.api.maps.model.LatLng;
import com.amap.api.maps.model.Marker;
import com.amap.api.maps.model.MarkerOptions;
import com.ping.greendao.gen.MediafileDao;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;
import java.util.List;
import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;
import cn.flightfeather.thirdapp.CommonApplication;
import cn.flightfeather.thirdapp.R;
import cn.flightfeather.thirdapp.activity.PhotoViewerActivity;
import cn.flightfeather.thirdapp.bean.Mediafile;
import cn.flightfeather.thirdapp.bean.vo.MediafileVo;
import cn.flightfeather.thirdapp.bean.vo.ProblemlistVo;
import cn.flightfeather.thirdapp.httpservice.ProblemListService;
import cn.flightfeather.thirdapp.module.base.BaseTakePicActivity;
import cn.flightfeather.thirdapp.util.Constant;
import cn.flightfeather.thirdapp.util.DateFormatter;
import cn.flightfeather.thirdapp.util.DialogUtil;
import cn.flightfeather.thirdapp.util.Domain;
import cn.flightfeather.thirdapp.util.UUIDGenerator;
import cn.flightfeather.thirdapp.util.file.FileUtil;
import cn.flightfeather.thirdapp.util.photo.OnUpLoadListener;
import cn.flightfeather.thirdapp.util.photo.PhotoUtil;
import okhttp3.ResponseBody;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
public class ProblemChangeDetailActivity extends AppCompatActivity {
//
public static final String ARG_PROBLEM = "problems";
public static final String ARG_PROBLEM_TYPE = "problemType";
public static final String ARG_TEXT_DATA = "textData";
//展示的问题对象
private ProblemlistVo problemlistVo = new ProblemlistVo();
/**
* 两种问题类型 {@link Constant#CHANGED}和{@link Constant#UNCHANGED}
*/
private String problemType = "";
//内部数据类,前一个context传入一些不变的展示数据
private TextData textData = new TextData();
//requestCode
private final int TAKE_PROBLEM_PHOTO =13;
private final int TAKE_CHANGE_PHOTO =14;
private final int PICK_PROBLEM_PHOTO =15;
private final int PICK_CHANGE_PHOTO = 16;
private CommonApplication application;
private MediafileDao mediafileDao;
//是否已编辑过
private boolean isEdited;
private Unbinder unbinder;
//
//
public static class TextData implements Serializable {
//离整改承诺期限剩余或逾期多少天
String text_deadline_status = "";
String text_remaining_days = "";
//是否承诺整改
String text_promise = "";
boolean text_promise_selected;
//截止日期
String text_change_deadline = "";
int change_deadline_visible;
//超时整改天数
int cl_deadline_visible;
public TextData() {
}
public TextData(String text_deadline_status, String text_remaining_days, String text_promise, boolean text_promise_selected, String text_change_deadline, int change_deadline_visible, int cl_deadline_visible) {
this.text_deadline_status = text_deadline_status;
this.text_remaining_days = text_remaining_days;
this.text_promise = text_promise;
this.text_promise_selected = text_promise_selected;
this.text_change_deadline = text_change_deadline;
this.change_deadline_visible = change_deadline_visible;
this.cl_deadline_visible = cl_deadline_visible;
}
}
//
//
@RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
//设置允许使用转场动画
getWindow().setEnterTransition(new Explode().setDuration(500));
getWindow().setExitTransition(new Explode().setDuration(300));
setContentView(R.layout.activity_problem_change_detail);
unbinder = ButterKnife.bind(this);
this.application = (CommonApplication) getApplication();
initParam();
updateMediafile();
initView();
initProblemPhoto(this.mediafileListProblem, this.imageViewProblemList);
initChangingPhoto(this.mediafileListChange, this.imageViewChangeList);
initMap(savedInstanceState);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode ==TAKE_CHANGE_PHOTO){
if (resultCode == RESULT_OK && fileCurrent != null && !mediafileListProblem.isEmpty()) {
Mediafile mediafile = MediafileVo.newMediaFile(fileCurrent, true, this.mediafileListProblem.get(0));
this.mediafileListChange.add(mediafile);
this.mediafileDao.insert(mediafile);
this.isEdited = true;
initChangingPhoto(this.mediafileListChange, this.imageViewChangeList);
}
}else if (requestCode ==PICK_CHANGE_PHOTO){
if (resultCode ==RESULT_OK){
List paths = data.getStringArrayListExtra(BaseTakePicActivity.EXTRA_SELECT_IMAGES);
for (String p : paths) {
File oldFile = new File(p);
File newFile = getFilePath();
try {
FileUtil.copyFile(oldFile,newFile);
if (!mediafileListProblem.isEmpty()) {
Mediafile mediafile = MediafileVo.newMediaFile(newFile, true, this.mediafileListProblem.get(0));
this.mediafileListChange.add(mediafile);
this.mediafileDao.insert(mediafile);
this.isEdited = true;
}
} catch (IOException e) {
e.printStackTrace();
}
}
initChangingPhoto(this.mediafileListChange, this.imageViewChangeList);
if (this.problemlistVo.getIschanged()) {
refreshSaveBtn(3);
}
}
} else if (requestCode == PhotoViewerActivity.CHANGE_PHOTO && resultCode == RESULT_OK) {
if (data!=null){
int position = data.getIntExtra("position",-1);
if (position>-1){
mediafileDao.deleteByKey(mediafileListChange.get(position).getGuid());
this.mediafileListChange.remove(position);
//fixme 2020/11/12 由于删除单张图片是直接联网将服务器数据一并删除,因此当图片全部删除后,立即更新问题为未整改状态
if (mediafileListChange.isEmpty()) {
uploadProblem();
}
this.isEdited = !mediafileListChange.isEmpty();
initChangingPhoto(this.mediafileListChange, this.imageViewChangeList);
if (this.problemlistVo.getIschanged()) {
refreshSaveBtn(3);
}
}
}
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (unbinder != null) {
unbinder.unbind();
}
}
//
//
@BindView(R.id.iv_problem_photo1) ImageView iv_problem_photo1;
@BindView(R.id.iv_problem_photo2) ImageView iv_problem_photo2;
@BindView(R.id.iv_problem_photo3) ImageView iv_problem_photo3;
List imageViewProblemList = new ArrayList<>();
@BindView(R.id.iv_problem_change_photo1) ImageView iv_problem_change_photo1;
@BindView(R.id.iv_problem_change_photo2) ImageView iv_problem_change_photo2;
@BindView(R.id.iv_problem_change_photo3) ImageView iv_problem_change_photo3;
List imageViewChangeList = new ArrayList<>();
private void initParam() {
Bundle bundle = getIntent().getExtras();
if (bundle != null) {
this.problemlistVo = (ProblemlistVo) bundle.getSerializable(ARG_PROBLEM);
this.problemType = bundle.getString(ARG_PROBLEM_TYPE);
this.textData = (TextData) bundle.getSerializable(ARG_TEXT_DATA);
}
this.mediafileDao = application.getDaoSession().getMediafileDao();
this.imageViewProblemList.add(iv_problem_photo1);
this.imageViewProblemList.add(iv_problem_photo2);
this.imageViewProblemList.add(iv_problem_photo3);
this.imageViewChangeList.add(iv_problem_change_photo1);
this.imageViewChangeList.add(iv_problem_change_photo2);
this.imageViewChangeList.add(iv_problem_change_photo3);
}
//
//
@BindView(R.id.tv_problem_type_name) TextView tv_problem_type_name;
@BindView(R.id.tv_problem_time) TextView tv_problem_time;
@BindView(R.id.tv_problem_location) TextView tv_problem_location;
@BindView(R.id.text_deadline_status) TextView text_deadline_status;
@BindView(R.id.text_remaining_days) TextView text_remaining_days;
@BindView(R.id.tv_problem_name) TextView tv_problem_name;
@BindView(R.id.image_promise) ImageView image_promise;
@BindView(R.id.text_promise) TextView text_promise;
@BindView(R.id.text_change_deadline) TextView text_change_deadline;
@BindView(R.id.text_change_type) TextView text_change_type;
@BindView(R.id.text_change_tips) TextView text_change_tips;
@BindView(R.id.text_upload_photo) TextView text_upload_photo;
@BindView(R.id.cl_deadline) ConstraintLayout cl_deadline;
@BindView(R.id.group_change_effect) Group group_change_effect;
@BindView(R.id.btn_save) Button btn_save;
private void initView() {
//问题基本信息
tv_problem_type_name.setText(this.problemlistVo.getTypename());
tv_problem_time.setText(DateFormatter.dateTimeFormat.format(this.problemlistVo.getTime()));
tv_problem_location.setText(this.problemlistVo.getLocation());
tv_problem_name.setText(this.problemlistVo.getProblemname());
//逾期天数
text_deadline_status.setText(this.textData.text_deadline_status);
text_remaining_days.setText(this.textData.text_remaining_days);
//是否整改
image_promise.setSelected(this.textData.text_promise_selected);
text_promise.setText(this.textData.text_promise);
//整改截止日期
text_change_deadline.setText(this.textData.text_change_deadline);
text_change_deadline.setVisibility(this.textData.change_deadline_visible);
//整改类型
if (problemType.equals(Constant.CHANGED)
&& problemlistVo.getChangecatalog() != null) {
text_change_type.setText(problemlistVo.getChangecatalog());
} else {
text_change_type.setVisibility(View.INVISIBLE);
}
cl_deadline.setVisibility(this.textData.cl_deadline_visible);
//整改建议
if (problemlistVo.getAdvise() != null) {
text_change_tips.setText(problemlistVo.getAdvise());
}
if (problemType.equals(Constant.UNCHANGED)) {
refreshSaveBtn(1);
text_upload_photo.setText(R.string.click_to_upload_photo);
text_upload_photo.setTextColor(getResources().getColor(R.color.black));
group_change_effect.setVisibility(View.GONE);
} else {
refreshSaveBtn(2);
text_upload_photo.setText(R.string.changed_photo);
group_change_effect.setVisibility(View.VISIBLE);
}
}
//
private void refreshSaveBtn(int status) {
switch (status) {
case 1: {
//未整改
btn_save.setText(R.string.submit);
btn_save.setBackgroundResource(R.drawable.shape_button_blue);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonClick();
}
});
break;
}
case 2: {
//已整改
btn_save.setText(R.string.changed);
btn_save.setBackgroundResource(R.drawable.shape_button_green);
btn_save.setOnClickListener(null);
break;
}
case 3: {
//提交后修改图片
btn_save.setText(R.string.submit_again);
btn_save.setBackgroundResource(R.drawable.shape_button_blue);
btn_save.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
onButtonClick();
}
});
break;
}
}
}
//
@BindView(R.id.image_no_map) ImageView image_no_map;
@BindView(R.id.tmv_main) TextureMapView tmv_main;
private void initMap(Bundle savedInstanceState) {
tmv_main.onCreate(savedInstanceState);
AMap aMap = tmv_main.getMap();
LatLng latLng = null;
MarkerOptions options = new MarkerOptions();
Bitmap bm = null;
//获取问题定位
Double latitude = this.problemlistVo.getLatitude();
Double longitude =this.problemlistVo.getLongitude();
if ((latitude != null && longitude != null)
&& (latitude != 0 && longitude != 0)) {
latLng = new LatLng(this.problemlistVo.getLatitude(), this.problemlistVo.getLongitude());
bm = BitmapFactory.decodeResource(getResources(), R.drawable.icon_mark_red);
Matrix matrix = new Matrix();
matrix.setScale(0.28f, 0.28f);
Bitmap bmBig = Bitmap.createBitmap(bm, 0, 0, bm.getWidth(),
bm.getHeight(), matrix, true);
options.icon(BitmapDescriptorFactory.fromBitmap(bmBig));
options.position(latLng);
Marker marker = aMap.addMarker(options);
marker.setInfoWindowEnable(false);
aMap.moveCamera(CameraUpdateFactory.newLatLngZoom(latLng, 14f));
//隐藏默认图片
tmv_main.setVisibility(View.VISIBLE);
image_no_map.setVisibility(View.GONE);
} else {
tmv_main.setVisibility(View.GONE);
image_no_map.setVisibility(View.VISIBLE);
}
// double scenseLat = application.getCurrentUser()
// double scenseLng = intent.getDoubleExtra("scenseLng",0);
// MarkerOptions siteMarkeroptions = new MarkerOptions().position(new LatLng(scenseLat,scenseLng));
// aMap.addMarker(siteMarkeroptions);
}
//
//
private List mediafileListChange = new ArrayList<>();//整改图片列表
private List mediafileListProblem = new ArrayList<>();//问题图片列表
private void updateMediafile() {
List mediafileList = mediafileDao.queryBuilder()
.where(MediafileDao.Properties.Businessguid.eq(problemlistVo.getGuid()))
.where(MediafileDao.Properties.Remark.eq("未上传"))
.list();
if (problemlistVo.getMediafileList() != null) {
mediafileList.addAll(problemlistVo.getMediafileList());
}
for (Mediafile m : mediafileList) {
if (m.getIschanged()) {
this.mediafileListChange.add(m);
} else {
this.mediafileListProblem.add(m);
}
}
}
//
//
private void initProblemPhoto(List mediafileList, List imageViews) {
int photoCount = PhotoUtil.loadImage(mediafileList, imageViews, this);//将图片加载至对应的imageView上
List files = new ArrayList<>();
for (Mediafile m :
mediafileList) {
File tempFile = FileUtil.getFileFromMediaFile(m);
files.add(tempFile);
}
for (int i = 0; i < photoCount; i++) {//添加图片放大点击事件
imageViews.get(i)
.setOnClickListener(PhotoUtil.OnPhotoViewClickListener(this,
files,
i,
mediafileList,
PhotoViewerActivity.PROBLEM_PHOTO,
false));
}
}
//
//
//整改图片
/**
*
* @param mediafileList
* @param imageViews
* @deprecated 20190719 上传完成后,客户需要能够修改上传图片的功能,即也能拍照和删除图片,此函数删去
*/
@Deprecated
private void initChangedPhoto(List mediafileList, List imageViews) {
int photoCount = PhotoUtil.loadImage(mediafileList, imageViews, this);//将图片加载至对应的imageView上
List files = new ArrayList<>();
for (Mediafile m :
mediafileList) {
File tempFile = FileUtil.getFileFromMediaFile(m);
files.add(tempFile);
}
for (int i = 0; i < photoCount; i++) {//添加图片放大点击事件
imageViews.get(i)
.setOnClickListener(PhotoUtil.OnPhotoViewClickListener(this,
files,
i,
mediafileList,
PhotoViewerActivity.CHANGE_PHOTO,
true));
}
}
//
//
/**
*
* @param mediafileList 在未整改时,列表{@link #mediafileListChange}从数据库获取的结果是空,因此此处可直接将该列表对象用于整改时的新图片上传
* @param imageViews
*/
private void initChangingPhoto(final List mediafileList, final List imageViews) {
int photoCount = PhotoUtil.loadImage(mediafileList, imageViews, this);
List files = new ArrayList<>();
for (Mediafile m :
mediafileList) {
File tempFile = FileUtil.getFileFromMediaFile(m);
files.add(tempFile);
}
for (int i = 0; i < photoCount; i++) {//添加图片放大点击事件
imageViews.get(i)
.setOnClickListener(PhotoUtil.OnPhotoViewClickListener(this,
files,
i,
mediafileList,
PhotoViewerActivity.CHANGE_PHOTO,
true));
}
if (photoCount >= 3) return;
final int leftCount = 3 - photoCount > 0 ? 3 - photoCount : 0;
ImageView image_addPhoto = imageViews.get(photoCount);
image_addPhoto.setImageResource(R.drawable.icon_add_photo);
image_addPhoto.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
PhotoUtil.pickPhoto2(ProblemChangeDetailActivity.this, PICK_CHANGE_PHOTO, leftCount);
}
});
while (++photoCount < imageViews.size()) {
imageViews.get(photoCount).setImageResource(R.drawable.icon_add_photo_blank);
}
}
//
//
private File fileCurrent;
private File getFilePath() {
Mediafile mediafile = this.mediafileListProblem.get(0);
String path = mediafile.getPath()
+ "整改/"
+ this.problemlistVo.getProblemname()
+ " "
+ this.problemlistVo.getLocation()
+" 整改 "
+ UUIDGenerator.generateUUID(4)
+".jpg";
File newFile = new File(Environment.getExternalStorageDirectory(), path);
if (!newFile.getParentFile().exists()) {
newFile.getParentFile().mkdirs();
}
this.fileCurrent = newFile;
return newFile;
}
//
//
private void refreshProblemStatus() {
//整改图片全删除后,问题退回未整改审核通过状态
if (mediafileListChange.isEmpty()) {
this.problemlistVo.setIschanged(false);//设置为未整改
this.problemlistVo.setExtension3(Domain.PROBLEM_CHECK_PASS);
this.problemlistVo.setChangedtime(null);
this.problemlistVo.setChangecatalog(null);
} else {
this.problemlistVo.setIschanged(true);//设置为已整改
this.problemlistVo.setExtension3(Domain.CHANGE_UNCHECKED);//设置为整改待审核
this.problemlistVo.setChangedtime(new Date());//设置整改时间为当前时间
this.problemlistVo.getMediafileList().clear();
this.problemlistVo.getMediafileList().addAll(this.mediafileListProblem);
this.problemlistVo.getMediafileList().addAll(this.mediafileListChange);//将新增的图片添加进媒体文件列表,用于app数据刷新
Calendar calendar = Calendar.getInstance();
int today = calendar.get(Calendar.DAY_OF_YEAR);
calendar.setTime(this.problemlistVo.getTime());
int problemDay = calendar.get(Calendar.DAY_OF_YEAR);
//有承诺整改的日期
if (this.problemlistVo.getExtension1() != null) {
//整改时间已超过监管当天的24点,则认为是承诺整改
if (today > problemDay) {
this.problemlistVo.setChangecatalog(Domain.PROMISE_CHANGE);
}
//否则是现场当天整改
else {
this.problemlistVo.setChangecatalog(Domain.LOACAL_CHANGE);
}
}
//没有承诺整改的日期是未承诺整改
else {
this.problemlistVo.setChangecatalog(Domain.UNPROMISE_CHANGE);
}
}
}
//提交问题
Dialog loadingDialog;
public void onButtonClick() {
// if (!problemType.equals(Constant.UNCHANGED)) {
// return;
// }
if (this.isEdited){
loadingDialog = DialogUtil.createLoadingDialog(this, "");
upLoadPic();
}else {
Toast.makeText(application, R.string.take_one_change_photo_at_least, Toast.LENGTH_SHORT).show();
}
}
@Override
public void onBackPressed() {
// super.onBackPressed();
close();
}
@OnClick(R.id.image_close)
void onCloseClick() {
close();
}
private void close() {
if (this.isEdited) {
AlertDialog.Builder dialog = new AlertDialog.Builder(this);
dialog.setMessage("整改图片还未提交,是否退出?");
dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialogInterface, int i) {
finish();
}
});
dialog.setNegativeButton("取消", null);
dialog.show();
} else {
supportFinishAfterTransition();
}
}
private void uploadProblem() {
refreshProblemStatus();
Call updateProblemList = application.getRetrofit().create(ProblemListService.class).updateProblemList(this.problemlistVo);
updateProblemList.enqueue(new Callback() {
@Override
public void onResponse(Call call, Response response) {
if (response.body()!=null){
Toast.makeText(application, R.string.submit_success, Toast.LENGTH_LONG).show();
if (loadingDialog != null && loadingDialog.isShowing()) {
loadingDialog.dismiss();
}
//返回requestCode
Intent intent = new Intent();
intent.putExtra(ARG_PROBLEM, problemlistVo);
setResult(RESULT_OK, intent);
//发送数据更新广播
Intent intent1 = new Intent("cn.flightfeather.thirdapp.broadcasereceiver.MainReceiver");
intent1.setAction(Constant.REFRESH_DATA);
intent1.putExtra(ARG_PROBLEM, problemlistVo);
sendBroadcast(intent1);
finish();
}else if (response.errorBody()!=null){
Toast.makeText(application, R.string.submit_unsuccess, Toast.LENGTH_LONG).show();
loadingDialog.dismiss();
}
}
@Override
public void onFailure(Call call, Throwable t) {
Toast.makeText(application, R.string.network_error, Toast.LENGTH_LONG).show();
loadingDialog.dismiss();
}
});
}
private void upLoadPic() {
//筛选“未上传”的图片
List mediafiles = new ArrayList<>();
for (Mediafile m : this.mediafileListChange) {
if (m.getRemark().equals("未上传")) {
mediafiles.add(m);
}
}
PhotoUtil photoUtil = new PhotoUtil(mediafiles.size(), application, new OnUpLoadListener() {
@Override
public void onSuccess() {
uploadProblem();
}
@Override
public void onFail() {
Toast.makeText(application, "上传失败,请重试", Toast.LENGTH_SHORT).show();
loadingDialog.dismiss();
// AlertDialog.Builder dialog = new AlertDialog.Builder(application);
// dialog.setMessage("图片上传失败,请重试");
// dialog.setPositiveButton("确定", new DialogInterface.OnClickListener() {
// @Override
// public void onClick(DialogInterface dialogInterface, int i) {
// finish();
// }
// });
// dialog.show();
}
});
photoUtil.upLoadPhoto(mediafiles);
}
//
}