changes after code review

revert-dabc3590
Nirothipan 8 years ago
parent ad09d0d21f
commit 7fc365f205

@ -32,30 +32,24 @@ class Constants {
static final String WINDOWS_OS = "windows"; static final String WINDOWS_OS = "windows";
static final String WINDOWS_EXTENSION_EXE = ".exe"; static final String WINDOWS_EXTENSION_EXE = ".exe";
static final String WINDOWS_EXTENSION_BAT = ".bat"; static final String WINDOWS_EXTENSION_BAT = ".bat";
static final String NAME = "name="; static final String MAC_HAXM_EXTENSION = ".sh";
static final String MAC_DARWIN = "darwin";
// System properties
static final String SDK_TOOLS_URL = "sdk.tools.url"; static final String SDK_TOOLS_URL = "sdk.tools.url";
static final String PLATFORM_TOOLS_URL = "platform.tools.url"; static final String PLATFORM_TOOLS_URL = "platform.tools.url";
static final String BUILD_TOOL_URL = "build.tools.url"; static final String BUILD_TOOL_URL = "build.tools.url";
static final String PLATFORM_URL = "platform.url"; static final String PLATFORM_URL = "platform.url";
static final String SYSTEM_IMAGE_URL = "sys.img.url"; static final String SYSTEM_IMAGE_URL = "sys.img.url";
static final String HAXM_URL = "haxm.url"; static final String HAXM_URL = "haxm.url";
static final String DOWNLOADED_BUILD_TOOL_NAME = "downloaded.build.tool.name";
static final String BUILD_TOOLS_VERSION = "build.tool.version";
static final String DOWNLOADED_PLATFORM_NAME = "downloaded.platform.name";
static final String TARGET_VERSION = "target.version";
static final String OS_TARGET = "os.target";
static final String DOWNLOADED_BUILD_TOOL_NAME = "downloaded.build.tool.name"; //"android-7.1.1"; // WSO2 AVD specific variables
static final String BUILD_TOOLS_VERSION = "build.tool.version"; //"25.0.2";
static final String DOWNLOADED_PLATFORM_NAME = "downloaded.platform.name"; //"android-6.0";
static final String TARGET_VERSION = "target.version"; //"android-23";
static final String OS_TARGET = "os.target";//"x86";
static final String WSO2_AVD_NAME = "WSO2_AVD"; static final String WSO2_AVD_NAME = "WSO2_AVD";
static final String APK_LOCATION = File.separator + "resources" + File.separator + "android-agent.apk"; static final String APK_LOCATION = File.separator + "resources" + File.separator + "android-agent.apk";
static final String WSO2_CONFIG_LOCATION = File.separator + "resources" + File.separator + "config.ini"; static final String WSO2_CONFIG_LOCATION = File.separator + "resources" + File.separator + "config.ini";
static final String MAC_HAXM_EXTENSION = ".sh";
static final String MAC_DARWIN = "darwin";
// class AndroidCommands{
// static final String LIST_AVD = "-list-avds";
// }
} }

