1/*
2 * Copyright (c) 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 { AlbumDefine, Constants } from '@ohos/common';
17import type Want from '@ohos.app.ability.Want';
18import type { RecommendationOptions } from '../common/SmartPickerManager';
19import { SmartPickerManager } from '../common/SmartPickerManager';
20import SmartPickerConstants from '../common/SmartPickerConstants';
21
22import { Log } from '@ohos/common/src/main/ets/default/utils/Log';
23import type common from '@ohos.app.ability.common';
24
25const TAG: string = 'SmartPickerUtils';
26
27export class SmartPickerUtils {
28  static readonly THUMBNAIL_WIDTH = 256;
29
30  static initIfNeeded(context: common.Context, want: Want, storage: LocalStorage): boolean {
31    try {
32      let wantParam: Record<string, Object> = want.parameters as Record<string, Object>;
33      let filterMediaType = wantParam?.filterMediaType as string;
34      if (filterMediaType !== AlbumDefine.FILTER_MEDIA_TYPE_IMAGE && filterMediaType !== AlbumDefine.FILTER_MEDIA_TYPE_ALL) {
35        return false;
36      }
37      Log.debug(TAG, 'initIfNeeded wantParam:' + JSON.stringify(wantParam));
38      if (wantParam?.recommendationOptions === null || wantParam?.recommendationOptions === undefined) {
39        return false;
40      }
41      let recommendation: string = JSON.stringify(wantParam?.recommendationOptions);
42      let recommendationOptions: RecommendationOptions = JSON.parse(recommendation);
43
44      if (recommendationOptions === undefined || recommendationOptions === null) {
45        Log.error(TAG, 'initIfNeeded recommendationOptions is invalid');
46        return false;
47      }
48      let callerBundleName = wantParam[Constants.KEY_WANT_PARAMETERS_CALLER_BUNDLE_NAME] as string;
49      Log.info(TAG, 'initIfNeeded callerBundleName:' + callerBundleName + ' storage:' + storage);
50      if (storage) {
51        let smartPickerManager: SmartPickerManager = new SmartPickerManager(context, recommendationOptions, callerBundleName);
52        storage.setOrCreate(SmartPickerConstants.SMART_PICKER_MANAGER, smartPickerManager);
53      }
54      return true;
55    } catch (err) {
56      Log.error(TAG, 'initIfNeeded err:' + err);
57    }
58    return false;
59  }
60
61  static getThumbnailSafe(sourceUri: string, path: string, size?): string {
62    try {
63      if (size) {
64        if (size.width !== 0 && size.height !== 0) {
65          return `${sourceUri}?oper=thumbnail&width=${size.width}&height=${size.height}&path=${path}`;
66        } else {
67          Log.warn(TAG, 'getThumbnailSafe with width==0 and height==0, so do not use thumbnail' + JSON.stringify(size));
68          return `${sourceUri}`;
69        }
70      } else {
71        return `${sourceUri}?oper=thumbnail&width=${SmartPickerUtils.THUMBNAIL_WIDTH}&height=${SmartPickerUtils.THUMBNAIL_WIDTH}&path=${path}`;
72      }
73    } catch (err) {
74      Log.warn(TAG, `get Thumbnail Failed! msg:${err}`);
75      return null;
76    }
77  }
78}