1/** 2 * @file Describe the file 3 * Copyright (c) 2023 Huawei Device Co., Ltd. 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17import Want from '@ohos.app.ability.Want'; 18import Extension from '@ohos.application.DataShareExtensionAbility'; 19import dataSharePredicates from '@ohos.data.dataSharePredicates'; 20import relationalStore from '@ohos.data.relationalStore'; 21 22import { Log } from '@ohos/common/src/main/ets/utils/Log'; 23import dataShareProxy from '@ohos/dataprovider/src/main/ets/DataShareAbilityAuthenticateProxy'; 24import { Permissions } from '@ohos.privacyManager'; 25import { GlobalThis } from '@ohos/common/src/main/ets/utils/GlobalThis'; 26 27const TAG = 'DataShareExtAbility'; 28 29let isDatabaseInitializing = false; 30 31let isDatabaseInitComplete = false; 32 33const WRITE_CALENDAR: Permissions = "ohos.permission.WRITE_CALENDAR"; 34const READ_CALENDAR: Permissions = "ohos.permission.READ_CALENDAR"; 35 36export default class DataShareExtAbility extends Extension { 37 onCreate(want: Want, callback: Function) { 38 Log.info(TAG, '[onCreate] enter'); 39 try { 40 GlobalThis.setExtensionContext(this.context); 41 callback(); 42 } catch (err) { 43 Log.error(TAG, `err=${err?.message}`); 44 } 45 Log.info(TAG, '[onCreate] leave'); 46 } 47 48 insert(uri: string, value: relationalStore.ValuesBucket, callback: Function) { 49 if (isDatabaseInitComplete) { 50 // If the database has been created, perform data operations directly 51 this.doInsert(uri, value, callback); 52 } else { 53 // If the table is not created, try to create the database 54 this.tryInitDatabase(); 55 // Recursively check whether the database is built, per 100ms 56 setTimeout(() => { 57 this.insert(uri, value, callback); 58 }, 100); 59 } 60 } 61 62 update(uri: string, predicates: dataSharePredicates.DataSharePredicates, value: relationalStore.ValuesBucket, callback: Function) { 63 if (isDatabaseInitComplete) { 64 this.doUpdate(uri, predicates, value, callback); 65 } else { 66 this.tryInitDatabase(); 67 setTimeout(() => { 68 this.update(uri, predicates, value, callback); 69 }, 100); 70 } 71 } 72 73 query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) { 74 if (isDatabaseInitComplete) { 75 this.doQuery(uri, predicates, columns, callback); 76 } else { 77 this.tryInitDatabase(); 78 setTimeout(() => { 79 this.query(uri, predicates, columns, callback); 80 }, 100); 81 } 82 } 83 84 delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) { 85 if (isDatabaseInitComplete) { 86 this.doDelete(uri, predicates, callback); 87 } else { 88 this.tryInitDatabase(); 89 setTimeout(() => { 90 this.delete(uri, predicates, callback); 91 }, 100); 92 } 93 } 94 95 doInsert(uri: string, value: relationalStore.ValuesBucket, callback: Function) { 96 Log.info(TAG, '[insert] enter'); 97 try { 98 dataShareProxy.insertByProxy(uri, value, WRITE_CALENDAR, callback); 99 } catch (err) { 100 Log.error(TAG, `err=${err?.message}`); 101 } 102 Log.info(TAG, '[insert] leave'); 103 } 104 105 doDelete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) { 106 Log.info(TAG, '[delete] enter'); 107 try { 108 dataShareProxy.deleteByProxy(uri, predicates, WRITE_CALENDAR, callback); 109 } catch (err) { 110 Log.error(TAG, `err=${err?.message}`); 111 } 112 Log.info(TAG, '[delete] leave'); 113 } 114 115 doUpdate(uri: string, predicates: dataSharePredicates.DataSharePredicates, value: relationalStore.ValuesBucket, callback: Function) { 116 Log.info(TAG, '[update] enter'); 117 try { 118 dataShareProxy.updateByProxy(uri, value, predicates, WRITE_CALENDAR, callback) 119 } catch (err) { 120 Log.error(TAG, `err=${err?.message}`); 121 } 122 Log.info(TAG, '[update] leave'); 123 } 124 125 doQuery(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) { 126 Log.info(TAG, '[query] enter'); 127 try { 128 dataShareProxy.queryByProxy(uri, columns, predicates, READ_CALENDAR, callback); 129 } catch (err) { 130 Log.error(TAG, `err=${err?.message}`); 131 } 132 Log.info(TAG, '[query] leave'); 133 } 134 135 private tryInitDatabase() { 136 // If a table is being created, return directly 137 if (isDatabaseInitializing) { 138 return; 139 } 140 // Change the status of isDatabaseInitializing 141 isDatabaseInitializing = true; 142 this.initDatabase(); 143 } 144 145 async initDatabase() { 146 try { 147 // Change the status of isDatabaseInitializing and isDatabaseInitComplete 148 isDatabaseInitComplete = await dataShareProxy.init(); 149 } finally { 150 isDatabaseInitializing = false; 151 } 152 } 153}