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'; 19 20const TAG: string = 'thiSel_CameraGridItemComponent'; 21 22@Component 23export struct CameraGridItemComponent { 24 selectParams: SelectParams = SelectParams.defaultParam(); 25 resultUri: string = ''; 26 @Consume @Watch('onShow') isShow: boolean; 27 updateDataFunc: Function; 28 29 aboutToAppear(): void { 30 Log.info(TAG, `aboutToAppear`); 31 } 32 33 aboutToDisappear(): void { 34 Log.info(TAG, `aboutToDisappear`); 35 } 36 37 build() { 38 Stack() { 39 Column() { 40 } 41 .width('100%') 42 .height('100%') 43 .backgroundColor($r('sys.color.ohos_id_color_primary')) 44 .opacity($r('sys.float.ohos_id_alpha_inapptip_bg')) 45 46 Column() { 47 Image($r('app.media.ic_public_camera_grid_item')) 48 .fillColor($r('sys.color.ohos_id_color_secondary')) 49 .width($r('app.float.camera_icon_size')) 50 .height($r('app.float.camera_icon_size')) 51 .objectFit(ImageFit.Contain) 52 Text(this.selectParams.filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_VIDEO 53 ? $r('app.string.camera_btn_text_shooting') 54 : $r('app.string.camera_btn_text_photo') 55 ) 56 .fontSize($r('sys.float.ohos_id_text_size_body3')) 57 .fontFamily($r('app.string.id_text_font_family_regular')) 58 .fontColor($r('sys.color.ohos_id_color_text_secondary')) 59 .fontWeight(FontWeight.Regular) 60 .margin({ 61 top: $r('app.float.photo_grid_gap') 62 }) 63 } 64 .key('PickerCamera') 65 .justifyContent(FlexAlign.Center) 66 .alignItems(HorizontalAlign.Center) 67 .onClick((event: ClickEvent) => { 68 this.jumpCameraTakePhoto().then((result) => { 69 Log.info(TAG, `resourceUri = ${JSON.stringify(result)}`); 70 this.resultUri = result?.want?.parameters?.resourceUri; 71 }).catch((err) => { 72 Log.error(TAG, `jumpCameraTakephoto err: ${err}`); 73 }); 74 }) 75 } 76 .width('100%') 77 .aspectRatio(1) 78 } 79 80 private onShow(): void { 81 Log.info(TAG, `onShow: isShow=${this.isShow}, uri=${this.resultUri}`); 82 if (this.isShow && this.resultUri?.length > 0) { 83 if (this.selectParams.isMultiPick) { 84 this.updateDataFunc && this.updateDataFunc(this.resultUri); 85 } else { 86 this.setPickResult(this.resultUri); 87 } 88 } 89 } 90 91 private async jumpCameraTakePhoto(): Promise<any> { 92 let action = this.selectParams.filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_VIDEO 93 ? wantConstant.Action.ACTION_VIDEO_CAPTURE 94 : wantConstant.Action.ACTION_IMAGE_CAPTURE; 95 let uri = this.selectParams.filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_VIDEO 96 ? Constants.CAMERA_TYPE_VIDEO 97 : Constants.CAMERA_TYPE_CAPTURE; 98 ReportToBigDataUtil.report(BigDataConstants.SELECT_PICKER_CLICK_CAMERA_ID, { 'action': action }); 99 100 let supportMultiMode: boolean = (this.selectParams.filterMediaType == AlbumDefine.FILTER_MEDIA_TYPE_ALL); 101 let want = { 102 'action': action, 103 'bundleName': Constants.CAMERA_BUNDLE_NAME, 104 'parameters': { 105 uri: uri, 106 supportMultiMode: supportMultiMode, 107 callBundleName: this.selectParams.bundleName 108 } 109 }; 110 111 Log.debug(TAG, `jump camera want: ${JSON.stringify(want)}`); 112 let result = await globalThis.photosAbilityContext.startAbilityForResult(want); 113 return result; 114 } 115 116 private setPickResult(uri: string): void { 117 if (uri == null || uri == undefined) { 118 Log.error(TAG, `no valid uri!`); 119 return; 120 } 121 let abilityResult = { 122 'resultCode': 0, 123 'want': { 124 'parameters': { 125 'select-item-list': [uri], 126 } 127 } 128 }; 129 globalThis.photosAbilityContext.terminateSelfWithResult(abilityResult).then((result) => { 130 Log.info(TAG, `terminateSelf result: ${result}`); 131 }); 132 } 133} 134