OLD | NEW |
(Empty) | |
| 1 package com.google.dart.tools.debug.ui.internal.dialogs; |
| 2 |
| 3 import com.google.dart.tools.core.DartCore; |
| 4 import com.google.dart.tools.core.mobile.AndroidDebugBridge; |
| 5 import com.google.dart.tools.core.mobile.AndroidDevice; |
| 6 import com.google.dart.tools.debug.ui.internal.mobile.MobileMainTab; |
| 7 import com.google.dart.tools.ui.internal.util.ExternalBrowserUtil; |
| 8 |
| 9 import org.eclipse.core.runtime.IStatus; |
| 10 import org.eclipse.core.runtime.Status; |
| 11 import org.eclipse.jface.dialogs.Dialog; |
| 12 import org.eclipse.jface.dialogs.IDialogConstants; |
| 13 import org.eclipse.swt.SWT; |
| 14 import org.eclipse.swt.events.SelectionAdapter; |
| 15 import org.eclipse.swt.events.SelectionEvent; |
| 16 import org.eclipse.swt.events.SelectionListener; |
| 17 import org.eclipse.swt.graphics.Point; |
| 18 import org.eclipse.swt.layout.GridData; |
| 19 import org.eclipse.swt.layout.GridLayout; |
| 20 import org.eclipse.swt.widgets.Button; |
| 21 import org.eclipse.swt.widgets.Composite; |
| 22 import org.eclipse.swt.widgets.Control; |
| 23 import org.eclipse.swt.widgets.Label; |
| 24 import org.eclipse.swt.widgets.Link; |
| 25 import org.eclipse.swt.widgets.Shell; |
| 26 |
| 27 import java.net.ConnectException; |
| 28 |
| 29 public class MobilePortForwardDialog extends Dialog { |
| 30 |
| 31 private final String pageUrl; |
| 32 private Link descriptionLabel; |
| 33 private Label testResultLabel; |
| 34 |
| 35 public MobilePortForwardDialog(Shell parentShell, String pageUrl, boolean loca
lhostOverUsb) { |
| 36 super(parentShell); |
| 37 this.pageUrl = pageUrl; |
| 38 setShellStyle(SWT.DIALOG_TRIM | SWT.RESIZE); |
| 39 } |
| 40 |
| 41 @Override |
| 42 protected void configureShell(Shell newShell) { |
| 43 super.configureShell(newShell); |
| 44 newShell.setText("Mobile Launch"); |
| 45 } |
| 46 |
| 47 @Override |
| 48 protected Control createButtonBar(Composite parent) { |
| 49 Composite buttonBar = (Composite) super.createButtonBar(parent); |
| 50 ((GridLayout) buttonBar.getLayout()).makeColumnsEqualWidth = false; |
| 51 buttonBar.setLayoutData(new GridData(GridData.FILL_HORIZONTAL | GridData.GRA
B_HORIZONTAL |
| 52 | GridData.VERTICAL_ALIGN_CENTER)); |
| 53 return buttonBar; |
| 54 } |
| 55 |
| 56 @Override |
| 57 protected void createButtonsForButtonBar(Composite parent) { |
| 58 |
| 59 final Button testButton = createButton( |
| 60 parent, |
| 61 IDialogConstants.CLIENT_ID, |
| 62 "Test Connection", |
| 63 false); |
| 64 testButton.addSelectionListener(new SelectionAdapter() { |
| 65 @Override |
| 66 public void widgetDefaultSelected(SelectionEvent e) { |
| 67 widgetSelected(e); |
| 68 } |
| 69 |
| 70 @Override |
| 71 public void widgetSelected(SelectionEvent e) { |
| 72 testConnection(); |
| 73 } |
| 74 }); |
| 75 testButton.getDisplay().asyncExec(new Runnable() { |
| 76 @Override |
| 77 public void run() { |
| 78 testButton.setFocus(); |
| 79 } |
| 80 }); |
| 81 |
| 82 // increment the number of columns in the button bar |
| 83 ((GridLayout) parent.getLayout()).numColumns++; |
| 84 testResultLabel = new Label(parent, SWT.NONE); |
| 85 testResultLabel.setLayoutData(new GridData(SWT.FILL, SWT.CENTER, true, false
, 1, 1)); |
| 86 |
| 87 Button closeButton = createButton( |
| 88 parent, |
| 89 IDialogConstants.CLOSE_ID, |
| 90 IDialogConstants.CLOSE_LABEL, |
| 91 true); |
| 92 closeButton.addSelectionListener(new SelectionAdapter() { |
| 93 @Override |
| 94 public void widgetDefaultSelected(SelectionEvent e) { |
| 95 widgetSelected(e); |
| 96 } |
| 97 |
| 98 @Override |
| 99 public void widgetSelected(SelectionEvent e) { |
| 100 close(); |
| 101 } |
| 102 }); |
| 103 } |
| 104 |
| 105 @Override |
| 106 protected Control createDialogArea(Composite parent) { |
| 107 Composite container = (Composite) super.createDialogArea(parent); |
| 108 container.setLayout(new GridLayout(2, false)); |
| 109 |
| 110 descriptionLabel = new Link(container, SWT.WRAP); |
| 111 GridData gd_descriptionLabel = new GridData(SWT.LEFT, SWT.CENTER, true, fals
e, 2, 1); |
| 112 gd_descriptionLabel.verticalIndent = 20; |
| 113 gd_descriptionLabel.horizontalIndent = 20; |
| 114 descriptionLabel.setLayoutData(gd_descriptionLabel); |
| 115 descriptionLabel.setText("Unable to access the web server" |
| 116 + " on the developer machine from the mobile device.\n\n" |
| 117 + "Looks like port forwarding has not been setup between device and the
machine.\n" |
| 118 + "Follow the steps listed under " // |
| 119 + " <a href=\"" + MobileMainTab.MOBILE_DOC_URL + "\">Set up port forward
ing</a>\n" |
| 120 + "\n\nOnce you are done, use Test Connection to verify the setup."); |
| 121 descriptionLabel.addSelectionListener(new SelectionListener() { |
| 122 @Override |
| 123 public void widgetDefaultSelected(SelectionEvent e) { |
| 124 widgetSelected(e); |
| 125 } |
| 126 |
| 127 @Override |
| 128 public void widgetSelected(SelectionEvent e) { |
| 129 ExternalBrowserUtil.openInExternalBrowser(e.text.trim()); |
| 130 } |
| 131 }); |
| 132 |
| 133 return container; |
| 134 } |
| 135 |
| 136 /** |
| 137 * Return the initial size of the dialog. |
| 138 */ |
| 139 @Override |
| 140 protected Point getInitialSize() { |
| 141 return getShell().computeSize(SWT.DEFAULT, SWT.DEFAULT); |
| 142 } |
| 143 |
| 144 /** |
| 145 * Test the connection between the mobile device and the webserver on the deve
lopment machine. |
| 146 */ |
| 147 private void testConnection() { |
| 148 testResultLabel.setText("Testing connection..."); |
| 149 |
| 150 // Test connection on a background thread |
| 151 Thread thread = new Thread("Test Mobile Port Forwarding") { |
| 152 |
| 153 @Override |
| 154 public void run() { |
| 155 long startTime = System.currentTimeMillis(); |
| 156 |
| 157 // Test connection from mobile device to developer machine |
| 158 final IStatus status = testConnectionInBackground(); |
| 159 |
| 160 // Ensure text is visible for at least 1 1/2 seconds |
| 161 long delta = startTime + 1500 - System.currentTimeMillis(); |
| 162 if (delta > 0) { |
| 163 try { |
| 164 Thread.sleep(delta); |
| 165 } catch (InterruptedException e) { |
| 166 //$FALL-THROUGH$ |
| 167 } |
| 168 } |
| 169 |
| 170 getShell().getDisplay().asyncExec(new Runnable() { |
| 171 @Override |
| 172 public void run() { |
| 173 String message; |
| 174 if (status.isOK()) { |
| 175 message = "Connection validated"; |
| 176 } else { |
| 177 message = status.getMessage(); |
| 178 } |
| 179 if (message.startsWith(ConnectException.class.getName())) { |
| 180 message = message.substring(ConnectException.class.getName().lengt
h() + 2).trim(); |
| 181 } |
| 182 testResultLabel.setText(message); |
| 183 } |
| 184 }); |
| 185 } |
| 186 }; |
| 187 thread.setDaemon(true); |
| 188 thread.start(); |
| 189 } |
| 190 |
| 191 private IStatus testConnectionInBackground() { |
| 192 AndroidDebugBridge debugBridge = AndroidDebugBridge.getAndroidDebugBridge(); |
| 193 |
| 194 // Check that the device is still connected and authorized |
| 195 AndroidDevice device = debugBridge.getConnectedDevice(); |
| 196 if (device == null) { |
| 197 return new Status(IStatus.ERROR, DartCore.PLUGIN_ID, "No device connected"
); |
| 198 } |
| 199 if (!device.isAuthorized()) { |
| 200 return new Status(IStatus.ERROR, DartCore.PLUGIN_ID, "Connected device is
not authorized"); |
| 201 } |
| 202 |
| 203 return debugBridge.isHtmlPageAccessible(device, pageUrl); |
| 204 } |
| 205 } |
OLD | NEW |