18779efd5Sopenharmony_ci/** 28779efd5Sopenharmony_ci * Copyright (c) 2022 Huawei Device Co., Ltd. 38779efd5Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 48779efd5Sopenharmony_ci * you may not use this file except in compliance with the License. 58779efd5Sopenharmony_ci * You may obtain a copy of the License at 68779efd5Sopenharmony_ci * 78779efd5Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 88779efd5Sopenharmony_ci * 98779efd5Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 108779efd5Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 118779efd5Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 128779efd5Sopenharmony_ci * See the License for the specific language governing permissions and 138779efd5Sopenharmony_ci * limitations under the License. 148779efd5Sopenharmony_ci */ 158779efd5Sopenharmony_ci 168779efd5Sopenharmony_ciimport data_preferences from '@ohos.data.preferences'; 178779efd5Sopenharmony_ciimport contextConstant from '@ohos.app.ability.contextConstant' 188779efd5Sopenharmony_ci 198779efd5Sopenharmony_ciconst NAME: string = "CONTACT_PREFERENCE"; 208779efd5Sopenharmony_citype ValueType = number | string | boolean | Array<number> | Array<string> | Array<boolean>; 218779efd5Sopenharmony_ci 228779efd5Sopenharmony_ciclass SharedPreferencesUtils { 238779efd5Sopenharmony_ci private mPreferences: data_preferences.Preferences; 248779efd5Sopenharmony_ci private context: Context; 258779efd5Sopenharmony_ci 268779efd5Sopenharmony_ci constructor() { 278779efd5Sopenharmony_ci this.getPreferences().then((data) => { 288779efd5Sopenharmony_ci this.mPreferences = data; 298779efd5Sopenharmony_ci }) 308779efd5Sopenharmony_ci } 318779efd5Sopenharmony_ci 328779efd5Sopenharmony_ci /* 338779efd5Sopenharmony_ci * init if Call From serviceAbility globalThis.context is Null 348779efd5Sopenharmony_ci *@param ctx Context used for dataShare 358779efd5Sopenharmony_ci */ 368779efd5Sopenharmony_ci init(ctx: Context) { 378779efd5Sopenharmony_ci this.context = ctx; 388779efd5Sopenharmony_ci } 398779efd5Sopenharmony_ci 408779efd5Sopenharmony_ci /** 418779efd5Sopenharmony_ci * getFromPreferences 428779efd5Sopenharmony_ci * 438779efd5Sopenharmony_ci * @return the value get from Preferences 448779efd5Sopenharmony_ci */ 458779efd5Sopenharmony_ci public async getFromPreferences(key: string, defValue) { 468779efd5Sopenharmony_ci let preferences: data_preferences.Preferences = await this.getPreferences(); 478779efd5Sopenharmony_ci return await preferences.get(key, defValue); 488779efd5Sopenharmony_ci } 498779efd5Sopenharmony_ci 508779efd5Sopenharmony_ci /** 518779efd5Sopenharmony_ci * saveToPreferences 528779efd5Sopenharmony_ci * 538779efd5Sopenharmony_ci * @param key save to Preferences 548779efd5Sopenharmony_ci * @param value save to Preferences 558779efd5Sopenharmony_ci */ 568779efd5Sopenharmony_ci public async saveToPreferences(key: string, value: ValueType) { 578779efd5Sopenharmony_ci let preferences: data_preferences.Preferences = await this.getPreferences(); 588779efd5Sopenharmony_ci await preferences.put(key, value); 598779efd5Sopenharmony_ci preferences.flush() 608779efd5Sopenharmony_ci } 618779efd5Sopenharmony_ci 628779efd5Sopenharmony_ci private async getPreferences() { 638779efd5Sopenharmony_ci if (this.mPreferences) { 648779efd5Sopenharmony_ci return this.mPreferences; 658779efd5Sopenharmony_ci } 668779efd5Sopenharmony_ci let ctx: Context = this.context ? this.context : globalThis.context; 678779efd5Sopenharmony_ci ctx.area = contextConstant.AreaMode.EL1 688779efd5Sopenharmony_ci return await data_preferences.getPreferences(ctx, NAME); 698779efd5Sopenharmony_ci } 708779efd5Sopenharmony_ci} 718779efd5Sopenharmony_ci 728779efd5Sopenharmony_ciexport const sharedPreferencesUtils: SharedPreferencesUtils = new SharedPreferencesUtils();