1 /*
2  * Copyright (C) 2017 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 package com.android.tradefed.targetprep;
17 
18 import com.android.tradefed.build.IBuildInfo;
19 import com.android.tradefed.config.Option;
20 import com.android.tradefed.config.OptionClass;
21 import com.android.tradefed.device.DeviceNotAvailableException;
22 import com.android.tradefed.device.ITestDevice;
23 
24 import java.util.ArrayList;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.concurrent.TimeUnit;
28 
29 /**
30  * Reads the list of packages on the phone and sets all packages to be 'last used' 24 hrs ago.
31  * Writes to /data/system/package-usage.list and deletes it at teardown.
32  */
33 @OptionClass(alias = "set-packages-recently-used")
34 public class SetPackagesRecentlyUsed extends BaseTargetPreparer implements ITargetCleaner {
35 
36     private static final String LINE_PREFIX = "package:";
37     private static final String PACKAGE_USAGE_FILE = "/data/system/package-usage.list";
38 
39     @Option(
40         name = "package-recently-used-time",
41         description = "Time since package last used",
42         isTimeVal = true
43     )
44     private long mRecentTimeMillis = TimeUnit.DAYS.toMillis(1);
45 
46     @Option(
47             name = "package-recently-used-name",
48             description = "Package(s) to set. If none specified, then all are set."
49     )
50     private List<String> mPackages = new ArrayList<>();
51 
52     @Override
setUp(ITestDevice device, IBuildInfo buildInfo)53     public void setUp(ITestDevice device, IBuildInfo buildInfo)
54             throws TargetSetupError, BuildError, DeviceNotAvailableException {
55         long deviceTimeMillis = TimeUnit.SECONDS.toMillis(device.getDeviceDate());
56         long deviceRecentMillis = deviceTimeMillis - mRecentTimeMillis;
57         StringBuilder builder = new StringBuilder();
58         builder.append("PACKAGE_USAGE__VERSION_1\n");
59         for (String p : getPackagesToSet(device)) {
60             if (p.startsWith(LINE_PREFIX)) {
61                 builder.append(p.substring(LINE_PREFIX.length()));
62                 builder.append(" ");
63                 builder.append(Long.toString(deviceRecentMillis));
64                 builder.append(" 0 0 0 0 0 0 0\n");
65             }
66         }
67         device.pushString(builder.toString(), PACKAGE_USAGE_FILE);
68     }
69 
getPackagesToSet(ITestDevice device)70     private List<String> getPackagesToSet(ITestDevice device) throws DeviceNotAvailableException {
71         if (mPackages.isEmpty()) {
72             String[] packages = device.executeShellCommand("cmd package list package").split("\n");
73             return Arrays.asList(packages);
74         } else {
75             return mPackages;
76         }
77     }
78 
79     @Override
tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)80     public void tearDown(ITestDevice device, IBuildInfo buildInfo, Throwable e)
81             throws DeviceNotAvailableException {
82         device.executeShellCommand("rm " + PACKAGE_USAGE_FILE);
83     }
84 }
85