1 /*
2  * Copyright (C) 2012 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.config.Option;
19 import com.android.tradefed.config.OptionClass;
20 import com.android.tradefed.device.DeviceNotAvailableException;
21 import com.android.tradefed.device.ITestDevice;
22 import com.android.tradefed.invoker.TestInformation;
23 
24 import java.util.ArrayList;
25 import java.util.Collection;
26 
27 /** A {@link ITargetPreparer} that wipes user data on the device. */
28 @OptionClass(alias = "erase-user-data")
29 public class EraseUserDataPreparer extends BaseTargetPreparer {
30 
31     @Option(name="wipe-skip-list", description=
32             "list of /data subdirectories to NOT wipe when doing UserDataFlashOption.TESTS_ZIP")
33         private Collection<String> mDataWipeSkipList = new ArrayList<String>();
34 
35     @Option(name="wait-for-available",
36             description="Wait until device is available for testing before performing erase")
37     private boolean mWaitForAvailable = false;
38 
39     private ITestsZipInstaller mTestsZipInstaller = null;
40 
41     /** {@inheritDoc} */
42     @Override
setUp(TestInformation testInfo)43     public void setUp(TestInformation testInfo)
44             throws TargetSetupError, BuildError, DeviceNotAvailableException {
45         if (isDisabled()) {
46             return;
47         }
48         ITestDevice device = testInfo.getDevice();
49         if (mWaitForAvailable) {
50             device.waitForDeviceAvailable();
51         }
52 
53         device.enableAdbRoot();
54         if (mTestsZipInstaller == null) {
55             if (mDataWipeSkipList.isEmpty()) {
56                 // Maintain backwards compatibility
57                 // TODO: deprecate and remove
58                 mDataWipeSkipList.add("media");
59             }
60             mTestsZipInstaller = new DefaultTestsZipInstaller(mDataWipeSkipList);
61         }
62         mTestsZipInstaller.deleteData(device);
63         device.reboot();
64     }
65 }
66