1048147e0Sopenharmony_ci/** 2048147e0Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 3048147e0Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 4048147e0Sopenharmony_ci * you may not use this file except in compliance with the License. 5048147e0Sopenharmony_ci * You may obtain a copy of the License at 6048147e0Sopenharmony_ci * 7048147e0Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 8048147e0Sopenharmony_ci * 9048147e0Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 10048147e0Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 11048147e0Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12048147e0Sopenharmony_ci * See the License for the specific language governing permissions and 13048147e0Sopenharmony_ci * limitations under the License. 14048147e0Sopenharmony_ci */ 15048147e0Sopenharmony_ci 16048147e0Sopenharmony_ciimport common from '../data/commonData'; 17048147e0Sopenharmony_ciimport LooseObject from '../data/LooseObject'; 18048147e0Sopenharmony_ciimport HiLog from './HiLog'; 19048147e0Sopenharmony_ci 20048147e0Sopenharmony_ciimport createOrGet from './SingleInstanceUtils'; 21048147e0Sopenharmony_ciimport preferences from '@ohos.data.preferences'; 22048147e0Sopenharmony_ci 23048147e0Sopenharmony_ciconst TAG = 'MmsPreferences'; 24048147e0Sopenharmony_ci 25048147e0Sopenharmony_ci/** 26048147e0Sopenharmony_ci * Obtaining a Lightweight Preference Database Instance 27048147e0Sopenharmony_ci */ 28048147e0Sopenharmony_ciclass MmsPreferences { 29048147e0Sopenharmony_ci private static sInstance: MmsPreferences; 30048147e0Sopenharmony_ci private static sMap = new Map<string, string | number | boolean>(); 31048147e0Sopenharmony_ci private preferences: preferences.Preferences; 32048147e0Sopenharmony_ci private static readonly PREFERENCES_Mms_FORM_STORE = 'MmsStore' 33048147e0Sopenharmony_ci 34048147e0Sopenharmony_ci getInstance(): MmsPreferences { 35048147e0Sopenharmony_ci if (MmsPreferences.sInstance == null) { 36048147e0Sopenharmony_ci MmsPreferences.sInstance = new MmsPreferences(); 37048147e0Sopenharmony_ci } 38048147e0Sopenharmony_ci return MmsPreferences.sInstance; 39048147e0Sopenharmony_ci } 40048147e0Sopenharmony_ci 41048147e0Sopenharmony_ci public async initPreferences(): Promise<void> { 42048147e0Sopenharmony_ci if (this.preferences != null) { 43048147e0Sopenharmony_ci return; 44048147e0Sopenharmony_ci } 45048147e0Sopenharmony_ci 46048147e0Sopenharmony_ci try { 47048147e0Sopenharmony_ci this.preferences = await preferences.getPreferences(globalThis.mmsContext, 48048147e0Sopenharmony_ci MmsPreferences.PREFERENCES_Mms_FORM_STORE); 49048147e0Sopenharmony_ci this.initMapData(); 50048147e0Sopenharmony_ci } catch (err) { 51048147e0Sopenharmony_ci HiLog.i(TAG, `initPreferences err ${err}`); 52048147e0Sopenharmony_ci } 53048147e0Sopenharmony_ci } 54048147e0Sopenharmony_ci 55048147e0Sopenharmony_ci /** 56048147e0Sopenharmony_ci * Init data to map 57048147e0Sopenharmony_ci */ 58048147e0Sopenharmony_ci initMapData(): void { 59048147e0Sopenharmony_ci this.asyncGetValueFromPreferences(common.string.KEY_OF_INTEGRATION_SWITCH); 60048147e0Sopenharmony_ci this.asyncGetValueFromPreferences(common.string.KEY_OF_MALICIOUS_WEB_SWITCH); 61048147e0Sopenharmony_ci this.asyncGetValueFromPreferences(common.string.KEY_OF_SHOW_CONTACT_SWITCH); 62048147e0Sopenharmony_ci this.asyncGetValueFromPreferences(common.string.KEY_OF_DELIVERY_REPORT_SWITCH); 63048147e0Sopenharmony_ci this.asyncGetValueFromPreferences(common.string.KEY_OF_AUTO_RETRIEVE_SWITCH); 64048147e0Sopenharmony_ci this.asyncGetValueFromPreferences(common.string.KEY_OF_RECALL_MESSAGE_SWITCH); 65048147e0Sopenharmony_ci this.asyncGetValueFromPreferences(common.string.KEY_OF_AUTO_DELETE_INFO_SWITCH); 66048147e0Sopenharmony_ci } 67048147e0Sopenharmony_ci 68048147e0Sopenharmony_ci /** 69048147e0Sopenharmony_ci * Get value from preferences 70048147e0Sopenharmony_ci */ 71048147e0Sopenharmony_ci async getValueFromPreferences(key: string) { 72048147e0Sopenharmony_ci if (this.preferences == null) { 73048147e0Sopenharmony_ci HiLog.i(TAG, `getValueFromPreferences preferences is null`); 74048147e0Sopenharmony_ci return common.string.EMPTY_STR; 75048147e0Sopenharmony_ci } 76048147e0Sopenharmony_ci try { 77048147e0Sopenharmony_ci let value = await this.preferences.get(key, common.string.EMPTY_STR); 78048147e0Sopenharmony_ci this.setValueToMap(key, value.toString()); 79048147e0Sopenharmony_ci return value; 80048147e0Sopenharmony_ci } catch (err) { 81048147e0Sopenharmony_ci HiLog.i(TAG, 'getValueFromPreferences(' + key + ') failed with err: ' + JSON.stringify(err)) 82048147e0Sopenharmony_ci return common.string.EMPTY_STR; 83048147e0Sopenharmony_ci } 84048147e0Sopenharmony_ci } 85048147e0Sopenharmony_ci 86048147e0Sopenharmony_ci /** 87048147e0Sopenharmony_ci * Async get value from preferences 88048147e0Sopenharmony_ci */ 89048147e0Sopenharmony_ci async asyncGetValueFromPreferences(key: string): Promise<string> { 90048147e0Sopenharmony_ci if (this.preferences == null) { 91048147e0Sopenharmony_ci HiLog.i(TAG, `asyncGetValueByKeyFromPreferences preferences is null`); 92048147e0Sopenharmony_ci return common.string.EMPTY_STR; 93048147e0Sopenharmony_ci } 94048147e0Sopenharmony_ci let getPromise = this.preferences.get(key, common.string.EMPTY_STR); 95048147e0Sopenharmony_ci getPromise.then((value) => { 96048147e0Sopenharmony_ci this.setValueToMap(key, value.toString()); 97048147e0Sopenharmony_ci return value; 98048147e0Sopenharmony_ci }).catch((err) => { 99048147e0Sopenharmony_ci HiLog.i(TAG, 'asyncGetValueFromPreferences(' + key + ') failed with err: ' + JSON.stringify(err)) 100048147e0Sopenharmony_ci return common.string.EMPTY_STR; 101048147e0Sopenharmony_ci }); 102048147e0Sopenharmony_ci } 103048147e0Sopenharmony_ci 104048147e0Sopenharmony_ci /** 105048147e0Sopenharmony_ci * Obtains the value of the notification integration switch. 106048147e0Sopenharmony_ci */ 107048147e0Sopenharmony_ci public getValueOfIntegrationSwitch(): string { 108048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_INTEGRATION_SWITCH, common.bool.TRUE); 109048147e0Sopenharmony_ci } 110048147e0Sopenharmony_ci 111048147e0Sopenharmony_ci /** 112048147e0Sopenharmony_ci * Obtains the value of the malicious URL identification switch. 113048147e0Sopenharmony_ci */ 114048147e0Sopenharmony_ci public getValueOfMaliciousWebSwitch(): string { 115048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_MALICIOUS_WEB_SWITCH, common.bool.FALSE); 116048147e0Sopenharmony_ci } 117048147e0Sopenharmony_ci 118048147e0Sopenharmony_ci /** 119048147e0Sopenharmony_ci * Obtains the value of the switch for displaying contact avatars. 120048147e0Sopenharmony_ci */ 121048147e0Sopenharmony_ci public getValueOfShowContactSwitch(): string { 122048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_SHOW_CONTACT_SWITCH, common.bool.TRUE); 123048147e0Sopenharmony_ci } 124048147e0Sopenharmony_ci 125048147e0Sopenharmony_ci /** 126048147e0Sopenharmony_ci * Obtains the value of the delivery report switch. 127048147e0Sopenharmony_ci */ 128048147e0Sopenharmony_ci public getValueOfDeliveryReportSwitch(): string { 129048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_DELIVERY_REPORT_SWITCH, common.DELIVERY_REPORTS.DISABLED); 130048147e0Sopenharmony_ci } 131048147e0Sopenharmony_ci 132048147e0Sopenharmony_ci /** 133048147e0Sopenharmony_ci * Obtains the value of the function of automatically downloading MMS messages. 134048147e0Sopenharmony_ci */ 135048147e0Sopenharmony_ci public getValueOfAutoRetrieveMmsSwitch(): string { 136048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_AUTO_RETRIEVE_SWITCH, 137048147e0Sopenharmony_ci common.AUTO_RETRIEVE_MMS.NOT_WHEN_ROAMING); 138048147e0Sopenharmony_ci } 139048147e0Sopenharmony_ci 140048147e0Sopenharmony_ci /** 141048147e0Sopenharmony_ci * Obtains the value of the send cancel switch. 142048147e0Sopenharmony_ci */ 143048147e0Sopenharmony_ci public getValueOfRecallMessageSwitch(): string { 144048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_RECALL_MESSAGE_SWITCH, common.bool.FALSE); 145048147e0Sopenharmony_ci } 146048147e0Sopenharmony_ci 147048147e0Sopenharmony_ci /** 148048147e0Sopenharmony_ci * Obtains the value of the automatic deletion notification switch. 149048147e0Sopenharmony_ci */ 150048147e0Sopenharmony_ci public getValueOfAutoDeleteInfoSwitch(): string { 151048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_AUTO_DELETE_INFO_SWITCH, common.bool.FALSE); 152048147e0Sopenharmony_ci } 153048147e0Sopenharmony_ci 154048147e0Sopenharmony_ci public isMultiSimCardEnabled(): boolean { 155048147e0Sopenharmony_ci if (this.getMaxSimCount() === common.int.SIM_COUNT) { 156048147e0Sopenharmony_ci return true; 157048147e0Sopenharmony_ci } 158048147e0Sopenharmony_ci return false; 159048147e0Sopenharmony_ci } 160048147e0Sopenharmony_ci 161048147e0Sopenharmony_ci public getMaxSimCount(): number { 162048147e0Sopenharmony_ci return <number> this.getValueFromMap(common.string.KEY_OF_MAX_SIM_COUNT, common.int.SIM_ONE); 163048147e0Sopenharmony_ci } 164048147e0Sopenharmony_ci 165048147e0Sopenharmony_ci public haveMultiSimCardReady(): boolean { 166048147e0Sopenharmony_ci return <boolean> this.getValueFromMap(common.string.KEY_OF_HAVE_MULTI_SIM_CARD_READY, false); 167048147e0Sopenharmony_ci } 168048147e0Sopenharmony_ci 169048147e0Sopenharmony_ci public haveSimCardReady(): boolean { 170048147e0Sopenharmony_ci return <boolean> this.getValueFromMap(common.string.KEY_OF_HAVE_SIM_CARD_READY, false); 171048147e0Sopenharmony_ci } 172048147e0Sopenharmony_ci 173048147e0Sopenharmony_ci public getSpnOfSim1(): string { 174048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_SIM_0_SPN, common.string.EMPTY_STR); 175048147e0Sopenharmony_ci } 176048147e0Sopenharmony_ci 177048147e0Sopenharmony_ci public getSpnOfSim2(): string { 178048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_SIM_1_SPN, common.string.EMPTY_STR); 179048147e0Sopenharmony_ci } 180048147e0Sopenharmony_ci 181048147e0Sopenharmony_ci public getNewSmscOfSim1(): string { 182048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_NEW_SIM_0_SMSC, common.string.EMPTY_STR); 183048147e0Sopenharmony_ci } 184048147e0Sopenharmony_ci 185048147e0Sopenharmony_ci public getNewSmscOfSim2(): string { 186048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_NEW_SIM_1_SMSC, common.string.EMPTY_STR); 187048147e0Sopenharmony_ci } 188048147e0Sopenharmony_ci 189048147e0Sopenharmony_ci public getTelephoneNumberOfSim1(): string { 190048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_SIM_0_NUMBER, common.string.EMPTY_STR); 191048147e0Sopenharmony_ci } 192048147e0Sopenharmony_ci 193048147e0Sopenharmony_ci public getTelephoneNumberOfSim2(): string { 194048147e0Sopenharmony_ci return <string> this.getValueFromMap(common.string.KEY_OF_SIM_1_NUMBER, common.string.EMPTY_STR); 195048147e0Sopenharmony_ci } 196048147e0Sopenharmony_ci 197048147e0Sopenharmony_ci public getSelectedSlotId(): number { 198048147e0Sopenharmony_ci return <number> this.getValueFromMap(common.string.KEY_OF_SELECTED_SLOTID, common.int.SIM_ONE); 199048147e0Sopenharmony_ci } 200048147e0Sopenharmony_ci 201048147e0Sopenharmony_ci public getDefaultSlotId(): number { 202048147e0Sopenharmony_ci return <number> this.getValueFromMap(common.string.KEY_OF_DEFAULT_SLOT, common.int.SIM_ONE); 203048147e0Sopenharmony_ci } 204048147e0Sopenharmony_ci 205048147e0Sopenharmony_ci public getSendMessageSlotId(): number { 206048147e0Sopenharmony_ci let sendMsgSlotId: number = common.int.SIM_ONE; 207048147e0Sopenharmony_ci if (this.haveMultiSimCardReady()) { 208048147e0Sopenharmony_ci sendMsgSlotId = this.getSelectedSlotId(); 209048147e0Sopenharmony_ci } else { 210048147e0Sopenharmony_ci sendMsgSlotId = this.getDefaultSlotId(); 211048147e0Sopenharmony_ci } 212048147e0Sopenharmony_ci return sendMsgSlotId; 213048147e0Sopenharmony_ci } 214048147e0Sopenharmony_ci 215048147e0Sopenharmony_ci /** 216048147e0Sopenharmony_ci * Set a value to preferences 217048147e0Sopenharmony_ci * 218048147e0Sopenharmony_ci * @param keyOfSwitch 219048147e0Sopenharmony_ci * @param valueOfSwitch 220048147e0Sopenharmony_ci */ 221048147e0Sopenharmony_ci public setValueForSwitch(keyOfSwitch: string, valueOfSwitch: string): void { 222048147e0Sopenharmony_ci this.setValueToMap(keyOfSwitch, valueOfSwitch); 223048147e0Sopenharmony_ci if (this.preferences == null) { 224048147e0Sopenharmony_ci HiLog.i(TAG, `setValueForSwitch preferences is null`); 225048147e0Sopenharmony_ci return 226048147e0Sopenharmony_ci } 227048147e0Sopenharmony_ci let putPromise = this.preferences.put(keyOfSwitch, valueOfSwitch); 228048147e0Sopenharmony_ci putPromise.then(() => { 229048147e0Sopenharmony_ci this.preferences.flush(); 230048147e0Sopenharmony_ci }).catch((err) => { 231048147e0Sopenharmony_ci HiLog.i(TAG, `setValueForSwitch failed with err: ${err}`); 232048147e0Sopenharmony_ci }); 233048147e0Sopenharmony_ci } 234048147e0Sopenharmony_ci 235048147e0Sopenharmony_ci /** 236048147e0Sopenharmony_ci * Get value from map by key 237048147e0Sopenharmony_ci * 238048147e0Sopenharmony_ci * @param key 239048147e0Sopenharmony_ci * @param defaultValue 240048147e0Sopenharmony_ci */ 241048147e0Sopenharmony_ci public getValueFromMap(key: string, defaultValue: string | number | boolean): 242048147e0Sopenharmony_ci string | number | boolean { 243048147e0Sopenharmony_ci let value = MmsPreferences.sMap.get(key); 244048147e0Sopenharmony_ci if (value == null) { 245048147e0Sopenharmony_ci value = defaultValue; 246048147e0Sopenharmony_ci } 247048147e0Sopenharmony_ci return value; 248048147e0Sopenharmony_ci } 249048147e0Sopenharmony_ci 250048147e0Sopenharmony_ci /** 251048147e0Sopenharmony_ci * Set key-value to map 252048147e0Sopenharmony_ci * 253048147e0Sopenharmony_ci * @param key 254048147e0Sopenharmony_ci * @param value 255048147e0Sopenharmony_ci */ 256048147e0Sopenharmony_ci public setValueToMap(key: string, value: string | number | boolean): void { 257048147e0Sopenharmony_ci MmsPreferences.sMap.set(key, value); 258048147e0Sopenharmony_ci } 259048147e0Sopenharmony_ci} 260048147e0Sopenharmony_ci 261048147e0Sopenharmony_cilet dataStore = createOrGet(MmsPreferences, TAG); 262048147e0Sopenharmony_ci 263048147e0Sopenharmony_ciexport default dataStore as MmsPreferences;