1/** 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16import common from '../data/commonData'; 17import LooseObject from '../data/LooseObject'; 18import HiLog from './HiLog'; 19 20import createOrGet from './SingleInstanceUtils'; 21import preferences from '@ohos.data.preferences'; 22 23const TAG = 'MmsPreferences'; 24 25/** 26 * Obtaining a Lightweight Preference Database Instance 27 */ 28class MmsPreferences { 29 private static sInstance: MmsPreferences; 30 private static sMap = new Map<string, string | number | boolean>(); 31 private preferences: preferences.Preferences; 32 private static readonly PREFERENCES_Mms_FORM_STORE = 'MmsStore' 33 34 getInstance(): MmsPreferences { 35 if (MmsPreferences.sInstance == null) { 36 MmsPreferences.sInstance = new MmsPreferences(); 37 } 38 return MmsPreferences.sInstance; 39 } 40 41 public async initPreferences(): Promise<void> { 42 if (this.preferences != null) { 43 return; 44 } 45 46 try { 47 this.preferences = await preferences.getPreferences(globalThis.mmsContext, 48 MmsPreferences.PREFERENCES_Mms_FORM_STORE); 49 this.initMapData(); 50 } catch (err) { 51 HiLog.i(TAG, `initPreferences err ${err}`); 52 } 53 } 54 55 /** 56 * Init data to map 57 */ 58 initMapData(): void { 59 this.asyncGetValueFromPreferences(common.string.KEY_OF_INTEGRATION_SWITCH); 60 this.asyncGetValueFromPreferences(common.string.KEY_OF_MALICIOUS_WEB_SWITCH); 61 this.asyncGetValueFromPreferences(common.string.KEY_OF_SHOW_CONTACT_SWITCH); 62 this.asyncGetValueFromPreferences(common.string.KEY_OF_DELIVERY_REPORT_SWITCH); 63 this.asyncGetValueFromPreferences(common.string.KEY_OF_AUTO_RETRIEVE_SWITCH); 64 this.asyncGetValueFromPreferences(common.string.KEY_OF_RECALL_MESSAGE_SWITCH); 65 this.asyncGetValueFromPreferences(common.string.KEY_OF_AUTO_DELETE_INFO_SWITCH); 66 } 67 68 /** 69 * Get value from preferences 70 */ 71 async getValueFromPreferences(key: string) { 72 if (this.preferences == null) { 73 HiLog.i(TAG, `getValueFromPreferences preferences is null`); 74 return common.string.EMPTY_STR; 75 } 76 try { 77 let value = await this.preferences.get(key, common.string.EMPTY_STR); 78 this.setValueToMap(key, value.toString()); 79 return value; 80 } catch (err) { 81 HiLog.i(TAG, 'getValueFromPreferences(' + key + ') failed with err: ' + JSON.stringify(err)) 82 return common.string.EMPTY_STR; 83 } 84 } 85 86 /** 87 * Async get value from preferences 88 */ 89 async asyncGetValueFromPreferences(key: string): Promise<string> { 90 if (this.preferences == null) { 91 HiLog.i(TAG, `asyncGetValueByKeyFromPreferences preferences is null`); 92 return common.string.EMPTY_STR; 93 } 94 let getPromise = this.preferences.get(key, common.string.EMPTY_STR); 95 getPromise.then((value) => { 96 this.setValueToMap(key, value.toString()); 97 return value; 98 }).catch((err) => { 99 HiLog.i(TAG, 'asyncGetValueFromPreferences(' + key + ') failed with err: ' + JSON.stringify(err)) 100 return common.string.EMPTY_STR; 101 }); 102 } 103 104 /** 105 * Obtains the value of the notification integration switch. 106 */ 107 public getValueOfIntegrationSwitch(): string { 108 return <string> this.getValueFromMap(common.string.KEY_OF_INTEGRATION_SWITCH, common.bool.TRUE); 109 } 110 111 /** 112 * Obtains the value of the malicious URL identification switch. 113 */ 114 public getValueOfMaliciousWebSwitch(): string { 115 return <string> this.getValueFromMap(common.string.KEY_OF_MALICIOUS_WEB_SWITCH, common.bool.FALSE); 116 } 117 118 /** 119 * Obtains the value of the switch for displaying contact avatars. 120 */ 121 public getValueOfShowContactSwitch(): string { 122 return <string> this.getValueFromMap(common.string.KEY_OF_SHOW_CONTACT_SWITCH, common.bool.TRUE); 123 } 124 125 /** 126 * Obtains the value of the delivery report switch. 127 */ 128 public getValueOfDeliveryReportSwitch(): string { 129 return <string> this.getValueFromMap(common.string.KEY_OF_DELIVERY_REPORT_SWITCH, common.DELIVERY_REPORTS.DISABLED); 130 } 131 132 /** 133 * Obtains the value of the function of automatically downloading MMS messages. 134 */ 135 public getValueOfAutoRetrieveMmsSwitch(): string { 136 return <string> this.getValueFromMap(common.string.KEY_OF_AUTO_RETRIEVE_SWITCH, 137 common.AUTO_RETRIEVE_MMS.NOT_WHEN_ROAMING); 138 } 139 140 /** 141 * Obtains the value of the send cancel switch. 142 */ 143 public getValueOfRecallMessageSwitch(): string { 144 return <string> this.getValueFromMap(common.string.KEY_OF_RECALL_MESSAGE_SWITCH, common.bool.FALSE); 145 } 146 147 /** 148 * Obtains the value of the automatic deletion notification switch. 149 */ 150 public getValueOfAutoDeleteInfoSwitch(): string { 151 return <string> this.getValueFromMap(common.string.KEY_OF_AUTO_DELETE_INFO_SWITCH, common.bool.FALSE); 152 } 153 154 public isMultiSimCardEnabled(): boolean { 155 if (this.getMaxSimCount() === common.int.SIM_COUNT) { 156 return true; 157 } 158 return false; 159 } 160 161 public getMaxSimCount(): number { 162 return <number> this.getValueFromMap(common.string.KEY_OF_MAX_SIM_COUNT, common.int.SIM_ONE); 163 } 164 165 public haveMultiSimCardReady(): boolean { 166 return <boolean> this.getValueFromMap(common.string.KEY_OF_HAVE_MULTI_SIM_CARD_READY, false); 167 } 168 169 public haveSimCardReady(): boolean { 170 return <boolean> this.getValueFromMap(common.string.KEY_OF_HAVE_SIM_CARD_READY, false); 171 } 172 173 public getSpnOfSim1(): string { 174 return <string> this.getValueFromMap(common.string.KEY_OF_SIM_0_SPN, common.string.EMPTY_STR); 175 } 176 177 public getSpnOfSim2(): string { 178 return <string> this.getValueFromMap(common.string.KEY_OF_SIM_1_SPN, common.string.EMPTY_STR); 179 } 180 181 public getNewSmscOfSim1(): string { 182 return <string> this.getValueFromMap(common.string.KEY_OF_NEW_SIM_0_SMSC, common.string.EMPTY_STR); 183 } 184 185 public getNewSmscOfSim2(): string { 186 return <string> this.getValueFromMap(common.string.KEY_OF_NEW_SIM_1_SMSC, common.string.EMPTY_STR); 187 } 188 189 public getTelephoneNumberOfSim1(): string { 190 return <string> this.getValueFromMap(common.string.KEY_OF_SIM_0_NUMBER, common.string.EMPTY_STR); 191 } 192 193 public getTelephoneNumberOfSim2(): string { 194 return <string> this.getValueFromMap(common.string.KEY_OF_SIM_1_NUMBER, common.string.EMPTY_STR); 195 } 196 197 public getSelectedSlotId(): number { 198 return <number> this.getValueFromMap(common.string.KEY_OF_SELECTED_SLOTID, common.int.SIM_ONE); 199 } 200 201 public getDefaultSlotId(): number { 202 return <number> this.getValueFromMap(common.string.KEY_OF_DEFAULT_SLOT, common.int.SIM_ONE); 203 } 204 205 public getSendMessageSlotId(): number { 206 let sendMsgSlotId: number = common.int.SIM_ONE; 207 if (this.haveMultiSimCardReady()) { 208 sendMsgSlotId = this.getSelectedSlotId(); 209 } else { 210 sendMsgSlotId = this.getDefaultSlotId(); 211 } 212 return sendMsgSlotId; 213 } 214 215 /** 216 * Set a value to preferences 217 * 218 * @param keyOfSwitch 219 * @param valueOfSwitch 220 */ 221 public setValueForSwitch(keyOfSwitch: string, valueOfSwitch: string): void { 222 this.setValueToMap(keyOfSwitch, valueOfSwitch); 223 if (this.preferences == null) { 224 HiLog.i(TAG, `setValueForSwitch preferences is null`); 225 return 226 } 227 let putPromise = this.preferences.put(keyOfSwitch, valueOfSwitch); 228 putPromise.then(() => { 229 this.preferences.flush(); 230 }).catch((err) => { 231 HiLog.i(TAG, `setValueForSwitch failed with err: ${err}`); 232 }); 233 } 234 235 /** 236 * Get value from map by key 237 * 238 * @param key 239 * @param defaultValue 240 */ 241 public getValueFromMap(key: string, defaultValue: string | number | boolean): 242 string | number | boolean { 243 let value = MmsPreferences.sMap.get(key); 244 if (value == null) { 245 value = defaultValue; 246 } 247 return value; 248 } 249 250 /** 251 * Set key-value to map 252 * 253 * @param key 254 * @param value 255 */ 256 public setValueToMap(key: string, value: string | number | boolean): void { 257 MmsPreferences.sMap.set(key, value); 258 } 259} 260 261let dataStore = createOrGet(MmsPreferences, TAG); 262 263export default dataStore as MmsPreferences;