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
16 #include "aging/aging_util.h"
17
18 #include "app_log_wrapper.h"
19 #include "aging/aging_bundle_info.h"
20 #include "aging/aging_constants.h"
21 #include "aging/aging_request.h"
22 #include "bundle_util.h"
23
24 namespace OHOS {
25 namespace AppExecFwk {
GetNowSysTimeMs()26 int64_t AgingUtil::GetNowSysTimeMs()
27 {
28 return BundleUtil::GetCurrentTimeMs();
29 }
30
SortAgingBundles(std::vector<AgingBundleInfo> & bundles)31 void AgingUtil::SortAgingBundles(std::vector<AgingBundleInfo> &bundles)
32 {
33 sort(bundles.begin(), bundles.end(), SortTwoAgingBundleInfos);
34 }
35
SortTwoAgingBundleInfos(AgingBundleInfo & bundle1,AgingBundleInfo & bundle2)36 bool AgingUtil::SortTwoAgingBundleInfos(AgingBundleInfo &bundle1, AgingBundleInfo &bundle2)
37 {
38 int64_t currentTimeMs = GetNowSysTimeMs();
39 int64_t time10DaysAgo = GetUnusedTimeMsBaseOnCurrentTime(currentTimeMs, AgingConstants::TIME_10_DAYS);
40 int64_t time20DaysAgo = GetUnusedTimeMsBaseOnCurrentTime(currentTimeMs, AgingConstants::TIME_20_DAYS);
41 int64_t time30DaysAgo = GetUnusedTimeMsBaseOnCurrentTime(currentTimeMs, AgingConstants::TIME_30_DAYS);
42 bool sortByUseTimes =
43 (bundle1.GetRecentlyUsedTime() >= time10DaysAgo && bundle2.GetRecentlyUsedTime() >= time10DaysAgo) ||
44 (bundle1.GetRecentlyUsedTime() < time10DaysAgo && bundle1.GetRecentlyUsedTime() >= time20DaysAgo &&
45 bundle2.GetRecentlyUsedTime() < time10DaysAgo && bundle2.GetRecentlyUsedTime() >= time20DaysAgo) ||
46 (bundle1.GetRecentlyUsedTime() < time20DaysAgo && bundle1.GetRecentlyUsedTime() >= time30DaysAgo &&
47 bundle2.GetRecentlyUsedTime() < time20DaysAgo && bundle2.GetRecentlyUsedTime() >= time30DaysAgo) ||
48 (bundle1.GetRecentlyUsedTime() < time30DaysAgo && bundle2.GetRecentlyUsedTime() < time30DaysAgo);
49 if (sortByUseTimes && (bundle1.GetStartCount() != bundle2.GetStartCount())) {
50 return bundle1.GetStartCount() < bundle2.GetStartCount();
51 }
52
53 return bundle1.GetRecentlyUsedTime() <= bundle2.GetRecentlyUsedTime();
54 }
55
GetUnusedTimeMsBaseOnCurrentTime(int64_t currentTimeMs,int32_t days)56 int64_t AgingUtil::GetUnusedTimeMsBaseOnCurrentTime(int64_t currentTimeMs, int32_t days)
57 {
58 return currentTimeMs - days * AgingRequest::GetOneDayTimeMs();
59 }
60 } // namespace AppExecFwk
61 } // namespace OHOS
62