--- /dev/null
+package com.samsung.tizen.common.connection.explorer;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.lang.reflect.InvocationTargetException;
+import java.util.ArrayList;
+
+import org.eclipse.core.runtime.IProgressMonitor;
+import org.eclipse.jface.dialogs.ProgressMonitorDialog;
+import org.eclipse.jface.operation.IRunnableWithProgress;
+import org.eclipse.swt.SWT;
+import org.eclipse.swt.widgets.Display;
+import org.eclipse.swt.widgets.Shell;
+
+import com.samsung.tizen.common.properties.InstallPathConfig;
+import com.samsung.tizen.sdblib.IDevice;
+import com.samsung.tizen.sdblib.SdbCommandRejectedException;
+import com.samsung.tizen.sdblib.SdbShellProcess;
+import com.samsung.tizen.sdblib.SyncService;
+import com.samsung.tizen.sdblib.SyncService.SyncResult;
+import com.samsung.tizen.sdblib.TimeoutException;
+
+class PackageInstallMonitorDialog extends ProgressMonitorDialog {
+ public PackageInstallMonitorDialog(Shell parent) {
+ super(parent);
+ int shellStyle = getShellStyle();
+ shellStyle &= ~SWT.APPLICATION_MODAL;
+ setShellStyle(shellStyle);
+ }
+}
+
+class InstallProgress implements IRunnableWithProgress {
+
+ final private String DEVELOPER_PATH = "/home/developer/";
+ final private String TOOLS_TARGET_PATH = "/home/developer/sdk_tools/";
+ final private String TOOLS_HOST_PATH = InstallPathConfig.getSDKPath()
+ + "/SDK/develop-tool/";
+ final private String CMD_RESULT_CHECK = "; echo $?";
+
+ private IDevice device = null;
+
+ IProgressMonitor monitor = null;
+
+ private boolean checkDirectory(String dir) {
+ boolean checkResult = false;
+ SdbShellProcess lsProc;
+ try {
+ lsProc = device.executeShellCommand("ls " + dir + CMD_RESULT_CHECK);
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ lsProc.getInputStream()));
+ String lsOut = null;
+ String lastOut = null;
+ while (null != (lsOut = br.readLine())) {
+ lastOut = lsOut;
+ }
+ if (null != lastOut && lastOut.equals("0")) {
+ checkResult = true;
+ }
+ } catch (IOException e) {
+ return false;
+ }
+ return checkResult;
+ }
+
+ private boolean makeDirectory(String dir) {
+ boolean makeResult = false;
+ SdbShellProcess lsProc;
+ try {
+ lsProc = device.executeShellCommand("mkdir " + dir
+ + CMD_RESULT_CHECK);
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ lsProc.getInputStream()));
+ String lsOut = null;
+ String lastOut = null;
+ while (null != (lsOut = br.readLine())) {
+ lastOut = lsOut;
+ }
+ if (null != lastOut && lastOut.equals("0")) {
+ makeResult = true;
+ }
+ br.close();
+ } catch (IOException e) {
+ return false;
+ }
+ return makeResult;
+ }
+
+ private ArrayList<String> getPackageList() {
+ ArrayList<String> packageList = new ArrayList<String>();
+ try {
+ Process lsProc = Runtime.getRuntime().exec(
+ "ls " + TOOLS_HOST_PATH + " -1");
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ lsProc.getInputStream()));
+
+ String lsMsg = null;
+ while (null != (lsMsg = br.readLine())) {
+ if (lsMsg.contains(".tar") && lsMsg.contains("_")) {
+ packageList.add(lsMsg);
+ }
+ }
+ br.close();
+ return packageList;
+
+ } catch (IOException e) {
+ return packageList;
+ }
+ }
+
+ private boolean isInstalled(String packageFile) {
+ boolean ret = false;
+ try {
+ SdbShellProcess lsProc = device.executeShellCommand("ls "
+ + TOOLS_TARGET_PATH + "."
+ + getPackageNameFromFileName(packageFile) + ".txt"
+ + CMD_RESULT_CHECK);
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ lsProc.getInputStream()));
+ String lsOut = null;
+ String lastOut = null;
+ while (null != (lsOut = br.readLine())) {
+ lastOut = lsOut;
+ }
+ if (null != lastOut && lastOut.equals("0")) {
+ ret = true;
+ }
+ br.close();
+ } catch (IOException e) {
+ return true;
+ }
+ return ret;
+
+ }
+
+ private boolean copyPackage(String packageFile) {
+ boolean pushResult = false;
+ try {
+ SyncService syncService = device.getSyncService();
+ SyncResult pushSyncResult = syncService.pushFile(TOOLS_HOST_PATH
+ + packageFile, TOOLS_TARGET_PATH + packageFile,
+ SyncService.getNullProgressMonitor());
+ if (SyncService.RESULT_OK == pushSyncResult.getCode()) {
+ pushResult = true;
+ }
+ } catch (TimeoutException e) {
+ return false;
+ } catch (SdbCommandRejectedException e) {
+ return false;
+ } catch (IOException e) {
+ return false;
+ }
+ return pushResult;
+ }
+
+ private void unzipPackage(String packageFile) {
+ try {
+ SdbShellProcess tarProc = device.executeShellCommand("cd "
+ + TOOLS_TARGET_PATH + " && tar -xzf " + packageFile);
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ tarProc.getInputStream()));
+ while (null != br.readLine()) {
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ private void makeInstallFile(String packageName) {
+
+ try {
+ SdbShellProcess echoProc = device
+ .executeShellCommand("echo installed > "
+ + TOOLS_TARGET_PATH + "." + packageName + ".txt");
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ echoProc.getInputStream()));
+ while (null != br.readLine()) {
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+
+ }
+
+ private void removeTar(String packageFile) {
+ try {
+ SdbShellProcess echoProc = device.executeShellCommand("rm "
+ + TOOLS_TARGET_PATH + packageFile);
+ BufferedReader br = new BufferedReader(new InputStreamReader(
+ echoProc.getInputStream()));
+ while (null != br.readLine()) {
+ }
+ } catch (IOException e) {
+ e.printStackTrace();
+ }
+ }
+
+ final private void installPackage(String packageFile) {
+ String packageName = getPackageNameFromFileName(packageFile);
+ boolean isCopied = copyPackage(packageFile);
+ monitor.worked(5);
+ if (isCopied) {
+ unzipPackage(packageFile);
+ monitor.worked(2);
+ makeInstallFile(packageName);
+ monitor.worked(1);
+ removeTar(packageFile);
+ monitor.worked(1);
+ }
+ }
+
+ private void delay() {
+ if (device.isOffline()) {
+ for (int i = 0; i < 20; i++) {
+ try {
+ Thread.sleep(50);
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ if (device.isOnline()) {
+ break;
+ }
+ }
+ }
+ }
+
+ private String getPackageNameFromFileName(String fileName) {
+ String packageName = null;
+ String[] packageElement = fileName.split("_");
+ if (null != packageElement && packageElement.length > 1) {
+ packageName = packageElement[0];
+ }
+ return packageName;
+ }
+
+ @Override
+ public void run(IProgressMonitor monitor) throws InvocationTargetException,
+ InterruptedException {
+
+ // mkdir /home/developer/sdk_tools/
+ final IProgressMonitor fmonitor = monitor;
+ this.monitor = monitor;
+ delay();
+
+ if (device.isEmulator()) {
+ return;
+ }
+ if (device.isOffline()) {
+ return;
+ }
+ monitor.beginTask("Package Install Progress", 100);
+ monitor.subTask("Prepare install");
+ // make derectory
+ if (!checkDirectory(TOOLS_TARGET_PATH)) {
+ if (checkDirectory(DEVELOPER_PATH)) {
+ // mkdir 'home/developer/sdk_tools'
+ makeDirectory(TOOLS_TARGET_PATH);
+ } else {
+ // mkdir 'home/developer'
+ makeDirectory(DEVELOPER_PATH);
+ // mkdir 'home/developer/sdk_tools'
+ makeDirectory(TOOLS_TARGET_PATH);
+ }
+ }
+
+ // if (!checkDirectory(TOOLS_TARGET_PATH)) {
+ // makeDirectory(TOOLS_TARGET_PATH);
+ // }
+
+ monitor.worked(10);
+
+ // get package list to install
+ final ArrayList<String> packageList = getPackageList();
+ monitor.worked(15);
+
+ // install loop (cp, tar)
+ int packageCnt = packageList.size();
+ for (int i = 0; i < packageCnt; i++) {
+ final boolean isInstalled = isInstalled(packageList.get(i));
+ monitor.subTask("Install "
+ + getPackageNameFromFileName(packageList.get(i)));
+ monitor.worked(1);
+ final int index = i;
+ Thread thread = new Thread(null, new Runnable() {
+
+ @Override
+ public void run() {
+ if (!isInstalled) {
+ installPackage(packageList.get(index));
+ } else {
+ fmonitor.worked(7);
+ }
+ }
+ }, "package check and install");
+ thread.start();
+ }
+ monitor.done();
+ }
+
+ public void setDevice(IDevice device) {
+ this.device = device;
+ }
+
+}
+
+public class PackageInstaller {
+
+ public static void installPackages(IDevice device) {
+ final IDevice tDevice = device;
+ Display.getDefault().asyncExec(new Runnable() {
+
+ @Override
+ public void run() {
+ final PackageInstallMonitorDialog installDialog = new PackageInstallMonitorDialog(
+ Display.getDefault().getActiveShell());
+ try {
+ InstallProgress installProgress = new InstallProgress();
+ installProgress.setDevice(tDevice);
+ installDialog.run(true, false, installProgress);
+ } catch (InvocationTargetException e) {
+ return;
+ } catch (InterruptedException e) {
+ return;
+ }
+
+ }
+ });
+
+ }
+}