• 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 notification from '@ohos.notification';
17import featureAbility from '@ohos.ability.featureAbility';
18import wantAgent from '@ohos.wantAgent';
19import backgroundTaskManager from '@ohos.backgroundTaskManager';
20import particleAbility from '@ohos.ability.particleAbility';
21import commonEvent from '@ohos.commonEvent';
22
23const TAG = "BGMODE_TEST ";
24
25let event;
26
27let request = {
28    content: {
29        contentType: notification.ContentType.NOTIFICATION_CONTENT_BASIC_TEXT,
30        normal: {
31            title: "title",
32            text: "text"
33        }
34    },
35    id: 1
36};
37
38let wantAgentInfo = {
39    wants: [
40        {
41            bundleName: "ohos.acts.resourceschedule.taskmgr.js.function",
42            abilityName: "ohos.acts.resourceschedule.taskmgr.js.function.MainAbility"
43        }
44    ],
45    operationType: wantAgent.OperationType.START_ABILITY,
46    requestCode: 0,
47    wantAgentFlags: [wantAgent.WantAgentFlags.UPDATE_PRESENT_FLAG]
48};
49
50function callback(err, data) {
51    if (err) {
52        console.error(TAG + " Operation failed");
53    } else {
54        console.info(TAG + " Operation succeed");
55        commonEvent.publish(event, (err) => {
56            if (err.code) {
57                console.error(TAG + "PublishCallBack failed");
58            } else {
59                console.info(TAG + "Publish succeed");
60            }
61        })
62    }
63}
64
65function startContinuousTaskUseApi7Callback() {
66    event = "startTaskUseApi7Callback";
67    particleAbility.startBackgroundRunning(1, request, callback);
68}
69
70function stopContinuousTaskUseApi7Callback() {
71    event = "stopTaskUseApi7Callback";
72    particleAbility.startBackgroundRunning(1, request).then(() => {
73        particleAbility.cancelBackgroundRunning(callback);
74    });
75}
76
77function startContinuousTaskUseApi8Callback() {
78    event = "startTaskUseApi8Callback";
79    wantAgent.getWantAgent(wantAgentInfo).then((data) => {
80        backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
81            backgroundTaskManager.BackgroundMode.LOCATION, data, callback);
82    });
83}
84
85function stopContinuousTaskUseApi8Callback() {
86    event = "stopTaskUseApi8Callback";
87    wantAgent.getWantAgent(wantAgentInfo).then((data) => {
88        return backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
89            backgroundTaskManager.BackgroundMode.LOCATION, data);
90    }).then(() => {
91        backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext(), callback);
92    });
93}
94
95function startContinuousTaskUseApi7Promise() {
96    particleAbility.startBackgroundRunning(1, request).then(() => {
97        commonEvent.publish("startTaskUseApi7Promise", (err) => {
98            if (err.code) {
99                console.error(TAG + "PublishCallBack failed");
100            } else {
101                console.info(TAG + "Publish succeed");
102            }
103        });
104    });
105}
106
107function stopContinuousTaskUseApi7Promise() {
108    particleAbility.startBackgroundRunning(1, request).then(() => {
109        return particleAbility.cancelBackgroundRunning();
110    }).then(() => {
111        commonEvent.publish("stopTaskUseApi7Promise", (err) => {
112            if (err.code) {
113                console.error(TAG + "PublishCallBack failed");
114            } else {
115                console.info(TAG + "Publish succeed");
116            }
117        });
118    });
119}
120
121function startContinuousTaskUseApi8Promise() {
122    wantAgent.getWantAgent(wantAgentInfo).then((data) => {
123        return backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
124            backgroundTaskManager.BackgroundMode.LOCATION, data);
125    }).then(() => {
126        commonEvent.publish("startTaskUseApi8Promise", (err) => {
127            if (err.code) {
128                console.error(TAG + "PublishCallBack failed");
129            } else {
130                console.info(TAG + "Publish succeed");
131            }
132        });
133    });
134}
135
136function stopContinuousTaskUseApi8Promise() {
137    wantAgent.getWantAgent(wantAgentInfo).then((data) => {
138        return backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
139            backgroundTaskManager.BackgroundMode.LOCATION, data);
140    }).then(() => {
141        return backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext());
142    }).then(() => {
143        commonEvent.publish("stopTaskUseApi8Promise", (err) => {
144            if (err.code) {
145                console.error(TAG + "PublishCallBack failed");
146            } else {
147                console.info(TAG + "Publish succeed");
148            }
149        })
150    })
151}
152
153function startContinuousTaskInvalidBgmode() {
154    wantAgent.getWantAgent(wantAgentInfo).then((data) => {
155        return backgroundTaskManager.startBackgroundRunning(featureAbility.getContext(),
156            backgroundTaskManager.BackgroundMode.BLUETOOTH_INTERACTION, data);
157    }).catch(() => {
158        commonEvent.publish("startTaskInvalidBgmode", (err) => {
159            if (err.code) {
160                console.error(TAG + "PublishCallBack failed");
161            } else {
162                console.info(TAG + "Publish succeed");
163            }
164        });
165    });
166}
167
168function handleOption(want) {
169    switch (want.parameters.option) {
170        case "testcase1":
171            startContinuousTaskUseApi7Callback();
172            break;
173        case "testcase2":
174            stopContinuousTaskUseApi7Callback();
175            break;
176        case "testcase3":
177            startContinuousTaskUseApi8Callback();
178            break;
179        case "testcase4":
180            stopContinuousTaskUseApi8Callback();
181            break;
182        case "testcase5":
183            startContinuousTaskUseApi7Promise();
184            break;
185        case "testcase6":
186            stopContinuousTaskUseApi7Promise();
187            break;
188        case "testcase7":
189            startContinuousTaskUseApi8Promise();
190            break;
191        case "testcase8":
192            stopContinuousTaskUseApi8Promise();
193            break;
194        case "testcase9":
195            startContinuousTaskInvalidBgmode();
196            break;
197        default:
198            console.warn(TAG + 'Unknown option');
199            break;
200    }
201}
202
203export default {
204    onStart() {
205        console.info(TAG + 'ServiceAbility onStart');
206    },
207    onStop() {
208        console.info(TAG + 'ServiceAbility onStop');
209    },
210    onCommand(want, startId) {
211        console.info(TAG + 'ServiceAbility onCommand');
212        console.info(TAG + "Get onCommand want: " + JSON.stringify(want));
213        particleAbility.cancelBackgroundRunning();
214        backgroundTaskManager.stopBackgroundRunning(featureAbility.getContext());
215        handleOption(want);
216    },
217    onConnect(want) {
218        console.info(TAG + 'ServiceAbility onConnect');
219    },
220    onDisConnect(want) {
221        console.info(TAG + 'ServiceAbility onDisConnect');
222    },
223};