1/* 2 * Copyright (C) 2023 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 photoAccessHelper from '@ohos.file.photoAccessHelper'; 17import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; 18import bundleManager from '@ohos.bundle.bundleManager'; 19import dataSharePredicates from '@ohos.data.dataSharePredicates'; 20import abilityDelegatorRegistry from '@ohos.application.abilityDelegatorRegistry'; 21 22const delegator = abilityDelegatorRegistry.getAbilityDelegator(); 23const photoType = photoAccessHelper.PhotoType; 24const photoKeys = photoAccessHelper.PhotoKeys; 25const albumKeys = photoAccessHelper.AlbumKeys; 26const albumType = photoAccessHelper.AlbumType; 27const albumSubtype = photoAccessHelper.AlbumSubtype; 28const DEFAULT_SLEEP_TIME = 10; 29export async function sleep(times = DEFAULT_SLEEP_TIME) : Promise<void> { 30 await new Promise(res => setTimeout(res, times)); 31}; 32 33export function fetchAllOption() : photoAccessHelper.FetchOptions { 34 const predicates = new dataSharePredicates.DataSharePredicates(); 35 const ops : photoAccessHelper.FetchOptions = { 36 fetchColumns: [], 37 predicates: predicates 38 }; 39 return ops; 40}; 41 42export function fetchOption(testNum, key, value) : photoAccessHelper.FetchOptions { 43 const predicates = new dataSharePredicates.DataSharePredicates(); 44 predicates.equalTo(key, value); 45 const ops : photoAccessHelper.FetchOptions = { 46 fetchColumns: [], 47 predicates: predicates 48 }; 49 console.info(`${testNum} queryOps: ${key} = ${value}`); 50 return ops; 51}; 52 53export function albumFetchOption(testNum, key, value) : photoAccessHelper.FetchOptions { 54 const predicates = new dataSharePredicates.DataSharePredicates(); 55 predicates.equalTo(key, value); 56 const ops : photoAccessHelper.FetchOptions = { 57 fetchColumns: [], 58 predicates: predicates 59 }; 60 console.info(`${testNum} queryOps: ${key} = ${value}`); 61 return ops; 62}; 63 64export function photoFetchOption(testNum, key, value) : photoAccessHelper.FetchOptions { 65 const predicates = new dataSharePredicates.DataSharePredicates(); 66 predicates.equalTo(key, value); 67 const ops : photoAccessHelper.FetchOptions = { 68 fetchColumns: [ 69 photoKeys.URI, 70 photoKeys.PHOTO_TYPE, 71 photoKeys.DISPLAY_NAME, 72 photoKeys.DATE_ADDED, 73 photoKeys.DATE_MODIFIED, 74 photoKeys.DURATION, 75 photoKeys.WIDTH, 76 photoKeys.HEIGHT, 77 photoKeys.DATE_TAKEN, 78 photoKeys.ORIENTATION, 79 photoKeys.FAVORITE, 80 photoKeys.SIZE, 81 photoKeys.TITLE, 82 photoKeys.POSITION, 83 photoKeys.DATE_TRASHED, 84 photoKeys.HIDDEN, 85 photoKeys.CAMERA_SHOT_KEY, 86 photoKeys.USER_COMMENT, 87 'all_exif', 88 ], 89 predicates: predicates 90 }; 91 console.info(`${testNum} queryOps: ${key} = ${value}`); 92 return ops; 93}; 94 95export async function getPermission(name = 'ohos.acts.multimedia.photoaccess') : Promise<void> { 96 try { 97 console.info('getPermission start', name); 98 let permissionState = new Map(); 99 const permissions = [ 100 'ohos.permission.MEDIA_LOCATION', 101 'ohos.permission.READ_IMAGEVIDEO', 102 'ohos.permission.WRITE_IMAGEVIDEO', 103 ]; 104 105 const atManager = abilityAccessCtrl.createAtManager(); 106 const appFlags = bundleManager.ApplicationFlag.GET_APPLICATION_INFO_DEFAULT; 107 const userId = 100; 108 const appInfo = await bundleManager.getApplicationInfo(name, appFlags, userId); 109 const tokenID = appInfo.accessTokenId; 110 for (const permission of permissions) { 111 console.info('getPermission permission: ' + permission); 112 try { 113 await atManager.grantUserGrantedPermission(tokenID, permission, 1); 114 } catch (error) { 115 console.info(`getPermission ${permission} failed`); 116 } 117 permissionState.set(permission, await atManager.verifyAccessToken(tokenID, permission)); 118 } 119 permissionState.forEach((value, key, map) => { 120 if (value !== 0) { 121 console.info(`getPermission failed; permission: ${key}, state: ${value}`); 122 } 123 }); 124 console.info('getPermission end'); 125 } catch (error) { 126 console.info(`getPermission failed, error: ${error}`); 127 } 128}; 129 130export function isNum(value) : boolean { 131 return typeof value === 'number' && !isNaN(value); 132}; 133 134export function getAssetId(uri) : string { 135 const tag = 'Photo/'; 136 const index = uri.indexOf(tag); 137 let str = uri.substring(index + tag.length); 138 console.info(`getAssetId str: ${str}`); 139 return str; 140} 141 142export function getAlbumId(uri) : string { 143 const index = uri.lastIndexOf('/'); 144 let str = uri.substring(index + 1); 145 console.info(`getAlbumId str: ${str}`); 146 return str; 147} 148 149export function genRadomStr(len: number) : string { 150 const chars = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789'; 151 let randomStr = ''; 152 for (let i = 0; i < len; i++) { 153 randomStr += chars.charAt(Math.floor(Math.random() * chars.length)); 154 } 155 return randomStr; 156} 157 158export async function createUserAlbum(testNum, albumName) : Promise<photoAccessHelper.Album> { 159 console.info(`${testNum} createUserAlbum albumName: ${albumName}`); 160 let album: photoAccessHelper.Album; 161 try { 162 const helper = photoAccessHelper.getPhotoAccessHelper(globalThis.abilityContext); 163 album = await helper.createAlbum(albumName); 164 console.info(`${testNum} createUserAlbum suc`); 165 } catch (error) { 166 console.info(`Failed to createUserAlbum! error: ${error}`); 167 throw error; 168 } 169 170 return new Promise((resolve, reject) => { 171 resolve(album); 172 }); 173} 174 175export async function getFileAsset(testNum, fetchOps) : Promise<photoAccessHelper.PhotoAsset> { 176 let asset: photoAccessHelper.PhotoAsset; 177 try { 178 const helper = photoAccessHelper.getPhotoAccessHelper(globalThis.abilityContext); 179 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset>; 180 fetchResult = await helper.getAssets(fetchOps); 181 console.info(`${testNum} getFileAsset fetchResult: ${fetchResult.getCount()}`); 182 asset = await fetchResult.getFirstObject(); 183 fetchResult.close(); 184 } catch (error) { 185 console.info(`${testNum} getFileAsset error: ${error}`); 186 throw error; 187 } 188 189 return new Promise((resolve, reject) => { 190 resolve(asset); 191 }); 192} 193 194export function getFileAssetFetchResult() : photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset> { 195 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.PhotoAsset>; 196 return fetchResult; 197} 198 199export function getAlbumFetchResult() : photoAccessHelper.FetchResult<photoAccessHelper.Album> { 200 let fetchResult: photoAccessHelper.FetchResult<photoAccessHelper.Album>; 201 return fetchResult; 202} 203 204export function checkUserAlbum(expect, testNum, album, expectedName, expectedCover) : void { 205 console.info(`${testNum} checkUserAlbum album.albumName: ${album.albumName}, expectedName: ${expectedName}`); 206 expect(album.albumType).assertEqual(albumType.USER); 207 expect(album.albumSubtype).assertEqual(albumSubtype.USER_GENERIC); 208 expect(album.albumName).assertEqual(expectedName); 209 if (expectedCover === '') { 210 expect(album.coverUri).assertEqual(''); 211 } else { 212 expect(album.coverUri).assertEqual(expectedCover); 213 } 214 expect(album.albumUri !== '').assertEqual(true); 215 expect(album.count).assertEqual(0); 216} 217 218export function checkSystemAlbum(expect, testNum, album, expectedSubType) : void { 219 try { 220 console.info(`${testNum} checkSystemAlbum expectedSubType: ${expectedSubType}`); 221 expect(album.albumType).assertEqual(albumType.SYSTEM); 222 expect(album.albumSubtype).assertEqual(expectedSubType); 223 expect(album.albumName).assertEqual(''); 224 expect(album.albumUri !== '').assertEqual(true); 225 } catch (error) { 226 console.info(`Failed to delete all user albums! error: ${error}`); 227 throw error; 228 } 229} 230 231export async function startAbility(bundleName: string, abilityName: string) : Promise<void> { 232 await delegator.executeShellCommand(`aa start -b ${bundleName} -a ${abilityName}`).then(result => { 233 console.info(`[picker] start abilityFinished: ${result}`); 234 }).catch(err => { 235 console.error(`[picker] start abilityFailed: ${err}`); 236 }); 237} 238 239export { 240 photoType, 241 photoKeys, 242 albumKeys, 243 albumType, 244 albumSubtype, 245};