| | |
| | | package com.dayu.pipirrapp.activity; |
| | | |
| | | import android.animation.AnimatorSet; |
| | | import android.animation.ObjectAnimator; |
| | | import static com.dayu.pipirrapp.net.Constants.BASE_URL; |
| | | |
| | | import android.content.Context; |
| | | import android.content.Intent; |
| | | import android.net.Uri; |
| | | import android.os.Build; |
| | | import android.os.Bundle; |
| | | import android.provider.MediaStore; |
| | | import android.text.TextUtils; |
| | | import android.util.Log; |
| | | import android.view.LayoutInflater; |
| | | import android.view.View; |
| | | import android.view.animation.Animation; |
| | | import android.view.animation.AnimationUtils; |
| | | |
| | | import androidx.activity.result.ActivityResultLauncher; |
| | | import androidx.activity.result.contract.ActivityResultContracts; |
| | | import androidx.lifecycle.Observer; |
| | | import androidx.recyclerview.widget.GridLayoutManager; |
| | | import androidx.recyclerview.widget.RecyclerView; |
| | | import androidx.recyclerview.widget.SimpleItemAnimator; |
| | | |
| | | import com.dayu.pipirrapp.MyApplication; |
| | | import com.dayu.pipirrapp.adapter.AddPictureAdapter; |
| | | import com.dayu.pipirrapp.bean.db.LatLonBean; |
| | | import com.dayu.pipirrapp.bean.net.AddProcessingResult; |
| | | import com.dayu.pipirrapp.bean.net.InsectionResult; |
| | | import com.dayu.pipirrapp.bean.net.UplodFileState; |
| | | import com.dayu.pipirrapp.databinding.ActivityOrderDealBinding; |
| | | import com.dayu.pipirrapp.net.ApiManager; |
| | | import com.dayu.pipirrapp.net.BaseResponse; |
| | | import com.dayu.pipirrapp.net.subscribers.SubscriberListener; |
| | | import com.dayu.pipirrapp.net.upload.UploadFileListener; |
| | | import com.dayu.pipirrapp.service.MyLocationService; |
| | | import com.dayu.pipirrapp.tool.FullyGridLayoutManager; |
| | | import com.dayu.pipirrapp.tool.GlideEngine; |
| | | import com.dayu.pipirrapp.utils.CommonKeyName; |
| | | import com.dayu.pipirrapp.utils.ToastUtil; |
| | | import com.dayu.pipirrapp.view.TitleBar; |
| | | import com.jeremyliao.liveeventbus.LiveEventBus; |
| | | import com.luck.picture.lib.basic.PictureSelectionModel; |
| | | import com.luck.picture.lib.basic.PictureSelector; |
| | | import com.luck.picture.lib.config.PictureConfig; |
| | | import com.luck.picture.lib.config.PictureMimeType; |
| | | import com.luck.picture.lib.config.SelectMimeType; |
| | | import com.luck.picture.lib.config.SelectModeConfig; |
| | | import com.luck.picture.lib.decoration.GridSpacingItemDecoration; |
| | | import com.luck.picture.lib.engine.CompressFileEngine; |
| | | import com.luck.picture.lib.engine.ImageEngine; |
| | | import com.luck.picture.lib.entity.LocalMedia; |
| | | import com.luck.picture.lib.interfaces.OnGridItemSelectAnimListener; |
| | | import com.luck.picture.lib.interfaces.OnQueryFilterListener; |
| | | import com.luck.picture.lib.interfaces.OnSelectAnimListener; |
| | | import com.luck.picture.lib.entity.MediaExtraInfo; |
| | | import com.luck.picture.lib.interfaces.OnExternalPreviewEventListener; |
| | | import com.luck.picture.lib.interfaces.OnKeyValueResultCallbackListener; |
| | | import com.luck.picture.lib.utils.DateUtils; |
| | | import com.luck.picture.lib.utils.DensityUtil; |
| | | import com.luck.picture.lib.utils.MediaUtils; |
| | | import com.luck.picture.lib.utils.PictureFileUtils; |
| | | import com.tencent.bugly.crashreport.CrashReport; |
| | | |
| | | import java.io.File; |
| | | import java.util.ArrayList; |
| | | import java.util.HashSet; |
| | | import java.util.List; |
| | | import java.util.Set; |
| | | |
| | | import top.zibin.luban.CompressionPredicate; |
| | | import top.zibin.luban.Luban; |
| | | import top.zibin.luban.OnNewCompressListener; |
| | | import top.zibin.luban.OnRenameListener; |
| | | |
| | | /** |
| | | * OrderDealDetailActivity - |
| | |
| | | * @since 2024-11-27 |
| | | */ |
| | | public class OrderDealActivity extends BaseActivity { |
| | | |
| | | private String TAG = "OrderDealActivity"; |
| | | ActivityOrderDealBinding binding; |
| | | RecyclerView mRecyclerView; |
| | | AddPictureAdapter mAdapter; |
| | | int maxSelectNum = 10;//最大照片 |
| | | int maxSelectVideoNum = 0;//最大视频 |
| | | private final List<LocalMedia> mData = new ArrayList<>(); |
| | | private ActivityResultLauncher<Intent> launcherResult; |
| | | private ImageEngine imageEngine; |
| | | List<UplodFileState> uplodFileStates = new ArrayList<>(); |
| | | String workOrderId; |
| | | LatLonBean latLonBean; |
| | | |
| | | /** |
| | | * 定位监听 |
| | | */ |
| | | private Observer<Object> locationObserver = new Observer<Object>() { |
| | | @Override |
| | | public void onChanged(Object o) { |
| | | latLonBean = (LatLonBean) o; |
| | | } |
| | | }; |
| | | |
| | | @Override |
| | | protected void onCreate(Bundle savedInstanceState) { |
| | | super.onCreate(savedInstanceState); |
| | | binding = ActivityOrderDealBinding.inflate(LayoutInflater.from(this)); |
| | | setContentView(binding.getRoot()); |
| | | workOrderId = this.getIntent().getStringExtra("workOrderId"); |
| | | launcherResult = createActivityResultLauncher(); |
| | | initView(); |
| | | startLocation(); |
| | | } |
| | | |
| | | |
| | | void initView() { |
| | | new TitleBar(this).setTitleText("处理工单").setLeftIco().setLeftIcoListening(v -> OrderDealActivity.this.finish()); |
| | | mRecyclerView = binding.recycler; |
| | | FullyGridLayoutManager manager = new FullyGridLayoutManager(this, |
| | | 4, GridLayoutManager.VERTICAL, false); |
| | |
| | | mAdapter = new AddPictureAdapter(this, mData); |
| | | mAdapter.setSelectMax(maxSelectNum + maxSelectVideoNum); |
| | | mRecyclerView.setAdapter(mAdapter); |
| | | imageEngine = GlideEngine.createGlideEngine(); |
| | | mAdapter.setOnItemClickListener(new AddPictureAdapter.OnItemClickListener() { |
| | | @Override |
| | | public void onItemClick(View v, int position) { |
| | | |
| | | // 预览图片、视频、音频 |
| | | PictureSelector.create(OrderDealActivity.this) |
| | | .openPreview() |
| | | .setImageEngine(imageEngine) |
| | | .setExternalPreviewEventListener(new MyExternalPreviewEventListener()) |
| | | .startActivityPreview(position, true, mAdapter.getData()); |
| | | } |
| | | |
| | | @Override |
| | | public void openPicture() { |
| | | //添加图片 |
| | | mOpenPicture(); |
| | | } |
| | | }); |
| | | binding.dealButton.setOnClickListener(v -> { |
| | | boolean isAllPost = true; |
| | | for (UplodFileState imgData : uplodFileStates) { |
| | | if (imgData.getState() == 0) { |
| | | isAllPost = false; |
| | | } |
| | | } |
| | | |
| | | if (!TextUtils.isEmpty(binding.contentET.getText().toString())) { |
| | | if (isAllPost) { |
| | | if (uplodFileStates.size() > 0) { |
| | | postData(); |
| | | } else { |
| | | ToastUtil.showToast(OrderDealActivity.this, "请上传图片"); |
| | | } |
| | | } else { |
| | | ToastUtil.showToast(OrderDealActivity.this, "图片正在上传请稍后提交"); |
| | | } |
| | | } else { |
| | | ToastUtil.showToast(OrderDealActivity.this, "请输入反馈内容"); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 添加图片 |
| | | */ |
| | | private void mOpenPicture() { |
| | | // 进入相册 |
| | | PictureSelectionModel selectionModel = PictureSelector.create(this) |
| | | .openGallery(SelectMimeType.ofImage()) |
| | | .setMaxSelectNum(maxSelectNum) |
| | | .setMaxVideoSelectNum(maxSelectVideoNum) |
| | | .setImageEngine(GlideEngine.createGlideEngine()) |
| | | .setImageEngine(imageEngine) |
| | | //设置图片压缩 |
| | | .setCompressEngine(new ImageFileCompressEngine()) |
| | | .setSelectedData(mAdapter.getData()); |
| | | |
| | | selectionModel.forResult(PictureConfig.CHOOSE_REQUEST); |
| | | selectionModel.forResult(launcherResult); |
| | | } |
| | | |
| | | /** |
| | | * 创建一个ActivityResultLauncher |
| | | * |
| | | * @return |
| | | */ |
| | | private ActivityResultLauncher<Intent> createActivityResultLauncher() { |
| | | return registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), |
| | | result -> { |
| | | int resultCode = result.getResultCode(); |
| | | if (resultCode == RESULT_OK) { |
| | | ArrayList<LocalMedia> selectList = PictureSelector.obtainSelectorList(result.getData()); |
| | | analyticalSelectResults(selectList); |
| | | } else if (resultCode == RESULT_CANCELED) { |
| | | Log.i(TAG, "onActivityResult PictureSelector Cancel"); |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 外部预览监听事件 |
| | | */ |
| | | private class MyExternalPreviewEventListener implements OnExternalPreviewEventListener { |
| | | |
| | | @Override |
| | | public void onPreviewDelete(int position) { |
| | | mAdapter.remove(position); |
| | | mAdapter.notifyItemRemoved(position); |
| | | } |
| | | |
| | | @Override |
| | | public boolean onLongPressDownload(Context context, LocalMedia media) { |
| | | return false; |
| | | } |
| | | } |
| | | |
| | | /** |
| | | * 处理选择结果 |
| | | * |
| | | * @param result |
| | | */ |
| | | private void analyticalSelectResults(ArrayList<LocalMedia> result) { |
| | | Set<String> existingPaths = new HashSet<>(); |
| | | for (UplodFileState uplodData : uplodFileStates) { |
| | | existingPaths.add(uplodData.getFilePath()); |
| | | } |
| | | for (LocalMedia media : result) { |
| | | if (media.getWidth() == 0 || media.getHeight() == 0) { |
| | | if (PictureMimeType.isHasImage(media.getMimeType())) { |
| | | MediaExtraInfo imageExtraInfo = MediaUtils.getImageSize(this, media.getPath()); |
| | | media.setWidth(imageExtraInfo.getWidth()); |
| | | media.setHeight(imageExtraInfo.getHeight()); |
| | | } else if (PictureMimeType.isHasVideo(media.getMimeType())) { |
| | | MediaExtraInfo videoExtraInfo = MediaUtils.getVideoSize(this, media.getPath()); |
| | | media.setWidth(videoExtraInfo.getWidth()); |
| | | media.setHeight(videoExtraInfo.getHeight()); |
| | | } |
| | | } |
| | | Log.i(TAG, "文件名: " + media.getFileName()); |
| | | Log.i(TAG, "是否压缩:" + media.isCompressed()); |
| | | Log.i(TAG, "压缩:" + media.getCompressPath()); |
| | | Log.i(TAG, "初始路径:" + media.getPath()); |
| | | Log.i(TAG, "绝对路径:" + media.getRealPath()); |
| | | Log.i(TAG, "是否裁剪:" + media.isCut()); |
| | | Log.i(TAG, "裁剪路径:" + media.getCutPath()); |
| | | Log.i(TAG, "是否开启原图:" + media.isOriginal()); |
| | | Log.i(TAG, "原图路径:" + media.getOriginalPath()); |
| | | Log.i(TAG, "沙盒路径:" + media.getSandboxPath()); |
| | | Log.i(TAG, "水印路径:" + media.getWatermarkPath()); |
| | | Log.i(TAG, "视频缩略图:" + media.getVideoThumbnailPath()); |
| | | Log.i(TAG, "原始宽高: " + media.getWidth() + "x" + media.getHeight()); |
| | | Log.i(TAG, "裁剪宽高: " + media.getCropImageWidth() + "x" + media.getCropImageHeight()); |
| | | Log.i(TAG, "文件大小: " + PictureFileUtils.formatAccurateUnitFileSize(media.getSize())); |
| | | Log.i(TAG, "文件时长: " + media.getDuration()); |
| | | String compressPath = media.getCompressPath(); |
| | | //判断是否有这个路径,没有的话上传该图片 |
| | | if (!existingPaths.contains(compressPath)) { |
| | | UplodFileState uplodFileState = new UplodFileState(); |
| | | uplodFileState.setFilePath(media.getRealPath()); |
| | | uplodFileStates.add(uplodFileState); |
| | | // 执行上传图片的操作 |
| | | uplodeImg(uplodFileState); |
| | | } |
| | | |
| | | |
| | | } |
| | | runOnUiThread(new Runnable() { |
| | | @Override |
| | | public void run() { |
| | | boolean isMaxSize = result.size() == mAdapter.getSelectMax(); |
| | | int oldSize = mAdapter.getData().size(); |
| | | mAdapter.notifyItemRangeRemoved(0, isMaxSize ? oldSize + 1 : oldSize); |
| | | mAdapter.getData().clear(); |
| | | |
| | | mAdapter.getData().addAll(result); |
| | | mAdapter.notifyItemRangeInserted(0, result.size()); |
| | | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | |
| | | /** |
| | | * 自定义图片压缩 |
| | | */ |
| | | private static class ImageFileCompressEngine implements CompressFileEngine { |
| | | |
| | | @Override |
| | | public void onStartCompress(Context context, ArrayList<Uri> source, OnKeyValueResultCallbackListener call) { |
| | | Luban.with(context).load(source).ignoreBy(100).setRenameListener(new OnRenameListener() { |
| | | @Override |
| | | public String rename(String filePath) { |
| | | int indexOf = filePath.lastIndexOf("."); |
| | | String postfix = indexOf != -1 ? filePath.substring(indexOf) : ".jpg"; |
| | | return DateUtils.getCreateFileName("CMP_") + postfix; |
| | | } |
| | | }).filter(new CompressionPredicate() { |
| | | @Override |
| | | public boolean apply(String path) { |
| | | if (PictureMimeType.isUrlHasImage(path) && !PictureMimeType.isHasHttp(path)) { |
| | | return true; |
| | | } |
| | | return !PictureMimeType.isUrlHasGif(path); |
| | | } |
| | | }).setCompressListener(new OnNewCompressListener() { |
| | | @Override |
| | | public void onStart() { |
| | | |
| | | } |
| | | |
| | | @Override |
| | | public void onSuccess(String source, File compressFile) { |
| | | if (call != null) { |
| | | call.onCallback(source, compressFile.getAbsolutePath()); |
| | | } |
| | | } |
| | | |
| | | @Override |
| | | public void onError(String source, Throwable e) { |
| | | if (call != null) { |
| | | call.onCallback(source, null); |
| | | } |
| | | } |
| | | }).launch(); |
| | | } |
| | | } |
| | | |
| | | |
| | | //上传图片 |
| | | private void uplodeImg(UplodFileState uplodFileState) { |
| | | ApiManager.getInstance().uploadFile(this, uplodFileState, new UploadFileListener() { |
| | | @Override |
| | | public void onBack(UplodFileState state) { |
| | | for (UplodFileState uplodFile : uplodFileStates) { |
| | | if (uplodFile.getFilePath().equals(state.getFilePath())) { |
| | | int index = uplodFileStates.indexOf(uplodFile); |
| | | if (index != -1) { |
| | | uplodFileStates.set(index, uplodFile); // 更新对应的项 |
| | | } |
| | | } |
| | | |
| | | } |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 上报处理结果 |
| | | */ |
| | | private void postData() { |
| | | AddProcessingResult result = new AddProcessingResult(); |
| | | result.setImages(uplodFileStates); |
| | | result.setContent(binding.contentET.getText().toString()); |
| | | result.setInspectorId(MyApplication.myApplication.userId); |
| | | result.setWorkOrderId(workOrderId); |
| | | result.setCompleteTime(com.dayu.pipirrapp.utils.DateUtils.getNowDateToMMStr()); |
| | | if (latLonBean != null) { |
| | | result.setLat(String.valueOf(latLonBean.getLatitude())); |
| | | result.setLng(String.valueOf(latLonBean.getLongitude())); |
| | | } |
| | | ApiManager.getInstance().requestPostHideLoading(this, BASE_URL + "/app/workOrder/addProcessingResult", InsectionResult.class, result.toMap(result), new SubscriberListener<BaseResponse<List<InsectionResult>>>() { |
| | | @Override |
| | | public void onNext(BaseResponse<List<InsectionResult>> t) { |
| | | try { |
| | | if (t.isSuccess()) { |
| | | ToastUtil.showToastLong(OrderDealActivity.this, "上报成功"); |
| | | OrderDealActivity.this.finish(); |
| | | } else { |
| | | |
| | | } |
| | | } catch (Exception e) { |
| | | e.printStackTrace(); |
| | | CrashReport.postCatchedException(e); |
| | | } |
| | | |
| | | } |
| | | }); |
| | | } |
| | | |
| | | /** |
| | | * 开始定位相关逻辑 |
| | | */ |
| | | private void startLocation() { |
| | | Intent location = new Intent(this, MyLocationService.class); |
| | | location.putExtra("isSingle", true); |
| | | //开启定位 |
| | | if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { |
| | | startForegroundService(location); |
| | | } else { |
| | | startService(location); |
| | | } |
| | | //获取定位服务传过来的坐标点 |
| | | LiveEventBus.get(CommonKeyName.locationData).observeForever(locationObserver); |
| | | } |
| | | |
| | | |
| | | @Override |
| | | protected void onDestroy() { |
| | | super.onDestroy(); |
| | | LiveEventBus.get(CommonKeyName.locationData).removeObserver(locationObserver); |
| | | } |
| | | } |