1/*
2 * Copyright (C) 2010 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
17package vogar.android;
18
19import com.google.common.collect.ImmutableList;
20import java.io.File;
21import java.io.FileNotFoundException;
22import java.util.List;
23import java.util.regex.Matcher;
24import java.util.regex.Pattern;
25import vogar.Run;
26import vogar.Target;
27import vogar.commands.Command;
28
29public final class AdbTarget extends Target {
30    private final Run run;
31
32    public AdbTarget(Run run) {
33        this.run = run;
34    }
35
36    @Override public File defaultDeviceDir() {
37        return new File("/data/local/tmp/vogar");
38    }
39
40    @Override public List<String> targetProcessPrefix() {
41        return ImmutableList.of("adb", "shell");
42    }
43
44    // TODO: pull the methods from androidsdk into here
45
46    @Override public void await(File directory) {
47        run.androidSdk.waitForDevice();
48        run.androidSdk.ensureDirectory(directory);
49        run.androidSdk.remount();
50    }
51
52    @Override public List<File> ls(File directory) throws FileNotFoundException {
53        return run.androidSdk.deviceFilesystem.ls(directory);
54    }
55
56    @Override public String getDeviceUserName() {
57        // TODO: move this to device set up
58        // The default environment doesn't include $USER, so dalvikvm doesn't set "user.name".
59        // DeviceDalvikVm uses this to set "user.name" manually with -D.
60        String line = new Command(run.log, "adb", "shell", "id").execute().get(0);
61        // TODO: use 'id -un' when we don't need to support anything older than M
62        Matcher m = Pattern.compile("^uid=\\d+\\((\\S+)\\) gid=\\d+\\(\\S+\\).*").matcher(line);
63        return m.matches() ? m.group(1) : "root";
64    }
65
66    @Override public void rm(File file) {
67        run.androidSdk.rm(file);
68    }
69
70    @Override public void mkdirs(File file) {
71        run.androidSdk.deviceFilesystem.mkdirs(file);
72    }
73
74    @Override public void forwardTcp(int port) {
75        run.androidSdk.forwardTcp(port);
76    }
77
78    @Override public void push(File local, File remote) {
79        run.androidSdk.push(local, remote);
80    }
81
82    @Override public void pull(File remote, File local) {
83        run.androidSdk.pull(remote, local);
84    }
85}
86