1/* 2 * Copyright (c) 2022-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 { AlbumDefine, BigDataConstants, Constants, Log, ReportToBigDataUtil } from '@ohos/common'; 17import wantConstant from '@ohos.ability.wantConstant'; 18import { SelectParams } from '../utils/ThirdSelectConstants'; 19import ability from '@ohos.ability.ability'; 20import common from '@ohos.app.ability.common'; 21import Want from '@ohos.app.ability.Want'; 22import UIExtensionContentSession from '@ohos.app.ability.UIExtensionContentSession'; 23import fileShare from '@ohos.fileshare'; 24import { BusinessError } from '@ohos.base'; 25 26const TAG: string = 'thiSel_CameraGridItemComponent'; 27 28@Component 29export struct CameraGridItemComponent { 30 selectParams: SelectParams = SelectParams.defaultParam(); 31 resultUri: string = ''; 32 @Consume @Watch('onShow') isShow: boolean; 33 updateDataFunc: Function = (): void => {}; 34 35 aboutToAppear(): void { 36 Log.info(TAG, `aboutToAppear`); 37 } 38 39 aboutToDisappear(): void { 40 Log.info(TAG, `aboutToDisappear`); 41 } 42 43 build() { 44 Stack() { 45 Column() { 46 } 47 .width('100%') 48 .height('100%') 49 .backgroundColor($r('sys.color.ohos_id_color_primary')) 50 .opacity($r('sys.float.ohos_id_alpha_inapptip_bg')) 51 .onClick((event?: ClickEvent) => { 52 this.jumpCameraTakePhoto().then((result) => { 53 Log.info(TAG, `resourceUri = ${JSON.stringify(result)}`); 54 let want: Want | null = result.want as Want; 55 if(want == null || want.parameters == null) return; 56 this.resultUri = want.parameters['resourceUri'] as string; 57 }).catch((err: Error) => { 58 Log.error(TAG, `jumpCameraTakephoto err: ${err}`); 59 }); 60 }) 61 62 Column() { 63 Image($r('app.media.ic_public_camera_grid_item')) 64 .fillColor($r('sys.color.ohos_id_color_secondary')) 65 .width($r('app.float.camera_icon_size')) 66 .height($r('app.float.camera_icon_size')) 67 .objectFit(ImageFit.Contain) 68 Text(this.selectParams.filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_VIDEO 69 ? $r('app.string.camera_btn_text_shooting') 70 : $r('app.string.camera_btn_text_photo') 71 ) 72 .fontSize($r('sys.float.ohos_id_text_size_body3')) 73 .fontFamily($r('app.string.id_text_font_family_regular')) 74 .fontColor($r('sys.color.ohos_id_color_text_secondary')) 75 .fontWeight(FontWeight.Regular) 76 .margin({ 77 top: $r('app.float.photo_grid_gap') 78 }) 79 } 80 .key('PickerCamera') 81 .justifyContent(FlexAlign.Center) 82 .alignItems(HorizontalAlign.Center) 83 .hitTestBehavior(HitTestMode.Transparent) 84 } 85 .width('100%') 86 .aspectRatio(1) 87 } 88 89 private onShow(): void { 90 Log.info(TAG, `onShow: isShow=${this.isShow}, uri=${this.resultUri}`); 91 if (this.isShow && this.resultUri?.length > 0) { 92 if (this.selectParams.isMultiPick) { 93 this.updateDataFunc && this.updateDataFunc(this.resultUri); 94 } else { 95 this.setPickResult(this.resultUri); 96 } 97 } 98 } 99 100 private async jumpCameraTakePhoto(): Promise<ability.AbilityResult> { 101 let action = this.selectParams.filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_VIDEO 102 ? wantConstant.Action.ACTION_VIDEO_CAPTURE 103 : wantConstant.Action.ACTION_IMAGE_CAPTURE; 104 let uri = this.selectParams.filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_VIDEO 105 ? Constants.CAMERA_TYPE_VIDEO 106 : Constants.CAMERA_TYPE_CAPTURE; 107 interface Msg { 108 action: wantConstant.Action 109 } 110 let msg: Msg = { 111 action: action 112 } 113 ReportToBigDataUtil.report(BigDataConstants.SELECT_PICKER_CLICK_CAMERA_ID, msg); 114 115 let supportMultiMode: boolean = (this.selectParams.filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_ALL); 116 let want: Want = { 117 action: action, 118 bundleName: Constants.CAMERA_BUNDLE_NAME, 119 parameters: { 120 uri: uri, 121 supportMultiMode: supportMultiMode, 122 callBundleName: this.selectParams.bundleName 123 } 124 }; 125 126 Log.debug(TAG, `jump camera want: ${JSON.stringify(want)}`); 127 let context: common.UIAbilityContext = AppStorage.get<common.UIAbilityContext>('photosAbilityContext') as common.UIAbilityContext; 128 let result = await context.startAbilityForResult(want); 129 return result; 130 } 131 132 private setPickResult(uri: string): void { 133 if (uri == null || uri == undefined) { 134 Log.error(TAG, `no valid uri!`); 135 return; 136 } 137 let abilityResult: ability.AbilityResult = { 138 resultCode: (uri == null || uri.length <= 0) ? -1 : 0, 139 want: { 140 parameters: { 141 'select-item-list': [uri], 142 } 143 } 144 }; 145 let storage = LocalStorage.getShared(); 146 if (storage?.has(Constants.PHOTO_PICKER_SESSION_KEY)) { 147 let session = storage?.get<UIExtensionContentSession>(Constants.PHOTO_PICKER_SESSION_KEY); 148 let param = storage?.get<SelectParams>(Constants.PHOTO_PICKER_PARAMS_KEY); 149 try { 150 if (param?.bundleName) { 151 Log.debug(TAG, `grantUriPermission to ${param?.bundleName}`); 152 fileShare.grantUriPermission(uri, 153 param?.bundleName, 154 wantConstant.Flags.FLAG_AUTH_READ_URI_PERMISSION|wantConstant.Flags.FLAG_AUTH_WRITE_URI_PERMISSION, 155 (err: BusinessError):void => { 156 Log.error(TAG, `failed to grantUriPermission to ${param?.bundleName}`); 157 session?.terminateSelfWithResult(abilityResult).then((result: void) => { 158 Log.info(TAG, `session terminateSelfWithResult abilityResult: ${abilityResult} result: ${result}`); 159 }); 160 }); 161 } 162 } catch (err) { 163 Log.error(TAG, `err: ${JSON.stringify(err)}`); 164 session?.terminateSelfWithResult(abilityResult).then((result: void) => { 165 Log.info(TAG, `session terminateSelfWithResult abilityResult: ${abilityResult} result: ${result}`); 166 }); 167 } 168 } else { 169 let context: common.UIAbilityContext = AppStorage.get<common.UIAbilityContext>('photosAbilityContext') as common.UIAbilityContext; 170 context.terminateSelfWithResult(abilityResult as ability.AbilityResult).then((result: void) => { 171 Log.info(TAG, `terminateSelfWithResult abilityResult: ${abilityResult} result: ${result}`); 172 }); 173 } 174 } 175} 176