diff options
author | Jeffrey Sharkey <jsharkey@jsharkey.org> | 2008-10-26 00:00:36 +0000 |
---|---|---|
committer | Jeffrey Sharkey <jsharkey@jsharkey.org> | 2008-10-26 00:00:36 +0000 |
commit | 02b5d6cde18301bb5d1895896b18845c776c805d (patch) | |
tree | 4c91eb46ddf091efb243ef93ce841f17b17ef548 /src/org/connectbot/service/PromptHelper.java | |
parent | 2cecc0403f922721d6c19505b342935e9a29de14 (diff) | |
download | connectbot-02b5d6cde18301bb5d1895896b18845c776c805d.tar.gz connectbot-02b5d6cde18301bb5d1895896b18845c776c805d.tar.bz2 connectbot-02b5d6cde18301bb5d1895896b18845c776c805d.zip |
* refactored prompting gui component into PromptHelper, makes it easier now because it offers blocking methods to get strings
* also added boolean prompt support for hostkey yes/no from user
* added checking and storing of hostkeys into backend database, successfully tested
* created new icon using android style guide pdf, older icon is still here
Diffstat (limited to 'src/org/connectbot/service/PromptHelper.java')
-rw-r--r-- | src/org/connectbot/service/PromptHelper.java | 102 |
1 files changed, 102 insertions, 0 deletions
diff --git a/src/org/connectbot/service/PromptHelper.java b/src/org/connectbot/service/PromptHelper.java new file mode 100644 index 0000000..66f1591 --- /dev/null +++ b/src/org/connectbot/service/PromptHelper.java @@ -0,0 +1,102 @@ +package org.connectbot.service; + +import java.util.concurrent.Semaphore; + +import android.os.Handler; +import android.os.Message; + +/** + * Helps provide a relay for prompts and responses between a possible user + * interface and some underlying service. + * + * @author jsharkey + */ +public class PromptHelper { + + protected final Object tag; + + public PromptHelper(Object tag) { + this.tag = tag; + } + + protected Handler handler = null; + + /** + * Register a user interface handler, if available. + */ + public void setHandler(Handler handler) { + this.handler = handler; + } + + protected Semaphore promptResponse = new Semaphore(0); + + public String promptHint = null; + public Class promptRequested = null; + + protected Object response = null; + + /** + * Set an incoming value from an above user interface. Will automatically + * notify any waiting requests. + */ + public void setResponse(Object value) { + this.response = value; + this.promptResponse.release(); + } + + /** + * Return the internal response value just before erasing and returning it. + */ + protected Object popResponse() { + Object value = this.response; + this.response = null; + return value; + } + + + /** + * Request a prompt response from parent. This is a blocking call until user + * interface returns a value. + */ + public synchronized Object requestPrompt(String hint, Class type) throws Exception { + this.promptHint = hint; + this.promptRequested = type; + + // notify any parent watching for live events + if(handler != null) + Message.obtain(handler, -1, tag).sendToTarget(); + + // acquire lock until user passes back value + this.promptResponse.acquire(); + this.promptRequested = null; + return this.popResponse(); + } + + /** + * Request a string response from parent. This is a blocking call until user + * interface returns a value. + */ + public String requestStringPrompt(String hint) { + String value = null; + try { + value = (String)this.requestPrompt(hint, String.class); + } catch(Exception e) { + } + return value; + } + + /** + * Request a boolean response from parent. This is a blocking call until user + * interface returns a value. + */ + public Boolean requestBooleanPrompt(String hint) { + Boolean value = null; + try { + value = (Boolean)this.requestPrompt(hint, Boolean.class); + } catch(Exception e) { + } + return value; + } + + +} |