|
| 1 | +package io.jchat.android.utils; |
| 2 | + |
| 3 | +import android.content.Context; |
| 4 | + |
| 5 | +import com.google.gson.Gson; |
| 6 | + |
| 7 | +import java.io.File; |
| 8 | +import java.io.FileInputStream; |
| 9 | +import java.io.FileOutputStream; |
| 10 | +import java.util.HashMap; |
| 11 | +import java.util.List; |
| 12 | + |
| 13 | +import cn.jpush.im.android.api.event.GroupApprovalEvent; |
| 14 | + |
| 15 | + |
| 16 | +public class EventUtils { |
| 17 | + |
| 18 | + private static final String TAG = EventUtils.class.getSimpleName(); |
| 19 | + |
| 20 | + private static Gson gson = new Gson(); |
| 21 | + |
| 22 | + private static final String GROUP_APPROVAL_EVENT_NAME = "/jpush_event/group_approval/"; |
| 23 | + private static HashMap<String, GroupApprovalEvent> events = new HashMap<>(); |
| 24 | + |
| 25 | + |
| 26 | + public static void saveGroupApprovalEvent(Context context, GroupApprovalEvent event) { |
| 27 | + events.put(event.getEventId() + "", event); |
| 28 | + String s = gson.toJson(event); |
| 29 | + Logger.i(TAG, "Save group approval event:" + s); |
| 30 | + FileOutputStream outputStream; |
| 31 | + try { |
| 32 | +// outputStream = context.openFileOutput(GROUP_APPROVAL_EVENT_NAME+event.getEventId(), Context.MODE_PRIVATE); |
| 33 | + File file = new File(context.getFilesDir() + GROUP_APPROVAL_EVENT_NAME, event.getEventId() + ""); |
| 34 | + if (!file.exists()) { |
| 35 | + file.getParentFile().getParentFile().mkdir(); |
| 36 | + file.getParentFile().mkdir(); |
| 37 | + file.createNewFile(); |
| 38 | + } |
| 39 | + outputStream = new FileOutputStream(file); |
| 40 | + outputStream.write(s.getBytes()); |
| 41 | + outputStream.close(); |
| 42 | + } catch (Exception e) { |
| 43 | + e.printStackTrace(); |
| 44 | + } |
| 45 | + |
| 46 | + } |
| 47 | + |
| 48 | + public static GroupApprovalEvent getGroupApprovalEvent(Context context, String eventId) { |
| 49 | + GroupApprovalEvent groupApprovalEvent = events.get(eventId); |
| 50 | + if (groupApprovalEvent != null) { |
| 51 | + return groupApprovalEvent; |
| 52 | + } |
| 53 | + |
| 54 | + |
| 55 | + FileInputStream inputStream; |
| 56 | + GroupApprovalEvent event = null; |
| 57 | + try { |
| 58 | +// inputStream = context.openFileInput(GROUP_APPROVAL_EVENT_NAME+eventId); |
| 59 | + File file = new File(context.getFilesDir() + GROUP_APPROVAL_EVENT_NAME, eventId); |
| 60 | + inputStream = new FileInputStream(file); |
| 61 | + byte[] bytes = new byte[inputStream.available()]; |
| 62 | + StringBuffer sb = new StringBuffer(); |
| 63 | + int len = -1; |
| 64 | + while ((len = inputStream.read(bytes)) != -1) { |
| 65 | + sb.append(new String(bytes, 0, len)); |
| 66 | + } |
| 67 | + Logger.i(TAG, "Get group approval event:" + sb.toString()); |
| 68 | + inputStream.close(); |
| 69 | + event = gson.fromJson(sb.toString(), GroupApprovalEvent.class); |
| 70 | + |
| 71 | + } catch (Exception e) { |
| 72 | + e.printStackTrace(); |
| 73 | + } |
| 74 | + return event; |
| 75 | + } |
| 76 | + |
| 77 | + public static void removeGroupApprovalEvent(Context context, String eventId) { |
| 78 | + events.remove(eventId); |
| 79 | + File file = new File(context.getFilesDir() + GROUP_APPROVAL_EVENT_NAME, eventId); |
| 80 | + if (file.exists()) { |
| 81 | + Boolean isSucc = file.delete(); |
| 82 | + Logger.i(TAG, "Delete group approval event " + eventId + ": " + isSucc); |
| 83 | + } |
| 84 | + } |
| 85 | + |
| 86 | + public static void removeGroupApprovalEvents(Context context, List<GroupApprovalEvent> groupApprovalEventList) { |
| 87 | + for (int i = 0; i < groupApprovalEventList.size(); i++) { |
| 88 | + GroupApprovalEvent groupApprovalEvent = groupApprovalEventList.get(i); |
| 89 | + removeGroupApprovalEvent(context, groupApprovalEvent.getEventId() + ""); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + |
| 94 | +} |
0 commit comments