Skip to content

Commit

Permalink
Merge pull request #713 from Chilledheart/harmony_implement_start_sto…
Browse files Browse the repository at this point in the history
…p_worker
  • Loading branch information
Keeyou authored Jan 30, 2024
2 parents 7121a5a + 42f95de commit e69a7b6
Show file tree
Hide file tree
Showing 4 changed files with 113 additions and 174 deletions.
6 changes: 4 additions & 2 deletions harmony/entry/src/main/cpp/types/libyass/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,5 +7,7 @@ export const getPassword: () => string;
export const getCipher: () => string;
export const getCipherStrings: () => string[];
export const getTimeout: () => number;
export const init: () => undefined;
export const destroy: () => undefined;
export const init: () => void;
export const destroy: () => void;
export const startWorker: (cb: (err_code: number) => void) => void;
export const stopWorker: (cb: () => void) => void;
16 changes: 8 additions & 8 deletions harmony/entry/src/main/ets/entryability/EntryAbility.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,40 +10,40 @@ import yass from 'libyass.so';
*/
export default class EntryAbility extends UIAbility {
onCreate(want: Want, launchParam: AbilityConstant.LaunchParam): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onCreate');
hilog.info(0x0000, 'yass', '%{public}s', 'Ability onCreate');
yass.init();
}

onDestroy(): void {
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onDestroy');
hilog.info(0x0000, 'yass', '%{public}s', 'Ability onDestroy');
yass.destroy();
}

onWindowStageCreate(windowStage: window.WindowStage): void {
// Main window is created, set main page for this ability
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageCreate');
hilog.info(0x0000, 'yass', '%{public}s', 'Ability onWindowStageCreate');

windowStage.loadContent("pages/DetailPage", (err, data) => {
if (err.code) {
hilog.error(0x0000, 'testTag', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
hilog.error(0x0000, 'yass', 'Failed to load the content. Cause: %{public}s', JSON.stringify(err) ?? '');
return;
}
hilog.info(0x0000, 'testTag', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
hilog.info(0x0000, 'yass', 'Succeeded in loading the content. Data: %{public}s', JSON.stringify(data) ?? '');
});
}

onWindowStageDestroy(): void {
// Main window is destroyed, release UI related resources
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onWindowStageDestroy');
hilog.info(0x0000, 'yass', '%{public}s', 'Ability onWindowStageDestroy');
}

onForeground(): void {
// Ability has brought to foreground
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onForeground');
hilog.info(0x0000, 'yass', '%{public}s', 'Ability onForeground');
}

onBackground(): void {
// Ability has back to background
hilog.info(0x0000, 'testTag', '%{public}s', 'Ability onBackground');
hilog.info(0x0000, 'yass', '%{public}s', 'Ability onBackground');
}
}
48 changes: 46 additions & 2 deletions harmony/entry/src/main/ets/pages/DetailPage.ets
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,27 @@ import { DetailListComponent } from '../view/DetailListComponent';
import { CommonConstants } from '../constants/CommonConstants';
import { DataItem } from '../viewmodel/DataItem';
import AboutViewModel from '../viewmodel/AboutViewModel';
import hilog from '@ohos.hilog';
import yass from 'libyass.so';

/**
* Detail page. Click the item on about page to jump to the detail page.
*/
enum StartState {
STOPPED,
STOPPING,
STARTED,
STARTING
}

@Entry
@Component
struct DetailPage {
private titleParam: Resource = $r('app.string.title_name');
private dataParam: Array<DataItem>;

private state: StartState = StartState.STOPPED;

aboutToAppear() {
let appInfo: DataItem[] = AboutViewModel.getYassInfo()
this.titleParam = appInfo[0].title
Expand Down Expand Up @@ -52,9 +63,19 @@ struct DetailPage {
right: $r('app.float.grid_row_margin_right')
})
Row() {
Button($r('app.string.start_button'))
Button($r('app.string.start_button'), { type: ButtonType.Capsule,
stateEffect: this.state == StartState.STOPPED }).onClick((event: ClickEvent) => {
if (this.state == StartState.STOPPED) {
this.onStartClicked();
}
})
Blank()
Button($r('app.string.stop_button'))
Button($r('app.string.stop_button'), { type: ButtonType.Capsule,
stateEffect: this.state == StartState.STARTED }).onClick((event: ClickEvent) => {
if (this.state == StartState.STARTED) {
this.onStopClicked();
}
})
}
}
.width(CommonConstants.DETAIL_COLUMN_WIDTH_PERCENT)
Expand Down Expand Up @@ -83,4 +104,27 @@ struct DetailPage {
.width(CommonConstants.FULL_WIDTH_PERCENT)
.height($r('app.float.title_height'))
}

onStartClicked() {
this.state = StartState.STARTING;
yass.startWorker((error_code: number) => {
hilog.info(0x0000, 'yass', 'started %d', error_code);
if (error_code == 0) {
this.state = StartState.STARTED;
AlertDialog.show({ message: 'The start is successful' })
} else {
this.state = StartState.STOPPED;
AlertDialog.show({ message: 'The start failed' })
}
})
}

onStopClicked() {
this.state = StartState.STOPPING;
yass.stopWorker(() => {
hilog.info(0x0000, 'yass', 'stopped %d');
this.state = StartState.STOPPED;
AlertDialog.show({ message: 'The stop is successful' })
})
}
}
Loading

0 comments on commit e69a7b6

Please sign in to comment.