|
|
@@ -3,6 +3,15 @@ import Want from '@ohos.app.ability.Want';
|
|
|
import common from '@ohos.app.ability.common';
|
|
|
import { hilog } from '@kit.PerformanceAnalysisKit';
|
|
|
import { WidgetData, WidgetCommand, EventData, RequestData, WidgetControlParams } from './WidgetTypes';
|
|
|
+import {
|
|
|
+ WIDGET_CONTROL_EVENT,
|
|
|
+ WIDGET_REQUEST_STATE_EVENT,
|
|
|
+ PLAYER_STATE_CHANGED_EVENT,
|
|
|
+ PLAYER_SONG_CHANGED_EVENT,
|
|
|
+ PLAYER_PROGRESS_CHANGED_EVENT,
|
|
|
+ APP_BUNDLE_NAME,
|
|
|
+ APP_ABILITY_NAME
|
|
|
+} from './WidgetEventConstants';
|
|
|
|
|
|
const TAG = 'PlayerControlService';
|
|
|
|
|
|
@@ -30,9 +39,9 @@ export class PlayerControlService {
|
|
|
// 监听播放状态变化事件
|
|
|
const subscribeInfo: commonEventManager.CommonEventSubscribeInfo = {
|
|
|
events: [
|
|
|
- 'com.ttmusic.player.state.changed',
|
|
|
- 'com.ttmusic.player.song.changed',
|
|
|
- 'com.ttmusic.player.progress.changed'
|
|
|
+ PLAYER_STATE_CHANGED_EVENT,
|
|
|
+ PLAYER_SONG_CHANGED_EVENT,
|
|
|
+ PLAYER_PROGRESS_CHANGED_EVENT
|
|
|
]
|
|
|
};
|
|
|
|
|
|
@@ -54,11 +63,12 @@ export class PlayerControlService {
|
|
|
/**
|
|
|
* 发送控制命令到主应用
|
|
|
*/
|
|
|
- async sendControlCommand(command: WidgetCommand, params?: Object): Promise<boolean> {
|
|
|
+ async sendControlCommand(command: WidgetCommand, params?: WidgetControlParams): Promise<boolean> {
|
|
|
try {
|
|
|
+ const defaultParams: WidgetControlParams = {};
|
|
|
const eventData: EventData = {
|
|
|
command: command,
|
|
|
- params: (params as WidgetControlParams) || ({} as WidgetControlParams),
|
|
|
+ params: params || defaultParams,
|
|
|
timestamp: Date.now(),
|
|
|
source: 'widget'
|
|
|
};
|
|
|
@@ -67,7 +77,7 @@ export class PlayerControlService {
|
|
|
const publishInfo: commonEventManager.CommonEventPublishData = {
|
|
|
data: JSON.stringify(eventData)
|
|
|
};
|
|
|
- await commonEventManager.publish('com.ttmusic.widget.control', publishInfo, (err) => {
|
|
|
+ await commonEventManager.publish(WIDGET_CONTROL_EVENT, publishInfo, (err) => {
|
|
|
if (err) {
|
|
|
hilog.error(0x0000, TAG, `Failed to publish event: ${err}`);
|
|
|
}
|
|
|
@@ -91,7 +101,7 @@ export class PlayerControlService {
|
|
|
const requestInfo: commonEventManager.CommonEventPublishData = {
|
|
|
data: JSON.stringify(requestData)
|
|
|
};
|
|
|
- await commonEventManager.publish('com.ttmusic.widget.request.state', requestInfo, (err) => {
|
|
|
+ await commonEventManager.publish(WIDGET_REQUEST_STATE_EVENT, requestInfo, (err) => {
|
|
|
if (err) {
|
|
|
hilog.error(0x0000, TAG, `Failed to publish request state event: ${err}`);
|
|
|
}
|
|
|
@@ -119,10 +129,12 @@ export class PlayerControlService {
|
|
|
async launchMainApp(page?: string): Promise<boolean> {
|
|
|
try {
|
|
|
const want: Want = {
|
|
|
- bundleName: 'com.ttmusic.app',
|
|
|
- abilityName: 'EntryAbility',
|
|
|
+ bundleName: APP_BUNDLE_NAME,
|
|
|
+ abilityName: APP_ABILITY_NAME,
|
|
|
parameters: {
|
|
|
- page: page || 'main'
|
|
|
+ page: page || 'main',
|
|
|
+ source: 'widget', // 标识来源是卡片
|
|
|
+ timestamp: Date.now().toString()
|
|
|
}
|
|
|
};
|
|
|
|
|
|
@@ -133,7 +145,23 @@ export class PlayerControlService {
|
|
|
return true;
|
|
|
} catch (error) {
|
|
|
hilog.error(0x0000, TAG, `Failed to launch main app: ${error}`);
|
|
|
- return false;
|
|
|
+
|
|
|
+ // 如果启动失败,尝试启动到默认页面
|
|
|
+ try {
|
|
|
+ const fallbackWant: Want = {
|
|
|
+ bundleName: APP_BUNDLE_NAME,
|
|
|
+ abilityName: APP_ABILITY_NAME
|
|
|
+ };
|
|
|
+
|
|
|
+ const context = getContext() as common.UIAbilityContext;
|
|
|
+ await context.startAbility(fallbackWant);
|
|
|
+
|
|
|
+ hilog.info(0x0000, TAG, 'Main app launched with fallback method');
|
|
|
+ return true;
|
|
|
+ } catch (fallbackError) {
|
|
|
+ hilog.error(0x0000, TAG, `Fallback launch also failed: ${fallbackError}`);
|
|
|
+ return false;
|
|
|
+ }
|
|
|
}
|
|
|
}
|
|
|
|
|
|
@@ -146,7 +174,7 @@ export class PlayerControlService {
|
|
|
const widgetData = this.convertToWidgetData(data);
|
|
|
|
|
|
// 通知所有监听器
|
|
|
- this.stateListeners.forEach(listener => {
|
|
|
+ this.stateListeners.forEach((listener: (data: WidgetData) => void) => {
|
|
|
try {
|
|
|
listener(widgetData);
|
|
|
} catch (error) {
|