1 /*
2 * Copyright (c) 2022 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
23 namespace OHOS {
24 namespace AppExecFwk {
SortAgingBundles(std::vector<AgingBundleInfo> & bundles)25 void AgingUtil::SortAgingBundles(std::vector<AgingBundleInfo> &bundles)
26 {
27 sort(bundles.begin(), bundles.end(), SortTwoAgingBundleInfos);
28 }
SortTwoAgingBundleInfos(AgingBundleInfo & bundle1,AgingBundleInfo & bundle2)29 bool AgingUtil::SortTwoAgingBundleInfos(AgingBundleInfo &bundle1, AgingBundleInfo &bundle2)
30 {
31 int64_t currentTimeMs = GetNowSysTimeMs();
32 int64_t time10DaysAgo = GetUnusedTimeMsBaseOnCurrentTime(currentTimeMs, AgingConstants::TIME_10_DAYS);
33 int64_t time20DaysAgo = GetUnusedTimeMsBaseOnCurrentTime(currentTimeMs, AgingConstants::TIME_20_DAYS);
34 /*
35 * bundle1 in [10,20) days and bundle2 in [10,20) order by spaceSize;
36 * other order by lastUsedTime;
37 */
38 if (bundle1.GetRecentlyUsedTime() <= time10DaysAgo && bundle1.GetRecentlyUsedTime() > time20DaysAgo
39 && bundle2.GetRecentlyUsedTime() <= time10DaysAgo && bundle2.GetRecentlyUsedTime() > time20DaysAgo) {
40 return bundle1.GetDataBytes() > bundle2.GetDataBytes();
41 } else {
42 return bundle1.GetRecentlyUsedTime() < bundle2.GetRecentlyUsedTime();
43 }
44 return false;
45 }
46
GetUnusedTimeMsBaseOnCurrentTime(int64_t currentTimeMs,int32_t days)47 int64_t AgingUtil::GetUnusedTimeMsBaseOnCurrentTime(int64_t currentTimeMs, int32_t days)
48 {
49 return currentTimeMs - days * AgingRequest::GetOneDayTimeMs();
50 }
51 } // namespace AppExecFwk
52 } // namespace OHOS
53