Compare View
Commits (2)
Showing
3 changed files
Show diff stats
app/src/main/java/com/hjx/parent/ImageActivity.java
| 1 | package com.hjx.parent; | 1 | package com.hjx.parent; |
| 2 | 2 | ||
| 3 | import android.annotation.SuppressLint; | 3 | import android.annotation.SuppressLint; |
| 4 | import android.app.Dialog; | 4 | import android.app.Dialog; |
| 5 | import android.content.Intent; | 5 | import android.content.Intent; |
| 6 | import android.graphics.Bitmap; | 6 | import android.graphics.Bitmap; |
| 7 | import android.graphics.BitmapFactory; | 7 | import android.graphics.BitmapFactory; |
| 8 | import android.graphics.Rect; | 8 | import android.graphics.Rect; |
| 9 | import android.net.Uri; | 9 | import android.net.Uri; |
| 10 | import android.os.Bundle; | 10 | import android.os.Bundle; |
| 11 | import android.os.Handler; | 11 | import android.os.Handler; |
| 12 | import android.provider.MediaStore; | 12 | import android.provider.MediaStore; |
| 13 | import android.text.TextUtils; | 13 | import android.text.TextUtils; |
| 14 | import android.util.Log; | 14 | import android.util.Log; |
| 15 | import android.view.KeyEvent; | 15 | import android.view.KeyEvent; |
| 16 | import android.view.View; | 16 | import android.view.View; |
| 17 | 17 | ||
| 18 | 18 | ||
| 19 | import androidx.annotation.Nullable; | 19 | import androidx.annotation.Nullable; |
| 20 | import androidx.core.content.FileProvider; | 20 | import androidx.core.content.FileProvider; |
| 21 | import androidx.viewpager2.widget.ViewPager2; | 21 | import androidx.viewpager2.widget.ViewPager2; |
| 22 | 22 | ||
| 23 | import com.google.gson.Gson; | 23 | import com.google.gson.Gson; |
| 24 | import com.google.gson.JsonObject; | 24 | import com.google.gson.JsonObject; |
| 25 | import com.hjx.parent.adapter.ImageAdapter; | 25 | import com.hjx.parent.adapter.ImageAdapter; |
| 26 | import com.hjx.parent.databinding.ActivityImageBinding; | 26 | import com.hjx.parent.databinding.ActivityImageBinding; |
| 27 | import com.hjx.parent.dialog.AddHomeworkDialog; | 27 | import com.hjx.parent.dialog.AddHomeworkDialog; |
| 28 | import com.hjx.parent.dialog.ErrorInputDialog; | 28 | import com.hjx.parent.dialog.ErrorInputDialog; |
| 29 | import com.hjx.parent.dialog.PhotoViewDialog; | 29 | import com.hjx.parent.dialog.PhotoViewDialog; |
| 30 | import com.hjx.parent.dialog.TipDialog; | 30 | import com.hjx.parent.dialog.TipDialog; |
| 31 | import com.hjx.parent.rx.BaseRxActivity; | 31 | import com.hjx.parent.rx.BaseRxActivity; |
| 32 | import com.prws.common.bean.CutPicBean; | 32 | import com.prws.common.bean.CutPicBean; |
| 33 | import com.prws.common.bean.ImageBean; | 33 | import com.prws.common.bean.ImageBean; |
| 34 | import com.prws.common.bean.TopicBean; | 34 | import com.prws.common.bean.TopicBean; |
| 35 | import com.prws.common.net.NetWorks; | 35 | import com.prws.common.net.NetWorks; |
| 36 | import com.prws.common.utils.BitmapUtils; | 36 | import com.prws.common.utils.BitmapUtils; |
| 37 | import com.prws.common.utils.CommonUtil; | 37 | import com.prws.common.utils.CommonUtil; |
| 38 | import com.xiasuhuei321.loadingdialog.view.LoadingDialog; | 38 | import com.xiasuhuei321.loadingdialog.view.LoadingDialog; |
| 39 | 39 | ||
| 40 | import java.io.File; | 40 | import java.io.File; |
| 41 | import java.util.ArrayList; | 41 | import java.util.ArrayList; |
| 42 | import java.util.List; | 42 | import java.util.List; |
| 43 | 43 | ||
| 44 | import io.reactivex.Observer; | 44 | import io.reactivex.Observer; |
| 45 | import io.reactivex.disposables.Disposable; | 45 | import io.reactivex.disposables.Disposable; |
| 46 | import retrofit2.Call; | 46 | import retrofit2.Call; |
| 47 | import retrofit2.Callback; | 47 | import retrofit2.Callback; |
| 48 | import retrofit2.Response; | 48 | import retrofit2.Response; |
| 49 | 49 | ||
| 50 | public class ImageActivity extends BaseRxActivity<ActivityImageBinding> implements View.OnClickListener { | 50 | public class ImageActivity extends BaseRxActivity<ActivityImageBinding> implements View.OnClickListener { |
| 51 | 51 | ||
| 52 | private List<String> images = new ArrayList<>(); | 52 | private List<String> images = new ArrayList<>(); |
| 53 | private ImageAdapter imageAdapter; | 53 | private ImageAdapter imageAdapter; |
| 54 | private List<ImageBean> list = new ArrayList<>(); | 54 | private List<ImageBean> list = new ArrayList<>(); |
| 55 | private String filePath; | 55 | private String filePath; |
| 56 | 56 | ||
| 57 | private int type = 0; | 57 | private int type = 0; |
| 58 | private AddHomeworkDialog addHomeworkDialog; | 58 | private AddHomeworkDialog addHomeworkDialog; |
| 59 | 59 | ||
| 60 | @Override | 60 | @Override |
| 61 | public void initView(Bundle savedInstanceState) { | 61 | public void initView(Bundle savedInstanceState) { |
| 62 | type = getIntent().getIntExtra("type", 0); | 62 | type = getIntent().getIntExtra("type", 0); |
| 63 | binding.tvSave.setText(type == 0 ? "保存错题" : "录入题目"); | 63 | binding.tvSave.setText(type == 0 ? "保存错题" : "录入题目"); |
| 64 | 64 | ||
| 65 | images = getIntent().getStringArrayListExtra("images"); | 65 | images = getIntent().getStringArrayListExtra("images"); |
| 66 | if (images == null || images.size() <= 1) { | 66 | if (images == null || images.size() <= 1) { |
| 67 | binding.rlPrevious.setVisibility(View.GONE); | 67 | binding.rlPrevious.setVisibility(View.GONE); |
| 68 | binding.rlNext.setVisibility(View.GONE); | 68 | binding.rlNext.setVisibility(View.GONE); |
| 69 | } | 69 | } |
| 70 | for (String s : images) { | 70 | for (String s : images) { |
| 71 | String filepath = context.getExternalFilesDir("images").getPath() + "/" + System.currentTimeMillis() + CommonUtil.getStr() + ".jpg"; | 71 | String filepath = context.getExternalFilesDir("images").getPath() + "/" + System.currentTimeMillis() + CommonUtil.getStr() + ".jpg"; |
| 72 | ImageBean imageBean = new ImageBean(); | 72 | ImageBean imageBean = new ImageBean(); |
| 73 | Bitmap bitmap = BitmapUtils.getimage(s); | 73 | Bitmap bitmap = BitmapUtils.getimage(s); |
| 74 | CommonUtil.saveBitmapToUri(bitmap, filepath); | 74 | CommonUtil.saveBitmapToUri(bitmap, filepath); |
| 75 | imageBean.setPath(filepath); | 75 | imageBean.setPath(filepath); |
| 76 | imageBean.source = filepath; | 76 | imageBean.source = filepath; |
| 77 | list.add(imageBean); | 77 | list.add(imageBean); |
| 78 | } | 78 | } |
| 79 | imageAdapter = new ImageAdapter(this, list); | 79 | imageAdapter = new ImageAdapter(this, list); |
| 80 | binding.viewPager.setAdapter(imageAdapter); | 80 | binding.viewPager.setAdapter(imageAdapter); |
| 81 | binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { | 81 | binding.viewPager.registerOnPageChangeCallback(new ViewPager2.OnPageChangeCallback() { |
| 82 | @Override | 82 | @Override |
| 83 | public void onPageSelected(int position) { | 83 | public void onPageSelected(int position) { |
| 84 | if (list.get(position).getCuts() == null) { | 84 | if (list.get(position).getCuts() == null) { |
| 85 | // binding.ivPrevious.setEnabled(false); | 85 | // binding.ivPrevious.setEnabled(false); |
| 86 | // binding.ivNext.setEnabled(false); | 86 | // binding.ivNext.setEnabled(false); |
| 87 | clearText(); | 87 | clearText(); |
| 88 | } | 88 | } |
| 89 | binding.tvTitle.setText((binding.viewPager.getCurrentItem() + 1) + "/" + list.size()); | 89 | binding.tvTitle.setText((binding.viewPager.getCurrentItem() + 1) + "/" + list.size()); |
| 90 | if (position == 0) { | 90 | if (position == 0) { |
| 91 | binding.ivPrevious.setImageResource(R.mipmap.ic_previous_gray); | 91 | binding.ivPrevious.setImageResource(R.mipmap.ic_previous_gray); |
| 92 | // binding.ivPrevious.setEnabled(true); | 92 | // binding.ivPrevious.setEnabled(true); |
| 93 | } else { | 93 | } else { |
| 94 | binding.ivPrevious.setImageResource(R.mipmap.ic_previous); | 94 | binding.ivPrevious.setImageResource(R.mipmap.ic_previous); |
| 95 | // binding.ivPrevious.setEnabled(false); | 95 | // binding.ivPrevious.setEnabled(false); |
| 96 | } | 96 | } |
| 97 | if (position == list.size() - 1) { | 97 | if (position == list.size() - 1) { |
| 98 | binding.ivNext.setImageResource(R.mipmap.ic_next_gray); | 98 | binding.ivNext.setImageResource(R.mipmap.ic_next_gray); |
| 99 | // binding.ivNext.setEnabled(true); | 99 | // binding.ivNext.setEnabled(true); |
| 100 | } else { | 100 | } else { |
| 101 | binding.ivNext.setImageResource(R.mipmap.ic_next); | 101 | binding.ivNext.setImageResource(R.mipmap.ic_next); |
| 102 | // binding.ivNext.setEnabled(false); | 102 | // binding.ivNext.setEnabled(false); |
| 103 | } | 103 | } |
| 104 | super.onPageSelected(position); | 104 | super.onPageSelected(position); |
| 105 | } | 105 | } |
| 106 | }); | 106 | }); |
| 107 | binding.rlNext.setOnClickListener(this); | 107 | binding.rlNext.setOnClickListener(this); |
| 108 | binding.rlPrevious.setOnClickListener(this); | 108 | binding.rlPrevious.setOnClickListener(this); |
| 109 | binding.ivBack.setOnClickListener(this); | 109 | binding.ivBack.setOnClickListener(this); |
| 110 | binding.tvPhoto.setOnClickListener(this); | 110 | binding.tvPhoto.setOnClickListener(this); |
| 111 | binding.tvAdd.setOnClickListener(this); | 111 | binding.tvAdd.setOnClickListener(this); |
| 112 | binding.llEdit.setOnClickListener(this); | 112 | binding.llEdit.setOnClickListener(this); |
| 113 | binding.tvSave.setOnClickListener(this); | 113 | binding.tvSave.setOnClickListener(this); |
| 114 | binding.btnShowSource.setOnClickListener(v -> { | 114 | binding.btnShowSource.setOnClickListener(v -> { |
| 115 | if (list.isEmpty()) return; | 115 | if (list.isEmpty()) return; |
| 116 | ImageBean data = list.get(binding.viewPager.getCurrentItem()); | 116 | ImageBean data = list.get(binding.viewPager.getCurrentItem()); |
| 117 | new PhotoViewDialog(this).show(data.source); | 117 | new PhotoViewDialog(this).show(data.source); |
| 118 | }); | 118 | }); |
| 119 | } | 119 | } |
| 120 | 120 | ||
| 121 | public void clearText() { | 121 | public void clearText() { |
| 122 | int index = binding.viewPager.getCurrentItem(); | 122 | int index = binding.viewPager.getCurrentItem(); |
| 123 | showLoadingDialog(getString(R.string.loading_text), new LoadingDialog.DismissListener() { | 123 | showLoadingDialog(getString(R.string.loading_text), new LoadingDialog.DismissListener() { |
| 124 | @Override | 124 | @Override |
| 125 | public void dimiss() { | 125 | public void dimiss() { |
| 126 | // binding.ivPrevious.setEnabled(index != 0); | 126 | // binding.ivPrevious.setEnabled(index != 0); |
| 127 | // binding.ivNext.setEnabled(index != list.size() - 1); | 127 | // binding.ivNext.setEnabled(index != list.size() - 1); |
| 128 | } | 128 | } |
| 129 | }); | 129 | }); |
| 130 | Log.i("clearText", list.get(index).getPath()); | 130 | Log.i("clearText", list.get(index).getPath()); |
| 131 | String base64 = BitmapUtils.imageToBase64(list.get(index).getPath()); | 131 | String base64 = BitmapUtils.imageToBase64(list.get(index).getPath()); |
| 132 | NetWorks.removeWriting(base64, new Observer<JsonObject>() { | 132 | NetWorks.removeWriting(base64, new Observer<JsonObject>() { |
| 133 | @Override | 133 | @Override |
| 134 | public void onSubscribe(Disposable d) { | 134 | public void onSubscribe(Disposable d) { |
| 135 | 135 | ||
| 136 | } | 136 | } |
| 137 | 137 | ||
| 138 | @Override | 138 | @Override |
| 139 | public void onNext(JsonObject jsonObject) { | 139 | public void onNext(JsonObject jsonObject) { |
| 140 | if (jsonObject != null && jsonObject.get("image_processed") != null) { | 140 | if (jsonObject != null && jsonObject.get("image_processed") != null) { |
| 141 | String image = jsonObject.get("image_processed").getAsString(); | 141 | String image = jsonObject.get("image_processed").getAsString(); |
| 142 | if (image != null) { | 142 | if (image != null) { |
| 143 | String filepath = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; | 143 | String filepath = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; |
| 144 | CommonUtil.saveBitmapToUri(BitmapUtils.stringToBitmap(image), filepath); | 144 | CommonUtil.saveBitmapToUri(BitmapUtils.stringToBitmap(image), filepath); |
| 145 | list.get(index).setPath(filepath); | 145 | list.get(index).setPath(filepath); |
| 146 | if (list.get(index).getCuts() == null) { | 146 | if (list.get(index).getCuts() == null) { |
| 147 | list.get(index).setCuts(new ArrayList<>()); | 147 | list.get(index).setCuts(new ArrayList<>()); |
| 148 | } | 148 | } |
| 149 | imageAdapter.notifyItemChanged(index); | 149 | imageAdapter.notifyItemChanged(index); |
| 150 | cut(filepath); | 150 | cut(filepath); |
| 151 | } else { | 151 | } else { |
| 152 | loadFail("去除字迹失败"); | 152 | loadFail("去除字迹失败"); |
| 153 | } | 153 | } |
| 154 | } else { | 154 | } else { |
| 155 | loadFail("去除字迹失败"); | 155 | loadFail("去除字迹失败"); |
| 156 | } | 156 | } |
| 157 | } | 157 | } |
| 158 | 158 | ||
| 159 | @Override | 159 | @Override |
| 160 | public void onError(Throwable e) { | 160 | public void onError(Throwable e) { |
| 161 | e.printStackTrace(); | ||
| 161 | loadFail("去除字迹失败"); | 162 | loadFail("去除字迹失败"); |
| 162 | } | 163 | } |
| 163 | 164 | ||
| 164 | @Override | 165 | @Override |
| 165 | public void onComplete() { | 166 | public void onComplete() { |
| 166 | 167 | ||
| 167 | } | 168 | } |
| 168 | }); | 169 | }); |
| 169 | } | 170 | } |
| 170 | 171 | ||
| 171 | public void cut(String path) { | 172 | public void cut(String path) { |
| 172 | String base64 = BitmapUtils.imageToBase64(path); | 173 | String base64 = BitmapUtils.imageToBase64(path); |
| 173 | Log.i("figLoaction", path); | 174 | Log.i("figLoaction", path); |
| 174 | NetWorks.cut(base64, new Observer<CutPicBean>() { | 175 | NetWorks.cut(base64, new Observer<CutPicBean>() { |
| 175 | @Override | 176 | @Override |
| 176 | public void onSubscribe(Disposable d) { | 177 | public void onSubscribe(Disposable d) { |
| 177 | 178 | ||
| 178 | } | 179 | } |
| 179 | 180 | ||
| 180 | @Override | 181 | @Override |
| 181 | public void onNext(CutPicBean cutPicBean) { | 182 | public void onNext(CutPicBean cutPicBean) { |
| 182 | if (cutPicBean != null) { | 183 | if (cutPicBean != null) { |
| 183 | if (cutPicBean.getDirection() != 0) { | 184 | if (cutPicBean.getDirection() != 0) { |
| 184 | rotatingImage(cutPicBean); | 185 | rotatingImage(cutPicBean); |
| 185 | } else { | 186 | } else { |
| 186 | if (cutPicBean.getQus_figure() != null && cutPicBean.getQus_figure().size() > 0) { | 187 | if (cutPicBean.getQus_figure() != null && cutPicBean.getQus_figure().size() > 0) { |
| 187 | List<CutPicBean.FigLoaction> figLoactions = new ArrayList<>(); | 188 | List<CutPicBean.FigLoaction> figLoactions = new ArrayList<>(); |
| 188 | for (CutPicBean.CutLocation cutLocation : cutPicBean.getQus_figure()) { | 189 | for (CutPicBean.CutLocation cutLocation : cutPicBean.getQus_figure()) { |
| 189 | figLoactions.add(cutLocation.getFig_location()); | 190 | figLoactions.add(cutLocation.getFig_location()); |
| 190 | } | 191 | } |
| 191 | new Handler().postDelayed(new Runnable() { | 192 | new Handler().postDelayed(new Runnable() { |
| 192 | @Override | 193 | @Override |
| 193 | public void run() { | 194 | public void run() { |
| 194 | getRects(figLoactions); | 195 | getRects(figLoactions); |
| 195 | } | 196 | } |
| 196 | }, 1000); | 197 | }, 1000); |
| 197 | } else { | 198 | } else { |
| 198 | cancelLoadingDialog(); | 199 | cancelLoadingDialog(); |
| 199 | } | 200 | } |
| 200 | } | 201 | } |
| 201 | } else { | 202 | } else { |
| 202 | loadFail("自动切题失败"); | 203 | loadFail("自动切题失败"); |
| 203 | } | 204 | } |
| 204 | } | 205 | } |
| 205 | 206 | ||
| 206 | @Override | 207 | @Override |
| 207 | public void onError(Throwable e) { | 208 | public void onError(Throwable e) { |
| 208 | loadFail("自动切题失败"); | 209 | loadFail("自动切题失败"); |
| 209 | } | 210 | } |
| 210 | 211 | ||
| 211 | @Override | 212 | @Override |
| 212 | public void onComplete() { | 213 | public void onComplete() { |
| 213 | 214 | ||
| 214 | } | 215 | } |
| 215 | }); | 216 | }); |
| 216 | } | 217 | } |
| 217 | 218 | ||
| 218 | private void getRects(List<CutPicBean.FigLoaction> figLoactions) { | 219 | private void getRects(List<CutPicBean.FigLoaction> figLoactions) { |
| 219 | Log.i("figLoaction", new Gson().toJson(figLoactions)); | 220 | Log.i("figLoaction", new Gson().toJson(figLoactions)); |
| 220 | int index = binding.viewPager.getCurrentItem(); | 221 | int index = binding.viewPager.getCurrentItem(); |
| 221 | List<ImageBean.Cut> cuts = new ArrayList<>(); | 222 | List<ImageBean.Cut> cuts = new ArrayList<>(); |
| 222 | for (CutPicBean.FigLoaction figLoaction : figLoactions) { | 223 | for (CutPicBean.FigLoaction figLoaction : figLoactions) { |
| 223 | ImageBean.Cut cut = new ImageBean.Cut(); | 224 | ImageBean.Cut cut = new ImageBean.Cut(); |
| 224 | cut.setRect(figLoaction); | 225 | cut.setRect(figLoaction); |
| 225 | cut.setSelect(false); | 226 | cut.setSelect(false); |
| 226 | cuts.add(cut); | 227 | cuts.add(cut); |
| 227 | } | 228 | } |
| 228 | list.get(index).setCuts(cuts); | 229 | list.get(index).setCuts(cuts); |
| 229 | imageAdapter.notifyItemChanged(index); | 230 | imageAdapter.notifyItemChanged(index); |
| 230 | cancelLoadingDialog(); | 231 | cancelLoadingDialog(); |
| 231 | } | 232 | } |
| 232 | 233 | ||
| 233 | private void rotatingImage(CutPicBean cutPicBean) { | 234 | private void rotatingImage(CutPicBean cutPicBean) { |
| 234 | int index = binding.viewPager.getCurrentItem(); | 235 | int index = binding.viewPager.getCurrentItem(); |
| 235 | Bitmap bitmap = BitmapFactory.decodeFile(list.get(index).getPath()); | 236 | Bitmap bitmap = BitmapFactory.decodeFile(list.get(index).getPath()); |
| 236 | int angle = 0; | 237 | int angle = 0; |
| 237 | switch (cutPicBean.getDirection()) { | 238 | switch (cutPicBean.getDirection()) { |
| 238 | case 1: | 239 | case 1: |
| 239 | angle = 90; | 240 | angle = 90; |
| 240 | break; | 241 | break; |
| 241 | case 2: | 242 | case 2: |
| 242 | angle = 180; | 243 | angle = 180; |
| 243 | break; | 244 | break; |
| 244 | case 3: | 245 | case 3: |
| 245 | angle = 270; | 246 | angle = 270; |
| 246 | break; | 247 | break; |
| 247 | } | 248 | } |
| 248 | if (cutPicBean.getQus_figure() != null && cutPicBean.getQus_figure().size() > 0) { | 249 | if (cutPicBean.getQus_figure() != null && cutPicBean.getQus_figure().size() > 0) { |
| 249 | List<CutPicBean.FigLoaction> figLoactions = new ArrayList<>(); | 250 | List<CutPicBean.FigLoaction> figLoactions = new ArrayList<>(); |
| 250 | for (CutPicBean.CutLocation cutLocation : cutPicBean.getQus_figure()) { | 251 | for (CutPicBean.CutLocation cutLocation : cutPicBean.getQus_figure()) { |
| 251 | figLoactions.add(cutLocation.getFig_location()); | 252 | figLoactions.add(cutLocation.getFig_location()); |
| 252 | } | 253 | } |
| 253 | figLoactions = CommonUtil.rotaingLocation(angle, figLoactions, bitmap.getWidth(), bitmap.getHeight()); | 254 | figLoactions = CommonUtil.rotaingLocation(angle, figLoactions, bitmap.getWidth(), bitmap.getHeight()); |
| 254 | bitmap = CommonUtil.rotaingImageView(angle, bitmap); | 255 | bitmap = CommonUtil.rotaingImageView(angle, bitmap); |
| 255 | String filepath = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; | 256 | String filepath = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; |
| 256 | CommonUtil.saveBitmapToUri(bitmap, filepath); | 257 | CommonUtil.saveBitmapToUri(bitmap, filepath); |
| 257 | list.get(index).setPath(filepath); | 258 | list.get(index).setPath(filepath); |
| 258 | imageAdapter.notifyItemChanged(index); | 259 | imageAdapter.notifyItemChanged(index); |
| 259 | List<CutPicBean.FigLoaction> finalFigLoactions = figLoactions; | 260 | List<CutPicBean.FigLoaction> finalFigLoactions = figLoactions; |
| 260 | new Handler().postDelayed(new Runnable() { | 261 | new Handler().postDelayed(new Runnable() { |
| 261 | @Override | 262 | @Override |
| 262 | public void run() { | 263 | public void run() { |
| 263 | getRects(finalFigLoactions); | 264 | getRects(finalFigLoactions); |
| 264 | } | 265 | } |
| 265 | }, 500); | 266 | }, 500); |
| 266 | } else { | 267 | } else { |
| 267 | cancelLoadingDialog(); | 268 | cancelLoadingDialog(); |
| 268 | } | 269 | } |
| 269 | } | 270 | } |
| 270 | 271 | ||
| 271 | @Override | 272 | @Override |
| 272 | protected ActivityImageBinding getViewBinding() { | 273 | protected ActivityImageBinding getViewBinding() { |
| 273 | return ActivityImageBinding.inflate(getLayoutInflater()); | 274 | return ActivityImageBinding.inflate(getLayoutInflater()); |
| 274 | } | 275 | } |
| 275 | 276 | ||
| 276 | @SuppressLint("SetTextI18n") | 277 | @SuppressLint("SetTextI18n") |
| 277 | public void refreshNum() { | 278 | public void refreshNum() { |
| 278 | int num = 0; | 279 | int num = 0; |
| 279 | for (ImageBean imageBean : list) { | 280 | for (ImageBean imageBean : list) { |
| 280 | if (imageBean.getCuts() != null && imageBean.getCuts().size() > 0) { | 281 | if (imageBean.getCuts() != null && imageBean.getCuts().size() > 0) { |
| 281 | for (ImageBean.Cut cut : imageBean.getCuts()) { | 282 | for (ImageBean.Cut cut : imageBean.getCuts()) { |
| 282 | if (cut.isSelect()) { | 283 | if (cut.isSelect()) { |
| 283 | num++; | 284 | num++; |
| 284 | } | 285 | } |
| 285 | } | 286 | } |
| 286 | } | 287 | } |
| 287 | } | 288 | } |
| 288 | String btnText = type == 0 ? "保存错题" : "录入题目"; | 289 | String btnText = type == 0 ? "保存错题" : "录入题目"; |
| 289 | if (num > 0) { | 290 | if (num > 0) { |
| 290 | binding.tvSave.setEnabled(true); | 291 | binding.tvSave.setEnabled(true); |
| 291 | binding.tvSave.setBackgroundResource(R.drawable.bg_soild_blue_5); | 292 | binding.tvSave.setBackgroundResource(R.drawable.bg_soild_blue_5); |
| 292 | binding.tvSave.setText(btnText + "(" + num + ")"); | 293 | binding.tvSave.setText(btnText + "(" + num + ")"); |
| 293 | } else { | 294 | } else { |
| 294 | binding.tvSave.setEnabled(false); | 295 | binding.tvSave.setEnabled(false); |
| 295 | binding.tvSave.setBackgroundResource(R.drawable.bg_soild_blue_light_5); | 296 | binding.tvSave.setBackgroundResource(R.drawable.bg_soild_blue_light_5); |
| 296 | binding.tvSave.setText(btnText); | 297 | binding.tvSave.setText(btnText); |
| 297 | } | 298 | } |
| 298 | } | 299 | } |
| 299 | 300 | ||
| 300 | @Override | 301 | @Override |
| 301 | public void onClick(View v) { | 302 | public void onClick(View v) { |
| 302 | switch (v.getId()) { | 303 | switch (v.getId()) { |
| 303 | case R.id.iv_back: | 304 | case R.id.iv_back: |
| 304 | back(); | 305 | back(); |
| 305 | break; | 306 | break; |
| 306 | case R.id.rl_next: | 307 | case R.id.rl_next: |
| 307 | if (binding.viewPager.getCurrentItem() + 1 < list.size()) { | 308 | if (binding.viewPager.getCurrentItem() + 1 < list.size()) { |
| 308 | binding.viewPager.setCurrentItem(binding.viewPager.getCurrentItem() + 1); | 309 | binding.viewPager.setCurrentItem(binding.viewPager.getCurrentItem() + 1); |
| 309 | } | 310 | } |
| 310 | break; | 311 | break; |
| 311 | case R.id.rl_previous: | 312 | case R.id.rl_previous: |
| 312 | if (binding.viewPager.getCurrentItem() != 0) { | 313 | if (binding.viewPager.getCurrentItem() != 0) { |
| 313 | binding.viewPager.setCurrentItem(binding.viewPager.getCurrentItem() - 1); | 314 | binding.viewPager.setCurrentItem(binding.viewPager.getCurrentItem() - 1); |
| 314 | } | 315 | } |
| 315 | break; | 316 | break; |
| 316 | case R.id.ll_edit: | 317 | case R.id.ll_edit: |
| 317 | Intent intent = new Intent(context, ImageEditActivity.class); | 318 | Intent intent = new Intent(context, ImageEditActivity.class); |
| 318 | intent.putExtra("path", list.get(binding.viewPager.getCurrentItem()).getPath()); | 319 | intent.putExtra("path", list.get(binding.viewPager.getCurrentItem()).getPath()); |
| 319 | startActivityForResult(intent, 1001); | 320 | startActivityForResult(intent, 1001); |
| 320 | break; | 321 | break; |
| 321 | case R.id.tv_photo: | 322 | case R.id.tv_photo: |
| 322 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | 323 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 323 | //确保有相机来处理Intent | 324 | //确保有相机来处理Intent |
| 324 | filePath = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; | 325 | filePath = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; |
| 325 | File photoFile = new File(filePath); | 326 | File photoFile = new File(filePath); |
| 326 | if (photoFile != null) { | 327 | if (photoFile != null) { |
| 327 | //适配Android 7.0文件权限,通过FileProvider创建一个content类型的Uri | 328 | //适配Android 7.0文件权限,通过FileProvider创建一个content类型的Uri |
| 328 | Uri photoUri = FileProvider.getUriForFile(activity, "com.hjx.parent.fileprovider", photoFile); | 329 | Uri photoUri = FileProvider.getUriForFile(activity, "com.hjx.parent.fileprovider", photoFile); |
| 329 | takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); | 330 | takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 330 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); | 331 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); |
| 331 | activity.startActivityForResult(takePictureIntent, 1002); | 332 | activity.startActivityForResult(takePictureIntent, 1002); |
| 332 | } | 333 | } |
| 333 | break; | 334 | break; |
| 334 | case R.id.tv_add: | 335 | case R.id.tv_add: |
| 335 | Intent intent1 = new Intent(context, ImageSelectActivity.class); | 336 | Intent intent1 = new Intent(context, ImageSelectActivity.class); |
| 336 | intent1.putExtra("path", list.get(binding.viewPager.getCurrentItem()).getPath()); | 337 | intent1.putExtra("path", list.get(binding.viewPager.getCurrentItem()).getPath()); |
| 337 | intent1.putExtra("source", list.get(binding.viewPager.getCurrentItem()).source); | 338 | intent1.putExtra("source", list.get(binding.viewPager.getCurrentItem()).source); |
| 338 | startActivityForResult(intent1, 1001); | 339 | startActivityForResult(intent1, 1001); |
| 339 | break; | 340 | break; |
| 340 | case R.id.tv_save: | 341 | case R.id.tv_save: |
| 341 | List<TopicBean> topicBeans = new ArrayList<>(); | 342 | List<TopicBean> topicBeans = new ArrayList<>(); |
| 342 | for (ImageBean imageBean : list) { | 343 | for (ImageBean imageBean : list) { |
| 343 | if (imageBean.getCuts() != null && imageBean.getCuts().size() > 0) { | 344 | if (imageBean.getCuts() != null && imageBean.getCuts().size() > 0) { |
| 344 | for (ImageBean.Cut cut : imageBean.getCuts()) { | 345 | for (ImageBean.Cut cut : imageBean.getCuts()) { |
| 345 | if (cut.isSelect()) { | 346 | if (cut.isSelect()) { |
| 346 | TopicBean bean = new TopicBean(); | 347 | TopicBean bean = new TopicBean(); |
| 347 | String path = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; | 348 | String path = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; |
| 348 | BitmapUtils.saveBitmap(cut.getBitmap(), path); | 349 | BitmapUtils.saveBitmap(cut.getBitmap(), path); |
| 349 | bean.setPath(path); | 350 | bean.setPath(path); |
| 350 | topicBeans.add(bean); | 351 | topicBeans.add(bean); |
| 351 | } | 352 | } |
| 352 | } | 353 | } |
| 353 | } | 354 | } |
| 354 | } | 355 | } |
| 355 | save(topicBeans); | 356 | save(topicBeans); |
| 356 | break; | 357 | break; |
| 357 | } | 358 | } |
| 358 | } | 359 | } |
| 359 | 360 | ||
| 360 | private void save(List<TopicBean> topicBeans) { | 361 | private void save(List<TopicBean> topicBeans) { |
| 361 | if (type == 0) { | 362 | if (type == 0) { |
| 362 | ErrorInputDialog dialog = new ErrorInputDialog(this, topicBeans); | 363 | ErrorInputDialog dialog = new ErrorInputDialog(this, topicBeans); |
| 363 | dialog.show(); | 364 | dialog.show(); |
| 364 | } else { | 365 | } else { |
| 365 | List<String> paths = new ArrayList<>(); | 366 | List<String> paths = new ArrayList<>(); |
| 366 | for (TopicBean it: topicBeans) { | 367 | for (TopicBean it: topicBeans) { |
| 367 | paths.add(it.getPath()); | 368 | paths.add(it.getPath()); |
| 368 | } | 369 | } |
| 369 | if (addHomeworkDialog == null) { | 370 | if (addHomeworkDialog == null) { |
| 370 | addHomeworkDialog = new AddHomeworkDialog(this); | 371 | addHomeworkDialog = new AddHomeworkDialog(this); |
| 371 | } | 372 | } |
| 372 | addHomeworkDialog.show(paths, student -> { | 373 | addHomeworkDialog.show(paths, student -> { |
| 373 | Intent intent = new Intent(context, AddSuccessActivity.class); | 374 | Intent intent = new Intent(context, AddSuccessActivity.class); |
| 374 | intent.putExtra("type", type); | 375 | intent.putExtra("type", type); |
| 375 | intent.putExtra("studentJson", new Gson().toJson(student)); | 376 | intent.putExtra("studentJson", new Gson().toJson(student)); |
| 376 | startActivity(intent); | 377 | startActivity(intent); |
| 377 | finish(); | 378 | finish(); |
| 378 | }); | 379 | }); |
| 379 | } | 380 | } |
| 380 | } | 381 | } |
| 381 | 382 | ||
| 382 | @Override | 383 | @Override |
| 383 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | 384 | protected void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
| 384 | super.onActivityResult(requestCode, resultCode, data); | 385 | super.onActivityResult(requestCode, resultCode, data); |
| 385 | if (requestCode == 1001) { | 386 | if (requestCode == 1001) { |
| 386 | if (resultCode == 1002) { | 387 | if (resultCode == 1002) { |
| 387 | if (list.size() > 1) { | 388 | if (list.size() > 1) { |
| 388 | list.remove(binding.viewPager.getCurrentItem()); | 389 | list.remove(binding.viewPager.getCurrentItem()); |
| 389 | if (list.size() <= 1) { | 390 | if (list.size() <= 1) { |
| 390 | binding.rlPrevious.setVisibility(View.GONE); | 391 | binding.rlPrevious.setVisibility(View.GONE); |
| 391 | binding.rlNext.setVisibility(View.GONE); | 392 | binding.rlNext.setVisibility(View.GONE); |
| 392 | } | 393 | } |
| 393 | imageAdapter.notifyItemRemoved(binding.viewPager.getCurrentItem()); | 394 | imageAdapter.notifyItemRemoved(binding.viewPager.getCurrentItem()); |
| 394 | binding.tvTitle.setText((binding.viewPager.getCurrentItem() + 1) + "/" + list.size()); | 395 | binding.tvTitle.setText((binding.viewPager.getCurrentItem() + 1) + "/" + list.size()); |
| 395 | refreshNum(); | 396 | refreshNum(); |
| 396 | } else { | 397 | } else { |
| 397 | finish(); | 398 | finish(); |
| 398 | } | 399 | } |
| 399 | } else if (resultCode == 1003) { | 400 | } else if (resultCode == 1003) { |
| 400 | int position = binding.viewPager.getCurrentItem(); | 401 | int position = binding.viewPager.getCurrentItem(); |
| 401 | String src = list.get(position).source; | 402 | String src = list.get(position).source; |
| 402 | list.remove(position); | 403 | list.remove(position); |
| 403 | String path = data.getStringExtra("path"); | 404 | String path = data.getStringExtra("path"); |
| 404 | ImageBean imageBean = new ImageBean(); | 405 | ImageBean imageBean = new ImageBean(); |
| 405 | imageBean.setPath(path); | 406 | imageBean.setPath(path); |
| 406 | imageBean.source = src; | 407 | imageBean.source = src; |
| 407 | list.add(position, imageBean); | 408 | list.add(position, imageBean); |
| 408 | imageAdapter.notifyItemChanged(position); | 409 | imageAdapter.notifyItemChanged(position); |
| 409 | clearText(); | 410 | clearText(); |
| 410 | refreshNum(); | 411 | refreshNum(); |
| 411 | } else if (resultCode == 1004) { | 412 | } else if (resultCode == 1004) { |
| 412 | Rect rect = data.getParcelableExtra("cut"); | 413 | Rect rect = data.getParcelableExtra("cut"); |
| 413 | if (rect != null) { | 414 | if (rect != null) { |
| 414 | int position = binding.viewPager.getCurrentItem(); | 415 | int position = binding.viewPager.getCurrentItem(); |
| 415 | ImageBean.Cut cut = new ImageBean.Cut(); | 416 | ImageBean.Cut cut = new ImageBean.Cut(); |
| 416 | cut.setRect1(rect); | 417 | cut.setRect1(rect); |
| 417 | cut.setSelect(false); | 418 | cut.setSelect(false); |
| 418 | if (list.get(position).getCuts() != null) { | 419 | if (list.get(position).getCuts() != null) { |
| 419 | list.get(position).getCuts().add(cut); | 420 | list.get(position).getCuts().add(cut); |
| 420 | } else { | 421 | } else { |
| 421 | List<ImageBean.Cut> cuts =new ArrayList<>(); | 422 | List<ImageBean.Cut> cuts =new ArrayList<>(); |
| 422 | cuts.add(cut); | 423 | cuts.add(cut); |
| 423 | list.get(position).setCuts(cuts); | 424 | list.get(position).setCuts(cuts); |
| 424 | } | 425 | } |
| 425 | imageAdapter.notifyItemChanged(position); | 426 | imageAdapter.notifyItemChanged(position); |
| 426 | refreshNum(); | 427 | refreshNum(); |
| 427 | } | 428 | } |
| 428 | } else if (resultCode == 1005) { | 429 | } else if (resultCode == 1005) { |
| 429 | int i = data.getIntExtra("position", 0); | 430 | int i = data.getIntExtra("position", 0); |
| 430 | int position = binding.viewPager.getCurrentItem(); | 431 | int position = binding.viewPager.getCurrentItem(); |
| 431 | Rect rect = data.getParcelableExtra("cut"); | 432 | Rect rect = data.getParcelableExtra("cut"); |
| 432 | if (rect != null) { | 433 | if (rect != null) { |
| 433 | ImageBean.Cut cut = new ImageBean.Cut(); | 434 | ImageBean.Cut cut = new ImageBean.Cut(); |
| 434 | cut.setRect1(rect); | 435 | cut.setRect1(rect); |
| 435 | cut.setSelect(false); | 436 | cut.setSelect(false); |
| 436 | list.get(position).getCuts().remove(i); | 437 | list.get(position).getCuts().remove(i); |
| 437 | list.get(position).getCuts().add(i, cut); | 438 | list.get(position).getCuts().add(i, cut); |
| 438 | imageAdapter.notifyItemChanged(position); | 439 | imageAdapter.notifyItemChanged(position); |
| 439 | refreshNum(); | 440 | refreshNum(); |
| 440 | } else { | 441 | } else { |
| 441 | list.get(position).getCuts().remove(i); | 442 | list.get(position).getCuts().remove(i); |
| 442 | imageAdapter.notifyItemChanged(position); | 443 | imageAdapter.notifyItemChanged(position); |
| 443 | refreshNum(); | 444 | refreshNum(); |
| 444 | } | 445 | } |
| 445 | } | 446 | } |
| 446 | } else if (requestCode == 1002 && resultCode == RESULT_OK && !TextUtils.isEmpty(filePath)) { | 447 | } else if (requestCode == 1002 && resultCode == RESULT_OK && !TextUtils.isEmpty(filePath)) { |
| 447 | Bitmap b = BitmapUtils.compress(BitmapFactory.decodeFile(filePath)); | 448 | Bitmap b = BitmapUtils.compress(BitmapFactory.decodeFile(filePath)); |
| 448 | String path = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; | 449 | String path = context.getExternalFilesDir("images").getPath() + "/" + String.valueOf(System.currentTimeMillis()) + CommonUtil.getStr() + ".jpg"; |
| 449 | CommonUtil.saveBitmapToUri(b, path); | 450 | CommonUtil.saveBitmapToUri(b, path); |
| 450 | int position = binding.viewPager.getCurrentItem(); | 451 | int position = binding.viewPager.getCurrentItem(); |
| 451 | list.remove(position); | 452 | list.remove(position); |
| 452 | ImageBean imageBean = new ImageBean(); | 453 | ImageBean imageBean = new ImageBean(); |
| 453 | imageBean.setPath(path); | 454 | imageBean.setPath(path); |
| 454 | imageBean.source = path; | 455 | imageBean.source = path; |
| 455 | list.add(position, imageBean); | 456 | list.add(position, imageBean); |
| 456 | imageAdapter.notifyItemChanged(position); | 457 | imageAdapter.notifyItemChanged(position); |
| 457 | clearText(); | 458 | clearText(); |
| 458 | refreshNum(); | 459 | refreshNum(); |
| 459 | } | 460 | } |
| 460 | } | 461 | } |
| 461 | 462 | ||
| 462 | private void back() { | 463 | private void back() { |
| 463 | showTipDialog("温馨提示", "是否确认退出?", "取消", "确定", new TipDialog.TipDialogInterface() { | 464 | showTipDialog("温馨提示", "是否确认退出?", "取消", "确定", new TipDialog.TipDialogInterface() { |
| 464 | @Override | 465 | @Override |
| 465 | public void onConfirm(Dialog dialog) { | 466 | public void onConfirm(Dialog dialog) { |
| 466 | dialog.dismiss(); | 467 | dialog.dismiss(); |
| 467 | finish(); | 468 | finish(); |
| 468 | } | 469 | } |
| 469 | 470 | ||
| 470 | @Override | 471 | @Override |
| 471 | public void onCancel(Dialog dialog) { | 472 | public void onCancel(Dialog dialog) { |
| 472 | dialog.dismiss(); | 473 | dialog.dismiss(); |
| 473 | } | 474 | } |
| 474 | }); | 475 | }); |
| 475 | } | 476 | } |
| 476 | 477 | ||
| 477 | @Override | 478 | @Override |
| 478 | public boolean onKeyDown(int keyCode, KeyEvent event) { | 479 | public boolean onKeyDown(int keyCode, KeyEvent event) { |
| 479 | if (keyCode == KeyEvent.KEYCODE_BACK) { | 480 | if (keyCode == KeyEvent.KEYCODE_BACK) { |
| 480 | back(); | 481 | back(); |
| 481 | return true; | 482 | return true; |
| 482 | } | 483 | } |
| 483 | return super.onKeyDown(keyCode, event); | 484 | return super.onKeyDown(keyCode, event); |
| 484 | } | 485 | } |
| 485 | } | 486 | } |
| 486 | 487 |
app/src/main/java/com/hjx/parent/fragment/ErrorFragment.java
| 1 | package com.hjx.parent.fragment; | 1 | package com.hjx.parent.fragment; |
| 2 | 2 | ||
| 3 | import static android.app.Activity.RESULT_OK; | 3 | import static android.app.Activity.RESULT_OK; |
| 4 | 4 | ||
| 5 | import android.annotation.SuppressLint; | 5 | import android.annotation.SuppressLint; |
| 6 | import android.content.Intent; | 6 | import android.content.Intent; |
| 7 | import android.graphics.Bitmap; | 7 | import android.graphics.Bitmap; |
| 8 | import android.graphics.BitmapFactory; | 8 | import android.graphics.BitmapFactory; |
| 9 | import android.net.Uri; | 9 | import android.net.Uri; |
| 10 | import android.os.Bundle; | 10 | import android.os.Bundle; |
| 11 | import android.provider.MediaStore; | 11 | import android.provider.MediaStore; |
| 12 | import android.text.TextUtils; | 12 | import android.text.TextUtils; |
| 13 | import android.util.Log; | 13 | import android.util.Log; |
| 14 | import android.view.LayoutInflater; | 14 | import android.view.LayoutInflater; |
| 15 | import android.view.View; | 15 | import android.view.View; |
| 16 | import android.view.ViewGroup; | 16 | import android.view.ViewGroup; |
| 17 | import android.widget.TextView; | 17 | import android.widget.TextView; |
| 18 | import android.widget.Toast; | 18 | import android.widget.Toast; |
| 19 | 19 | ||
| 20 | import androidx.annotation.NonNull; | 20 | import androidx.annotation.NonNull; |
| 21 | import androidx.annotation.Nullable; | 21 | import androidx.annotation.Nullable; |
| 22 | import androidx.core.content.FileProvider; | 22 | import androidx.core.content.FileProvider; |
| 23 | import androidx.recyclerview.widget.RecyclerView; | 23 | import androidx.recyclerview.widget.RecyclerView; |
| 24 | 24 | ||
| 25 | import com.bumptech.glide.Glide; | 25 | import com.bumptech.glide.Glide; |
| 26 | import com.bumptech.glide.load.resource.bitmap.RoundedCorners; | 26 | import com.bumptech.glide.load.resource.bitmap.RoundedCorners; |
| 27 | import com.bumptech.glide.request.RequestOptions; | 27 | import com.bumptech.glide.request.RequestOptions; |
| 28 | import com.chad.library.adapter.base.BaseQuickAdapter; | 28 | import com.chad.library.adapter.base.BaseQuickAdapter; |
| 29 | import com.chad.library.adapter.base.BaseViewHolder; | 29 | import com.chad.library.adapter.base.BaseViewHolder; |
| 30 | import com.google.android.material.tabs.TabLayout; | 30 | import com.google.android.material.tabs.TabLayout; |
| 31 | import com.google.gson.Gson; | 31 | import com.google.gson.Gson; |
| 32 | import com.hjq.permissions.OnPermissionCallback; | 32 | import com.hjq.permissions.OnPermissionCallback; |
| 33 | import com.hjq.permissions.Permission; | 33 | import com.hjq.permissions.Permission; |
| 34 | import com.hjq.permissions.XXPermissions; | 34 | import com.hjq.permissions.XXPermissions; |
| 35 | import com.hjx.parent.ErrorListActivity; | 35 | import com.hjx.parent.ErrorListActivity; |
| 36 | import com.hjx.parent.HomeworkDetailActivity; | 36 | import com.hjx.parent.HomeworkDetailActivity; |
| 37 | import com.hjx.parent.HomeworkShareActivity; | 37 | import com.hjx.parent.HomeworkShareActivity; |
| 38 | import com.hjx.parent.ImageActivity; | 38 | import com.hjx.parent.ImageActivity; |
| 39 | import com.hjx.parent.R; | 39 | import com.hjx.parent.R; |
| 40 | import com.hjx.parent.StuHomeworkActivity; | 40 | import com.hjx.parent.StuHomeworkActivity; |
| 41 | import com.hjx.parent.TeacherChooseActivity; | 41 | import com.hjx.parent.TeacherChooseActivity; |
| 42 | import com.hjx.parent.adapter.ErrorAdapter; | 42 | import com.hjx.parent.adapter.ErrorAdapter; |
| 43 | import com.hjx.parent.adapter.HomeworkAdapter; | 43 | import com.hjx.parent.adapter.HomeworkAdapter; |
| 44 | import com.hjx.parent.bean.StudentBean; | 44 | import com.hjx.parent.bean.StudentBean; |
| 45 | import com.hjx.parent.databinding.FragmentErrorBookBinding; | 45 | import com.hjx.parent.databinding.FragmentErrorBookBinding; |
| 46 | import com.hjx.parent.rx.BaseRxFragment; | 46 | import com.hjx.parent.rx.BaseRxFragment; |
| 47 | import com.prws.common.bean.PageInfo; | 47 | import com.prws.common.bean.PageInfo; |
| 48 | import com.prws.common.bean.ResponseResult; | 48 | import com.prws.common.bean.ResponseResult; |
| 49 | import com.prws.common.bean.Student; | 49 | import com.prws.common.bean.Student; |
| 50 | import com.prws.common.bean.TopicBean; | 50 | import com.prws.common.bean.TopicBean; |
| 51 | import com.prws.common.bean.homework.HomeworkList; | 51 | import com.prws.common.bean.homework.HomeworkList; |
| 52 | import com.prws.common.net.NetWorks; | 52 | import com.prws.common.net.NetWorks; |
| 53 | import com.prws.common.utils.BitmapUtils; | 53 | import com.prws.common.utils.BitmapUtils; |
| 54 | import com.prws.common.utils.CommonUtil; | 54 | import com.prws.common.utils.CommonUtil; |
| 55 | import com.prws.common.utils.ScreenUtils; | 55 | import com.prws.common.utils.ScreenUtils; |
| 56 | import com.prws.common.utils.SharedPreferencesUtil; | 56 | import com.prws.common.utils.SharedPreferencesUtil; |
| 57 | import com.prws.common.utils.dialog.MyButtomDialog; | 57 | import com.prws.common.utils.dialog.MyButtomDialog; |
| 58 | import com.trello.rxlifecycle2.android.RxLifecycleAndroid; | 58 | import com.trello.rxlifecycle2.android.RxLifecycleAndroid; |
| 59 | import com.zhangteng.imagepicker.bean.ImageInfo; | 59 | import com.zhangteng.imagepicker.bean.ImageInfo; |
| 60 | import com.zhangteng.imagepicker.config.ImagePickerConfig; | 60 | import com.zhangteng.imagepicker.config.ImagePickerConfig; |
| 61 | import com.zhangteng.imagepicker.config.ImagePickerEnum; | 61 | import com.zhangteng.imagepicker.config.ImagePickerEnum; |
| 62 | import com.zhangteng.imagepicker.config.ImagePickerOpen; | 62 | import com.zhangteng.imagepicker.config.ImagePickerOpen; |
| 63 | import com.zhangteng.imagepicker.imageloader.GlideImageLoader; | 63 | import com.zhangteng.imagepicker.imageloader.GlideImageLoader; |
| 64 | import com.zhangteng.utils.IHandlerCallBack; | 64 | import com.zhangteng.utils.IHandlerCallBack; |
| 65 | 65 | ||
| 66 | import java.io.File; | 66 | import java.io.File; |
| 67 | import java.text.SimpleDateFormat; | 67 | import java.text.SimpleDateFormat; |
| 68 | import java.util.ArrayList; | 68 | import java.util.ArrayList; |
| 69 | import java.util.Arrays; | 69 | import java.util.Arrays; |
| 70 | import java.util.HashMap; | 70 | import java.util.HashMap; |
| 71 | import java.util.List; | 71 | import java.util.List; |
| 72 | import java.util.Locale; | 72 | import java.util.Locale; |
| 73 | import java.util.Map; | 73 | import java.util.Map; |
| 74 | 74 | ||
| 75 | import io.reactivex.Observable; | 75 | import io.reactivex.Observable; |
| 76 | import io.reactivex.Observer; | 76 | import io.reactivex.Observer; |
| 77 | import io.reactivex.android.schedulers.AndroidSchedulers; | 77 | import io.reactivex.android.schedulers.AndroidSchedulers; |
| 78 | import io.reactivex.disposables.Disposable; | 78 | import io.reactivex.disposables.Disposable; |
| 79 | import io.reactivex.schedulers.Schedulers; | 79 | import io.reactivex.schedulers.Schedulers; |
| 80 | 80 | ||
| 81 | public class ErrorFragment extends BaseRxFragment<FragmentErrorBookBinding> { | 81 | public class ErrorFragment extends BaseRxFragment<FragmentErrorBookBinding> { |
| 82 | StudentBean studentBean; | 82 | StudentBean studentBean; |
| 83 | private int subject; | 83 | private int subject; |
| 84 | private ErrorAdapter errorAdapter; | 84 | private ErrorAdapter errorAdapter; |
| 85 | private HomeworkAdapter homeworkAdapter; | 85 | private HomeworkAdapter homeworkAdapter; |
| 86 | private String filePath; | 86 | private String filePath; |
| 87 | 87 | ||
| 88 | @Override | 88 | @Override |
| 89 | protected FragmentErrorBookBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) { | 89 | protected FragmentErrorBookBinding onCreateViewBinding(@NonNull LayoutInflater inflater, @Nullable ViewGroup parent) { |
| 90 | return FragmentErrorBookBinding.inflate(inflater, parent, false); | 90 | return FragmentErrorBookBinding.inflate(inflater, parent, false); |
| 91 | } | 91 | } |
| 92 | 92 | ||
| 93 | public void refreshStudent() { | 93 | public void refreshStudent() { |
| 94 | String student = (String) SharedPreferencesUtil.getData("student", ""); | 94 | String student = (String) SharedPreferencesUtil.getData("student", ""); |
| 95 | try { | 95 | try { |
| 96 | studentBean = new Gson().fromJson(student, StudentBean.class); | 96 | studentBean = new Gson().fromJson(student, StudentBean.class); |
| 97 | getBinding().tvChoose.setText(studentBean.getStuName()); | 97 | getBinding().tvChoose.setText(studentBean.getStuName()); |
| 98 | } catch (Exception e) { | 98 | } catch (Exception e) { |
| 99 | e.printStackTrace(); | 99 | e.printStackTrace(); |
| 100 | } | 100 | } |
| 101 | } | 101 | } |
| 102 | 102 | ||
| 103 | @Override | 103 | @Override |
| 104 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { | 104 | public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) { |
| 105 | super.onViewCreated(view, savedInstanceState); | 105 | super.onViewCreated(view, savedInstanceState); |
| 106 | String photo = (String) SharedPreferencesUtil.getData("photo", ""); | 106 | String photo = (String) SharedPreferencesUtil.getData("photo", ""); |
| 107 | if (!TextUtils.isEmpty(photo)) { | 107 | if (!TextUtils.isEmpty(photo)) { |
| 108 | RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(getContext(), 45)); | 108 | RoundedCorners roundedCorners = new RoundedCorners(ScreenUtils.dpToPx(getContext(), 45)); |
| 109 | RequestOptions options = RequestOptions.bitmapTransform(roundedCorners); | 109 | RequestOptions options = RequestOptions.bitmapTransform(roundedCorners); |
| 110 | Glide.with(getContext()).load(photo).apply(options).into(getBinding().ivStudent); | 110 | Glide.with(getContext()).load(photo).apply(options).into(getBinding().ivStudent); |
| 111 | } | 111 | } |
| 112 | String name = (String) SharedPreferencesUtil.getData("name", ""); | 112 | String name = (String) SharedPreferencesUtil.getData("name", ""); |
| 113 | getBinding().tvName.setText(name); | 113 | getBinding().tvName.setText(name); |
| 114 | refreshStudent(); | 114 | refreshStudent(); |
| 115 | getBinding().tvChoose.setOnClickListener(v -> startActivity(new Intent(getContext(), TeacherChooseActivity.class))); | 115 | getBinding().tvChoose.setOnClickListener(v -> startActivity(new Intent(getContext(), TeacherChooseActivity.class))); |
| 116 | getBinding().rlTakePhoto.setOnClickListener(v -> takePhoto(0)); | 116 | getBinding().rlTakePhoto.setOnClickListener(v -> takePhoto(0)); |
| 117 | getBinding().flEntryHomework.setOnClickListener(v -> takePhoto(1)); | 117 | getBinding().flEntryHomework.setOnClickListener(v -> takePhoto(1)); |
| 118 | BaseQuickAdapter adapter = new BaseQuickAdapter(R.layout.item_subject, Arrays.asList(getResources().getStringArray(R.array.filter_subject))) { | 118 | BaseQuickAdapter adapter = new BaseQuickAdapter(R.layout.item_subject, Arrays.asList(getResources().getStringArray(R.array.filter_subject))) { |
| 119 | @Override | 119 | @Override |
| 120 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { | 120 | public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) { |
| 121 | 121 | ||
| 122 | } | 122 | } |
| 123 | 123 | ||
| 124 | @Override | 124 | @Override |
| 125 | protected void convert(@NonNull BaseViewHolder item, Object o) { | 125 | protected void convert(@NonNull BaseViewHolder item, Object o) { |
| 126 | int position = item.getBindingAdapterPosition(); | 126 | int position = item.getBindingAdapterPosition(); |
| 127 | String name = getResources().getStringArray(R.array.filter_subject)[position]; | 127 | String name = getResources().getStringArray(R.array.filter_subject)[position]; |
| 128 | item.setText(R.id.tv_subject, name); | 128 | item.setText(R.id.tv_subject, name); |
| 129 | switch (name) { | 129 | switch (name) { |
| 130 | case "语文": | 130 | case "语文": |
| 131 | item.setImageResource(R.id.iv_subject, R.drawable.state_ic_chinese); | 131 | item.setImageResource(R.id.iv_subject, R.drawable.state_ic_chinese); |
| 132 | break; | 132 | break; |
| 133 | case "数学": | 133 | case "数学": |
| 134 | item.setImageResource(R.id.iv_subject, R.drawable.state_ic_math); | 134 | item.setImageResource(R.id.iv_subject, R.drawable.state_ic_math); |
| 135 | break; | 135 | break; |
| 136 | case "英语": | 136 | case "英语": |
| 137 | item.setImageResource(R.id.iv_subject, R.drawable.state_ic_english); | 137 | item.setImageResource(R.id.iv_subject, R.drawable.state_ic_english); |
| 138 | break; | 138 | break; |
| 139 | case "物理": | 139 | case "物理": |
| 140 | item.setImageResource(R.id.iv_subject, R.drawable.state_ic_physics); | 140 | item.setImageResource(R.id.iv_subject, R.drawable.state_ic_physics); |
| 141 | break; | 141 | break; |
| 142 | case "化学": | 142 | case "化学": |
| 143 | item.setImageResource(R.id.iv_subject, R.drawable.state_ic_chemistry); | 143 | item.setImageResource(R.id.iv_subject, R.drawable.state_ic_chemistry); |
| 144 | break; | 144 | break; |
| 145 | } | 145 | } |
| 146 | item.itemView.setActivated(subject == position); | 146 | item.itemView.setActivated(subject == position); |
| 147 | ViewGroup.LayoutParams layoutParams = item.getView(R.id.rl_subject).getLayoutParams(); | 147 | ViewGroup.LayoutParams layoutParams = item.getView(R.id.rl_subject).getLayoutParams(); |
| 148 | layoutParams.width = (CommonUtil.getScreenWidth(mContext) - CommonUtil.dpToPx(mContext, 30)) / 4; | 148 | layoutParams.width = (CommonUtil.getScreenWidth(mContext) - CommonUtil.dpToPx(mContext, 30)) / 4; |
| 149 | item.getView(R.id.rl_subject).setLayoutParams(layoutParams); | 149 | item.getView(R.id.rl_subject).setLayoutParams(layoutParams); |
| 150 | item.itemView.setOnClickListener(v -> { | 150 | item.itemView.setOnClickListener(v -> { |
| 151 | if (subject != position) { | 151 | if (subject != position) { |
| 152 | subject = position; | 152 | subject = position; |
| 153 | String subStr = getResources().getStringArray(R.array.filter_subject)[subject]; | 153 | String subStr = getResources().getStringArray(R.array.filter_subject)[subject]; |
| 154 | getError(subStr); | 154 | getError(subStr); |
| 155 | getHomework(subStr); | 155 | getHomework(subStr); |
| 156 | notifyDataSetChanged(); | 156 | notifyDataSetChanged(); |
| 157 | } | 157 | } |
| 158 | }); | 158 | }); |
| 159 | } | 159 | } |
| 160 | }; | 160 | }; |
| 161 | getBinding().rvSubject.setAdapter(adapter); | 161 | getBinding().rvSubject.setAdapter(adapter); |
| 162 | subject = 0; | 162 | subject = 0; |
| 163 | getError(getResources().getStringArray(R.array.filter_subject)[subject]); | 163 | getError(getResources().getStringArray(R.array.filter_subject)[subject]); |
| 164 | getBinding().llAll.setOnClickListener(v -> { | 164 | getBinding().llAll.setOnClickListener(v -> { |
| 165 | if (binding.tabLayout.getSelectedTabPosition() == 0) { | 165 | if (binding.tabLayout.getSelectedTabPosition() == 0) { |
| 166 | Intent intent = new Intent(getContext(), ErrorListActivity.class); | 166 | Intent intent = new Intent(getContext(), ErrorListActivity.class); |
| 167 | startActivity(intent); | 167 | startActivity(intent); |
| 168 | } else { | 168 | } else { |
| 169 | Intent intent = new Intent(getContext(), StuHomeworkActivity.class); | 169 | Intent intent = new Intent(getContext(), StuHomeworkActivity.class); |
| 170 | startActivity(intent); | 170 | startActivity(intent); |
| 171 | } | 171 | } |
| 172 | }); | 172 | }); |
| 173 | 173 | ||
| 174 | errorAdapter = new ErrorAdapter(getActivity(), new ArrayList<>(), false); | 174 | errorAdapter = new ErrorAdapter(getActivity(), new ArrayList<>(), false); |
| 175 | homeworkAdapter = new HomeworkAdapter(); | 175 | homeworkAdapter = new HomeworkAdapter(); |
| 176 | homeworkAdapter.setOnItemClickListener((baseQuickAdapter, view1, i) -> { | 176 | homeworkAdapter.setOnItemClickListener((baseQuickAdapter, view1, i) -> { |
| 177 | HomeworkList data = homeworkAdapter.getData().get(i); | 177 | HomeworkList data = homeworkAdapter.getData().get(i); |
| 178 | Gson gson = new Gson(); | 178 | Gson gson = new Gson(); |
| 179 | String json = gson.toJson(studentBean); | 179 | String json = gson.toJson(studentBean); |
| 180 | Student s = gson.fromJson(json, Student.class); | 180 | Student s = gson.fromJson(json, Student.class); |
| 181 | if (data.getRight() == null) { | 181 | if (data.getRight() == null) { |
| 182 | Intent intent = new Intent(requireContext(), HomeworkDetailActivity.class); | 182 | Intent intent = new Intent(requireContext(), HomeworkDetailActivity.class); |
| 183 | intent.putExtra("student", s); | 183 | intent.putExtra("student", s); |
| 184 | intent.putExtra("data", data); | 184 | intent.putExtra("data", data); |
| 185 | startActivity(intent); | 185 | startActivity(intent); |
| 186 | } else { | 186 | } else { |
| 187 | Intent intent = new Intent(requireContext(), HomeworkShareActivity.class); | 187 | Intent intent = new Intent(requireContext(), HomeworkShareActivity.class); |
| 188 | intent.putExtra("student", s); | 188 | intent.putExtra("student", s); |
| 189 | intent.putExtra("id", data.getId()); | 189 | intent.putExtra("id", data.getId()); |
| 190 | intent.putExtra("grade", data.getGrade()); | 190 | intent.putExtra("grade", data.getGrade()); |
| 191 | intent.putExtra("subject", data.getSubject()); | 191 | intent.putExtra("subject", data.getSubject()); |
| 192 | String name1 = new SimpleDateFormat("yyyy年M月d日作业", Locale.CHINA).format(data.uploadTime); | 192 | String name1 = new SimpleDateFormat("yyyy年M月d日作业", Locale.CHINA).format(data.uploadTime); |
| 193 | intent.putExtra("name", name1); | 193 | intent.putExtra("name", name1); |
| 194 | startActivity(intent); | 194 | startActivity(intent); |
| 195 | } | 195 | } |
| 196 | }); | 196 | }); |
| 197 | binding.rvMain.setAdapter(errorAdapter); | 197 | binding.rvMain.setAdapter(errorAdapter); |
| 198 | // binding.tabLayout.addTab(binding.tabLayout.newTab().setText("最近错题")); | 198 | // binding.tabLayout.addTab(binding.tabLayout.newTab().setText("最近错题")); |
| 199 | // binding.tabLayout.addTab(binding.tabLayout.newTab().setText("最近作业")); | 199 | // binding.tabLayout.addTab(binding.tabLayout.newTab().setText("最近作业")); |
| 200 | binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { | 200 | binding.tabLayout.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() { |
| 201 | @Override | 201 | @Override |
| 202 | public void onTabSelected(TabLayout.Tab tab) { | 202 | public void onTabSelected(TabLayout.Tab tab) { |
| 203 | if (tab.getPosition() == 0) { | 203 | if (tab.getPosition() == 0) { |
| 204 | binding.rvMain.setAdapter(errorAdapter); | 204 | binding.rvMain.setAdapter(errorAdapter); |
| 205 | binding.rvMain.setVisibility(errorAdapter.list.isEmpty() ? View.GONE : View.VISIBLE); | 205 | binding.rvMain.setVisibility(errorAdapter.list.isEmpty() ? View.GONE : View.VISIBLE); |
| 206 | binding.llEmpty.setVisibility(errorAdapter.list.isEmpty() ? View.VISIBLE : View.GONE); | 206 | binding.llEmpty.setVisibility(errorAdapter.list.isEmpty() ? View.VISIBLE : View.GONE); |
| 207 | binding.tvListTitle.setText("最近错题"); | 207 | binding.tvListTitle.setText("最近错题"); |
| 208 | binding.tvEmpty.setText("暂无错题~"); | 208 | binding.tvEmpty.setText("暂无错题~"); |
| 209 | } else { | 209 | } else { |
| 210 | binding.rvMain.setAdapter(homeworkAdapter); | 210 | binding.rvMain.setAdapter(homeworkAdapter); |
| 211 | binding.rvMain.setVisibility(homeworkAdapter.getData().isEmpty() ? View.GONE : View.VISIBLE); | 211 | binding.rvMain.setVisibility(homeworkAdapter.getData().isEmpty() ? View.GONE : View.VISIBLE); |
| 212 | binding.llEmpty.setVisibility(homeworkAdapter.getData().isEmpty() ? View.VISIBLE : View.GONE); | 212 | binding.llEmpty.setVisibility(homeworkAdapter.getData().isEmpty() ? View.VISIBLE : View.GONE); |
| 213 | binding.tvListTitle.setText("最近作业"); | 213 | binding.tvListTitle.setText("最近作业"); |
| 214 | binding.tvEmpty.setText("暂无作业~"); | 214 | binding.tvEmpty.setText("暂无作业~"); |
| 215 | } | 215 | } |
| 216 | } | 216 | } |
| 217 | @Override | 217 | @Override |
| 218 | public void onTabUnselected(TabLayout.Tab tab) { | 218 | public void onTabUnselected(TabLayout.Tab tab) { |
| 219 | } | 219 | } |
| 220 | @Override | 220 | @Override |
| 221 | public void onTabReselected(TabLayout.Tab tab) { | 221 | public void onTabReselected(TabLayout.Tab tab) { |
| 222 | } | 222 | } |
| 223 | }); | 223 | }); |
| 224 | } | 224 | } |
| 225 | 225 | ||
| 226 | private void takePhoto(int type) { | 226 | private void takePhoto(int type) { |
| 227 | if (!XXPermissions.isGranted(getContext(), Permission.CAMERA, Permission.WRITE_EXTERNAL_STORAGE)) { | 227 | if (!XXPermissions.isGranted(getContext(), Permission.CAMERA, Permission.WRITE_EXTERNAL_STORAGE)) { |
| 228 | XXPermissions.with(getContext()) | 228 | XXPermissions.with(getContext()) |
| 229 | // 申请多个权限 | 229 | // 申请多个权限 |
| 230 | .permission(Permission.CAMERA).request(new OnPermissionCallback() { | 230 | .permission(Permission.CAMERA).request(new OnPermissionCallback() { |
| 231 | @Override | 231 | @Override |
| 232 | public void onGranted(List<String> permissions, boolean all) { | 232 | public void onGranted(List<String> permissions, boolean all) { |
| 233 | if (all) { | 233 | if (all) { |
| 234 | //开启扫码界面 | 234 | //开启扫码界面 |
| 235 | showButtonDialog(type); | 235 | showButtonDialog(type); |
| 236 | } else { | 236 | } else { |
| 237 | Toast.makeText(getContext(), "需要权限", Toast.LENGTH_SHORT).show(); | 237 | Toast.makeText(getContext(), "需要权限", Toast.LENGTH_SHORT).show(); |
| 238 | } | 238 | } |
| 239 | } | 239 | } |
| 240 | 240 | ||
| 241 | @Override | 241 | @Override |
| 242 | public void onDenied(List<String> permissions, boolean never) { | 242 | public void onDenied(List<String> permissions, boolean never) { |
| 243 | XXPermissions.startPermissionActivity(getContext(), permissions); | 243 | XXPermissions.startPermissionActivity(getContext(), permissions); |
| 244 | } | 244 | } |
| 245 | }); | 245 | }); |
| 246 | } else { | 246 | } else { |
| 247 | showButtonDialog(type); | 247 | showButtonDialog(type); |
| 248 | } | 248 | } |
| 249 | } | 249 | } |
| 250 | 250 | ||
| 251 | private void showButtonDialog(int type) { | 251 | private void showButtonDialog(int type) { |
| 252 | MyButtomDialog dialog = new MyButtomDialog(getActivity(), R.style.BottomSheetDialog); | 252 | MyButtomDialog dialog = new MyButtomDialog(getActivity(), R.style.BottomSheetDialog); |
| 253 | View bottomView = LayoutInflater.from(getActivity()).inflate(R.layout.bottom_sheet_layout, null); | 253 | View bottomView = LayoutInflater.from(getActivity()).inflate(R.layout.bottom_sheet_layout, null); |
| 254 | dialog.setContentView(bottomView); | 254 | dialog.setContentView(bottomView); |
| 255 | bottomView.findViewById(R.id.take_photo).setOnClickListener(new View.OnClickListener() { | 255 | bottomView.findViewById(R.id.take_photo).setOnClickListener(new View.OnClickListener() { |
| 256 | @Override | 256 | @Override |
| 257 | public void onClick(View view) { | 257 | public void onClick(View view) { |
| 258 | dialog.dismiss(); | 258 | dialog.dismiss(); |
| 259 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); | 259 | Intent takePictureIntent = new Intent(MediaStore.ACTION_IMAGE_CAPTURE); |
| 260 | //确保有相机来处理Intent | 260 | //确保有相机来处理Intent |
| 261 | filePath = requireActivity().getExternalFilesDir("images").getPath() + "/" + System.currentTimeMillis() + CommonUtil.getStr() + ".jpg"; | 261 | filePath = requireActivity().getExternalFilesDir("images").getPath() + "/" + System.currentTimeMillis() + CommonUtil.getStr() + ".jpg"; |
| 262 | File photoFile = new File(filePath); | 262 | File photoFile = new File(filePath); |
| 263 | 263 | ||
| 264 | //适配Android 7.0文件权限,通过FileProvider创建一个content类型的Uri | 264 | //适配Android 7.0文件权限,通过FileProvider创建一个content类型的Uri |
| 265 | Uri photoUri = FileProvider.getUriForFile(requireActivity(), "com.hjx.parent.fileprovider", photoFile); | 265 | Uri photoUri = FileProvider.getUriForFile(requireActivity(), "com.hjx.parent.fileprovider", photoFile); |
| 266 | takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); | 266 | takePictureIntent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION); |
| 267 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); | 267 | takePictureIntent.putExtra(MediaStore.EXTRA_OUTPUT, photoUri); |
| 268 | startActivityForResult(takePictureIntent, type == 0 ? 0xA01 : 0xA02); | 268 | startActivityForResult(takePictureIntent, type == 0 ? 0xA01 : 0xA02); |
| 269 | } | 269 | } |
| 270 | }); | 270 | }); |
| 271 | bottomView.findViewById(R.id.choose_photo).setOnClickListener(new View.OnClickListener() { | 271 | bottomView.findViewById(R.id.choose_photo).setOnClickListener(new View.OnClickListener() { |
| 272 | @Override | 272 | @Override |
| 273 | public void onClick(View view) { | 273 | public void onClick(View view) { |
| 274 | dialog.dismiss(); | 274 | dialog.dismiss(); |
| 275 | ImagePickerConfig imagePickerConfig = new ImagePickerConfig.Builder().provider(getActivity().getPackageName() + ".fileprovider").imageLoader(new GlideImageLoader()) //图片加载器 | 275 | ImagePickerConfig imagePickerConfig = new ImagePickerConfig.Builder().provider(getActivity().getPackageName() + ".fileprovider").imageLoader(new GlideImageLoader()) //图片加载器 |
| 276 | .iHandlerCallBack(new HandlerCallBack(type)) //图片选择器生命周期监听(直接打开摄像头时无效) | 276 | .iHandlerCallBack(new HandlerCallBack(type)) //图片选择器生命周期监听(直接打开摄像头时无效) |
| 277 | .multiSelect(true) //是否多选 | 277 | .multiSelect(true) //是否多选 |
| 278 | .isShowCamera(false).isVideoPicker(false) //是否选择视频 默认false | 278 | .isShowCamera(false).isVideoPicker(false) //是否选择视频 默认false |
| 279 | .isImagePicker(true).imagePickerType(ImagePickerEnum.PHOTO_PICKER) //选择器打开类型 | 279 | .isImagePicker(true).imagePickerType(ImagePickerEnum.PHOTO_PICKER) //选择器打开类型 |
| 280 | .maxImageSelectable(5) //图片可选择数 | 280 | .maxImageSelectable(5) //图片可选择数 |
| 281 | .isCrop(false).pathList(new ArrayList<>()).pickerThemeColorRes(R.color.image_picker_white).pickerTitleColorRes(R.color.image_picker_text_black).cropThemeColorRes(R.color.image_picker_white).cropTitleColorRes(R.color.image_picker_text_black).pickerBackRes(R.mipmap.image_picker_back_black).pickerFolderRes(R.mipmap.image_picker_folder_black).build(); | ||
| 282 | ImagePickerOpen.getInstance().setImagePickerConfig(imagePickerConfig).pathList(new ArrayList<>()).open(getActivity()); | ||
| 283 | |||
| 284 | } | 281 | .isCrop(false).pathList(new ArrayList<>()).pickerThemeColorRes(R.color.image_picker_white).pickerTitleColorRes(R.color.image_picker_text_black).cropThemeColorRes(R.color.image_picker_white).cropTitleColorRes(R.color.image_picker_text_black).pickerBackRes(R.mipmap.image_picker_back_black).pickerFolderRes(R.mipmap.image_picker_folder_black).build(); |
| 285 | }); | 282 | ImagePickerOpen.getInstance().setImagePickerConfig(imagePickerConfig).pathList(new ArrayList<>()).open(getActivity()); |
| 286 | bottomView.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() { | 283 | |
| 287 | @Override | 284 | } |
| 288 | public void onClick(View view) { | 285 | }); |
| 289 | dialog.dismiss(); | 286 | bottomView.findViewById(R.id.cancel).setOnClickListener(new View.OnClickListener() { |
| 290 | } | 287 | @Override |
| 291 | }); | 288 | public void onClick(View view) { |
| 292 | dialog.show(); | 289 | dialog.dismiss(); |
| 293 | } | 290 | } |
| 294 | 291 | }); | |
| 295 | @SuppressLint("CheckResult") | 292 | dialog.show(); |
| 296 | protected void getHomework(String subject) { | 293 | } |
| 297 | if (studentBean == null) return; | 294 | |
| 298 | NetWorks.service_url.getStudentHomework(NetWorks.getHeader(), studentBean.getStuId()) | 295 | @SuppressLint("CheckResult") |
| 299 | .subscribeOn(Schedulers.io()) | 296 | protected void getHomework(String subject) { |
| 300 | .observeOn(AndroidSchedulers.mainThread()) | 297 | if (studentBean == null) return; |
| 301 | .compose(RxLifecycleAndroid.bindFragment(getRxLifecycle())) | 298 | NetWorks.service_url.getStudentHomework(NetWorks.getHeader(), studentBean.getStuId()) |
| 302 | .map(ResponseResult::getData) | 299 | .subscribeOn(Schedulers.io()) |
| 303 | .toObservable() | 300 | .observeOn(AndroidSchedulers.mainThread()) |
| 304 | .concatMap(Observable::fromIterable) | 301 | .compose(RxLifecycleAndroid.bindFragment(getRxLifecycle())) |
| 305 | .filter(data -> subject.equals(data.getSubject())) | 302 | .map(ResponseResult::getData) |
| 306 | //.filter(data -> data.getRight() == null) | 303 | .toObservable() |
| 307 | .take(5) | 304 | .concatMap(Observable::fromIterable) |
| 308 | .toList() | 305 | .filter(data -> subject.equals(data.getSubject())) |
| 309 | .subscribe((data, th) -> { | 306 | //.filter(data -> data.getRight() == null) |
| 310 | if (th != null) th.printStackTrace(); | 307 | .take(5) |
| 311 | homeworkAdapter.setNewData(data); | 308 | .toList() |
| 312 | if (binding.tabLayout.getSelectedTabPosition() == 1) { | 309 | .subscribe((data, th) -> { |
| 313 | binding.rvMain.setVisibility(data == null || data.isEmpty() ? View.GONE : View.VISIBLE); | 310 | if (th != null) th.printStackTrace(); |
| 314 | binding.llEmpty.setVisibility(data == null || data.isEmpty() ? View.VISIBLE : View.GONE); | 311 | homeworkAdapter.setNewData(data); |
| 315 | } | 312 | if (binding.tabLayout.getSelectedTabPosition() == 1) { |
| 316 | }); | 313 | binding.rvMain.setVisibility(data == null || data.isEmpty() ? View.GONE : View.VISIBLE); |
| 317 | } | 314 | binding.llEmpty.setVisibility(data == null || data.isEmpty() ? View.VISIBLE : View.GONE); |
| 318 | 315 | } | |
| 319 | private void getError(String subject) { | 316 | }); |
| 320 | if (studentBean == null) { | 317 | } |
| 321 | return; | 318 | |
| 322 | } | 319 | private void getError(String subject) { |
| 323 | Map<String, Object> map = new HashMap<>(); | 320 | if (studentBean == null) { |
| 324 | map.put("subject", subject); | 321 | return; |
| 325 | map.put("stuId", studentBean.getStuId()); | 322 | } |
| 326 | map.put("pageSize", 5); | 323 | Map<String, Object> map = new HashMap<>(); |
| 327 | map.put("pageNum", 1); | 324 | map.put("subject", subject); |
| 328 | NetWorks.getError(map, new Observer<ResponseResult<PageInfo<TopicBean>>>() { | 325 | map.put("stuId", studentBean.getStuId()); |
| 329 | @Override | 326 | map.put("pageSize", 5); |
| 330 | public void onSubscribe(Disposable d) { | 327 | map.put("pageNum", 1); |
| 331 | } | 328 | NetWorks.getError(map, new Observer<ResponseResult<PageInfo<TopicBean>>>() { |
| 332 | @Override | 329 | @Override |
| 333 | public void onNext(ResponseResult<PageInfo<TopicBean>> result) { | 330 | public void onSubscribe(Disposable d) { |
| 334 | if (result.getData() != null && result.getCode() == 200) { | 331 | } |
| 335 | List<TopicBean> topicBeans = result.getData().getList(); | 332 | @Override |
| 336 | errorAdapter.refresh(topicBeans); | 333 | public void onNext(ResponseResult<PageInfo<TopicBean>> result) { |
| 337 | if (binding.tabLayout.getSelectedTabPosition() == 0) { | 334 | if (result.getData() != null && result.getCode() == 200) { |
| 338 | if (topicBeans.size() > 0) { | 335 | List<TopicBean> topicBeans = result.getData().getList(); |
| 339 | getBinding().rvMain.setVisibility(View.VISIBLE); | 336 | errorAdapter.refresh(topicBeans); |
| 340 | getBinding().llEmpty.setVisibility(View.GONE); | 337 | if (binding.tabLayout.getSelectedTabPosition() == 0) { |
| 341 | } else { | 338 | if (topicBeans.size() > 0) { |
| 342 | getBinding().rvMain.setVisibility(View.GONE); | 339 | getBinding().rvMain.setVisibility(View.VISIBLE); |
| 343 | getBinding().llEmpty.setVisibility(View.VISIBLE); | 340 | getBinding().llEmpty.setVisibility(View.GONE); |
| 344 | } | 341 | } else { |
| 345 | } | 342 | getBinding().rvMain.setVisibility(View.GONE); |
| 346 | } | 343 | getBinding().llEmpty.setVisibility(View.VISIBLE); |
| 347 | } | 344 | } |
| 348 | @Override | 345 | } |
| 349 | public void onError(Throwable e) { | 346 | } |
| 350 | e.printStackTrace(); | 347 | } |
| 351 | } | 348 | @Override |
| 352 | @Override | 349 | public void onError(Throwable e) { |
| 353 | public void onComplete() { | 350 | e.printStackTrace(); |
| 354 | } | 351 | } |
| 355 | }); | 352 | @Override |
| 356 | } | 353 | public void onComplete() { |
| 357 | 354 | } | |
| 358 | @Override | 355 | }); |
| 359 | public void onResume() { | 356 | } |
| 360 | super.onResume(); | 357 | |
| 361 | String subStr = getResources().getStringArray(R.array.filter_subject)[subject]; | 358 | @Override |
| 362 | getError(subStr); | 359 | public void onResume() { |
| 363 | getHomework(subStr); | 360 | super.onResume(); |
| 364 | } | 361 | String subStr = getResources().getStringArray(R.array.filter_subject)[subject]; |
| 365 | 362 | getError(subStr); | |
| 366 | public class HandlerCallBack implements IHandlerCallBack<ImageInfo> { | 363 | getHomework(subStr); |
| 367 | int type; | 364 | } |
| 368 | public HandlerCallBack(int type) { | 365 | |
| 369 | this.type = type; | 366 | public class HandlerCallBack implements IHandlerCallBack<ImageInfo> { |
| 370 | } | 367 | int type; |
| 371 | 368 | public HandlerCallBack(int type) { | |
| 372 | private String TAG = "---ImagePicker---"; | 369 | this.type = type; |
| 373 | List<ImageInfo> photoList = new ArrayList<>(); | 370 | } |
| 374 | 371 | ||
| 375 | @Override | 372 | private String TAG = "---ImagePicker---"; |
| 376 | public void onStart() { | 373 | List<ImageInfo> photoList = new ArrayList<>(); |
| 377 | Log.i(TAG, "onStart: 开启"); | 374 | |
| 378 | } | 375 | @Override |
| 379 | 376 | public void onStart() { | |
| 380 | @Override | 377 | Log.i(TAG, "onStart: 开启"); |
| 381 | public void onSuccess(List<ImageInfo> photoList) { | 378 | } |
| 382 | this.photoList = photoList; | 379 | |
| 383 | Log.i(TAG, "onSuccess: 返回数据"); | 380 | @Override |
| 384 | } | 381 | public void onSuccess(List<ImageInfo> photoList) { |
| 385 | 382 | this.photoList = photoList; | |
| 386 | @Override | 383 | Log.i(TAG, "onSuccess: 返回数据"); |
| 387 | public void onCancel() { | 384 | } |
| 388 | Log.i(TAG, "onCancel: 取消"); | 385 | |
| 389 | } | 386 | @Override |
| 390 | 387 | public void onCancel() { | |
| 391 | @Override | 388 | Log.i(TAG, "onCancel: 取消"); |
| 392 | public void onFinish(List<ImageInfo> selectImage) { | 389 | } |
| 393 | if (selectImage.size() == photoList.size()) { | 390 | |
| 394 | ArrayList<String> strings = new ArrayList<>(); | 391 | @Override |
| 395 | Intent intent = new Intent(getActivity(), ImageActivity.class); | 392 | public void onFinish(List<ImageInfo> selectImage) { |
| 396 | for (ImageInfo imageInfo : selectImage) { | 393 | if (selectImage.size() == photoList.size()) { |
| 397 | strings.add(imageInfo.getPath()); | 394 | ArrayList<String> strings = new ArrayList<>(); |
| 398 | } | 395 | Intent intent = new Intent(getActivity(), ImageActivity.class); |
| 399 | intent.putStringArrayListExtra("images", strings); | 396 | for (ImageInfo imageInfo : selectImage) { |
| 400 | intent.putExtra("type", type); | 397 | strings.add(imageInfo.getPath()); |
| 401 | startActivity(intent); | 398 | } |
| 402 | } | 399 | intent.putStringArrayListExtra("images", strings); |
| 403 | } | 400 | intent.putExtra("type", type); |
| 404 | 401 | startActivity(intent); | |
| 405 | @Override | 402 | } |
| 406 | public void onError() { | 403 | } |
| 407 | Log.i(TAG, "onError: 出错"); | 404 | |
| 408 | } | 405 | @Override |
| 409 | } | 406 | public void onError() { |
| 410 | 407 | Log.i(TAG, "onError: 出错"); | |
| 411 | @Override | 408 | } |
| 412 | public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { | 409 | } |
| 413 | super.onActivityResult(requestCode, resultCode, data); | 410 | |
| 414 | if (resultCode != RESULT_OK) return; | 411 | @Override |
| 415 | if (requestCode == 0xA01 || requestCode == 0xA02 && !TextUtils.isEmpty(filePath)) { | 412 | public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
| 416 | Bitmap b = BitmapUtils.compress(BitmapFactory.decodeFile(filePath)); | 413 | super.onActivityResult(requestCode, resultCode, data); |
| 417 | String path = getContext().getExternalFilesDir("images").getPath() + "/" + System.currentTimeMillis() + CommonUtil.getStr() + ".jpg"; | 414 | if (resultCode != RESULT_OK) return; |
| 418 | CommonUtil.saveBitmapToUri(b, path); | 415 | if (requestCode == 0xA01 || requestCode == 0xA02 && !TextUtils.isEmpty(filePath)) { |
| 419 | ArrayList<String> strings = new ArrayList<>(); | 416 | Bitmap b = BitmapUtils.compress(BitmapFactory.decodeFile(filePath)); |
| 420 | Intent intent = new Intent(getContext(), ImageActivity.class); | 417 | String path = getContext().getExternalFilesDir("images").getPath() + "/" + System.currentTimeMillis() + CommonUtil.getStr() + ".jpg"; |
| 421 | strings.add(path); | 418 | CommonUtil.saveBitmapToUri(b, path); |
| 422 | intent.putStringArrayListExtra("images", strings); | 419 | ArrayList<String> strings = new ArrayList<>(); |
| 423 | intent.putExtra("type", requestCode == 0xA01 ? 0 : 1); | 420 | Intent intent = new Intent(getContext(), ImageActivity.class); |
| 424 | startActivity(intent); | 421 | strings.add(path); |
| 425 | } | 422 | intent.putStringArrayListExtra("images", strings); |
| 426 | } | 423 | intent.putExtra("type", requestCode == 0xA01 ? 0 : 1); |
| 427 | } | 424 | startActivity(intent); |
| 428 | 425 | } |
libs/common/src/main/java/com/prws/common/net/PersistentCookieStore.java
| 1 | package com.prws.common.net; | 1 | package com.prws.common.net; |
| 2 | 2 | ||
| 3 | import android.content.Context; | 3 | import android.content.Context; |
| 4 | import android.content.SharedPreferences; | 4 | import android.content.SharedPreferences; |
| 5 | import android.text.TextUtils; | 5 | import android.text.TextUtils; |
| 6 | import android.util.Log; | 6 | import android.util.Log; |
| 7 | 7 | ||
| 8 | import java.io.ByteArrayInputStream; | 8 | import java.io.ByteArrayInputStream; |
| 9 | import java.io.ByteArrayOutputStream; | 9 | import java.io.ByteArrayOutputStream; |
| 10 | import java.io.IOException; | 10 | import java.io.IOException; |
| 11 | import java.io.ObjectInputStream; | 11 | import java.io.ObjectInputStream; |
| 12 | import java.io.ObjectOutputStream; | 12 | import java.io.ObjectOutputStream; |
| 13 | import java.util.ArrayList; | 13 | import java.util.ArrayList; |
| 14 | import java.util.HashMap; | 14 | import java.util.HashMap; |
| 15 | import java.util.List; | 15 | import java.util.List; |
| 16 | import java.util.Locale; | 16 | import java.util.Locale; |
| 17 | import java.util.Map; | 17 | import java.util.Map; |
| 18 | import java.util.concurrent.ConcurrentHashMap; | 18 | import java.util.concurrent.ConcurrentHashMap; |
| 19 | 19 | ||
| 20 | import okhttp3.Cookie; | 20 | import okhttp3.Cookie; |
| 21 | import okhttp3.HttpUrl; | 21 | import okhttp3.HttpUrl; |
| 22 | 22 | ||
| 23 | /** | 23 | /** |
| 24 | * 持久化cookies | 24 | * 持久化cookies |
| 25 | * Created by Administrator on 2016-5-9. | 25 | * Created by Administrator on 2016-5-9. |
| 26 | */ | 26 | */ |
| 27 | public class PersistentCookieStore { | 27 | public class PersistentCookieStore { |
| 28 | private static final String COOKIE_PREFS = "Cookies_Prefs"; | 28 | private static final String COOKIE_PREFS = "Cookies_Prefs"; |
| 29 | private static final String LOG_TAG = "PersistentCookieStore"; | 29 | private static final String LOG_TAG = "PersistentCookieStore"; |
| 30 | private final SharedPreferences cookiePrefs; | 30 | private final SharedPreferences cookiePrefs; |
| 31 | private final HashMap<String, ConcurrentHashMap<String, Cookie>> cookies; | 31 | private final HashMap<String, ConcurrentHashMap<String, Cookie>> cookies; |
| 32 | 32 | ||
| 33 | public PersistentCookieStore(Context context) { | 33 | public PersistentCookieStore(Context context) { |
| 34 | cookiePrefs = context.getSharedPreferences(COOKIE_PREFS, 0); | 34 | cookiePrefs = context.getSharedPreferences(COOKIE_PREFS, 0); |
| 35 | cookies = new HashMap<>(); | 35 | cookies = new HashMap<>(); |
| 36 | 36 | ||
| 37 | //将持久化的cookies缓存到内存中,即map中 | 37 | //将持久化的cookies缓存到内存中,即map中 |
| 38 | Map<String, ?> prefsMap = cookiePrefs.getAll(); | 38 | Map<String, ?> prefsMap = cookiePrefs.getAll(); |
| 39 | for (Map.Entry<String, ?> entry : prefsMap.entrySet()) { | 39 | for (Map.Entry<String, ?> entry : prefsMap.entrySet()) { |
| 40 | String[] cookieNames = TextUtils.split((String) entry.getValue(), ","); | 40 | String[] cookieNames = TextUtils.split((String) entry.getValue(), ","); |
| 41 | 41 | ||
| 42 | for (String name : cookieNames) { | 42 | for (String name : cookieNames) { |
| 43 | String encodedCookie = cookiePrefs.getString(name, null); | 43 | String encodedCookie = cookiePrefs.getString(name, null); |
| 44 | 44 | ||
| 45 | if (!TextUtils.isEmpty(encodedCookie)) { | 45 | if (!TextUtils.isEmpty(encodedCookie)) { |
| 46 | Cookie decodeCookie = decodeCookie(encodedCookie); | 46 | Cookie decodeCookie = decodeCookie(encodedCookie); |
| 47 | if (null != decodeCookie) { | 47 | if (null != decodeCookie) { |
| 48 | cookies.put(entry.getKey(), new ConcurrentHashMap<String, Cookie>()); | 48 | cookies.put(entry.getKey(), new ConcurrentHashMap<String, Cookie>()); |
| 49 | } | 49 | } |
| 50 | cookies.get(entry.getKey()).put(name, decodeCookie); | 50 | cookies.get(entry.getKey()).put(name, decodeCookie); |
| 51 | } | 51 | } |
| 52 | } | 52 | } |
| 53 | } | 53 | } |
| 54 | 54 | ||
| 55 | 55 | ||
| 56 | } | 56 | } |
| 57 | 57 | ||
| 58 | 58 | ||
| 59 | /** | 59 | /** |
| 60 | * 获取cookieToken | 60 | * 获取cookieToken |
| 61 | * | 61 | * |
| 62 | * @param cookie | 62 | * @param cookie |
| 63 | * @return | 63 | * @return |
| 64 | */ | 64 | */ |
| 65 | protected String getCookieToken(Cookie cookie) { | 65 | protected String getCookieToken(Cookie cookie) { |
| 66 | return cookie.name() + "@" + cookie.domain(); | 66 | return cookie.name() + "@" + cookie.domain(); |
| 67 | } | 67 | } |
| 68 | 68 | ||
| 69 | /** | 69 | /** |
| 70 | * 添加到持久化 | 70 | * 添加到持久化 |
| 71 | * | 71 | * |
| 72 | * @param url | 72 | * @param url |
| 73 | * @param cookie | 73 | * @param cookie |
| 74 | */ | 74 | */ |
| 75 | public void add(HttpUrl url, Cookie cookie) { | 75 | public void add(HttpUrl url, Cookie cookie) { |
| 76 | String name = getCookieToken(cookie); | 76 | if (!cookies.containsKey(url.host())) { |
| 77 | cookies.put(url.host(), new ConcurrentHashMap<String, Cookie>()); | ||
| 78 | } | ||
| 77 | 79 | ||
| 80 | String name = getCookieToken(cookie); | ||
| 78 | //将cookies缓存到内存中,如果缓存过期,就重置此cookie | 81 | //将cookies缓存到内存中,如果缓存过期,就重置此cookie |
| 79 | if (!cookie.persistent()) { | 82 | if (!cookie.persistent()) { |
| 80 | if (!cookies.containsKey(url.host())) { | ||
| 81 | cookies.put(url.host(), new ConcurrentHashMap<String, Cookie>()); | ||
| 82 | } | ||
| 83 | cookies.get(url.host()).put(name, cookie); | 83 | cookies.get(url.host()).put(name, cookie); |
| 84 | } else { | 84 | } else { |
| 85 | if (cookies.containsKey(url.host())) { | 85 | if (cookies.containsKey(url.host())) { |
| 86 | cookies.get(url.host()).remove(name); | 86 | cookies.get(url.host()).remove(name); |
| 87 | } | 87 | } |
| 88 | } | 88 | } |
| 89 | //将cookies持久化到本地 | 89 | //将cookies持久化到本地 |
| 90 | SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); | 90 | SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); |
| 91 | prefsWriter.putString(url.host(), TextUtils.join(",", cookies.get(url.host()).keySet())); | 91 | prefsWriter.putString(url.host(), TextUtils.join(",", cookies.get(url.host()).keySet())); |
| 92 | prefsWriter.putString(name, encodeCookie(new SerializableOkHttpCookies(cookie))); | 92 | prefsWriter.putString(name, encodeCookie(new SerializableOkHttpCookies(cookie))); |
| 93 | prefsWriter.apply(); | 93 | prefsWriter.apply(); |
| 94 | 94 | ||
| 95 | } | 95 | } |
| 96 | 96 | ||
| 97 | /** | 97 | /** |
| 98 | * 通过url得到存储cookie集合 | 98 | * 通过url得到存储cookie集合 |
| 99 | * | 99 | * |
| 100 | * @param url | 100 | * @param url |
| 101 | * @return | 101 | * @return |
| 102 | */ | 102 | */ |
| 103 | public List<Cookie> get(HttpUrl url) { | 103 | public List<Cookie> get(HttpUrl url) { |
| 104 | ArrayList<Cookie> ret = new ArrayList<>(); | 104 | ArrayList<Cookie> ret = new ArrayList<>(); |
| 105 | if (cookies.containsKey(url.host())) | 105 | if (cookies.containsKey(url.host())) |
| 106 | ret.addAll(cookies.get(url.host()).values()); | 106 | ret.addAll(cookies.get(url.host()).values()); |
| 107 | return ret; | 107 | return ret; |
| 108 | } | 108 | } |
| 109 | 109 | ||
| 110 | /** | 110 | /** |
| 111 | * 清除cookies本地持久化 | 111 | * 清除cookies本地持久化 |
| 112 | * | 112 | * |
| 113 | * @return | 113 | * @return |
| 114 | */ | 114 | */ |
| 115 | public boolean removeAll() { | 115 | public boolean removeAll() { |
| 116 | SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); | 116 | SharedPreferences.Editor prefsWriter = cookiePrefs.edit(); |
| 117 | prefsWriter.clear(); | 117 | prefsWriter.clear(); |
| 118 | prefsWriter.apply(); | 118 | prefsWriter.apply(); |
| 119 | cookies.clear(); | 119 | cookies.clear(); |
| 120 | return true; | 120 | return true; |
| 121 | } | 121 | } |
| 122 | 122 | ||
| 123 | /** | 123 | /** |
| 124 | * 获取cookie集合 | 124 | * 获取cookie集合 |
| 125 | * | 125 | * |
| 126 | * @return | 126 | * @return |
| 127 | */ | 127 | */ |
| 128 | public List<Cookie> getCookies() { | 128 | public List<Cookie> getCookies() { |
| 129 | ArrayList<Cookie> ret = new ArrayList<>(); | 129 | ArrayList<Cookie> ret = new ArrayList<>(); |
| 130 | for (String key : cookies.keySet()) | 130 | for (String key : cookies.keySet()) |
| 131 | ret.addAll(cookies.get(key).values()); | 131 | ret.addAll(cookies.get(key).values()); |
| 132 | 132 | ||
| 133 | return ret; | 133 | return ret; |
| 134 | } | 134 | } |
| 135 | 135 | ||
| 136 | /** | 136 | /** |
| 137 | * cookies 序列化成 string | 137 | * cookies 序列化成 string |
| 138 | * | 138 | * |
| 139 | * @param cookie 要序列化的cookie | 139 | * @param cookie 要序列化的cookie |
| 140 | * @return 序列化之后的string | 140 | * @return 序列化之后的string |
| 141 | */ | 141 | */ |
| 142 | protected String encodeCookie(SerializableOkHttpCookies cookie) { | 142 | protected String encodeCookie(SerializableOkHttpCookies cookie) { |
| 143 | if (cookie == null) | 143 | if (cookie == null) |
| 144 | return null; | 144 | return null; |
| 145 | ByteArrayOutputStream os = new ByteArrayOutputStream(); | 145 | ByteArrayOutputStream os = new ByteArrayOutputStream(); |
| 146 | try { | 146 | try { |
| 147 | ObjectOutputStream outputStream = new ObjectOutputStream(os); | 147 | ObjectOutputStream outputStream = new ObjectOutputStream(os); |
| 148 | outputStream.writeObject(cookie); | 148 | outputStream.writeObject(cookie); |
| 149 | } catch (IOException e) { | 149 | } catch (IOException e) { |
| 150 | Log.d(LOG_TAG, "IOException in encodeCookie", e); | 150 | Log.d(LOG_TAG, "IOException in encodeCookie", e); |
| 151 | return null; | 151 | return null; |
| 152 | } | 152 | } |
| 153 | 153 | ||
| 154 | return byteArrayToHexString(os.toByteArray()); | 154 | return byteArrayToHexString(os.toByteArray()); |
| 155 | } | 155 | } |
| 156 | 156 | ||
| 157 | /** | 157 | /** |
| 158 | * 将字符串反序列化成cookies | 158 | * 将字符串反序列化成cookies |
| 159 | * | 159 | * |
| 160 | * @param cookieString cookies string | 160 | * @param cookieString cookies string |
| 161 | * @return cookie object | 161 | * @return cookie object |
| 162 | */ | 162 | */ |
| 163 | protected Cookie decodeCookie(String cookieString) { | 163 | protected Cookie decodeCookie(String cookieString) { |
| 164 | byte[] bytes = hexStringToByteArray(cookieString); | 164 | byte[] bytes = hexStringToByteArray(cookieString); |
| 165 | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); | 165 | ByteArrayInputStream byteArrayInputStream = new ByteArrayInputStream(bytes); |
| 166 | Cookie cookie = null; | 166 | Cookie cookie = null; |
| 167 | try { | 167 | try { |
| 168 | ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); | 168 | ObjectInputStream objectInputStream = new ObjectInputStream(byteArrayInputStream); |
| 169 | cookie = ((SerializableOkHttpCookies) objectInputStream.readObject()).getCookies(); | 169 | cookie = ((SerializableOkHttpCookies) objectInputStream.readObject()).getCookies(); |
| 170 | } catch (IOException e) { | 170 | } catch (IOException e) { |
| 171 | Log.d(LOG_TAG, "IOException in decodeCookie", e); | 171 | Log.d(LOG_TAG, "IOException in decodeCookie", e); |
| 172 | } catch (ClassNotFoundException e) { | 172 | } catch (ClassNotFoundException e) { |
| 173 | Log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e); | 173 | Log.d(LOG_TAG, "ClassNotFoundException in decodeCookie", e); |
| 174 | } | 174 | } |
| 175 | 175 | ||
| 176 | return cookie; | 176 | return cookie; |
| 177 | } | 177 | } |
| 178 | 178 | ||
| 179 | /** | 179 | /** |
| 180 | * 二进制数组转十六进制字符串 | 180 | * 二进制数组转十六进制字符串 |
| 181 | * | 181 | * |
| 182 | * @param bytes byte array to be converted | 182 | * @param bytes byte array to be converted |
| 183 | * @return string containing hex values | 183 | * @return string containing hex values |
| 184 | */ | 184 | */ |
| 185 | protected String byteArrayToHexString(byte[] bytes) { | 185 | protected String byteArrayToHexString(byte[] bytes) { |
| 186 | StringBuilder sb = new StringBuilder(bytes.length * 2); | 186 | StringBuilder sb = new StringBuilder(bytes.length * 2); |
| 187 | for (byte element : bytes) { | 187 | for (byte element : bytes) { |
| 188 | int v = element & 0xff; | 188 | int v = element & 0xff; |
| 189 | if (v < 16) { | 189 | if (v < 16) { |
| 190 | sb.append('0'); | 190 | sb.append('0'); |
| 191 | } | 191 | } |
| 192 | sb.append(Integer.toHexString(v)); | 192 | sb.append(Integer.toHexString(v)); |
| 193 | } | 193 | } |
| 194 | return sb.toString().toUpperCase(Locale.US); | 194 | return sb.toString().toUpperCase(Locale.US); |
| 195 | } | 195 | } |
| 196 | 196 | ||
| 197 | /** | 197 | /** |
| 198 | * 十六进制字符串转二进制数组 | 198 | * 十六进制字符串转二进制数组 |
| 199 | * | 199 | * |
| 200 | * @param hexString string of hex-encoded values | 200 | * @param hexString string of hex-encoded values |
| 201 | * @return decoded byte array | 201 | * @return decoded byte array |
| 202 | */ | 202 | */ |
| 203 | protected byte[] hexStringToByteArray(String hexString) { | 203 | protected byte[] hexStringToByteArray(String hexString) { |
| 204 | int len = hexString.length(); | 204 | int len = hexString.length(); |
| 205 | byte[] data = new byte[len / 2]; | 205 | byte[] data = new byte[len / 2]; |
| 206 | for (int i = 0; i < len; i += 2) { | 206 | for (int i = 0; i < len; i += 2) { |
| 207 | data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16)); | 207 | data[i / 2] = (byte) ((Character.digit(hexString.charAt(i), 16) << 4) + Character.digit(hexString.charAt(i + 1), 16)); |
| 208 | } | 208 | } |
| 209 | return data; | 209 | return data; |
| 210 | } | 210 | } |