ex
Fork of mbed-os-example-mbed5-blinky by
dcs-sdk-java-master/app/src/main/java/com/baidu/duer/dcs/androidsystemimpl/alert/AlertsFileDataStoreImpl.java@45:2aa9f933c8d2, 2017-07-18 (annotated)
- Committer:
- TMBOY
- Date:
- Tue Jul 18 16:34:48 2017 +0800
- Revision:
- 45:2aa9f933c8d2
?
Who changed what in which revision?
| User | Revision | Line number | New contents of line |
|---|---|---|---|
| TMBOY | 45:2aa9f933c8d2 | 1 | /* |
| TMBOY | 45:2aa9f933c8d2 | 2 | * Copyright (c) 2017 Baidu, Inc. All Rights Reserved. |
| TMBOY | 45:2aa9f933c8d2 | 3 | * |
| TMBOY | 45:2aa9f933c8d2 | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| TMBOY | 45:2aa9f933c8d2 | 5 | * you may not use this file except in compliance with the License. |
| TMBOY | 45:2aa9f933c8d2 | 6 | * You may obtain a copy of the License at |
| TMBOY | 45:2aa9f933c8d2 | 7 | * |
| TMBOY | 45:2aa9f933c8d2 | 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| TMBOY | 45:2aa9f933c8d2 | 9 | * |
| TMBOY | 45:2aa9f933c8d2 | 10 | * Unless required by applicable law or agreed to in writing, software |
| TMBOY | 45:2aa9f933c8d2 | 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| TMBOY | 45:2aa9f933c8d2 | 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| TMBOY | 45:2aa9f933c8d2 | 13 | * See the License for the specific language governing permissions and |
| TMBOY | 45:2aa9f933c8d2 | 14 | * limitations under the License. |
| TMBOY | 45:2aa9f933c8d2 | 15 | */ |
| TMBOY | 45:2aa9f933c8d2 | 16 | package com.baidu.duer.dcs.androidsystemimpl.alert; |
| TMBOY | 45:2aa9f933c8d2 | 17 | |
| TMBOY | 45:2aa9f933c8d2 | 18 | import android.os.Handler; |
| TMBOY | 45:2aa9f933c8d2 | 19 | |
| TMBOY | 45:2aa9f933c8d2 | 20 | import com.baidu.duer.dcs.devicemodule.alerts.message.Alert; |
| TMBOY | 45:2aa9f933c8d2 | 21 | import com.baidu.duer.dcs.systeminterface.IAlertsDataStore; |
| TMBOY | 45:2aa9f933c8d2 | 22 | import com.baidu.duer.dcs.util.CommonUtil; |
| TMBOY | 45:2aa9f933c8d2 | 23 | import com.baidu.duer.dcs.util.FileUtil; |
| TMBOY | 45:2aa9f933c8d2 | 24 | import com.baidu.duer.dcs.util.LogUtil; |
| TMBOY | 45:2aa9f933c8d2 | 25 | import com.baidu.duer.dcs.util.ObjectMapperUtil; |
| TMBOY | 45:2aa9f933c8d2 | 26 | |
| TMBOY | 45:2aa9f933c8d2 | 27 | import org.codehaus.jackson.map.ObjectReader; |
| TMBOY | 45:2aa9f933c8d2 | 28 | import org.codehaus.jackson.map.ObjectWriter; |
| TMBOY | 45:2aa9f933c8d2 | 29 | import org.codehaus.jackson.type.TypeReference; |
| TMBOY | 45:2aa9f933c8d2 | 30 | |
| TMBOY | 45:2aa9f933c8d2 | 31 | import java.io.BufferedReader; |
| TMBOY | 45:2aa9f933c8d2 | 32 | import java.io.File; |
| TMBOY | 45:2aa9f933c8d2 | 33 | import java.io.FileNotFoundException; |
| TMBOY | 45:2aa9f933c8d2 | 34 | import java.io.FileReader; |
| TMBOY | 45:2aa9f933c8d2 | 35 | import java.io.IOException; |
| TMBOY | 45:2aa9f933c8d2 | 36 | import java.io.PrintWriter; |
| TMBOY | 45:2aa9f933c8d2 | 37 | import java.util.List; |
| TMBOY | 45:2aa9f933c8d2 | 38 | import java.util.concurrent.ExecutorService; |
| TMBOY | 45:2aa9f933c8d2 | 39 | import java.util.concurrent.Executors; |
| TMBOY | 45:2aa9f933c8d2 | 40 | |
| TMBOY | 45:2aa9f933c8d2 | 41 | /** |
| TMBOY | 45:2aa9f933c8d2 | 42 | * 持久化alert数据到文件-android中实现 |
| TMBOY | 45:2aa9f933c8d2 | 43 | * <p> |
| TMBOY | 45:2aa9f933c8d2 | 44 | * Created by guxiuzhong@baidu.com on 2017/5/18. |
| TMBOY | 45:2aa9f933c8d2 | 45 | */ |
| TMBOY | 45:2aa9f933c8d2 | 46 | public class AlertsFileDataStoreImpl implements IAlertsDataStore { |
| TMBOY | 45:2aa9f933c8d2 | 47 | private static final String TAG = AlertsFileDataStoreImpl.class.getSimpleName(); |
| TMBOY | 45:2aa9f933c8d2 | 48 | private static final ExecutorService mExecutor = Executors.newSingleThreadExecutor(); |
| TMBOY | 45:2aa9f933c8d2 | 49 | private Handler mHandler = new Handler(); |
| TMBOY | 45:2aa9f933c8d2 | 50 | |
| TMBOY | 45:2aa9f933c8d2 | 51 | public AlertsFileDataStoreImpl() { |
| TMBOY | 45:2aa9f933c8d2 | 52 | } |
| TMBOY | 45:2aa9f933c8d2 | 53 | |
| TMBOY | 45:2aa9f933c8d2 | 54 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 55 | public void readFromDisk(final IAlertsDataStore.ReadResultListener listener) { |
| TMBOY | 45:2aa9f933c8d2 | 56 | mExecutor.execute(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 57 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 58 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 59 | FileReader fis; |
| TMBOY | 45:2aa9f933c8d2 | 60 | BufferedReader br = null; |
| TMBOY | 45:2aa9f933c8d2 | 61 | ObjectReader reader = ObjectMapperUtil.instance() |
| TMBOY | 45:2aa9f933c8d2 | 62 | .getObjectReader() |
| TMBOY | 45:2aa9f933c8d2 | 63 | .withType(new TypeReference<List<Alert>>() { |
| TMBOY | 45:2aa9f933c8d2 | 64 | }); |
| TMBOY | 45:2aa9f933c8d2 | 65 | try { |
| TMBOY | 45:2aa9f933c8d2 | 66 | File file = FileUtil.getAlarmFile(); |
| TMBOY | 45:2aa9f933c8d2 | 67 | if (file == null) { |
| TMBOY | 45:2aa9f933c8d2 | 68 | postReadFailed(listener, "create file failed file is null"); |
| TMBOY | 45:2aa9f933c8d2 | 69 | return; |
| TMBOY | 45:2aa9f933c8d2 | 70 | } |
| TMBOY | 45:2aa9f933c8d2 | 71 | fis = new FileReader(file); |
| TMBOY | 45:2aa9f933c8d2 | 72 | br = new BufferedReader(fis); |
| TMBOY | 45:2aa9f933c8d2 | 73 | List<Alert> alerts = reader.readValue(br); |
| TMBOY | 45:2aa9f933c8d2 | 74 | postReadSucceed(listener, alerts); |
| TMBOY | 45:2aa9f933c8d2 | 75 | } catch (final FileNotFoundException e) { |
| TMBOY | 45:2aa9f933c8d2 | 76 | postReadFailed(listener, "Failed to load alerts from disk."); |
| TMBOY | 45:2aa9f933c8d2 | 77 | } catch (IOException e) { |
| TMBOY | 45:2aa9f933c8d2 | 78 | postReadFailed(listener, e.getMessage()); |
| TMBOY | 45:2aa9f933c8d2 | 79 | } finally { |
| TMBOY | 45:2aa9f933c8d2 | 80 | CommonUtil.closeQuietly(br); |
| TMBOY | 45:2aa9f933c8d2 | 81 | } |
| TMBOY | 45:2aa9f933c8d2 | 82 | } |
| TMBOY | 45:2aa9f933c8d2 | 83 | }); |
| TMBOY | 45:2aa9f933c8d2 | 84 | } |
| TMBOY | 45:2aa9f933c8d2 | 85 | |
| TMBOY | 45:2aa9f933c8d2 | 86 | private void postReadFailed(final IAlertsDataStore.ReadResultListener listener, final String errorMessage) { |
| TMBOY | 45:2aa9f933c8d2 | 87 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 88 | mHandler.post(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 89 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 90 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 91 | listener.onFailed(errorMessage); |
| TMBOY | 45:2aa9f933c8d2 | 92 | } |
| TMBOY | 45:2aa9f933c8d2 | 93 | }); |
| TMBOY | 45:2aa9f933c8d2 | 94 | } |
| TMBOY | 45:2aa9f933c8d2 | 95 | } |
| TMBOY | 45:2aa9f933c8d2 | 96 | |
| TMBOY | 45:2aa9f933c8d2 | 97 | private void postReadSucceed(final IAlertsDataStore.ReadResultListener listener, final List<Alert> alerts) { |
| TMBOY | 45:2aa9f933c8d2 | 98 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 99 | mHandler.post(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 100 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 101 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 102 | listener.onSucceed(alerts); |
| TMBOY | 45:2aa9f933c8d2 | 103 | } |
| TMBOY | 45:2aa9f933c8d2 | 104 | }); |
| TMBOY | 45:2aa9f933c8d2 | 105 | } |
| TMBOY | 45:2aa9f933c8d2 | 106 | } |
| TMBOY | 45:2aa9f933c8d2 | 107 | |
| TMBOY | 45:2aa9f933c8d2 | 108 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 109 | public void writeToDisk(final List<Alert> alerts, final IAlertsDataStore.WriteResultListener listener) { |
| TMBOY | 45:2aa9f933c8d2 | 110 | mExecutor.execute(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 111 | |
| TMBOY | 45:2aa9f933c8d2 | 112 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 113 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 114 | ObjectWriter writer = ObjectMapperUtil.instance().getObjectWriter(); |
| TMBOY | 45:2aa9f933c8d2 | 115 | PrintWriter out = null; |
| TMBOY | 45:2aa9f933c8d2 | 116 | try { |
| TMBOY | 45:2aa9f933c8d2 | 117 | File file = FileUtil.getAlarmFile(); |
| TMBOY | 45:2aa9f933c8d2 | 118 | if (file == null) { |
| TMBOY | 45:2aa9f933c8d2 | 119 | postWriteFailed(listener, "create file failed file is null "); |
| TMBOY | 45:2aa9f933c8d2 | 120 | return; |
| TMBOY | 45:2aa9f933c8d2 | 121 | } |
| TMBOY | 45:2aa9f933c8d2 | 122 | out = new PrintWriter(file); |
| TMBOY | 45:2aa9f933c8d2 | 123 | out.print(writer.writeValueAsString(alerts)); |
| TMBOY | 45:2aa9f933c8d2 | 124 | out.flush(); |
| TMBOY | 45:2aa9f933c8d2 | 125 | LogUtil.e(TAG, "start postWriteSucceed"); |
| TMBOY | 45:2aa9f933c8d2 | 126 | postWriteSucceed(listener); |
| TMBOY | 45:2aa9f933c8d2 | 127 | } catch (IOException e) { |
| TMBOY | 45:2aa9f933c8d2 | 128 | LogUtil.e(TAG, "Failed to write to disk", e); |
| TMBOY | 45:2aa9f933c8d2 | 129 | postWriteFailed(listener, "Failed to write to disk,error:" + e.getMessage()); |
| TMBOY | 45:2aa9f933c8d2 | 130 | } finally { |
| TMBOY | 45:2aa9f933c8d2 | 131 | CommonUtil.closeQuietly(out); |
| TMBOY | 45:2aa9f933c8d2 | 132 | } |
| TMBOY | 45:2aa9f933c8d2 | 133 | } |
| TMBOY | 45:2aa9f933c8d2 | 134 | }); |
| TMBOY | 45:2aa9f933c8d2 | 135 | } |
| TMBOY | 45:2aa9f933c8d2 | 136 | |
| TMBOY | 45:2aa9f933c8d2 | 137 | private void postWriteFailed(final IAlertsDataStore.WriteResultListener listener, final String errorMessage) { |
| TMBOY | 45:2aa9f933c8d2 | 138 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 139 | mHandler.post(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 140 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 141 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 142 | listener.onFailed(errorMessage); |
| TMBOY | 45:2aa9f933c8d2 | 143 | } |
| TMBOY | 45:2aa9f933c8d2 | 144 | }); |
| TMBOY | 45:2aa9f933c8d2 | 145 | } |
| TMBOY | 45:2aa9f933c8d2 | 146 | } |
| TMBOY | 45:2aa9f933c8d2 | 147 | |
| TMBOY | 45:2aa9f933c8d2 | 148 | private void postWriteSucceed(final IAlertsDataStore.WriteResultListener listener) { |
| TMBOY | 45:2aa9f933c8d2 | 149 | if (listener != null) { |
| TMBOY | 45:2aa9f933c8d2 | 150 | mHandler.post(new Runnable() { |
| TMBOY | 45:2aa9f933c8d2 | 151 | @Override |
| TMBOY | 45:2aa9f933c8d2 | 152 | public void run() { |
| TMBOY | 45:2aa9f933c8d2 | 153 | listener.onSucceed(); |
| TMBOY | 45:2aa9f933c8d2 | 154 | } |
| TMBOY | 45:2aa9f933c8d2 | 155 | }); |
| TMBOY | 45:2aa9f933c8d2 | 156 | } |
| TMBOY | 45:2aa9f933c8d2 | 157 | } |
| TMBOY | 45:2aa9f933c8d2 | 158 | } |
