7 package com.google.appinventor.components.runtime;
25 import android.app.Activity;
26 import android.os.Handler;
27 import android.util.Log;
29 import org.apache.http.NameValuePair;
30 import org.apache.http.message.BasicNameValuePair;
31 import org.json.JSONArray;
32 import org.json.JSONException;
33 import org.json.JSONObject;
35 import java.util.ArrayList;
36 import java.util.List;
72 @DesignerComponent(version = YaVersion.VOTING_COMPONENT_VERSION,
73 designerHelpDescription =
"<p>The Voting component enables users to vote " +
74 "on a question by communicating with a Web service to retrieve a ballot " +
75 "and later sending back users' votes.</p>",
76 category = ComponentCategory.INTERNAL,
78 iconName =
"images/voting.png")
80 @UsesPermissions(permissionNames =
"android.permission.INTERNET")
83 private static final String LOG_TAG =
"Voting";
84 private static final String REQUESTBALLOT_COMMAND =
"requestballot";
85 private static final String SENDBALLOT_COMMAND =
"sendballot";
86 private static final String IS_POLLING_PARAMETER =
"isPolling";
87 private static final String ID_REQUESTED_PARAMETER =
"idRequested";
88 private static final String BALLOT_QUESTION_PARAMETER =
"question";
89 private static final String BALLOT_OPTIONS_PARAMETER =
"options";
90 private static final String USER_CHOICE_PARAMETER =
"userchoice";
91 private static final String USER_ID_PARAMETER =
"userid";
93 private Handler androidUIHandler;
95 private Activity activityContext;
97 private String userId;
98 private String serviceURL;
99 private String ballotQuestion;
100 private String ballotOptionsString;
103 private ArrayList<String> ballotOptions;
106 private Boolean idRequested;
107 private String userChoice;
108 private Boolean isPolling;
111 super(container.
$form());
112 serviceURL =
"http://androvote.appspot.com";
117 ballotOptions =
new ArrayList<String>();
120 androidUIHandler =
new Handler();
121 theContainer = container;
122 activityContext = container.
$context();
126 serviceURL =
"http://androvote.appspot.com";
133 description =
"The URL of the Voting service",
135 public String ServiceURL() {
145 defaultValue =
"http://androvote.appspot.com")
148 this.serviceURL = serviceURL;
155 description =
"The question to be voted on.",
157 public String BallotQuestion() {
158 return ballotQuestion;
165 description =
"The list of ballot options.",
167 public List<String> BallotOptions(){
168 return ballotOptions;
182 description =
"A text identifying the voter that is sent to the Voting " +
183 "server along with the vote. This must be set before " +
184 "<code>SendBallot</code> is called.",
186 public String UserId() {
197 this.userId = userId;
204 description =
"The ballot choice to send to the server, which must be " +
205 "set before <code>SendBallot</code> is called. " +
206 "This must be one of <code>BallotOptions</code>.",
208 public String UserChoice() {
219 this.userChoice = userChoice;
227 description =
"The email address associated with this device. This property has been " +
228 "deprecated and always returns the empty text value.",
230 public String UserEmailAddress() {
256 "Send a request for a ballot to the Web service specified " +
257 "by the property <code>ServiceURL</code>. When the " +
258 "completes, one of the following events will be raised: " +
259 "<code>GotBallot</code>, <code>NoOpenPoll</code>, or " +
260 "<code>WebServiceError</code>.")
261 public
void RequestBallot() {
262 final Runnable call =
new Runnable() {
263 public void run() { postRequestBallot(); }};
267 private void postRequestBallot(){
269 public void onSuccess(JSONObject result) {
270 if (result ==
null) {
273 androidUIHandler.post(
new Runnable() {
275 WebServiceError(
"The Web server did not respond to your request for a ballot");
281 Log.i(LOG_TAG,
"postRequestBallot: ballot retrieved " + result);
286 isPolling = result.getBoolean(IS_POLLING_PARAMETER);
289 idRequested = result.getBoolean(ID_REQUESTED_PARAMETER);
290 ballotQuestion = result.getString(BALLOT_QUESTION_PARAMETER);
291 ballotOptionsString = result.getString(BALLOT_OPTIONS_PARAMETER);
292 ballotOptions = JSONArrayToArrayList(
new JSONArray(ballotOptionsString));
293 androidUIHandler.post(
new Runnable() {
299 androidUIHandler.post(
new Runnable() {
305 }
catch (JSONException e) {
313 androidUIHandler.post(
new Runnable() {
315 WebServiceError(
"The Web server returned a garbled object");
322 public void onFailure(
final String message) {
323 Log.w(LOG_TAG,
"postRequestBallot Failure " + message);
324 androidUIHandler.post(
new Runnable() {
326 WebServiceError(message);
333 WebServiceUtil.getInstance().postCommandReturningObject(
335 REQUESTBALLOT_COMMAND,
341 private ArrayList<String> JSONArrayToArrayList(JSONArray ja)
throws JSONException {
342 ArrayList<String> a =
new ArrayList<String>();
343 for (
int i = 0; i < ja.length(); i++) {
344 a.add(ja.getString(i));
355 "Event indicating that a ballot was retrieved from the Web " +
356 "service and that the properties <code>BallotQuestion</code> and " +
357 "<code>BallotOptions</code> have been set. This is always preceded " +
358 "by a call to the method <code>RequestBallot</code>.")
359 public
void GotBallot() {
377 "Send a completed ballot to the Web service. This should " +
378 "not be called until the properties <code>UserId</code> " +
379 "and <code>UserChoice</code> have been set by the application.")
380 public
void SendBallot() {
381 final Runnable call =
new Runnable() {
382 public void run() { postSendBallot(userChoice, userId); }};
386 private void postSendBallot(String userChoice, String userId){
391 public void onSuccess(String response) {
392 androidUIHandler.post(
new Runnable() {
394 GotBallotConfirmation();
398 public void onFailure(
final String message) {
399 Log.w(LOG_TAG,
"postSendBallot Failure " + message);
400 androidUIHandler.post(
new Runnable() {
402 WebServiceError(message);
409 WebServiceUtil.getInstance().postCommand(serviceURL,
411 Lists.<NameValuePair>newArrayList(
412 new BasicNameValuePair(USER_CHOICE_PARAMETER, userChoice),
413 new BasicNameValuePair(USER_ID_PARAMETER, userId)),