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 featureAbility from '@ohos.ability.featureAbility' 17import Want from '@ohos.app.ability.Want'; 18import abilityAccessCtrl from '@ohos.abilityAccessCtrl'; 19import bundle from '@ohos.bundle.bundleManager'; 20import deviceManager from '@ohos.distributedDeviceManager'; 21import promptAction from '@ohos.promptAction'; 22import { BusinessError } from '@ohos.base'; 23import hilog from '@ohos.hilog'; 24 25const TAG: string = 'PagePageAbilitySecond' 26const domain: number = 0xFF00; 27 28@Entry 29@Component 30struct PagePageAbilitySecond { 31 @State deviceID: string = ''; 32 33 async requestPermission(): Promise<void> { 34 hilog.info(domain, TAG, 'RequestPermission begin'); 35 let array: Array<string> = ['ohos.permission.DISTRIBUTED_DATASYNC']; 36 let bundleFlag = 0; 37 let tokenID: number | undefined = undefined; 38 let userID = 100; 39 let appInfo: bundle.ApplicationInfo = await bundle.getApplicationInfo('com.samples.famodelabilitydevelop', bundleFlag, userID); 40 tokenID = appInfo.accessTokenId; 41 let atManager = abilityAccessCtrl.createAtManager(); 42 let requestPermissions: Array<string> = []; 43 for (let i = 0;i < array.length; i++) { 44 let result = await atManager.verifyAccessToken(tokenID, array[i]); 45 hilog.info(domain, TAG, 'checkAccessToken result:' + JSON.stringify(result)); 46 if (result != abilityAccessCtrl.GrantStatus.PERMISSION_GRANTED) { 47 requestPermissions.push(array[i]); 48 } 49 } 50 hilog.info(domain, TAG, 'requestPermissions:' + JSON.stringify(requestPermissions)); 51 if (requestPermissions.length == 0) { 52 return; 53 } 54 let context = featureAbility.getContext(); 55 context.requestPermissionsFromUser(requestPermissions, 1, (error, data) => { 56 hilog.info(domain, TAG, 'error:' + error.message + ',data:' + JSON.stringify(data)); 57 hilog.info(domain, TAG, 'data requestCode:' + data.requestCode); 58 hilog.info(domain, TAG, 'data permissions:' + data.permissions); 59 hilog.info(domain, TAG, 'data authResults:' + data.authResults); 60 }); 61 hilog.info(domain, TAG, 'RequestPermission end'); 62 } 63 64 getRemoteDeviceId(): void { 65 let dmClass: deviceManager.DeviceManager; 66 dmClass = deviceManager.createDeviceManager('com.samples.famodelabilitydevelop'); 67 try { 68 if (typeof dmClass === 'object' && dmClass !== null) { 69 let list = dmClass.getAvailableDeviceListSync(); 70 if (typeof (list) == undefined || list.length == 0) { 71 hilog.info(domain, TAG, 'EntryAbility onButtonClick getRemoteDeviceId err: list is null'); 72 return; 73 } 74 hilog.info(domain, TAG, `EntryAbility onButtonClick getRemoteDeviceId success[${list.length}]:` + JSON.stringify(list[0])); 75 if (list[0].networkId != undefined) { 76 this.deviceID = list[0].networkId; 77 } 78 promptAction.showToast({ 79 message: this.deviceID 80 }); 81 } else { 82 hilog.info(domain, TAG, 'EntryAbility onButtonClick getRemoteDeviceId err: dmClass is null'); 83 } 84 } catch (error) { 85 hilog.info(domain, TAG, `getRemoteDeviceId error, error=${error}, message=${error.message}`); 86 } 87 } 88 89 onStartRemoteAbility(): void { 90 hilog.info(domain, TAG, 'onStartRemoteAbility begin'); 91 let wantValue: Want = { 92 bundleName: 'ohos.samples.distributedmusicplayer', 93 abilityName: 'ohos.samples.distributedmusicplayer.MainAbility', 94 deviceId: this.deviceID, // this.deviceID的获取方式在前面的示例代码中 95 }; 96 hilog.info(domain, TAG, 'onStartRemoteAbility want=' + JSON.stringify(wantValue)); 97 featureAbility.startAbility({ 98 want: wantValue 99 }).then((data) => { 100 promptAction.showToast({ 101 message: $r('app.string.start_remote_success_toast') 102 }); 103 hilog.info(domain, TAG, 'onStartRemoteAbility finished, ' + JSON.stringify(data)); 104 }).catch((error: BusinessError) => { 105 promptAction.showToast({ 106 message: JSON.stringify(error) 107 }); 108 hilog.error(domain, TAG, 'onStartRemoteAbility failed: ' + JSON.stringify(error)); 109 }); 110 hilog.info(domain, TAG, 'onStartRemoteAbility end'); 111 } 112 113 build() { 114 Column() { 115 Row() { 116 Flex({ justifyContent: FlexAlign.Start, alignContent: FlexAlign.Center }) { 117 Text($r('app.string.pageAbility_second_button')) 118 .fontSize(24) 119 .fontWeight(FontWeight.Bold) 120 .textAlign(TextAlign.Start) 121 .margin({ top: 12, bottom: 11, right: 24, left: 24 }) 122 } 123 } 124 .width('100%') 125 .height(56) 126 .justifyContent(FlexAlign.Start) 127 .backgroundColor($r('app.color.backGrounding')) 128 129 List({ initialIndex: 0 }) { 130 ListItem() { 131 Row() { 132 Row() { 133 Text($r('app.string.obtain_permissions_button')) 134 .textAlign(TextAlign.Start) 135 .fontWeight(FontWeight.Medium) 136 .margin({ top: 13, bottom: 13, left: 0, right: 8 }) 137 .fontSize(16) 138 .width(232) 139 .height(22) 140 .fontColor($r('app.color.text_color')) 141 } 142 .height(48) 143 .width('100%') 144 .borderRadius(24) 145 .margin({ top: 4, bottom: 4, left: 12, right: 84 }) 146 } 147 .onClick(() => { 148 this.requestPermission(); 149 }) 150 } 151 .height(56) 152 .backgroundColor($r('app.color.start_window_background')) 153 .borderRadius(24) 154 .margin({ top: 8, right: 12, left: 12 }) 155 156 ListItem() { 157 Row() { 158 Row() { 159 Text($r('app.string.obtain_device_id_button')) 160 .textAlign(TextAlign.Start) 161 .fontWeight(FontWeight.Medium) 162 .margin({ top: 13, bottom: 13, left: 0, right: 8 }) 163 .fontSize(16) 164 .width(232) 165 .height(22) 166 .fontColor($r('app.color.text_color')) 167 } 168 .height(48) 169 .width('100%') 170 .borderRadius(24) 171 .margin({ top: 4, bottom: 4, left: 12, right: 84 }) 172 } 173 .onClick(() => { 174 this.getRemoteDeviceId(); 175 }) 176 } 177 .height(56) 178 .backgroundColor($r('app.color.start_window_background')) 179 .borderRadius(24) 180 .margin({ top: 12, right: 12, left: 12 }) 181 182 ListItem() { 183 Row() { 184 Row() { 185 Text($r('app.string.start_remote_ability_button')) 186 .textAlign(TextAlign.Start) 187 .fontWeight(FontWeight.Medium) 188 .margin({ top: 13, bottom: 13, left: 0, right: 8 }) 189 .fontSize(16) 190 .width(232) 191 .height(22) 192 .fontColor($r('app.color.text_color')) 193 } 194 .height(48) 195 .width('100%') 196 .borderRadius(24) 197 .margin({ top: 4, bottom: 4, left: 12, right: 84 }) 198 } 199 .onClick(() => { 200 this.onStartRemoteAbility(); 201 }) 202 } 203 .height(56) 204 .backgroundColor($r('app.color.start_window_background')) 205 .borderRadius(24) 206 .margin({ top: 12, right: 12, left: 12 }) 207 } 208 .height('100%') 209 .backgroundColor($r('app.color.backGrounding')) 210 } 211 .width('100%') 212 .margin({ top: 8 }) 213 .backgroundColor($r('app.color.backGrounding')) 214 } 215}