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