From 6546170ba11eaff23bfe93b6bc4b590a1684a774 Mon Sep 17 00:00:00 2001 From: Tim Su Date: Tue, 7 Feb 2012 19:37:52 -0800 Subject: [PATCH] First pass at web services view with google search and amazon. --- astrid/AndroidManifest.xml | 6 + astrid/res/layout/web_service_text_row.xml | 46 ++++ .../astrid/activity/AdTestActivity.java | 39 ++++ .../todoroo/astrid/ui/WebServicesView.java | 217 ++++++++++++++++++ .../com/todoroo/astrid/utility/Constants.java | 23 +- 5 files changed, 324 insertions(+), 7 deletions(-) create mode 100644 astrid/res/layout/web_service_text_row.xml create mode 100644 astrid/src/com/todoroo/astrid/activity/AdTestActivity.java create mode 100644 astrid/src/com/todoroo/astrid/ui/WebServicesView.java diff --git a/astrid/AndroidManifest.xml b/astrid/AndroidManifest.xml index a3a4474fe..b84379cfd 100644 --- a/astrid/AndroidManifest.xml +++ b/astrid/AndroidManifest.xml @@ -101,6 +101,12 @@ + + + + + + diff --git a/astrid/res/layout/web_service_text_row.xml b/astrid/res/layout/web_service_text_row.xml new file mode 100644 index 000000000..7ef479b7f --- /dev/null +++ b/astrid/res/layout/web_service_text_row.xml @@ -0,0 +1,46 @@ + + + + + + + + + + + + + + diff --git a/astrid/src/com/todoroo/astrid/activity/AdTestActivity.java b/astrid/src/com/todoroo/astrid/activity/AdTestActivity.java new file mode 100644 index 000000000..ad747a945 --- /dev/null +++ b/astrid/src/com/todoroo/astrid/activity/AdTestActivity.java @@ -0,0 +1,39 @@ +package com.todoroo.astrid.activity; + +import android.app.Activity; +import android.os.Bundle; +import android.view.ViewGroup.LayoutParams; +import android.widget.FrameLayout; +import android.widget.ScrollView; + +import com.todoroo.astrid.data.Task; +import com.todoroo.astrid.service.AstridDependencyInjector; +import com.todoroo.astrid.service.ThemeService; +import com.todoroo.astrid.ui.WebServicesView; + +public class AdTestActivity extends Activity { + + static { + AstridDependencyInjector.initialize(); + } + + @Override + protected void onCreate(Bundle savedInstanceState) { + ThemeService.applyTheme(this); + super.onCreate(savedInstanceState); + + WebServicesView webServicesView = new WebServicesView(this); + webServicesView.setLayoutParams(new FrameLayout.LayoutParams( + LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); + webServicesView.setPadding(10, 10, 10, 10); + ScrollView scrollView = new ScrollView(this); + scrollView.addView(webServicesView); + + setContentView(scrollView); + + Task task = new Task(); + task.setValue(Task.TITLE, "America (The Book)"); //$NON-NLS-1$ + webServicesView.setTask(task); + } + +} diff --git a/astrid/src/com/todoroo/astrid/ui/WebServicesView.java b/astrid/src/com/todoroo/astrid/ui/WebServicesView.java new file mode 100644 index 000000000..2b73774cb --- /dev/null +++ b/astrid/src/com/todoroo/astrid/ui/WebServicesView.java @@ -0,0 +1,217 @@ +package com.todoroo.astrid.ui; + +import java.io.IOException; +import java.io.UnsupportedEncodingException; +import java.net.URLEncoder; + +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + +import android.app.Activity; +import android.content.Context; +import android.graphics.Color; +import android.util.AttributeSet; +import android.util.DisplayMetrics; +import android.view.LayoutInflater; +import android.view.View; +import android.widget.FrameLayout; +import android.widget.HorizontalScrollView; +import android.widget.ImageView; +import android.widget.LinearLayout; +import android.widget.ProgressBar; +import android.widget.TextView; + +import com.timsu.astrid.R; +import com.todoroo.andlib.service.Autowired; +import com.todoroo.andlib.service.DependencyInjectionService; +import com.todoroo.andlib.service.ExceptionService; +import com.todoroo.andlib.service.RestClient; +import com.todoroo.astrid.data.Task; + +@SuppressWarnings("nls") +public class WebServicesView extends LinearLayout { + + private static final String GOOGLE_SEARCH_URL = "https://ajax.googleapis.com/ajax/services/search/web?v=1.0&q="; + + private Task task; + private final DisplayMetrics metrics = new DisplayMetrics(); + private LayoutInflater inflater; + private Activity activity; + + @Autowired RestClient restClient; + @Autowired ExceptionService exceptionService; + + public WebServicesView(Context context, AttributeSet attrs, int defStyle) { + super(context, attrs, defStyle); + } + + public WebServicesView(Context context, AttributeSet attrs) { + super(context, attrs); + } + + public WebServicesView(Context context) { + super(context); + } + + public void setTask(Task task) { + this.task = task; + initialize(); + } + + /** + * Initialize view + */ + private void initialize() { + DependencyInjectionService.getInstance().inject(this); + setOrientation(LinearLayout.VERTICAL); + + activity = (Activity) getContext(); + activity.getWindowManager().getDefaultDisplay().getMetrics(metrics); + inflater = (LayoutInflater) getContext().getSystemService( + Context.LAYOUT_INFLATER_SERVICE); + + initializeAmazon(); + + addSectionDivider(); + + initializeGoogleSearch(); + } + + protected void initializeAmazon() { + addSectionHeader("Amazon.com"); + + final LinearLayout body = addHorizontalScroller(); + + for(int i = 0; i < 10; i++) { + ImageView aiv = new ImageView(getContext()); + aiv.setImageResource(R.drawable.icon); + aiv.setLayoutParams(new LinearLayout.LayoutParams(200, 200)); + body.addView(aiv); + } + } + + /** + * Initialize Google search results + */ + protected void initializeGoogleSearch() { + addSectionHeader("Google Search"); + + final LinearLayout body = addHorizontalScroller(); + + ProgressBar progressBar = new ProgressBar(getContext()); + progressBar.setIndeterminate(true); + progressBar.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, + LayoutParams.FILL_PARENT)); + body.addView(progressBar); + + new Thread() { + @Override + public void run() { + Exception exception = null; + JSONObject searchResults = null; + + try { + String url = GOOGLE_SEARCH_URL + + URLEncoder.encode(task.getValue(Task.TITLE), "UTF-8"); + String result = restClient.get(url); + searchResults = new JSONObject(result); + } catch (UnsupportedEncodingException e) { + exception = e; + } catch (IOException e) { + exception = e; + } catch (JSONException e) { + exception = e; + } + + final Exception finalException = exception; + final JSONObject finalResults = searchResults; + activity.runOnUiThread(new Runnable() { + public void run() { + body.removeAllViews(); + + if(finalException != null) + displayError(finalException, body); + else { + try { + processGoogleSearchResults(body, + finalResults.getJSONObject("responseData")); + } catch (JSONException e) { + displayError(e, body); + } + } + } + }); + } + }.start(); + } + + protected void processGoogleSearchResults(LinearLayout body, + JSONObject searchResults) throws JSONException { + + JSONArray results = searchResults.getJSONArray("results"); + + for(int i = 0; i < results.length(); i++) { + JSONObject result = results.getJSONObject(i); + View view = inflater.inflate(R.layout.web_service_text_row, body, false); + ((TextView)view.findViewById(R.id.title)).setText(result.getString("titleNoFormatting")); + ((TextView)view.findViewById(R.id.url)).setText(result.getString("visibleUrl")); + body.addView(view); + + String url = result.getString("url"); + view.setTag(url); + } + + JSONObject cursor = searchResults.getJSONObject("cursor"); + String moreLabel = String.format("Show all %s results", + cursor.getString("estimatedResultCount")); + String url = cursor.getString("moreResultsUrl"); + + View view = inflater.inflate(R.layout.web_service_text_row, body, false); + ((TextView)view.findViewById(R.id.title)).setText(moreLabel); + view.setBackgroundColor(Color.rgb(200, 200, 200)); + view.setTag(url); + body.addView(view); + } + + protected void displayError(Exception exception, LinearLayout body) { + exceptionService.reportError("google-error", exception); + + TextView textView = new TextView(getContext()); + textView.setTextAppearance(getContext(), R.style.TextAppearance_Medium); + textView.setText(exception.toString()); + body.addView(textView); + } + + protected LinearLayout addHorizontalScroller() { + HorizontalScrollView scroll = new HorizontalScrollView(getContext()); + scroll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, + LayoutParams.WRAP_CONTENT)); + addView(scroll); + + LinearLayout body = new LinearLayout(getContext()); + body.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, + Math.round(100 * metrics.density))); + scroll.addView(body); + + return body; + } + + private void addSectionDivider() { + View view = new View(getContext()); + MarginLayoutParams mlp = new MarginLayoutParams(LayoutParams.FILL_PARENT, 1); + mlp.setMargins(10, 5, 10, 5); + view.setLayoutParams(mlp); + view.setBackgroundResource(R.drawable.black_white_gradient); + addView(view); + } + + private void addSectionHeader(String string) { + TextView textView = new TextView(getContext()); + textView.setText(string); + textView.setTextAppearance(getContext(), R.style.TextAppearance_Medium); + addView(textView); + } + + +} diff --git a/astrid/src/com/todoroo/astrid/utility/Constants.java b/astrid/src/com/todoroo/astrid/utility/Constants.java index eb346a14d..71ed4aa66 100644 --- a/astrid/src/com/todoroo/astrid/utility/Constants.java +++ b/astrid/src/com/todoroo/astrid/utility/Constants.java @@ -1,6 +1,6 @@ package com.todoroo.astrid.utility; - +@SuppressWarnings("nls") public final class Constants { // --- general application constants @@ -8,12 +8,12 @@ public final class Constants { /** * LCL API Key */ - public static final String LOCALYTICS_KEY = "ae35a010c66a997ab129ab7-3e2adf46-8bb3-11e0-fe8b-007f58cb3154"; //$NON-NLS-1$ + public static final String LOCALYTICS_KEY = "ae35a010c66a997ab129ab7-3e2adf46-8bb3-11e0-fe8b-007f58cb3154"; /** * Application Package */ - public static final String PACKAGE = "com.timsu.astrid"; //$NON-NLS-1$ + public static final String PACKAGE = "com.timsu.astrid"; /** * Whether this is an OEM installation @@ -39,7 +39,7 @@ public final class Constants { /** * Astrid Help URL */ - public static final String HELP_URL = "http://weloveastrid.com/help-user-guide-astrid-v3/active-tasks/"; //$NON-NLS-1$ + public static final String HELP_URL = "http://weloveastrid.com/help-user-guide-astrid-v3/active-tasks/"; // --- task list activity source strings @@ -69,9 +69,18 @@ public final class Constants { // --- crittercism - public static final String CRITTERCISM_APP_ID = "4e8a796fddf5203b6f0097c5"; //$NON-NLS-1$ + public static final String CRITTERCISM_APP_ID = "4e8a796fddf5203b6f0097c5"; + + public static final String CRITTERCISM_SECRET = "9mhdwlu85lc6sovpxkabq1cbzzmxe2oi"; + + public static final String CRITTERCISM_OATH_KEY = "4e8a796fddf5203b6f0097c5nn35ziwt"; + + // --- amazon + + public static final String AWS_ACCESS_KEY_ID = ""; + + public static final String AWS_SECRET_KEY_ID = ""; - public static final String CRITTERCISM_SECRET = "9mhdwlu85lc6sovpxkabq1cbzzmxe2oi"; //$NON-NLS-1$ + public static final String AWS_ENDPOINT = "ecs.amazonaws.com"; - public static final String CRITTERCISM_OATH_KEY = "4e8a796fddf5203b6f0097c5nn35ziwt"; //$NON-NLS-1$ }