summaryrefslogtreecommitdiff
path: root/src/plugins/remote.device/src/com/motorola/studio/android/remote/handlers/USBModeServiceHandler.java
blob: 61bab7d54a94bc3e303f0f842946c653ef74f08c (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
/*
 * Copyright (C) 2012 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.motorola.studio.android.remote.handlers;

import java.io.IOException;
import java.util.Map;
import java.util.Properties;

import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.IStatus;
import org.eclipse.core.runtime.Status;
import org.eclipse.core.runtime.SubMonitor;
import org.eclipse.osgi.util.NLS;
import org.eclipse.sequoyah.device.framework.model.IInstance;
import org.eclipse.sequoyah.device.framework.model.IService;
import org.eclipse.sequoyah.device.framework.model.handler.IServiceHandler;
import org.eclipse.sequoyah.device.framework.model.handler.ServiceHandler;

import com.motorola.studio.android.adt.DDMSFacade;
import com.motorola.studio.android.adt.ISerialNumbered;
import com.motorola.studio.android.common.log.StudioLogger;
import com.motorola.studio.android.common.utilities.EclipseUtils;
import com.motorola.studio.android.remote.RemoteDevicePlugin;
import com.motorola.studio.android.remote.i18n.RemoteDeviceNLS;
import com.motorola.studio.android.remote.instance.RemoteDeviceInstance;

/**
 * Handler which switches back a remote device from TCP/IP to USB mode.
 */
public class USBModeServiceHandler extends ServiceHandler
{
    // Min SDK version that supports tcpip connection mode
    private static final int MIN_SDK_VERSION = 6;

    /**
     * Get the wireless service handler.
     */
    @Override
    public IServiceHandler newInstance()
    {
        return new USBModeServiceHandler();
    }

    /**
     * Get the phone IP, validate it and launch the Wireless wizard. 
     */
    @Override
    public IStatus runService(final IInstance instance, Map<Object, Object> arguments,
            final IProgressMonitor monitor)
    {

        final SubMonitor subMonitor = SubMonitor.convert(monitor, 1000);
        subMonitor.beginTask(
                RemoteDeviceNLS.USBModeServiceHandler_MsgStartingProcessOfSwitchingToUSBMode, 1000);

        final ISerialNumbered device = (ISerialNumbered) instance;

        int deviceSdkVersion = -1;
        try
        {
            deviceSdkVersion =
                    Integer.parseInt(DDMSFacade.getDeviceProperty(device.getSerialNumber(),
                            "ro.build.version.sdk")); //$NON-NLS-1$

            subMonitor.worked(100);
        }
        catch (Exception e)
        {
            StudioLogger.error(USBModeServiceHandler.class,
                    RemoteDeviceNLS.USBModeServiceHandler_2, e);
        }

        // if it was not possible to retrieve the sdk version
        // try to execute the service anyway
        if ((!subMonitor.isCanceled()) && (deviceSdkVersion < MIN_SDK_VERSION)
                && (deviceSdkVersion != -1))
        {
            EclipseUtils.showErrorDialog(RemoteDeviceNLS.Title_ReturningToUSBConnectionDialog,
                    RemoteDeviceNLS.USBModeServiceHandler_MsgUnableToSwitchToUSBDueToSDKVersion);
        }
        else
        {

            if (!subMonitor.isCanceled())
            {

                subMonitor
                        .setTaskName(RemoteDeviceNLS.WirelessServiceHandler_MsgRetrievingDeviceIPNumber);

                // retrieve the IP, Port and timeout and validate it
                Properties properties = instance.getProperties();
                String host = properties.getProperty(RemoteDeviceInstance.PROPERTY_HOST);
                String port = properties.getProperty(RemoteDeviceInstance.PROPERTY_PORT);
                String timeout = properties.getProperty(RemoteDeviceInstance.PROPERTY_TIMEOUT);

                subMonitor.worked(600);
                if (host == null)
                {
                    EclipseUtils.showErrorDialog(
                            RemoteDeviceNLS.Title_ReturningToUSBConnectionDialog,
                            RemoteDeviceNLS.ERR_WirelessWizard_No_IP);
                }

                else
                {
                    if (!subMonitor.isCanceled())
                    {
                        subMonitor
                                .setTaskName(RemoteDeviceNLS.USBModeServiceHandler_MsgSwithcingTCPToUSB);
                        // switch the device from connection mode from TCP/IP to USB
                        try
                        {
                            IStatus status =
                                    DDMSFacade.switchFromTCPConnectionModeToUSBConnectionMode(
                                            device, host, port, Integer.parseInt(timeout),
                                            subMonitor.newChild(300));

                            // in case the status is not OK, show an error message
                            if ((status != null) && (status.getSeverity() == IStatus.ERROR))
                            {
                                EclipseUtils
                                        .showErrorDialog(
                                                RemoteDeviceNLS.Title_ReturningToUSBConnectionDialog,
                                                NLS.bind(
                                                        RemoteDeviceNLS.USBModeServiceHandler_MsgItWasNotPossibleToSwitchDeviceToUSBMode,
                                                        host), status);
                            }
                            else
                            {
                                // show a success message
                                EclipseUtils.showInformationDialog(
                                        RemoteDeviceNLS.Title_ReturningToUSBConnectionDialog,
                                        NLS.bind(RemoteDeviceNLS.USBModeServiceHandler_MsgSuccess,
                                                host));
                            }
                        }
                        catch (IOException e)
                        {
                            StudioLogger
                                    .error(this.getClass(),
                                            NLS.bind(
                                                    "It was not possible to switch the android device {0} connection mode to USB.", //$NON-NLS-1$
                                                    device.getDeviceName()), e);
                            EclipseUtils
                                    .showErrorDialog(
                                            RemoteDeviceNLS.Title_ReturningToUSBConnectionDialog,
                                            NLS.bind(
                                                    RemoteDeviceNLS.USBModeServiceHandler_MsgItWasNotPossibleToSwitchToUSBMode,
                                                    device.getDeviceName()));
                        }
                    }
                }
            }
        }
        return Status.OK_STATUS;
    }

    /**
     * Simply Return an OK Status.
     */
    @Override
    public IStatus updatingService(IInstance arg0, IProgressMonitor arg1)
    {
        return Status.OK_STATUS;
    }

    @Override
    public void setService(IService service)
    {
        super.setService(service);
        if (service != null)
        {
            service.setVisible(RemoteDevicePlugin.isWifiServiceEnabled());
        }
    }
}