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