@ -220,10 +220,8 @@ public class TryIt {
private void startAVD() { private void startAVD() {
String wso2AvdLocation = userHome + File.separator + ".android" + File.separator + "avd" + File.separator String wso2AvdLocation = userHome + File.separator + ".android" + File.separator + "avd" + File.separator
+ Constants.WSO2_AVD_NAME + ".avd"; + Constants.WSO2_AVD_NAME + ".avd";
checkForPlatform(); checkForPlatform();
checkForSystemImages(); checkForSystemImages();
if (!new File(wso2AvdLocation).isDirectory()) { if (!new File(wso2AvdLocation).isDirectory()) {
Scanner read = new Scanner(System.in, "UTF-8"); Scanner read = new Scanner(System.in, "UTF-8");
System.out.print("Do you want to create WSO2_AVD with default configs (Y/n)?: "); System.out.print("Do you want to create WSO2_AVD with default configs (Y/n)?: ");
@ -232,7 +230,6 @@ public class TryIt {
return; return;
} }
} }
System.out.println("+----------------------------------------------------------------+"); System.out.println("+----------------------------------------------------------------+");
System.out.println("| WSO2 Android TryIt |"); System.out.println("| WSO2 Android TryIt |");
System.out.println("+----------------------------------------------------------------+"); System.out.println("+----------------------------------------------------------------+");
@ -242,7 +239,6 @@ public class TryIt {
emulatorLocation += Constants.WINDOWS_EXTENSION_EXE; emulatorLocation += Constants.WINDOWS_EXTENSION_EXE;
} }
setExecutePermission(emulatorLocation); setExecutePermission(emulatorLocation);
listAVDs(); listAVDs();
} }
@ -250,7 +246,6 @@ public class TryIt {
* This method gets the available AVDs' name from the system. * This method gets the available AVDs' name from the system.
*/ */
private void listAVDs() { private void listAVDs() {
ArrayList<String> devices = new ArrayList<>(); ArrayList<String> devices = new ArrayList<>();
BufferedReader reader = null; BufferedReader reader = null;
try { try {
@ -264,7 +259,7 @@ public class TryIt {
} }
selectAVD(devices); selectAVD(devices);
} catch (IOException e) { } catch (IOException e) {
//TODO handleException("Unable to list the available AVDs",e);
} finally { } finally {
try { try {
if (reader != null) { if (reader != null) {
@ -308,13 +303,11 @@ public class TryIt {
String avdManagerPath = androidSdkHome + File.separator + "tools" + File.separator + "bin" String avdManagerPath = androidSdkHome + File.separator + "tools" + File.separator + "bin"
+ File.separator + "avdmanager"; + File.separator + "avdmanager";
String androidPath = androidSdkHome + File.separator + "tools" + File.separator + "android"; String androidPath = androidSdkHome + File.separator + "tools" + File.separator + "android";
if (osSuffix.equals(Constants.WINDOWS_OS)) { if (osSuffix.equals(Constants.WINDOWS_OS)) {
avdManagerPath += Constants.WINDOWS_EXTENSION_BAT; avdManagerPath += Constants.WINDOWS_EXTENSION_BAT;
androidPath += Constants.WINDOWS_EXTENSION_BAT; androidPath += Constants.WINDOWS_EXTENSION_BAT;
} }
setExecutePermission(androidPath); setExecutePermission(androidPath);
System.out.println("Creating a new AVD device"); System.out.println("Creating a new AVD device");
try { try {
if (new File(avdManagerPath).exists()) { if (new File(avdManagerPath).exists()) {
@ -377,20 +370,29 @@ public class TryIt {
private void checkBuildTools() { private void checkBuildTools() {
File buildTools = new File(androidSdkHome + File.separator + "build-tools" File buildTools = new File(androidSdkHome + File.separator + "build-tools"
+ File.separator + System.getProperty(Constants.BUILD_TOOLS_VERSION)); + File.separator + System.getProperty(Constants.BUILD_TOOLS_VERSION));
if (!buildTools.exists()) { if (!buildTools.exists()) {
getTools(System.getProperty(Constants.BUILD_TOOL_URL), "_Android-build-tool.zip"); getTools(System.getProperty(Constants.BUILD_TOOL_URL), "_Android-build-tool.zip");
File buildTool = new File(androidSdkHome + File.separator
File buildTool = new File(androidSdkHome + File.separator + System.getProperty(Constants.DOWNLOADED_BUILD_TOOL_NAME)); + System.getProperty(Constants.DOWNLOADED_BUILD_TOOL_NAME));
if(!new File(androidSdkHome + File.separator + "build-tools").exists()
//noinspection ResultOfMethodCallIgnored && !new File(androidSdkHome + File.separator + "build-tools").mkdir()){
new File(androidSdkHome + File.separator + "build-tools").mkdir(); makeDirectoryError("build-tools",androidSdkHome );
//noinspection ResultOfMethodCallIgnored }
buildTool.renameTo(new File(androidSdkHome + File.separator + "build-tools" buildTool.renameTo(new File(androidSdkHome + File.separator + "build-tools"
+ File.separator + System.getProperty(Constants.BUILD_TOOLS_VERSION))); + File.separator + System.getProperty(Constants.BUILD_TOOLS_VERSION)));
} }
} }
/**
* This method make sure whether the directory can be created.
* @param name - name of the folder to be made
* @param location - location to make folder
*/
private void makeDirectoryError(String name,String location){
System.out.println("Unable to make folder named " + name + " in " + location);
System.exit(0);
}
/** /**
* This method halts the system until the emulator is fully booted * This method halts the system until the emulator is fully booted
* if boot process is not completed successfully, rest of the tasks won't be continued. * if boot process is not completed successfully, rest of the tasks won't be continued.
@ -399,7 +401,6 @@ public class TryIt {
BufferedReader reader = null; BufferedReader reader = null;
String readLine; String readLine;
Boolean sysBootComplete = false; Boolean sysBootComplete = false;
do { do {
ProcessBuilder systemBoot = new ProcessBuilder(adbLocation, "shell", "getprop", ProcessBuilder systemBoot = new ProcessBuilder(adbLocation, "shell", "getprop",
"sys.boot_completed"); "sys.boot_completed");
@ -437,7 +438,6 @@ public class TryIt {
*/ */
private void setAndroidSDK() { private void setAndroidSDK() {
sdkConfigFile = new File("sdkLocation"); sdkConfigFile = new File("sdkLocation");
if (!(sdkConfigFile.exists() && !sdkConfigFile.isDirectory())) { if (!(sdkConfigFile.exists() && !sdkConfigFile.isDirectory())) {
Scanner read = new Scanner(System.in, "UTF-8"); Scanner read = new Scanner(System.in, "UTF-8");
System.out.print("Do you have an Android SDK installed on your computer (y/N) ? : "); System.out.print("Do you have an Android SDK installed on your computer (y/N) ? : ");
@ -460,7 +460,6 @@ public class TryIt {
} }
} }
} }
adbLocation = androidSdkHome + File.separator + "platform-tools" + File.separator + "adb"; adbLocation = androidSdkHome + File.separator + "platform-tools" + File.separator + "adb";
if (osSuffix.equals(Constants.WINDOWS_OS)) { if (osSuffix.equals(Constants.WINDOWS_OS)) {
adbLocation += Constants.WINDOWS_EXTENSION_EXE; adbLocation += Constants.WINDOWS_EXTENSION_EXE;
@ -491,8 +490,8 @@ public class TryIt {
String readLine; String readLine;
BufferedReader reader = null; BufferedReader reader = null;
String apkFileLocation = workingDirectory + Constants.APK_LOCATION; String apkFileLocation = workingDirectory + Constants.APK_LOCATION;
String aaptLocation = androidSdkHome + File.separator + "build-tools" + File.separator + System.getProperty(Constants.BUILD_TOOLS_VERSION) String aaptLocation = androidSdkHome + File.separator + "build-tools" + File.separator
+ File.separator + "aapt"; + System.getProperty(Constants.BUILD_TOOLS_VERSION) + File.separator + "aapt";
if (osSuffix.equals(Constants.WINDOWS_OS)) { if (osSuffix.equals(Constants.WINDOWS_OS)) {
aaptLocation += Constants.WINDOWS_EXTENSION_EXE; aaptLocation += Constants.WINDOWS_EXTENSION_EXE;
} }
@ -547,7 +546,6 @@ public class TryIt {
BufferedReader reader = null; BufferedReader reader = null;
Boolean hasAgent = false; Boolean hasAgent = false;
ProcessBuilder listPackages = new ProcessBuilder(adbLocation, "shell", "pm", "list", "packages"); ProcessBuilder listPackages = new ProcessBuilder(adbLocation, "shell", "pm", "list", "packages");
try { try {
Process listPackagesProcess = listPackages.start(); Process listPackagesProcess = listPackages.start();
listPackagesProcess.waitFor(); listPackagesProcess.waitFor();
@ -559,14 +557,14 @@ public class TryIt {
} }
} }
} catch (IOException | InterruptedException ignored) { } catch (IOException | InterruptedException ignored) {
//TODO //
} finally { } finally {
try { try {
if (reader != null) { if (reader != null) {
reader.close(); reader.close();
} }
} catch (IOException ignored) { } catch (IOException ignored) {
//TODO //
} }
} }
return hasAgent; return hasAgent;
@ -577,9 +575,7 @@ public class TryIt {
*/ */
private void installAgent() { private void installAgent() {
String androidAgentLocation = workingDirectory + Constants.APK_LOCATION; String androidAgentLocation = workingDirectory + Constants.APK_LOCATION;
System.out.println("Installing agent ..."); System.out.println("Installing agent ...");
ProcessBuilder installAgentProcessBuilder = new ProcessBuilder(adbLocation, "install", ProcessBuilder installAgentProcessBuilder = new ProcessBuilder(adbLocation, "install",
androidAgentLocation); androidAgentLocation);
try { try {
@ -603,14 +599,13 @@ public class TryIt {
private void startPackage(String[] agents) { private void startPackage(String[] agents) {
String pkg = agents[0]; String pkg = agents[0];
String activity = agents[1]; String activity = agents[1];
ProcessBuilder pkgStartProcessBuilder = new ProcessBuilder(adbLocation, "shell", "am", "start", ProcessBuilder pkgStartProcessBuilder = new ProcessBuilder(adbLocation, "shell", "am", "start",
"-n", pkg + "/" + activity); "-n", pkg + "/" + activity);
try { try {
Process pkgStartProcess = pkgStartProcessBuilder.start(); Process pkgStartProcess = pkgStartProcessBuilder.start();
pkgStartProcess.waitFor(); pkgStartProcess.waitFor();
} catch (InterruptedException ignored) { } catch (InterruptedException ignored) {
// TODO //
} catch (IOException e) { } catch (IOException e) {
handleException("Unable to start WSO2 package", e); handleException("Unable to start WSO2 package", e);
} }
@ -622,11 +617,12 @@ public class TryIt {
private void checkForPlatform() { private void checkForPlatform() {
File platform = new File(androidSdkHome + File.separator + "platforms" + File.separator File platform = new File(androidSdkHome + File.separator + "platforms" + File.separator
+ System.getProperty(Constants.TARGET_VERSION)); + System.getProperty(Constants.TARGET_VERSION));
if (!platform.isDirectory()) { if (!platform.isDirectory()) {
getTools(System.getProperty(Constants.PLATFORM_URL), "_Android-platforms.zip"); getTools(System.getProperty(Constants.PLATFORM_URL), "_Android-platforms.zip");
//noinspection ResultOfMethodCallIgnored if(!new File(androidSdkHome + File.separator + "platforms").exists()
new File(androidSdkHome + File.separator + "platforms").mkdir(); && !new File(androidSdkHome + File.separator + "platforms").mkdir()){
makeDirectoryError("platforms",androidSdkHome);
}
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
new File(androidSdkHome + File.separator + System.getProperty(Constants.DOWNLOADED_PLATFORM_NAME)). new File(androidSdkHome + File.separator + System.getProperty(Constants.DOWNLOADED_PLATFORM_NAME)).
renameTo(new File(androidSdkHome + File.separator + "platforms" renameTo(new File(androidSdkHome + File.separator + "platforms"
@ -647,32 +643,29 @@ public class TryIt {
new File(androidSdkHome + File.separator + "system-images" + File.separator new File(androidSdkHome + File.separator + "system-images" + File.separator
+ System.getProperty(Constants.TARGET_VERSION) + File.separator + "default").mkdirs(); + System.getProperty(Constants.TARGET_VERSION) + File.separator + "default").mkdirs();
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
new File(androidSdkHome + File.separator + System.getProperty(Constants.OS_TARGET)).renameTo(new File(androidSdkHome new File(androidSdkHome + File.separator + System.getProperty(Constants.OS_TARGET))
+ File.separator + "system-images" + File.separator + System.getProperty(Constants.TARGET_VERSION) + File.separator .renameTo(new File(androidSdkHome + File.separator + "system-images" + File.separator
+ "default" + File.separator + System.getProperty(Constants.OS_TARGET))); + System.getProperty(Constants.TARGET_VERSION) + File.separator + "default"
+ File.separator + System.getProperty(Constants.OS_TARGET)));
} }
} }
/** /**
* This method install Hardware_Accelerated Execution_Manager in mac and windows os. * This method install Hardware_Accelerated Execution_Manager in mac and windows os.
*/ */
@SuppressWarnings("ResultOfMethodCallIgnored")
private void installHAXM() { private void installHAXM() {
String haxmLocation = androidSdkHome + File.separator + "extras" + File.separator + "intel" String haxmLocation = androidSdkHome + File.separator + "extras" + File.separator + "intel"
+ File.separator + "Hardware_Accelerated_Execution_Manager"; + File.separator + "Hardware_Accelerated_Execution_Manager";
if (!new File(haxmLocation).isDirectory()) { if (!new File(haxmLocation).isDirectory()) {
//System.out.println("Downloading intel HAXM..."); //System.out.println("Downloading intel HAXM...");
new File(haxmLocation).mkdirs(); if(!new File(haxmLocation).mkdirs()){
makeDirectoryError(haxmLocation,androidSdkHome);
}
String folderName = "_haxm.zip"; String folderName = "_haxm.zip";
getTools(Constants.HAXM_URL, haxmLocation + File.separator getTools(Constants.HAXM_URL, haxmLocation + File.separator
+ folderName); + folderName);
// downloadArtifacts(System.getProperty(Constants.HAXM_URL), haxmLocation + File.separator
// + folderName);
// System.out.println("Configuring HAXM...");
// extractFolder(haxmLocation + File.separator + folderName);
String haxmInstaller = haxmLocation + File.separator + "silent_install"; String haxmInstaller = haxmLocation + File.separator + "silent_install";
if (osSuffix.equals(Constants.WINDOWS_OS)) { if (osSuffix.equals(Constants.WINDOWS_OS)) {
haxmInstaller += Constants.WINDOWS_EXTENSION_BAT; haxmInstaller += Constants.WINDOWS_EXTENSION_BAT;
} else { } else {
@ -749,7 +742,6 @@ public class TryIt {
private void checkCacheImg(String deviceId) { private void checkCacheImg(String deviceId) {
File cacheImg = new File(userHome + File.separator + ".android" File cacheImg = new File(userHome + File.separator + ".android"
+ File.separator + "avd" + File.separator + deviceId + ".avd" + File.separator + "cache.img"); + File.separator + "avd" + File.separator + deviceId + ".avd" + File.separator + "cache.img");
while (!cacheImg.exists()) { while (!cacheImg.exists()) {
System.out.print("."); System.out.print(".");
try { try {
@ -788,19 +780,14 @@ public class TryIt {
try { try {
zip = new ZipFile(file); zip = new ZipFile(file);
String newPath = zipFile.substring(0, zipFile.lastIndexOf(File.separator)); String newPath = zipFile.substring(0, zipFile.lastIndexOf(File.separator));
//noinspection ResultOfMethodCallIgnored
new File(newPath).mkdirs(); new File(newPath).mkdirs();
Enumeration zipFileEntries = zip.entries(); Enumeration zipFileEntries = zip.entries();
while (zipFileEntries.hasMoreElements()) { while (zipFileEntries.hasMoreElements()) {
// grab a zip file entry // grab a zip file entry
ZipEntry entry = (ZipEntry) zipFileEntries.nextElement(); ZipEntry entry = (ZipEntry) zipFileEntries.nextElement();
String currentEntry = entry.getName(); String currentEntry = entry.getName();
File destFile = new File(newPath, currentEntry); File destFile = new File(newPath, currentEntry);
File destinationParent = destFile.getParentFile(); File destinationParent = destFile.getParentFile();
if (destinationParent == null) { if (destinationParent == null) {
destFile.mkdirs(); destFile.mkdirs();
continue; continue;
@ -808,7 +795,6 @@ public class TryIt {
//noinspection ResultOfMethodCallIgnored //noinspection ResultOfMethodCallIgnored
destinationParent.mkdirs(); destinationParent.mkdirs();
} }
if (!entry.isDirectory()) { if (!entry.isDirectory()) {
BufferedInputStream is; BufferedInputStream is;
try { try {
@ -816,12 +802,10 @@ public class TryIt {
int currentByte; int currentByte;
// establish buffer for writing file // establish buffer for writing file
byte data[] = new byte[BUFFER]; byte data[] = new byte[BUFFER];
// write the current file to disk // write the current file to disk
FileOutputStream fos = new FileOutputStream(destFile); FileOutputStream fos = new FileOutputStream(destFile);
BufferedOutputStream dest = new BufferedOutputStream(fos, BufferedOutputStream dest = new BufferedOutputStream(fos,
BUFFER); BUFFER);
// read and write until last byte is encountered // read and write until last byte is encountered
while ((currentByte = is.read(data, 0, BUFFER)) != -1) { while ((currentByte = is.read(data, 0, BUFFER)) != -1) {
dest.write(data, 0, currentByte); dest.write(data, 0, currentByte);

@ -52,7 +52,6 @@ public class TryItEmulator implements Runnable {
StandardCharsets.UTF_8); StandardCharsets.UTF_8);
while ((readLine = reader.readLine()) != null) { while ((readLine = reader.readLine()) != null) {
writer.append(readLine); writer.append(readLine);
writer.append(readLine);
} }
} catch (IOException e) { } catch (IOException e) {
System.out.println("Error in starting " + deviceId); System.out.println("Error in starting " + deviceId);

Loading…
Cancel
Save