[Title] fix to update device name after connecting is succeeded.
authorhyunsik.noh <hyunsik.noh@samsung.com>
Fri, 22 Feb 2013 06:59:06 +0000 (15:59 +0900)
committerhyunsik.noh <hyunsik.noh@samsung.com>
Fri, 22 Feb 2013 07:39:39 +0000 (16:39 +0900)
[Type]
[Module]common
[Priority]
[CQ#]
[Redmine#]8515
[Problem]
[Cause]
[Solution]

Change-Id: Ica826adef6f229f2d3876231fb375b17e94c0692

org.tizen.common.sdblib/src/org/tizen/sdblib/Device.java
org.tizen.common.sdblib/src/org/tizen/sdblib/DeviceMonitor.java

index 21b4347..74591e6 100644 (file)
@@ -21,14 +21,12 @@ import java.io.IOException;
 
 import org.tizen.sdblib.SyncService.SyncResult;
 
-
-
 /**
  * A Device. It can be a physical device or an emulator.
  */
 final class Device implements IDevice {
-       
-    private final static int INSTALL_TIMEOUT = 2*60*1000; //2min
+
+    private final static int INSTALL_TIMEOUT = 2 * 60 * 1000; // 2min
 
     final static String COMMAND_ARCHITECTURE_INFO = "uname -m";
     final static String EMULATOR = "emulator";
@@ -48,7 +46,7 @@ final class Device implements IDevice {
     private DeviceMonitor mMonitor;
 
     private static final String LOG_TAG = "Device";
-    
+
     private SdbShellProcess sdbShellProcess = null;
 
     public String getSerialNumber() {
@@ -88,12 +86,11 @@ final class Device implements IDevice {
         return mState == DeviceState.OFFLINE;
     }
 
-    public SyncService getSyncService()
-            throws TimeoutException, SdbCommandRejectedException, IOException {
+    public SyncService getSyncService() throws TimeoutException, SdbCommandRejectedException, IOException {
         SyncService syncService = new SyncService(SmartDevelopmentBridge.getSocketAddress(), this);
         if (syncService.openSync()) {
             return syncService;
-         }
+        }
 
         return null;
     }
@@ -101,55 +98,49 @@ final class Device implements IDevice {
     public FileListingService getFileListingService() {
         return new FileListingService(this);
     }
-    
+
     public SdbShellProcess executeShellCommand(String command) throws IOException {
-       SmartDevelopmentBridge sdb = SmartDevelopmentBridge.getBridge();
-       if (sdb == null)
-               return null;
-       String sdbPath = sdb.getSdbOsLocation();
-       if (sdbPath == null)
-               return null;
-       if (mSerialNumber == null)
-               return null;
-       
-       String sdbShellCmd = sdbPath + " -s " + mSerialNumber + " shell";
-       Process pSdb = Runtime.getRuntime().exec(sdbShellCmd);
-       sdbShellProcess = new SdbShellProcess(pSdb, command);
-       return sdbShellProcess;
+        SmartDevelopmentBridge sdb = SmartDevelopmentBridge.getBridge();
+        if (sdb == null)
+            return null;
+        String sdbPath = sdb.getSdbOsLocation();
+        if (sdbPath == null)
+            return null;
+        if (mSerialNumber == null)
+            return null;
+
+        String sdbShellCmd = sdbPath + " -s " + mSerialNumber + " shell";
+        Process pSdb = Runtime.getRuntime().exec(sdbShellCmd);
+        sdbShellProcess = new SdbShellProcess(pSdb, command);
+        return sdbShellProcess;
     }
-    
-    public void executeShellCommand(String command, IShellOutputReceiver receiver)
-            throws TimeoutException, SdbCommandRejectedException, ShellCommandUnresponsiveException,
-            IOException {
-        SdbHelper.executeRemoteCommand(SmartDevelopmentBridge.getSocketAddress(), command, this,
-                receiver, SdbPreferences.getTimeOut());
+
+    public void executeShellCommand(String command, IShellOutputReceiver receiver) throws TimeoutException,
+            SdbCommandRejectedException, ShellCommandUnresponsiveException, IOException {
+        SdbHelper.executeRemoteCommand(SmartDevelopmentBridge.getSocketAddress(), command, this, receiver,
+                SdbPreferences.getTimeOut());
     }
 
-    public void executeShellCommand(String command, IShellOutputReceiver receiver,
-            int maxTimeToOutputResponse)
-            throws TimeoutException, SdbCommandRejectedException, ShellCommandUnresponsiveException,
-            IOException {
-        SdbHelper.executeRemoteCommand(SmartDevelopmentBridge.getSocketAddress(), command, this,
-                receiver, maxTimeToOutputResponse);
+    public void executeShellCommand(String command, IShellOutputReceiver receiver, int maxTimeToOutputResponse)
+            throws TimeoutException, SdbCommandRejectedException, ShellCommandUnresponsiveException, IOException {
+        SdbHelper.executeRemoteCommand(SmartDevelopmentBridge.getSocketAddress(), command, this, receiver,
+                maxTimeToOutputResponse);
     }
 
-    public void runEventLogService(LogReceiver receiver)
-            throws TimeoutException, SdbCommandRejectedException, IOException {
+    public void runEventLogService(LogReceiver receiver) throws TimeoutException, SdbCommandRejectedException, IOException {
         SdbHelper.runEventLogService(SmartDevelopmentBridge.getSocketAddress(), this, receiver);
     }
 
-    public void runLogService(String logname, LogReceiver receiver)
-            throws TimeoutException, SdbCommandRejectedException, IOException {
+    public void runLogService(String logname, LogReceiver receiver) throws TimeoutException, SdbCommandRejectedException,
+            IOException {
         SdbHelper.runLogService(SmartDevelopmentBridge.getSocketAddress(), this, logname, receiver);
     }
 
-    public void createForward(int localPort, int remotePort)
-            throws TimeoutException, SdbCommandRejectedException, IOException {
+    public void createForward(int localPort, int remotePort) throws TimeoutException, SdbCommandRejectedException, IOException {
         SdbHelper.createForward(SmartDevelopmentBridge.getSocketAddress(), this, localPort, remotePort);
     }
 
-    public void removeForward(int localPort, int remotePort)
-            throws TimeoutException, SdbCommandRejectedException, IOException {
+    public void removeForward(int localPort, int remotePort) throws TimeoutException, SdbCommandRejectedException, IOException {
         SdbHelper.removeForward(SmartDevelopmentBridge.getSocketAddress(), this, localPort, remotePort);
     }
 
@@ -170,30 +161,41 @@ final class Device implements IDevice {
         return mMonitor;
     }
 
+    /**
+     * Update device state value.
+     * 
+     * @param changeMask
+     */
     void update(int changeMask) {
         mMonitor.getServer().deviceChanged(this, changeMask);
     }
 
-    public String syncPackageToDevice(String localFilePath)
-            throws IOException, SdbCommandRejectedException, TimeoutException {
+    /**
+     * Update device state and name value.
+     * 
+     * @param changeMask
+     * @param name
+     */
+    void update(int changeMask, String name) {
+        mDeviceName = name;
+        update(changeMask);
+    }
+
+    public String syncPackageToDevice(String localFilePath) throws IOException, SdbCommandRejectedException, TimeoutException {
         try {
             String packageFileName = getFileName(localFilePath);
             String remoteFilePath = String.format("/opt/apps/tmp/%1$s", packageFileName); //$NON-NLS-1$
 
-            Log.d(packageFileName, String.format("Uploading %1$s onto device '%2$s'",
-                    packageFileName, getSerialNumber()));
+            Log.d(packageFileName, String.format("Uploading %1$s onto device '%2$s'", packageFileName, getSerialNumber()));
 
             SyncService sync = getSyncService();
             if (sync != null) {
-                String message = String.format("Uploading file onto device '%1$s'",
-                        getSerialNumber());
+                String message = String.format("Uploading file onto device '%1$s'", getSerialNumber());
                 Log.d(LOG_TAG, message);
-                SyncResult result = sync.pushFile(localFilePath, remoteFilePath,
-                        SyncService.getNullProgressMonitor());
+                SyncResult result = sync.pushFile(localFilePath, remoteFilePath, SyncService.getNullProgressMonitor());
 
                 if (result.getCode() != SyncService.RESULT_OK) {
-                    throw new IOException(String.format("Unable to upload file: %1$s",
-                            result.getMessage()));
+                    throw new IOException(String.format("Unable to upload file: %1$s", result.getMessage()));
                 }
             } else {
                 throw new IOException("Unable to open sync connection!");
@@ -203,15 +205,16 @@ final class Device implements IDevice {
             Log.e(LOG_TAG, "Unable to open sync connection! Timeout.");
             throw e;
         } catch (IOException e) {
-            Log.e(LOG_TAG, String.format("Unable to open sync connection! reason: %1$s",
-                    e.getMessage()));
+            Log.e(LOG_TAG, String.format("Unable to open sync connection! reason: %1$s", e.getMessage()));
             throw e;
         }
     }
 
     /**
      * Helper method to retrieve the file name given a local file path
-     * @param filePath full directory path to file
+     * 
+     * @param filePath
+     *            full directory path to file
      * @return {@link String} file name
      */
     private String getFileName(String filePath) {
@@ -221,65 +224,50 @@ final class Device implements IDevice {
     /**
      * {@inheritDoc}
      */
-    public void removeRemotePackage(String remoteFilePath)
-            throws TimeoutException, SdbCommandRejectedException, ShellCommandUnresponsiveException,
-            IOException {
+    public void removeRemotePackage(String remoteFilePath) throws TimeoutException, SdbCommandRejectedException,
+            ShellCommandUnresponsiveException, IOException {
         // now we delete the app we sync'ed
         try {
             executeShellCommand("rm " + remoteFilePath, new NullOutputReceiver(), INSTALL_TIMEOUT);
         } catch (IOException e) {
-            Log.e(LOG_TAG, String.format("Failed to delete temporary package: %1$s",
-                    e.getMessage()));
+            Log.e(LOG_TAG, String.format("Failed to delete temporary package: %1$s", e.getMessage()));
             throw e;
         }
     }
 
-    public Arch getArch()
-    {
+    public Arch getArch() {
         return mArch;
     }
 
-    public void checkArchitecture()
-    {
-        try
-        {
+    public void checkArchitecture() {
+        try {
             executeShellCommand(COMMAND_ARCHITECTURE_INFO, new ArchInfoReceiver());
-        } catch (Exception e)
-        {
-               Log.e("sdb", "execute " + COMMAND_ARCHITECTURE_INFO + " failed:" +  e);
+        } catch (Exception e) {
+            Log.e("sdb", "execute " + COMMAND_ARCHITECTURE_INFO + " failed:" + e);
         }
     }
-    
-    public void checkDeviceType()
-    {
-        try
-        {
+
+    public void checkDeviceType() {
+        try {
             executeShellCommand(COMMAND_DEVICE_TYPE, new DeviceTypeReceiver());
-        } catch (Exception e)
-        {
-               Log.e("sdb", "execute " + COMMAND_DEVICE_TYPE + " failed:" +  e);
+        } catch (Exception e) {
+            Log.e("sdb", "execute " + COMMAND_DEVICE_TYPE + " failed:" + e);
         }
     }
 
-    class ArchInfoReceiver extends MultiLineReceiver
-    {
+    class ArchInfoReceiver extends MultiLineReceiver {
         @Override
-        public void processNewLines(String[] lines)
-        {
-            if(lines[0] != null && !lines[0].contains(Arch.ARM.getArch()))
-            {
+        public void processNewLines(String[] lines) {
+            if (lines[0] != null && !lines[0].contains(Arch.ARM.getArch())) {
                 mArch = Arch.X86;
             }
         }
     }
 
-    class DeviceTypeReceiver extends MultiLineReceiver
-    {
+    class DeviceTypeReceiver extends MultiLineReceiver {
         @Override
-        public void processNewLines(String[] lines)
-        {
-            if(EMULATOR.equals(lines[0]))
-            {
+        public void processNewLines(String[] lines) {
+            if (EMULATOR.equals(lines[0])) {
                 mIsEmulator = true;
             }
         }
index f88a007..d78c501 100644 (file)
@@ -28,7 +28,6 @@ import org.tizen.sdblib.Device.ArchInfoReceiver;
 import org.tizen.sdblib.IDevice.DeviceState;
 import org.tizen.sdblib.SdbHelper.SdbResponse;
 
-
 /**
  * A Device monitor. This connects to the Smart Development Bridge and get device.
  */
@@ -50,9 +49,10 @@ final class DeviceMonitor {
     private final ArrayList<Device> mDevices = new ArrayList<Device>();
 
     /**
-     * Creates a new {@link DeviceMonitor} object and links it to the running
-     * {@link SmartDevelopmentBridge} object.
-     * @param server the running {@link SmartDevelopmentBridge}.
+     * Creates a new {@link DeviceMonitor} object and links it to the running {@link SmartDevelopmentBridge} object.
+     * 
+     * @param server
+     *            the running {@link SmartDevelopmentBridge}.
      */
     DeviceMonitor(SmartDevelopmentBridge server) {
         mServer = server;
@@ -93,10 +93,9 @@ final class DeviceMonitor {
         }
     }
 
-
-
     /**
      * Returns if the monitor is currently connected to the debug bridge server.
+     * 
      * @return
      */
     boolean isMonitoring() {
@@ -141,8 +140,7 @@ final class DeviceMonitor {
                         Log.d("DeviceMonitor", "start sdb");
                         if (mServer.startSdb() == false) {
                             mRestartAttemptCount++;
-                            Log.e("DeviceMonitor",
-                                    "sdb restart attempts: " + mRestartAttemptCount);
+                            Log.e("DeviceMonitor", "sdb restart attempts: " + mRestartAttemptCount);
                         }
                         waitABit();
                     } else {
@@ -162,7 +160,8 @@ final class DeviceMonitor {
                         // read the incoming message
                         processIncomingDeviceData(length);
 
-                        // flag the fact that we have build the list at least once.
+                        // flag the fact that we have build the list at least
+                        // once.
                         mInitialDeviceListDone = true;
                     }
                 }
@@ -208,6 +207,7 @@ final class DeviceMonitor {
 
     /**
      * Attempts to connect to the debug bridge server.
+     * 
      * @return a connect socket if success, null otherwise
      */
     private SocketChannel openSdbConnection() {
@@ -226,7 +226,7 @@ final class DeviceMonitor {
     }
 
     /**
-     *
+     * 
      * @return
      * @throws IOException
      */
@@ -236,8 +236,7 @@ final class DeviceMonitor {
         try {
             SdbHelper.write(mMainSdbConnection, request);
 
-            SdbResponse resp = SdbHelper.readSdbResponse(mMainSdbConnection,
-                    false /* readDiagString */);
+            SdbResponse resp = SdbHelper.readSdbResponse(mMainSdbConnection, false /* readDiagString */);
 
             if (resp.okay == false) {
                 // request was refused by sdb!
@@ -254,6 +253,7 @@ final class DeviceMonitor {
 
     /**
      * Processes an incoming device message from the socket
+     * 
      * @param socket
      * @param length
      * @throws IOException
@@ -272,11 +272,10 @@ final class DeviceMonitor {
                 String[] param = d.split("\t"); // $NON-NLS-1$
                 if (param.length == 3) {
                     // new sdb uses only serial numbers to identify devices
-                    Device device = new Device(this, param[0].trim() /*serialnumber*/,
-                            DeviceState.getState(param[1].trim()),
-                            param[2].trim() /*device name*/);
+                    Device device = new Device(this, param[0].trim() /* serialnumber */, DeviceState.getState(param[1].trim()),
+                            param[2].trim() /* device name */);
 
-                    //add the device to the list
+                    // add the device to the list
                     list.add(device);
                     Log.d("DeviceMonitor", "deviceData: " + device.getSerialNumber() + " added to list");
                 }
@@ -288,7 +287,7 @@ final class DeviceMonitor {
     }
 
     /**
-     *  Updates the device list with the new items received from the monitoring service.
+     * Updates the device list with the new items received from the monitoring service.
      */
     private void updateDevices(ArrayList<Device> newList) {
         // because we are going to call mServer.deviceDisconnected which will acquire this lock
@@ -301,18 +300,17 @@ final class DeviceMonitor {
             synchronized (mDevices) {
                 // For each device in the current list, we look for a matching the new list.
                 // * if we find it, we update the current object with whatever new information
-                //   there is
-                //   (mostly state change, if the device becomes ready, we query for build info).
-                //   We also remove the device from the new list to mark it as "processed"
+                // there is (mostly state change, if the device becomes ready, we query for build info).
+                // We also remove the device from the new list to mark it as "processed"
                 // * if we do not find it, we remove it from the current list.
                 // Once this is done, the new list contains device we aren't monitoring yet, so we
                 // add them to the list, and start monitoring them.
-                for (int d = 0 ; d < mDevices.size() ;) {
+                for (int d = 0; d < mDevices.size();) {
                     Device device = mDevices.get(d);
                     // look for a similar device in the new list.
                     int count = newList.size();
                     boolean foundMatch = false;
-                    for (int dd = 0 ; dd < count ; dd++) {
+                    for (int dd = 0; dd < count; dd++) {
                         Device newDevice = newList.get(dd);
                         // see if it matches in id and serial number.
                         if (newDevice.getSerialNumber().equals(device.getSerialNumber())) {
@@ -322,13 +320,13 @@ final class DeviceMonitor {
                             if (device.getState() != newDevice.getState()) {
                                 device.setState(newDevice.getState());
 
-                                //should to check architecture if device is an emulator and state changed from "off-lined" to "on-lined"
-                                if(device.getState() == DeviceState.ONLINE)
-                                {
+                                // should to check architecture if device is an
+                                // emulator and state changed from "off-lined" to "on-lined"
+                                if (device.getState() == DeviceState.ONLINE) {
                                     device.checkArchitecture();
                                     device.checkDeviceType();
                                     Log.d("DeviceMonitor", "updateDevices: " + device.getSerialNumber() + " onlined");
-                                    device.update(Device.CHANGE_STATE);
+                                    device.update(Device.CHANGE_STATE, newDevice.getDeviceName());
                                 }
                             }
                             // remove the new device from the list since it's been used
@@ -338,8 +336,7 @@ final class DeviceMonitor {
                     }
 
                     if (foundMatch == false) {
-                        // the device is gone, we need to remove it, and keep current index
-                        // to process the next one.
+                        // the device is gone, we need to remove it, and keep current index to process the next one.
                         removeDevice(device);
                         mServer.deviceDisconnected(device);
                         Log.d("DeviceMonitor", "updateDevices: " + device.getSerialNumber() + " off-lined");
@@ -349,12 +346,10 @@ final class DeviceMonitor {
                     }
                 }
 
-                // at this point we should still have some new devices in newList, so we
-                // process them.
+                // at this point we should still have some new devices in newList, so we process them.
                 for (Device newDevice : newList) {
-                    //should to check architecture if new device is an emulator on-lined
-                    if(newDevice.isOnline())
-                    {
+                    // should to check architecture if new device is an emulator on-lined
+                    if (newDevice.isOnline()) {
                         newDevice.checkArchitecture();
                         newDevice.checkDeviceType();
                     }
@@ -377,12 +372,14 @@ final class DeviceMonitor {
         mDevices.remove(device);
     }
 
-
     /**
      * Reads the length of the next message from a socket.
-     * @param socket The {@link SocketChannel} to read from.
+     * 
+     * @param socket
+     *            The {@link SocketChannel} to read from.
      * @return the length, or 0 (zero) if no data is available from the socket.
-     * @throws IOException if the connection failed.
+     * @throws IOException
+     *             if the connection failed.
      */
     private int readLength(SocketChannel socket, byte[] buffer) throws IOException {
         String msg = read(socket, buffer);
@@ -394,7 +391,7 @@ final class DeviceMonitor {
                 // we'll throw an exception below.
                 Log.e("sdb", "parse integer failed:" + nfe);
             }
-       }
+        }
 
         // we receive something we can't read. It's better to reset the connection at this point.
         throw new IOException("Unable to read length");
@@ -402,6 +399,7 @@ final class DeviceMonitor {
 
     /**
      * Fills a buffer from a socket.
+     * 
      * @param socket
      * @param buffer
      * @return the content of the buffer as a string, or null if it failed to convert the buffer.