diff options
Diffstat (limited to 'sdkmanager/libs/sdkuilib')
6 files changed, 334 insertions, 15 deletions
diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/SdkUpdaterNoWindow.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/SdkUpdaterNoWindow.java index ef4864b..10f4074 100755 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/SdkUpdaterNoWindow.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/repository/SdkUpdaterNoWindow.java @@ -23,7 +23,9 @@ import com.android.sdklib.internal.repository.ITaskFactory; import com.android.sdklib.internal.repository.ITaskMonitor; import com.android.sdklib.internal.repository.NullTaskMonitor; import com.android.sdklib.repository.SdkRepoConstants; +import com.android.util.Pair; +import java.io.IOException; import java.util.ArrayList; import java.util.Properties; @@ -336,6 +338,74 @@ public class SdkUpdaterNoWindow { } /** + * Displays a prompt message to the user and read two values, + * login/password. + * <p> + * <i>Asks user for login/password information.</i> + * <p> + * This method shows a question in the standard output, asking for login + * and password.</br> + * <b>Method Output:</b></br> + * Title</br> + * Message</br> + * Login: (Wait for user input)</br> + * Password: (Wait for user input)</br> + * <p> + * + * @param title The title of the iteration. + * @param message The message to be displayed. + * @return A {@link Pair} holding the entered login and password. The + * <b>first element</b> is always the <b>Login</b>, and the + * <b>second element</b> is always the <b>Password</b>. This + * method will never return null, in case of error the pair will + * be filled with empty strings. + * @see ITaskMonitor#displayLoginPasswordPrompt(String, String) + */ + public Pair<String, String> displayLoginPasswordPrompt(String title, String message) { + String login = ""; //$NON-NLS-1$ + String password = ""; //$NON-NLS-1$ + mSdkLog.printf("\n%1$s\n%2$s", title, message); + byte[] readBuffer = new byte[2048]; + try { + mSdkLog.printf("\nLogin: "); + login = readLine(readBuffer); + mSdkLog.printf("\nPassword: "); + password = readLine(readBuffer); + /* + * TODO: Implement a way to don't echo the typed password On + * Java 5 there's no simple way to do this. There's just a + * workaround which is output backspaces on each keystroke. + * A good alternative is to use Java 6 java.io.Console + */ + } catch (IOException e) { + // Reset login/pass to empty Strings. + login = ""; //$NON-NLS-1$ + password = ""; //$NON-NLS-1$ + //Just print the error to console. + mSdkLog.printf("\nError occurred during login/pass query: %s\n", e.getMessage()); + } + + return Pair.of(login, password); + } + + private String readLine(byte[] buffer) throws IOException { + int count = System.in.read(buffer); + + // is the input longer than the buffer? + if (count == buffer.length && buffer[count-1] != 10) { + throw new IOException(String.format( + "Input is longer than the buffer size, (%1$s) bytes", buffer.length)); + } + + // ignore end whitespace + while (count > 0 && (buffer[count-1] == '\r' || buffer[count-1] == '\n')) { + count--; + } + + return new String(buffer, 0, count); + } + + /** * Creates a sub-monitor that will use up to tickCount on the progress bar. * tickCount must be 1 or more. */ @@ -433,6 +503,10 @@ public class SdkUpdaterNoWindow { return mRoot.displayPrompt(title, message); } + public Pair<String, String> displayLoginPasswordPrompt(String title, String message) { + return mRoot.displayLoginPasswordPrompt(title, message); + } + public ITaskMonitor createSubMonitor(int tickCount) { assert mSubCoef > 0; assert tickCount > 0; diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/IProgressUiProvider.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/IProgressUiProvider.java index 4a7922d..f6e13c6 100755 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/IProgressUiProvider.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/IProgressUiProvider.java @@ -17,6 +17,7 @@ package com.android.sdkuilib.internal.tasks; import com.android.sdklib.internal.repository.ITaskMonitor; +import com.android.util.Pair; import org.eclipse.swt.widgets.ProgressBar; @@ -90,4 +91,20 @@ interface IProgressUiProvider { */ public abstract boolean displayPrompt(String title, String message); + /** + * Launch an interface which asks for login and password. Implementations + * MUST allow this to be called from any thread, e.g. by making sure the + * dialog is opened synchronously in the UI thread. + * + * @param title The title of the dialog box. + * @param message The message to be displayed as an instruction. + * @return Returns a {@link Pair} holding the entered login and password. + * The information must always be in the following order: + * Login,Password. So in order to retrieve the <b>login</b> callers + * should retrieve the first element, and the second value for the + * <b>password</b>. This method should never return a null pair. + * It's elements however can be empty Strings + */ + public abstract Pair<String, String> displayLoginPasswordPrompt(String title, String message); + } diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/ProgressTaskDialog.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/ProgressTaskDialog.java index a4236fa..dbc871f 100755 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/ProgressTaskDialog.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/ProgressTaskDialog.java @@ -18,6 +18,9 @@ package com.android.sdkuilib.internal.tasks; import com.android.sdklib.SdkConstants;
import com.android.sdklib.internal.repository.ITaskMonitor;
+import com.android.sdkuilib.ui.AuthenticationDialog;
+import com.android.sdkuilib.ui.GridDialog;
+import com.android.util.Pair;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
@@ -154,7 +157,8 @@ final class ProgressTaskDialog extends Dialog implements IProgressUiProvider { });
mResultText = new Text(mRootComposite,
- SWT.BORDER | SWT.READ_ONLY | SWT.WRAP | SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
+ SWT.BORDER | SWT.READ_ONLY | SWT.WRAP |
+ SWT.H_SCROLL | SWT.V_SCROLL | SWT.CANCEL | SWT.MULTI);
mResultText.setEditable(true);
mResultText.setLayoutData(new GridData(SWT.FILL, SWT.FILL, true, true, 2, 1));
}
@@ -393,6 +397,45 @@ final class ProgressTaskDialog extends Dialog implements IProgressUiProvider { }
/**
+ * This method opens a pop-up window which requests for User Login and
+ * password.
+ *
+ * @param title The title of the window.
+ * @param message The message to displayed in the login/password window.
+ * @return Returns a {@link Pair} holding the entered login and password.
+ * The information must always be in the following order:
+ * Login,Password. So in order to retrieve the <b>login</b> callers
+ * should retrieve the first element, and the second value for the
+ * <b>password</b>.
+ * If operation is <b>canceled</b> by user the return value must be <b>null</b>.
+ * @see ITaskMonitor#displayLoginPasswordPrompt(String, String)
+ */
+ public Pair<String, String> displayLoginPasswordPrompt(
+ final String title, final String message) {
+ final String[] resultArray = new String[2];
+ Display display = mDialogShell.getDisplay();
+
+ // open dialog and request login and password
+ display.syncExec(new Runnable() {
+ public void run() {
+ AuthenticationDialog authenticationDialog = new AuthenticationDialog(mDialogShell,
+ title,
+ message);
+ int dlgResult= authenticationDialog.open();
+ if(dlgResult == GridDialog.OK) {
+ resultArray[0] = authenticationDialog.getLogin();
+ resultArray[1] = authenticationDialog.getPassword();
+ } else {
+ resultArray[0] = null;
+ resultArray[1] = null;
+ }
+ }
+ });
+
+ return resultArray[0] == null ? null : Pair.of(resultArray[0], resultArray[1]);
+ }
+
+ /**
* Starts the thread that runs the task.
* This is deferred till the UI is created.
*/
diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/ProgressView.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/ProgressView.java index d90eaed..9485885 100755 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/ProgressView.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/ProgressView.java @@ -19,11 +19,13 @@ package com.android.sdkuilib.internal.tasks; import com.android.sdklib.ISdkLog;
import com.android.sdklib.internal.repository.ITask;
import com.android.sdklib.internal.repository.ITaskMonitor;
+import com.android.sdkuilib.ui.AuthenticationDialog;
+import com.android.sdkuilib.ui.GridDialog;
+import com.android.util.Pair;
import org.eclipse.jface.dialogs.MessageDialog;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.Control;
-import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
@@ -325,20 +327,53 @@ public final class ProgressView implements IProgressUiProvider { public boolean displayPrompt(final String title, final String message) {
final boolean[] result = new boolean[] { false };
- if (!mProgressBar.isDisposed()) {
- final Shell shell = mProgressBar.getShell();
- Display display = shell.getDisplay();
-
- display.syncExec(new Runnable() {
- public void run() {
- result[0] = MessageDialog.openQuestion(shell, title, message);
- }
- });
- }
+ syncExec(mProgressBar, new Runnable() {
+ public void run() {
+ Shell shell = mProgressBar.getShell();
+ result[0] = MessageDialog.openQuestion(shell, title, message);
+ }
+ });
return result[0];
}
+ /**
+ * This method opens a pop-up window which requests for User Login and
+ * password.
+ *
+ * @param title The title of the window.
+ * @param message The message to displayed in the login/password window.
+ * @return Returns a {@link Pair} holding the entered login and password.
+ * The information must always be in the following order:
+ * Login,Password. So in order to retrieve the <b>login</b> callers
+ * should retrieve the first element, and the second value for the
+ * <b>password</b>.
+ * If operation is <b>canceled</b> by user the return value must be <b>null</b>.
+ * @see ITaskMonitor#displayLoginPasswordPrompt(String, String)
+ */
+ public Pair<String, String>
+ displayLoginPasswordPrompt(final String title, final String message) {
+ final String[] resultArray = new String[] {"", ""};
+ // open dialog and request login and password
+ syncExec(mProgressBar, new Runnable() {
+ public void run() {
+ Shell shell = mProgressBar.getShell();
+ AuthenticationDialog authenticationDialog = new AuthenticationDialog(shell,
+ title,
+ message);
+ int dlgResult = authenticationDialog.open();
+ if (dlgResult == GridDialog.OK) {
+ resultArray[0] = authenticationDialog.getLogin();
+ resultArray[1] = authenticationDialog.getPassword();
+ } else {
+ resultArray[0] = null;
+ resultArray[1] = null;
+ }
+ }
+ });
+ return resultArray[0] == null ? null : Pair.of(resultArray[0], resultArray[1]);
+ }
+
// ----
/**
diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/TaskMonitorImpl.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/TaskMonitorImpl.java index 5286df5..f3dff37 100755 --- a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/TaskMonitorImpl.java +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/internal/tasks/TaskMonitorImpl.java @@ -17,8 +17,7 @@ package com.android.sdkuilib.internal.tasks;
import com.android.sdklib.internal.repository.ITaskMonitor;
-
-import org.eclipse.swt.widgets.ProgressBar;
+import com.android.util.Pair;
/**
* Internal class that implements the logic of an {@link ITaskMonitor}.
@@ -156,7 +155,7 @@ class TaskMonitorImpl implements ITaskMonitor { }
/**
- * Display a yes/no question dialog box.
+ * Displays a yes/no question dialog box.
*
* This implementation allow this to be called from any thread, it
* makes sure the dialog is opened synchronously in the ui thread.
@@ -170,6 +169,21 @@ class TaskMonitorImpl implements ITaskMonitor { }
/**
+ * Displays a Login/Password dialog. This implementation allows this method to be
+ * called from any thread, it makes sure the dialog is opened synchronously
+ * in the ui thread.
+ *
+ * @param title The title of the dialog box
+ * @param message Message to be displayed
+ * @return Pair with entered login/password. Login is always the first
+ * element and Password is always the second. If any error occurs a
+ * pair with empty strings is returned.
+ */
+ public Pair<String, String> displayLoginPasswordPrompt(String title, String message) {
+ return mUi.displayLoginPasswordPrompt(title, message);
+ }
+
+ /**
* Creates a sub-monitor that will use up to tickCount on the progress bar.
* tickCount must be 1 or more.
*/
@@ -284,6 +298,10 @@ class TaskMonitorImpl implements ITaskMonitor { return mRoot.displayPrompt(title, message);
}
+ public Pair<String, String> displayLoginPasswordPrompt(String title, String message) {
+ return mRoot.displayLoginPasswordPrompt(title, message);
+ }
+
public ITaskMonitor createSubMonitor(int tickCount) {
assert mSubCoef > 0;
assert tickCount > 0;
diff --git a/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/ui/AuthenticationDialog.java b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/ui/AuthenticationDialog.java new file mode 100644 index 0000000..497c752 --- /dev/null +++ b/sdkmanager/libs/sdkuilib/src/com/android/sdkuilib/ui/AuthenticationDialog.java @@ -0,0 +1,132 @@ +/* + * Copyright (C) 2011 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.android.sdkuilib.ui; + +import org.eclipse.swt.SWT; +import org.eclipse.swt.events.ModifyEvent; +import org.eclipse.swt.events.ModifyListener; +import org.eclipse.swt.layout.GridData; +import org.eclipse.swt.layout.GridLayout; +import org.eclipse.swt.widgets.Composite; +import org.eclipse.swt.widgets.Label; +import org.eclipse.swt.widgets.Shell; +import org.eclipse.swt.widgets.Text; + +/** + * Dialog which collects from the user his/her login and password. + */ +public class AuthenticationDialog extends GridDialog { + + private Text mTxtLogin; + + private Text mTxtPassword; + + private String mTitle; + + private String mMessage; + + protected String mLogin; + + protected String mPassword; + + /** + * Constructor which retrieves the parent {@link Shell} and the message to + * be displayed in this dialog. + * + * @param parentShell Parent Shell + * @param title Title of the window. + * @param message Message the be displayed in this dialog. + */ + public AuthenticationDialog(Shell parentShell, String title, String message) { + super(parentShell, 1, false); + // assign fields + mTitle = title; + mMessage = message; + } + + @Override + public void createDialogContent(Composite parent) { + // Configure Dialog + getShell().setText(mTitle); + GridData data = new GridData(SWT.FILL, SWT.FILL, true, true); + parent.setLayoutData(data); + + // Upper Composite + Composite upperComposite = new Composite(parent, SWT.NONE); + GridLayout layout = new GridLayout(2, false); + layout.verticalSpacing = 10; + upperComposite.setLayout(layout); + data = new GridData(SWT.FILL, SWT.CENTER, true, true); + upperComposite.setLayoutData(data); + + // add message label + Label lblMessage = new Label(upperComposite, SWT.WRAP); + lblMessage.setText(mMessage); + data = new GridData(SWT.FILL, SWT.CENTER, true, true, 2, 1); + data.widthHint = 500; + lblMessage.setLayoutData(data); + + // add user name label and text field + Label lblUserName = new Label(upperComposite, SWT.NONE); + lblUserName.setText("Login:"); + data = new GridData(SWT.LEFT, SWT.CENTER, false, false); + lblUserName.setLayoutData(data); + + mTxtLogin = new Text(upperComposite, SWT.SINGLE | SWT.BORDER); + data = new GridData(SWT.FILL, SWT.CENTER, true, false); + mTxtLogin.setLayoutData(data); + mTxtLogin.setFocus(); + mTxtLogin.addModifyListener(new ModifyListener() { + public void modifyText(ModifyEvent arg0) { + mLogin = mTxtLogin.getText(); + } + }); + + // add password label and text field + Label lblPassword = new Label(upperComposite, SWT.NONE); + lblPassword.setText("Password:"); + data = new GridData(SWT.LEFT, SWT.CENTER, false, false); + lblPassword.setLayoutData(data); + + mTxtPassword = new Text(upperComposite, SWT.SINGLE | SWT.PASSWORD | SWT.BORDER); + data = new GridData(SWT.FILL, SWT.CENTER, true, false); + mTxtPassword.setLayoutData(data); + mTxtPassword.addModifyListener(new ModifyListener() { + public void modifyText(ModifyEvent arg0) { + mPassword = mTxtPassword.getText(); + } + }); + } + + /** + * Retrieves the Login field information + * + * @return Login field value or empty String. Return value is never null + */ + public String getLogin() { + return mLogin != null ? mLogin : ""; //$NON-NLS-1$ + } + + /** + * Retrieves the Password field information + * + * @return Password field value or empty String. Return value is never null + */ + public String getPassword() { + return mPassword != null ? mPassword : ""; //$NON-NLS-1$ + } +} |