1//@ts-nocheck
2/*
3 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *     http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16import Log from '../../../../../../../../common/src/main/ets/default/Log';
17import AbilityManager from '../../../../../../../../common/src/main/ets/default/abilitymanager/abilityManager';
18import BundleManager from '../../../../../../../../common/src/main/ets/default/abilitymanager/bundleManager';
19import Bundle from '@ohos.bundle.bundleManager';
20import { BundleInfo } from 'bundleManager/BundleInfo';
21import ResMgr from '@ohos.resourceManager';
22import {BusinessError} from '@ohos.base';
23import SwitchUserManager from '../../../../../../../../common/src/main/ets/default/SwitchUserManager';
24
25const INDEX = 0;
26const IS_INCLUDE_ABILITY_INFO = 0x00000001;
27
28const TAG = 'NotificationManagenment-BundleResourceModel';
29
30export interface BundleItemData {
31  appIcon?: string;
32  appTitle?: string;
33  appValue?: string;
34  appArrow?: string|Resource;
35  appSummary?: string;
36  appBundleName?: string;
37  appIconId?: string;
38  appUri?: string;
39  appUid?: number;
40  systemApp?: boolean;
41}
42
43export default class BundleResourceModel {
44  private readonly mBundleInfoList: BundleItemData[] = [];
45
46  async getAllBundleInfos(): Promise<void> {
47    let userId =(await SwitchUserManager.getInstance().getCurrentUserInfo()).userId;
48    await Bundle.getAllBundleInfo(IS_INCLUDE_ABILITY_INFO, userId).then((data: BundleInfo[]) => {
49      void this.getIconItem(INDEX, data.length, data);
50    });
51    Log.showInfo(TAG, 'getAllBundleInfos end');
52  }
53
54  async getIconItem(index: number, count: number, data: BundleInfo[]): Promise<void> {
55    Log.showInfo(TAG, `getIconItem data.length ${data.length}`);
56    let label = '';
57    let that = this;
58    try {
59      let appInfo = data[index].appInfo;
60      if (appInfo.labelResource.id > 0) {
61        BundleManager.getString(appInfo.labelResource, (value) => {
62          if (value) {
63            label = value;
64          }
65        })
66      } else {
67        label = appInfo.label;
68      }
69      Log.showDebug(TAG, 'getIconItem BundleManager.getString finish label:' + label);
70      if (appInfo.iconResource.id <= 0) {
71        this.nextIconItem(index, count, data, this.mBundleInfoList, that);
72        return;
73      }
74      BundleManager.getMediaBase64(appInfo.iconResource, (value) => {
75        if (value) {
76          Log.showInfo(TAG, `getIconItem BundleManager.getMediaBase64() imageValue:${value}`);
77          let bundleItemData: BundleItemData = {
78            appIcon: value,
79            appTitle: label,
80            appValue: '',
81            appArrow: $r('app.media.ic_settings_arrow'),
82            appSummary: data[index].versionName,
83            appBundleName: data[index].name,
84            appIconId: appInfo.iconId,
85            appUri: 'pages/setEnable',
86            appUid: appInfo.uid,
87            systemApp: appInfo.systemApp
88          };
89          this.mBundleInfoList.push(bundleItemData);
90        }
91        this.nextIconItem(index, count, data, this.mBundleInfoList, that);
92      });
93    } catch (error) {
94      Log.showError(TAG, `getIconItem catch error: ${JSON.stringify(error)}`);
95    }
96  }
97
98  nextIconItem(index: number, count: number, data: BundleInfo[], bundleInfoList: BundleItemData[], that: BundleResourceModel): void {
99    Log.showInfo(TAG, 'nextIconItem index:' + index + ' | count:' + count);
100    if (count - 1 > index) {
101      index = index + 1;
102      void that.getIconItem(index, count, data);
103    } else {
104      AppStorage.SetOrCreate('appManagementList', bundleInfoList);
105    }
106  }
107
108  async getBundleInfo(bundleName: string, callback: {(data: BundleItemData): void}): Promise<void> {
109    let mBundleInfo: BundleItemData = {};
110    let label = '';
111    let userInfo = await SwitchUserManager.getInstance().getCurrentUserInfo();
112
113    await Bundle.getBundleInfo(bundleName, IS_INCLUDE_ABILITY_INFO, userInfo.userId).then((data) => {
114      Log.showInfo(TAG, `getBundleInfo bundleInfo:${JSON.stringify(data)}`);
115      let appInfo = data.appInfo;
116      if (appInfo.labelResource.id > 0) {
117        BundleManager.getString(appInfo.labelResource, (value) => {
118          if (value) {
119            mBundleInfo.appTitle = value;
120          }
121        })
122      }
123      mBundleInfo.appTitle = appInfo.label;
124      mBundleInfo.appValue = '';
125      mBundleInfo.appArrow = $r('app.media.ic_settings_arrow');
126      mBundleInfo.appSummary = data.versionName;
127      mBundleInfo.appBundleName = data.name;
128      mBundleInfo.appIconId = appInfo.iconId;
129      mBundleInfo.appUri = '';
130      mBundleInfo.appUid = appInfo.uid;
131      mBundleInfo.systemApp = appInfo.systemApp;
132      Log.showDebug(TAG, 'getBundleInfo getResourceManager label:' + label);
133      if (appInfo.iconResource.id > 0) {
134        BundleManager.getMediaBase64(appInfo.iconResource, (imageValue) => {
135          if (!!imageValue) {
136            mBundleInfo.appIcon = imageValue;
137          }
138          callback(mBundleInfo);
139        });
140      } else {
141        callback(mBundleInfo);
142      }
143    });
144    Log.showInfo(TAG, 'getBundleInfo end');
145  }
146}
147
148