• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1/*
2 * Copyright (c) 2025 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 { HiLog } from '../../common/HiLog';
17import backgroundTaskManager from '@ohos.resourceschedule.backgroundTaskManager';
18import Constants from '../../common/constant';
19
20const TAG = 'ApplyEfficiencyManager';
21
22export default class ApplyEfficiencyManager {
23  private static instance: ApplyEfficiencyManager;
24  private static applyCount: number = 0;
25
26  private constructor() {
27  }
28
29  static getInstance(): ApplyEfficiencyManager {
30    if (!ApplyEfficiencyManager.instance) {
31      ApplyEfficiencyManager.instance = new ApplyEfficiencyManager();
32    }
33    return ApplyEfficiencyManager.instance;
34  }
35
36  public applyEfficiency() {
37    HiLog.info(TAG, `applyEfficiency applyCount ${ApplyEfficiencyManager.applyCount}`);
38    ApplyEfficiencyManager.applyCount++;
39    this.applyEfficiencyResource();
40  }
41
42  public releaseEfficiency() {
43    HiLog.info(TAG, `releaseEfficiency applyCount ${ApplyEfficiencyManager.applyCount}`);
44    if (--ApplyEfficiencyManager.applyCount === 0) {
45      this.releaseEfficiencyResource();
46    }
47  }
48
49  private applyEfficiencyResource(): boolean {
50    HiLog.info(TAG, 'applyEfficiencyResource');
51    let request: backgroundTaskManager.EfficiencyResourcesRequest = {
52      resourceTypes: backgroundTaskManager.ResourceType.CPU,
53      isApply: true,
54      timeOut: Constants.DECRYPT_TIMEOUT_TIME,
55      reason: 'apply efficiency resource for open dlp file',
56      isPersist: true,
57      isProcess: true
58    };
59    try {
60      backgroundTaskManager.applyEfficiencyResources(request);
61      HiLog.info(TAG, 'applyEfficiencyResources success');
62      return true;
63    } catch (error) {
64      HiLog.wrapError(TAG, error, 'applyEfficiencyResources error');
65      return false;
66    }
67  }
68
69  public releaseEfficiencyResource(): boolean {
70    HiLog.info(TAG, 'releaseEfficiencyResource');
71    try {
72      backgroundTaskManager.resetAllEfficiencyResources();
73      HiLog.info(TAG, 'releaseEfficiencyResource success');
74      return true;
75    } catch (error) {
76      HiLog.wrapError(TAG, error, 'resetAllEfficiencyResources error');
77      return false;
78    }
79  }
80};