blob: 6516525fbe2cea0ba10766eb0c67ea74d8e76195 [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
Adnan Begovicd5fdee92015-11-13 15:28:21 -080017import 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/17/15.
27 */
28public class FastbootCommand extends Command {
Adnan Begovicd5fdee92015-11-13 15:28:21 -080029 private static final String FASTBOOT_COMMAND = "fastboot";
30 private static final String REBOOT = "reboot";
31 private static final String DEVICES = "devices";
32 private static final String FLASH = "flash";
33
34 private String[] baseCommand;
35 private String baseArg;
36 private String image;
37 private String targetImage;
38
39 public FastbootCommand(int command, String[] args) {
40 switch (command) {
Adnan Begovicd5fdee92015-11-13 15:28:21 -080041 case Types.FASTBOOT_FLASH:
42 baseCommand = new String[] { FASTBOOT_COMMAND };
43 baseArg = FLASH;
44 image = args[0];
45 targetImage = args[1];
46 break;
47 case Types.FASTBOOT_DEVICES:
48 baseCommand = new String[] { FASTBOOT_COMMAND };
49 baseArg = DEVICES;
50 break;
51 case Types.FASTBOOT_REBOOT:
52 baseCommand = new String[] { FASTBOOT_COMMAND };
53 baseArg = REBOOT;
54 break;
55 default:
56 throw new UnsupportedOperationException("Unsupported operation " + command);
57 }
58 }
59
60 @Override
61 public void run() {
62 List<String> commandList = new ArrayList<String>(
63 baseCommand.length + 1);
64 commandList.addAll(Arrays.asList(baseCommand));
65 if (baseArg != null && baseArg.length() > 0) {
66 commandList.add(baseArg);
67 }
68 if (image != null &&image.length() > 0) {
69 commandList.add(image);
70 }
71 if (targetImage != null &&targetImage.length() > 0) {
72 commandList.add(targetImage);
73 }
74 String[] commands = commandList.toArray(new String[commandList.size()]);
75
76 if (MigrationTest.DEBUG) {
77 System.out.println("Using commands: " + Arrays.toString(commands));
78 }
79 try {
80 final Process process = Runtime.getRuntime().exec(commands);
81 final InputStream err = process.getErrorStream();
82
83 // Send error output to stderr.
84 Thread errThread = new Thread() {
85 @Override
86 public void run() {
87 copy(err, System.err);
88 }
89 };
90 errThread.setDaemon(true);
91 errThread.start();
92
93 BufferedReader in = new BufferedReader(
94 new InputStreamReader(process.getInputStream()));
95 String rx = in.readLine();
96 if (rx != null) {
97 if (MigrationTest.DEBUG) {
98 System.out.println("Received response " + rx);
99 }
100 }
101
102 try {
103 Thread.sleep(10000);
104 } catch (InterruptedException e) {
105 e.printStackTrace();
106 }
107
108 in.close();
109 err.close();
110 //Gross
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800111 process.destroy();
112 } catch (IOException e) {
113 System.err.println("Error ");
114 e.printStackTrace();
115 }
116 }
117
118 public final class Types {
Adnan Begovicd5fdee92015-11-13 15:28:21 -0800119 public static final int FASTBOOT_FLASH = 0;
120 public static final int FASTBOOT_DEVICES = 1;
121 public static final int FASTBOOT_REBOOT = 2;
122 }
123}