10fbfc30aSopenharmony_ci/**
20fbfc30aSopenharmony_ci * @file Describe the file
30fbfc30aSopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd.
40fbfc30aSopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License");
50fbfc30aSopenharmony_ci * you may not use this file except in compliance with the License.
60fbfc30aSopenharmony_ci * You may obtain a copy of the License at
70fbfc30aSopenharmony_ci *
80fbfc30aSopenharmony_ci *     http://www.apache.org/licenses/LICENSE-2.0
90fbfc30aSopenharmony_ci *
100fbfc30aSopenharmony_ci * Unless required by applicable law or agreed to in writing, software
110fbfc30aSopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS,
120fbfc30aSopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
130fbfc30aSopenharmony_ci * See the License for the specific language governing permissions and
140fbfc30aSopenharmony_ci * limitations under the License.
150fbfc30aSopenharmony_ci */
160fbfc30aSopenharmony_ci
170fbfc30aSopenharmony_ciimport Want from '@ohos.app.ability.Want';
180fbfc30aSopenharmony_ciimport Extension from '@ohos.application.DataShareExtensionAbility';
190fbfc30aSopenharmony_ciimport dataSharePredicates from '@ohos.data.dataSharePredicates';
200fbfc30aSopenharmony_ciimport relationalStore from '@ohos.data.relationalStore';
210fbfc30aSopenharmony_ci
220fbfc30aSopenharmony_ciimport { Log } from '@ohos/common/src/main/ets/utils/Log';
230fbfc30aSopenharmony_ciimport dataShareProxy from '@ohos/dataprovider/src/main/ets/DataShareAbilityAuthenticateProxy';
240fbfc30aSopenharmony_ciimport { Permissions } from '@ohos.privacyManager';
250fbfc30aSopenharmony_ciimport { GlobalThis } from '@ohos/common/src/main/ets/utils/GlobalThis';
260fbfc30aSopenharmony_ci
270fbfc30aSopenharmony_ciconst TAG = 'DataShareExtAbility';
280fbfc30aSopenharmony_ci
290fbfc30aSopenharmony_cilet isDatabaseInitializing = false;
300fbfc30aSopenharmony_ci
310fbfc30aSopenharmony_cilet isDatabaseInitComplete = false;
320fbfc30aSopenharmony_ci
330fbfc30aSopenharmony_ciconst WRITE_CALENDAR: Permissions = "ohos.permission.WRITE_CALENDAR";
340fbfc30aSopenharmony_ciconst READ_CALENDAR: Permissions = "ohos.permission.READ_CALENDAR";
350fbfc30aSopenharmony_ci
360fbfc30aSopenharmony_ciexport default class DataShareExtAbility extends Extension {
370fbfc30aSopenharmony_ci  onCreate(want: Want, callback: Function) {
380fbfc30aSopenharmony_ci    Log.info(TAG, '[onCreate] enter');
390fbfc30aSopenharmony_ci    try {
400fbfc30aSopenharmony_ci      GlobalThis.setExtensionContext(this.context);
410fbfc30aSopenharmony_ci      callback();
420fbfc30aSopenharmony_ci    } catch (err) {
430fbfc30aSopenharmony_ci      Log.error(TAG, `err=${err?.message}`);
440fbfc30aSopenharmony_ci    }
450fbfc30aSopenharmony_ci    Log.info(TAG, '[onCreate] leave');
460fbfc30aSopenharmony_ci  }
470fbfc30aSopenharmony_ci
480fbfc30aSopenharmony_ci  insert(uri: string, value: relationalStore.ValuesBucket, callback: Function) {
490fbfc30aSopenharmony_ci    if (isDatabaseInitComplete) {
500fbfc30aSopenharmony_ci      // If the database has been created, perform data operations directly
510fbfc30aSopenharmony_ci      this.doInsert(uri, value, callback);
520fbfc30aSopenharmony_ci    } else {
530fbfc30aSopenharmony_ci      // If the table is not created, try to create the database
540fbfc30aSopenharmony_ci      this.tryInitDatabase();
550fbfc30aSopenharmony_ci      // Recursively check whether the database is built, per 100ms
560fbfc30aSopenharmony_ci      setTimeout(() => {
570fbfc30aSopenharmony_ci        this.insert(uri, value, callback);
580fbfc30aSopenharmony_ci      }, 100);
590fbfc30aSopenharmony_ci    }
600fbfc30aSopenharmony_ci  }
610fbfc30aSopenharmony_ci
620fbfc30aSopenharmony_ci batchInsert(uri: string, value: relationalStore.ValuesBucket[], callback: Function) {
630fbfc30aSopenharmony_ci    if (isDatabaseInitComplete) {
640fbfc30aSopenharmony_ci      // If the database has been created, perform data operations directly
650fbfc30aSopenharmony_ci      this.doBachInsert(uri, value, callback);
660fbfc30aSopenharmony_ci    } else {
670fbfc30aSopenharmony_ci      // If the table is not created, try to create the database
680fbfc30aSopenharmony_ci      this.tryInitDatabase();
690fbfc30aSopenharmony_ci      // Recursively check whether the database is built, per 100ms
700fbfc30aSopenharmony_ci      setTimeout(() => {
710fbfc30aSopenharmony_ci        this.batchInsert(uri, value, callback);
720fbfc30aSopenharmony_ci      }, 100);
730fbfc30aSopenharmony_ci    }
740fbfc30aSopenharmony_ci  }
750fbfc30aSopenharmony_ci  
760fbfc30aSopenharmony_ci  update(uri: string, predicates: dataSharePredicates.DataSharePredicates, value: relationalStore.ValuesBucket, callback: Function) {
770fbfc30aSopenharmony_ci    if (isDatabaseInitComplete) {
780fbfc30aSopenharmony_ci      this.doUpdate(uri, predicates, value, callback);
790fbfc30aSopenharmony_ci    } else {
800fbfc30aSopenharmony_ci      this.tryInitDatabase();
810fbfc30aSopenharmony_ci      setTimeout(() => {
820fbfc30aSopenharmony_ci        this.update(uri, predicates, value, callback);
830fbfc30aSopenharmony_ci      }, 100);
840fbfc30aSopenharmony_ci    }
850fbfc30aSopenharmony_ci  }
860fbfc30aSopenharmony_ci
870fbfc30aSopenharmony_ci  query(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
880fbfc30aSopenharmony_ci    if (isDatabaseInitComplete) {
890fbfc30aSopenharmony_ci      this.doQuery(uri, predicates, columns, callback);
900fbfc30aSopenharmony_ci    } else {
910fbfc30aSopenharmony_ci      this.tryInitDatabase();
920fbfc30aSopenharmony_ci      setTimeout(() => {
930fbfc30aSopenharmony_ci        this.query(uri, predicates, columns, callback);
940fbfc30aSopenharmony_ci      }, 100);
950fbfc30aSopenharmony_ci    }
960fbfc30aSopenharmony_ci  }
970fbfc30aSopenharmony_ci
980fbfc30aSopenharmony_ci  delete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
990fbfc30aSopenharmony_ci    if (isDatabaseInitComplete) {
1000fbfc30aSopenharmony_ci      this.doDelete(uri, predicates, callback);
1010fbfc30aSopenharmony_ci    } else {
1020fbfc30aSopenharmony_ci      this.tryInitDatabase();
1030fbfc30aSopenharmony_ci      setTimeout(() => {
1040fbfc30aSopenharmony_ci        this.delete(uri, predicates, callback);
1050fbfc30aSopenharmony_ci      }, 100);
1060fbfc30aSopenharmony_ci    }
1070fbfc30aSopenharmony_ci  }
1080fbfc30aSopenharmony_ci
1090fbfc30aSopenharmony_ci  doInsert(uri: string, value: relationalStore.ValuesBucket, callback: Function) {
1100fbfc30aSopenharmony_ci    Log.info(TAG, '[insert] enter');
1110fbfc30aSopenharmony_ci    try {
1120fbfc30aSopenharmony_ci      dataShareProxy.insertByProxy(uri, value, WRITE_CALENDAR, callback);
1130fbfc30aSopenharmony_ci    } catch (err) {
1140fbfc30aSopenharmony_ci      Log.error(TAG, `err=${err?.message}`);
1150fbfc30aSopenharmony_ci    }
1160fbfc30aSopenharmony_ci    Log.info(TAG, '[insert] leave');
1170fbfc30aSopenharmony_ci  }
1180fbfc30aSopenharmony_ci
1190fbfc30aSopenharmony_ci  doBachInsert(uri: string, value: relationalStore.ValuesBucket[], callback: Function) {
1200fbfc30aSopenharmony_ci    Log.info(TAG, '[insert] enter');
1210fbfc30aSopenharmony_ci    try {
1220fbfc30aSopenharmony_ci      dataShareProxy.insertByProxy(uri, value, WRITE_CALENDAR, callback);
1230fbfc30aSopenharmony_ci    } catch (err) {
1240fbfc30aSopenharmony_ci      Log.error(TAG, `err=${err?.message}`);
1250fbfc30aSopenharmony_ci    }
1260fbfc30aSopenharmony_ci    Log.info(TAG, '[insert] leave');
1270fbfc30aSopenharmony_ci  }
1280fbfc30aSopenharmony_ci
1290fbfc30aSopenharmony_ci  doDelete(uri: string, predicates: dataSharePredicates.DataSharePredicates, callback: Function) {
1300fbfc30aSopenharmony_ci    Log.info(TAG, '[delete] enter');
1310fbfc30aSopenharmony_ci    try {
1320fbfc30aSopenharmony_ci      dataShareProxy.deleteByProxy(uri, predicates, WRITE_CALENDAR, callback);
1330fbfc30aSopenharmony_ci    } catch (err) {
1340fbfc30aSopenharmony_ci      Log.error(TAG, `err=${err?.message}`);
1350fbfc30aSopenharmony_ci    }
1360fbfc30aSopenharmony_ci    Log.info(TAG, '[delete] leave');
1370fbfc30aSopenharmony_ci  }
1380fbfc30aSopenharmony_ci
1390fbfc30aSopenharmony_ci  doUpdate(uri: string, predicates: dataSharePredicates.DataSharePredicates, value: relationalStore.ValuesBucket, callback: Function) {
1400fbfc30aSopenharmony_ci    Log.info(TAG, '[update] enter');
1410fbfc30aSopenharmony_ci    try {
1420fbfc30aSopenharmony_ci      dataShareProxy.updateByProxy(uri, value, predicates, WRITE_CALENDAR, callback)
1430fbfc30aSopenharmony_ci    } catch (err) {
1440fbfc30aSopenharmony_ci      Log.error(TAG, `err=${err?.message}`);
1450fbfc30aSopenharmony_ci    }
1460fbfc30aSopenharmony_ci    Log.info(TAG, '[update] leave');
1470fbfc30aSopenharmony_ci  }
1480fbfc30aSopenharmony_ci
1490fbfc30aSopenharmony_ci  doQuery(uri: string, predicates: dataSharePredicates.DataSharePredicates, columns: Array<string>, callback: Function) {
1500fbfc30aSopenharmony_ci    Log.info(TAG, '[query] enter');
1510fbfc30aSopenharmony_ci    try {
1520fbfc30aSopenharmony_ci      dataShareProxy.queryByProxy(uri, columns, predicates, READ_CALENDAR, callback);
1530fbfc30aSopenharmony_ci    } catch (err) {
1540fbfc30aSopenharmony_ci      Log.error(TAG, `err=${err?.message}`);
1550fbfc30aSopenharmony_ci    }
1560fbfc30aSopenharmony_ci    Log.info(TAG, '[query] leave');
1570fbfc30aSopenharmony_ci  }
1580fbfc30aSopenharmony_ci
1590fbfc30aSopenharmony_ci  private tryInitDatabase() {
1600fbfc30aSopenharmony_ci    // If a table is being created, return directly
1610fbfc30aSopenharmony_ci    if (isDatabaseInitializing) {
1620fbfc30aSopenharmony_ci      return;
1630fbfc30aSopenharmony_ci    }
1640fbfc30aSopenharmony_ci    // Change the status of isDatabaseInitializing
1650fbfc30aSopenharmony_ci    isDatabaseInitializing = true;
1660fbfc30aSopenharmony_ci    this.initDatabase();
1670fbfc30aSopenharmony_ci  }
1680fbfc30aSopenharmony_ci
1690fbfc30aSopenharmony_ci  async initDatabase() {
1700fbfc30aSopenharmony_ci    try {
1710fbfc30aSopenharmony_ci      // Change the status of isDatabaseInitializing and isDatabaseInitComplete
1720fbfc30aSopenharmony_ci      isDatabaseInitComplete = await dataShareProxy.init();
1730fbfc30aSopenharmony_ci    } finally {
1740fbfc30aSopenharmony_ci      isDatabaseInitializing = false;
1750fbfc30aSopenharmony_ci    }
1760fbfc30aSopenharmony_ci  }
1770fbfc30aSopenharmony_ci}