blob: bdc54640f6a2a1fbd08c398e14d5a7c65c7176f5 [file] [log] [blame]
Adnan Begovica3baf9c2015-11-29 14:34:56 -08001/*
2 * Copyright (C) 2015 The CyanogenMod 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
17import java.io.BufferedReader;
18import java.io.IOException;
19import java.io.InputStream;
20import java.io.InputStreamReader;
21import java.util.ArrayList;
22import java.util.Arrays;
23import java.util.List;
24
25/**
26 * Created by adnan on 11/29/15.
27 */
28public class AdbCommand extends Command {
29 private static final int MAX_RETRIES = 20;
30 private static final String[] ADB_REBOOT_BOOTLOADER = new String[] {
31 "adb", "reboot", "bootloader"
32 };
33 private static final String[] ADB_CHECK_BOOT_COMPLETE = new String[] {
34 "adb", "shell", "getprop", "sys.boot_completed"
35 };
36
37 private String[] baseCommand;
38
39 public AdbCommand(int command) {
40 switch (command) {
41 case Types.REBOOT_BOOTLOADER:
42 baseCommand = ADB_REBOOT_BOOTLOADER;
43 break;
44 case Types.CHECK_BOOT_COMPLETE:
45 baseCommand = ADB_CHECK_BOOT_COMPLETE;
46 break;
47 default:
48 throw new UnsupportedOperationException("Unsupported operation " + command);
49 }
50 }
51
52 @Override
53 public void run() {
54 List<String> commandList = new ArrayList<String>(
55 baseCommand.length + 1);
56 commandList.addAll(Arrays.asList(baseCommand));
Paul Keithc9f1d672019-02-17 05:20:04 +010057 String[] commands = commandList.toArray(new String[0]);
Adnan Begovica3baf9c2015-11-29 14:34:56 -080058
59 if (MigrationTest.DEBUG) {
60 System.out.println("Using commands: " + Arrays.toString(commands));
61 }
62 try {
63 Process process = Runtime.getRuntime().exec(commands);
64 final InputStream err = process.getErrorStream();
65 // Send error output to stderr.
66 Thread errThread = new Thread() {
67 @Override
68 public void run() {
69 copy(err, System.err);
70 }
71 };
72 errThread.setDaemon(true);
73 errThread.start();
74
75 BufferedReader in = new BufferedReader(
76 new InputStreamReader(process.getInputStream()));
77 String rx = in.readLine();
78 if (!baseCommand.equals(ADB_CHECK_BOOT_COMPLETE)) {
79 if (rx != null) {
80 if (MigrationTest.DEBUG) {
81 System.out.println("Received response " + rx);
82 }
83 }
84
85 try {
86 Thread.sleep(10000);
87 } catch (InterruptedException e) {
88 e.printStackTrace();
89 }
90
91 in.close();
92 err.close();
93 process.destroy();
94 } else {
95 for (int i = 1; i < MAX_RETRIES; i++) {
96 process = Runtime.getRuntime().exec(commands);
97 in = new BufferedReader(new InputStreamReader(process.getInputStream()));
98 if ((rx = in.readLine()) != null) {
99 if (rx.equals("1")) {
100 in.close();
101 process.destroy();
102 try {
103 System.out.println("Device up detected...");
104 Thread.sleep(10000);
105 } catch (InterruptedException e) {
106 e.printStackTrace();
107 }
108 break;
109 }
110 }
111 try {
112 System.out.println("Waiting for device to come up...");
113 Thread.sleep(10000);
114 } catch (InterruptedException e) {
115 e.printStackTrace();
116 }
117 in.close();
118 process.destroy();
119 }
120 }
121 } catch (IOException e) {
122 System.err.println("Error ");
123 e.printStackTrace();
124 }
125 }
126
127 public final class Types {
128 public static final int REBOOT_BOOTLOADER = 0;
129 public static final int CHECK_BOOT_COMPLETE = 1;
130 }
131}