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