Commit d8c0c1a275c26a587d496379a0a725ccfb05aa3a
1 parent
ca1e944251
Exists in
master
bugfix
Showing
2 changed files
with
5 additions
and
4 deletions
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 |
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 | } |