1/* 2 * Copyright (c) 2022-2023 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 { Log } from '@ohos/common/src/main/ets/default/utils/Log'; 17import type SmartPickerRecommendInfo from './SmartPickerRecommendInfo'; 18import type SmartPickerRecommendInfoListener from './SmartPickerRecommendInfoListener'; 19import SmartPickerConstants from './SmartPickerConstants'; 20import SmartPickerRecommendInfoCallback from './SmartPickerRecommendInfoCallback'; 21import SmartPickerDataAdapter from './SmartPickerDataAdapter'; 22import type common from '@ohos.app.ability.common'; 23 24const TAG: string = 'SmartPickerManager'; 25 26export class SmartPickerManager { 27 private recommendationType: number | undefined = undefined; 28 private tabInfoArray: Array<SmartPickerRecommendInfo> = []; 29 private pickerInfoListeners: Array<SmartPickerRecommendInfoListener> = new Array<SmartPickerRecommendInfoListener>(); 30 private hansNotify: boolean = false; 31 private dataAdapter: SmartPickerDataAdapter = undefined; 32 private context: common.Context; 33 34 constructor(context: common.Context, recommendationOptions: RecommendationOptions, callBundleName: string) { 35 Log.debug(TAG, 'constructor'); 36 this.context = context; 37 this.init(recommendationOptions, callBundleName); 38 } 39 40 async init(recommendationOptions: RecommendationOptions, callBundleName: string): Promise<void> { 41 if (recommendationOptions === undefined || recommendationOptions === null) { 42 Log.error(TAG, 'init recommendationOptions is invalid'); 43 return; 44 } 45 this.recommendationType = recommendationOptions?.recommendationType; 46 Log.error(TAG, 'init this.recommendationType:' + this.recommendationType); 47 if (this.recommendationType === SmartPickerConstants.INVALID_PHOTO_SELECT_TYPE || this.recommendationType === undefined) { 48 return; 49 } 50 try { 51 let recommendInfoCallback: SmartPickerRecommendInfoCallback = new SmartPickerRecommendInfoCallback(this); 52 this.dataAdapter = new SmartPickerDataAdapter(this.context); 53 this.dataAdapter.getTabInfoList(recommendInfoCallback, recommendationOptions); 54 } catch (err) { 55 Log.error(TAG, 'init err:' + err); 56 } 57 } 58 59 addPickerRecommendInfoListener(listener: SmartPickerRecommendInfoListener): void { 60 Log.info(TAG, 'addPickerRecommendInfoListener listener:' + listener); 61 if (listener && this.pickerInfoListeners.indexOf(listener) === SmartPickerConstants.INVALID) { 62 this.pickerInfoListeners.push(listener); 63 if (!this.hansNotify && this.tabInfoArray !== undefined && this.tabInfoArray.length > 0) { 64 this.notifyTabInfoFinished(this.tabInfoArray); 65 } 66 } 67 } 68 69 removePickerRecommendInfoListener(listener: SmartPickerRecommendInfoListener): void { 70 if (!listener) { 71 return; 72 } 73 let index = this.pickerInfoListeners.indexOf(listener); 74 if (index > SmartPickerConstants.INVALID) { 75 this.pickerInfoListeners.splice(index, 1); 76 } 77 } 78 79 notifyTabInfoFinished(tabInfoList: SmartPickerRecommendInfo[]): void { 80 Log.error(TAG, 'notifyTabInfoFinished'); 81 try { 82 if (tabInfoList === null || tabInfoList === undefined || tabInfoList.length <= 0) { 83 Log.error(TAG, 'notifyTabInfoFinished tabInfoList is invalid'); 84 return; 85 } 86 this.tabInfoArray = tabInfoList; 87 if (this.pickerInfoListeners.length === 0) { 88 Log.error(TAG, 'notifyTabInfoFinished pickerInfoListeners is 0'); 89 return; 90 } 91 if (!this.hansNotify) { 92 for (let listener of this.pickerInfoListeners) { 93 listener.onPickerRecommendInfoListReady(this.tabInfoArray, this.dataAdapter); 94 break; 95 } 96 this.hansNotify = true; 97 } 98 Log.debug(TAG, 'notifyTabInfoFinished end'); 99 } catch (err) { 100 Log.error(TAG, 'notifyTabInfoFinished err:' + err); 101 } 102 } 103} 104 105export class RecommendationOptions { 106 recommendationType?: RecommendationType; 107} 108 109enum RecommendationType { 110 QR_OR_BAR_CODE = 1, 111 QR_CODE = 2, 112 BAR_CODE = 3, 113 ID_CARD = 4, 114 PROFILE_PICTURE = 5 115} 116 117