|
|
@@ -0,0 +1,153 @@
|
|
|
+/*
|
|
|
+ * Copyright (c) 2025 Huawei Device Co., Ltd.
|
|
|
+ * Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
+ * you may not use this file except in compliance with the License.
|
|
|
+ * You may obtain a copy of the License at
|
|
|
+ *
|
|
|
+ * http://www.apache.org/licenses/LICENSE-2.0
|
|
|
+ *
|
|
|
+ * Unless required by applicable law or agreed to in writing, software
|
|
|
+ * distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
+ * See the License for the specific language governing permissions and
|
|
|
+ * limitations under the License.
|
|
|
+ */
|
|
|
+
|
|
|
+// [Start knock_controller_create]
|
|
|
+import { harmonyShare, systemShare } from '@kit.ShareKit';
|
|
|
+import { fileUri } from '@kit.CoreFileKit';
|
|
|
+import { BusinessError } from '@kit.BasicServicesKit';
|
|
|
+import { uniformTypeDescriptor } from '@kit.ArkData';
|
|
|
+import { common } from '@kit.AbilityKit';
|
|
|
+// [StartExclude knock_controller_create]
|
|
|
+// import { VIDEO_SOURCES, VideoData } from '../model/VideoData';
|
|
|
+import { window } from '@kit.ArkUI';
|
|
|
+import { VideoItem } from '../viewmodel/VideoItem';
|
|
|
+import Logger from '../common/util/Logger';
|
|
|
+import { LogUtil } from '@pura/harmony-utils';
|
|
|
+
|
|
|
+const TAG = 'KnockController';
|
|
|
+// [EndExclude knock_controller_create]
|
|
|
+export class KnockController {
|
|
|
+ private static controller: KnockController;
|
|
|
+ private context: common.UIAbilityContext | undefined = undefined;
|
|
|
+ // Knock listening status
|
|
|
+ private isKnockListening: boolean = false;
|
|
|
+ private songList: Array<VideoItem>| undefined = [];
|
|
|
+
|
|
|
+ public static getInstance(context: common.UIAbilityContext): KnockController {
|
|
|
+ if (!KnockController.controller) {
|
|
|
+ KnockController.controller = new KnockController(context);
|
|
|
+ }
|
|
|
+ return KnockController.controller;
|
|
|
+ }
|
|
|
+
|
|
|
+ constructor(context: common.UIAbilityContext) {
|
|
|
+ this.context = context;
|
|
|
+ }
|
|
|
+
|
|
|
+ // [Start share_linking]
|
|
|
+ /**
|
|
|
+ * knock listening callback
|
|
|
+ * @param target After the Huawei Share event is triggered,
|
|
|
+ * you can call back the parameters and share them across devices.
|
|
|
+ */
|
|
|
+ public immersiveCallback(target: harmonyShare.SharableTarget) {
|
|
|
+ // [StartExclude knock_controller_create]
|
|
|
+ // share app linking
|
|
|
+ LogUtil.info('onecold KnockController 碰一碰 ');
|
|
|
+ try {
|
|
|
+ let curIndex: number = AppStorage.get('curIndex') as number;
|
|
|
+ this.songList = AppStorage.get('songList');
|
|
|
+ console.info('onecold KnockController 碰一碰 curIndex '+curIndex);
|
|
|
+ console.info('onecold KnockController 碰一碰 this.songList '+this.songList?.length);
|
|
|
+ if(this.songList){
|
|
|
+ let mediaData: VideoItem = this.songList[curIndex];
|
|
|
+ // Video thumbnail image sandbox path
|
|
|
+ let coverUri: string = `${mediaData.pixelMapPath}`;
|
|
|
+ console.info('onecold KnockController 碰一碰 filePath '+coverUri);
|
|
|
+ // Get video thumbnail URI path
|
|
|
+ let shareData: systemShare.SharedData = new systemShare.SharedData({
|
|
|
+ // Set the shared data type to Link
|
|
|
+ utd: uniformTypeDescriptor.UniformDataType.AUDIO,
|
|
|
+ uri:mediaData.filePath,
|
|
|
+ // The shared App Linking link is replaced with the real address here
|
|
|
+ // content: `https://www.xgplayer.com?curIndex=${curIndex}`,
|
|
|
+ thumbnailUri: coverUri,
|
|
|
+ title: mediaData.name,
|
|
|
+ description: mediaData.artist
|
|
|
+ });
|
|
|
+ // Initiate a share
|
|
|
+ target.share(shareData).then(() => {
|
|
|
+ Logger.info(TAG, 'Share link success');
|
|
|
+ }).catch((error: BusinessError) => {
|
|
|
+ Logger.error(TAG, `Share link error. code: ${error.code}, message: ${error.message}`);
|
|
|
+ });
|
|
|
+ }
|
|
|
+
|
|
|
+
|
|
|
+ } catch (err) {
|
|
|
+ Logger.error(TAG, `Share link exception. code: ${err.code}, message: ${err.message}`);
|
|
|
+ }
|
|
|
+ // [EndExclude knock_controller_create]
|
|
|
+ }
|
|
|
+ // [End share_linking]
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Add knock listening
|
|
|
+ */
|
|
|
+ public immersiveListening() {
|
|
|
+ if (canIUse('SystemCapability.Collaboration.HarmonyShare') && !this.isKnockListening) {
|
|
|
+ harmonyShare.on('knockShare', (target: harmonyShare.SharableTarget) => {
|
|
|
+ this.immersiveCallback(target);
|
|
|
+ });
|
|
|
+ this.isKnockListening = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * remove knock listening
|
|
|
+ */
|
|
|
+ public immersiveDisableListening() {
|
|
|
+ if (canIUse('SystemCapability.Collaboration.HarmonyShare') && this.isKnockListening) {
|
|
|
+ harmonyShare.off('knockShare');
|
|
|
+ this.isKnockListening = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Add knock listening in 2in1 device type.
|
|
|
+ */
|
|
|
+ public immersiveListeningPC() {
|
|
|
+ if (canIUse('SystemCapability.Collaboration.HarmonyShare') && !this.isKnockListening) {
|
|
|
+ window.getLastWindow(this.context).then((data) => {
|
|
|
+ let mainWindowID: number = data.getWindowProperties().id;
|
|
|
+ // harmonyShare.on('knockShare', { windowId:mainWindowID }, (target: harmonyShare.SharableTarget) => {
|
|
|
+ // this.immersiveCallback(target);
|
|
|
+ // });
|
|
|
+
|
|
|
+ harmonyShare.on('knockShare', (target: harmonyShare.SharableTarget) => {
|
|
|
+ this.immersiveCallback(target);
|
|
|
+ });
|
|
|
+ })
|
|
|
+
|
|
|
+ this.isKnockListening = true;
|
|
|
+ }
|
|
|
+ }
|
|
|
+
|
|
|
+ /**
|
|
|
+ * Remove knock listening in 2in1 device type.
|
|
|
+ */
|
|
|
+ public immersiveDisableListeningPC() {
|
|
|
+ if (canIUse('SystemCapability.Collaboration.HarmonyShare') && this.isKnockListening) {
|
|
|
+ window.getLastWindow(this.context).then((data) => {
|
|
|
+ let mainWindowID: number = data.getWindowProperties().id;
|
|
|
+ // harmonyShare.off('knockShare', { windowId:mainWindowID });
|
|
|
+ harmonyShare.off('knockShare');
|
|
|
+ })
|
|
|
+
|
|
|
+ this.isKnockListening = false;
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
+// [End knock_controller_create]
|