1/* 2 * Copyright (c) 2021-2024 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 */ 15import appAccount from '@ohos.account.appAccount'; 16 17let authenticator; 18 19class MyAuthenticator extends appAccount.Authenticator { 20 authenticate(name, authType, callerBundleName, options, callback) { 21 let randNumber = Math.ceil(Math.random() * 10000); 22 let newTokenData = 'service/authenticate/tokenInfo' + randNumber; 23 console.log('====>Service authenticate name: ' + name + ', authType: ' + authType + ', options: ' + JSON.stringify(options)); 24 callback.onResult(0, { 'name': name, 'authType': authType, 'token': newTokenData }); 25 } 26 27 addAccountImplicitly(authType, callerBundleName, options, callback) { 28 console.log('====>Service addAccountImplicitly authType: ' + authType + ' callerBundleName: ' + callerBundleName + 'options: ' + JSON.stringify(options)); 29 callback.onRequestRedirected({ 30 bundleName: 'com.ohos.openharmonyappdemo.service', 31 abilityName: 'com.ohos.openharmonyappdemo.service.settingAbility', 32 }); 33 } 34 35 auth(name, authType, callerBundleName, callback) { 36 let randNumber = Math.ceil(Math.random() * 10000); 37 let newTokenData = 'service/authenticate/tokenInfo' + randNumber; 38 console.log('====>Service authenticate name: ' + name + ', authType: ' + authType); 39 callback.onResult(0, { 'name': name, 'authType': authType, 'token': newTokenData }); 40 } 41 42 createAccountImplicitly(options, callback) { 43 console.log('====>Service createAccountImplicitly options: ' + JSON.stringify(options)); 44 callback.onRequestRedirected({ 45 bundleName: 'com.ohos.openharmonyappdemo.service', 46 abilityName: 'com.ohos.openharmonyappdemo.service.settingAbility', 47 }); 48 } 49} 50 51class ServiceAbility { 52 onStart() { 53 console.info('ServiceAbility onStart'); 54 } 55 56 onStop() { 57 console.info('ServiceAbility onStop'); 58 } 59 60 onCommand(want, startId) { 61 console.info('ServiceAbility onCommand'); 62 console.info('ServiceAbility want: ' + JSON.stringify(want)); 63 console.info('ServiceAbility startId : ' + startId); 64 } 65 66 onConnect(want) { 67 console.info('ServiceAbility onConnect start'); 68 try { 69 console.info('ServiceAbility want: ' + typeof (want)); 70 console.info('ServiceAbility want: ' + JSON.stringify(want)); 71 console.info('ServiceAbility want name: ' + want.bundleName); 72 } catch (err) { 73 console.info('ServiceAbility error: ' + err); 74 } 75 console.info('ServiceAbility onConnect start'); 76 authenticator = new MyAuthenticator(); 77 return authenticator.getRemoteObject(); 78 } 79 80 onDisconnect(want) { 81 console.info('ServiceAbility onDisconnect'); 82 console.info('ServiceAbility want: ' + JSON.stringify(want)); 83 } 84} 85 86export default new ServiceAbility();