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 dataAbility from '@ohos.data.dataAbility' 17import featureAbility from '@ohos.ability.featureAbility' 18import prompt from '@ohos.prompt' 19import Logger from '../util/Logger' 20import { BASE_URI, COLUMNS } from '../model/DaHelperConst' 21 22const TAG: string = 'DataAbilityHelper' 23const DA_HELPER = featureAbility.acquireDataAbilityHelper(BASE_URI) 24 25// 对应Stage模型DataShareHelper 26class DataAbilityHelper { 27 private resultSet: any 28 29 // 在指定的远程路径中打开文件 30 openFile() { 31 DA_HELPER.openFile(BASE_URI, 'rwt', (err, data) => { 32 Logger.info(TAG, `openFile err = ${JSON.stringify(err)}, fd = ${data}`) 33 prompt.showToast({ 34 message: 'openFile success' 35 }) 36 }) 37 } 38 39 // 注册观察者以观察给定uri指定的数据 40 on() { 41 DA_HELPER.on('dataChange', BASE_URI, () => { 42 Logger.info(TAG, `on dataChange`) 43 prompt.showToast({ 44 message: 'on dataChange success' 45 }) 46 }) 47 } 48 49 // 注消观察者以停止观察给定uri指定的数据 50 off() { 51 DA_HELPER.off('dataChange', BASE_URI, () => { 52 Logger.info(TAG, `off dataChange`) 53 prompt.showToast({ 54 message: 'off dataChange success' 55 }) 56 }) 57 } 58 59 // 获取给定URI指定数据的MIME类型 60 getType() { 61 DA_HELPER.getType(BASE_URI, (err, data) => { 62 Logger.info(TAG, `getType err = ${JSON.stringify(err)}, data = ${data}`) 63 prompt.showToast({ 64 message: 'getType success' 65 }) 66 }) 67 } 68 69 // 获取支持的文件的MIME类型 70 getFileTypes() { 71 DA_HELPER.getFileTypes(BASE_URI, 'image/*', (err, data) => { 72 Logger.info(TAG, `getFileTypes err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 73 prompt.showToast({ 74 message: 'getFileTypes success' 75 }) 76 }) 77 } 78 79 // 将引用数据功能的给定uri转换为规范化uri,Stage模型DataShareHelper的normalizeUri 80 normalizeUri() { 81 DA_HELPER.normalizeUri(BASE_URI, (err, data) => { 82 Logger.info(TAG, `normalizeUri err = ${JSON.stringify(err)}, data = ${data}`) 83 prompt.showToast({ 84 message: 'normalizeUri success' 85 }) 86 }) 87 } 88 89 // 将由normalizeUri(uri)生成的给定规范化uri转换为非规范化uri,Stage模型DataShareHelper的denormalizeUri 90 denormalizeUri() { 91 DA_HELPER.denormalizeUri(BASE_URI, (err, data) => { 92 Logger.info(TAG, `denormalizeUri err = ${JSON.stringify(err)}, data = ${data}`) 93 prompt.showToast({ 94 message: 'denormalizeUri success' 95 }) 96 }) 97 } 98 99 // 通知已注册的观察者uri指定的数据资源的更改,Stage模型DataShareHelper的notifyChange 100 notifyChange() { 101 DA_HELPER.notifyChange(BASE_URI, (err) => { 102 Logger.info(TAG, `notifyChange success, err = ${JSON.stringify(err)}`) 103 prompt.showToast({ 104 message: 'notifyChange success' 105 }) 106 }) 107 } 108 109 // 调用DataAbility的扩展接口 110 call() { 111 DA_HELPER.call(BASE_URI, 'method', 'arg', { 'key1': 'value1' }, (err, data) => { 112 Logger.info(TAG, `call success err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 113 prompt.showToast({ 114 message: 'call success' 115 }) 116 }) 117 } 118 119 // 查询数据库中的数据 120 executeBatch() { 121 let operations = new Array() 122 operations.push({ uri: BASE_URI, type: featureAbility.DataAbilityOperationType.TYPE_INSERT, 123 valuesBucket: { name: 'Book name', introduction: 'Book introduction' } }) 124 DA_HELPER.executeBatch(BASE_URI, operations, (err, data) => { 125 Logger.info(TAG, `executeBatch success err = ${JSON.stringify(err)}, data = ${JSON.stringify(data)}`) 126 prompt.showToast({ 127 message: 'executeBatch success' 128 }) 129 }) 130 } 131 132 // 添加数据,对应Stage模型中DataShareHelper的insert 133 async insertBook() { 134 Logger.info(TAG, `insert onClick`) 135 let valuesBuckets = { name: 'Book name', introduction: 'Book introduction' } 136 let num = await DA_HELPER.insert(BASE_URI, valuesBuckets) 137 Logger.info(TAG, `insert num= ${num}`) 138 this.resultSet = await this.queryAll() 139 return this.resultSet 140 } 141 142 // 查询所有数据,对应Stage模型中DataShareHelper的queryAll 143 async queryAll() { 144 Logger.info(TAG, `queryAll start`) 145 let dahelper = featureAbility.acquireDataAbilityHelper(BASE_URI) 146 let predicates = new dataAbility.DataAbilityPredicates() 147 return await dahelper.query(BASE_URI, COLUMNS, predicates) 148 } 149 150 // 修改数据,对应stage模型中DataShareHelper的updateBook 151 async updateBook(book) { 152 let predicates = new dataAbility.DataAbilityPredicates() 153 predicates.equalTo('id', book.id) 154 let valuesBucket = { 155 'name': book.name, 156 'introduction': book.introduction 157 } 158 let num = await DA_HELPER.update(BASE_URI, valuesBucket, predicates) 159 Logger.info(TAG + `update book num= ${num}`) 160 this.resultSet = await this.queryAll() 161 return this.resultSet 162 } 163 164 // 删除指定数据,对应Stage模型中DataShareHelper的deleteBook 165 async deleteBook(book) { 166 if (book === null) { 167 return 168 } 169 let predicates = new dataAbility.DataAbilityPredicates() 170 predicates.equalTo('id', book.id) 171 let num = await DA_HELPER.delete(BASE_URI, predicates) 172 Logger.info(TAG, `delete num= ${num}`) 173 this.resultSet = await this.queryAll() 174 return this.resultSet 175 } 176} 177 178export default new DataAbilityHelper()