• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2022 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 Logger from './Logger'
17
18const TAG = '[Sample_StartMode]'
19let contextCaller, want
20
21interface EventHub {
22  emit(event: string, ...args: Object[]): void
23}
24
25interface AbilityContext {
26  eventHub: EventHub
27}
28
29export function getContextData(): any {
30  Logger.info(TAG, 'UtilPage getContextData start')
31  let context = getContext(this) as AbilityContext
32  let data = {
33    context: null,
34    launchWant: null
35  }
36  context.eventHub.emit("getAbilityData", data)
37  contextCaller = data.context // 拿到全局的context,即类似globalThis.mainAbilityContext
38  want = data.launchWant
39  Logger.info(TAG, 'UtilPage contextCaller ' + JSON.stringify(contextCaller))
40  return { 'want': want }
41}
42
43export function startMode(wantParameters: any, abilityName: string) {
44  Logger.info(TAG, `${abilityName} start`)
45  getContextData()
46  let want = {
47    bundleName: 'ohos.samples.startmode',
48    abilityName: abilityName,
49    parameters: wantParameters
50  }
51
52  Logger.info(TAG, `${abilityName} contextCaller ${JSON.stringify(contextCaller)}`)
53  contextCaller.startAbility(want).catch(err => {
54    Logger.info(TAG, 'err is' + JSON.stringify(err))
55  })
56  Logger.info(TAG, `${abilityName} end`)
57}
58
59export function totast() {
60  AlertDialog.show(
61    {
62      message: $r('app.string.totast'),
63      secondaryButton: {
64        value: 'ok',
65        action: () => {
66          Logger.info(TAG, 'Callback when the second button is clicked')
67        }
68      }
69    }
70  )
71}