diff --git a/astrid/AndroidManifest.xml b/astrid/AndroidManifest.xml index b84379cfd..217c03511 100644 --- a/astrid/AndroidManifest.xml +++ b/astrid/AndroidManifest.xml @@ -1,7 +1,7 @@ diff --git a/astrid/res/layout/web_service_amazon_row.xml b/astrid/res/layout/web_service_amazon_row.xml new file mode 100644 index 000000000..8c5007457 --- /dev/null +++ b/astrid/res/layout/web_service_amazon_row.xml @@ -0,0 +1,88 @@ + + + + + + + + + + + + + + + + + + + + + + + + diff --git a/astrid/res/layout/web_service_text_row.xml b/astrid/res/layout/web_service_row.xml similarity index 51% rename from astrid/res/layout/web_service_text_row.xml rename to astrid/res/layout/web_service_row.xml index 9c5d268cb..08334a007 100644 --- a/astrid/res/layout/web_service_text_row.xml +++ b/astrid/res/layout/web_service_row.xml @@ -1,43 +1,55 @@ + android:layout_height="100dip" + android:background="@android:drawable/list_selector_background"> + + + + android:gravity="center_vertical" + android:text="Probiotic - Wikepedia, the free encyclopedia" + android:ellipsize="end" /> - - + android:src="@drawable/icn_arrow_right" + android:scaleType="centerInside" /> diff --git a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java index 7743d49f6..4a5bd9299 100755 --- a/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskEditFragment.java @@ -417,6 +417,7 @@ public final class TaskEditFragment extends Fragment implements webServices = new WebServicesView(getActivity()); webServices.setLayoutParams(new FrameLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); + webServices.setPadding(10, 5, 10, 10); webServices.setTask(model); } else { webServices.refresh(); @@ -429,9 +430,6 @@ public final class TaskEditFragment extends Fragment implements mPager = (NestableViewPager) getView().findViewById(R.id.pager); mPager.setAdapter(mAdapter); - mPager.setScrollabelViews(webServices.getScrollableViews()); - ((NestableScrollView)getView().findViewById(R.id.edit_scroll)). - setScrollabelViews(webServices.getScrollableViews()); mIndicator = (TabPageIndicator) getView().findViewById( R.id.indicator); @@ -1151,6 +1149,7 @@ public final class TaskEditFragment extends Fragment implements * in the horizontal scroll view */ + @SuppressWarnings("nls") public View getPageView(int position) { if ((tabStyle == TAB_STYLE_WEB && position == 0) || (tabStyle != TAB_STYLE_WEB && position == 1)) { @@ -1220,6 +1219,15 @@ public final class TaskEditFragment extends Fragment implements @Override public void onPageSelected(int position) { this.setPagerHeightForPosition(position); + + NestableScrollView scrollView = (NestableScrollView)getView().findViewById(R.id.edit_scroll); + if((tabStyle == TAB_STYLE_WEB && position == 1) || + (tabStyle == TAB_STYLE_ACTIVITY_WEB && position == 2)) + scrollView. + setScrollabelViews(webServices.getScrollableViews()); + else + scrollView.setScrollabelViews(null); + } @Override diff --git a/astrid/src/com/todoroo/astrid/activity/TaskEditViewPager.java b/astrid/src/com/todoroo/astrid/activity/TaskEditViewPager.java index e8218c410..80b6ec5ef 100644 --- a/astrid/src/com/todoroo/astrid/activity/TaskEditViewPager.java +++ b/astrid/src/com/todoroo/astrid/activity/TaskEditViewPager.java @@ -48,12 +48,8 @@ public class TaskEditViewPager extends PagerAdapter implements TitleProvider { @Override public Object instantiateItem(View pager, int position) { - System.err.println("adding view for " + position + ", tab style " + tabStyle); - View pageView = parent.getPageView(position); - System.err.println("got pager " + pageView); - ((ViewPager) pager).addView(pageView, 0); return pageView; } diff --git a/astrid/src/com/todoroo/astrid/ui/NestableScrollView.java b/astrid/src/com/todoroo/astrid/ui/NestableScrollView.java index 8590dbef6..26b9ac4a3 100644 --- a/astrid/src/com/todoroo/astrid/ui/NestableScrollView.java +++ b/astrid/src/com/todoroo/astrid/ui/NestableScrollView.java @@ -1,10 +1,10 @@ package com.todoroo.astrid.ui; import android.content.Context; +import android.graphics.Rect; import android.util.AttributeSet; import android.view.MotionEvent; import android.view.View; -import android.view.ViewParent; import android.widget.ScrollView; public class NestableScrollView extends ScrollView { @@ -13,13 +13,21 @@ public class NestableScrollView extends ScrollView { public NestableScrollView(Context context, AttributeSet attrs) { super(context, attrs); } - @Override + + @Override public boolean onInterceptTouchEvent(MotionEvent event) { - for(int i = 0; i < scrollableViews.length; i++) { - View view = findViewById(scrollableViews[i]); - if (view instanceof ViewParent) - ((ViewParent)view).requestDisallowInterceptTouchEvent(true); - } + if (scrollableViews != null) { + for (int i = 0; i < scrollableViews.length; i++) { + View view = findViewById(scrollableViews[i]); + if (view != null) { + Rect rect = new Rect(); + view.getHitRect(rect); + if (rect.contains((int) event.getX(), (int) event.getY())) { + return false; + } + } + } + } return super.onInterceptTouchEvent(event); } diff --git a/astrid/src/com/todoroo/astrid/ui/NestableViewPager.java b/astrid/src/com/todoroo/astrid/ui/NestableViewPager.java index 3c6400458..0cd967ec7 100644 --- a/astrid/src/com/todoroo/astrid/ui/NestableViewPager.java +++ b/astrid/src/com/todoroo/astrid/ui/NestableViewPager.java @@ -4,26 +4,19 @@ import android.content.Context; import android.support.v4.view.ViewPager; import android.util.AttributeSet; import android.view.MotionEvent; -import android.view.View; -import android.view.ViewParent; public class NestableViewPager extends ViewPager { - private int[] scrollableViews = new int[0]; public NestableViewPager(Context context, AttributeSet attrs) { super(context, attrs); } + + /** + * Prevent horizontal scrolling + */ @Override public boolean onInterceptTouchEvent(MotionEvent event) { - for(int i = 0; i < scrollableViews.length; i++) { - View view = findViewById(scrollableViews[i]); - if (view instanceof ViewParent) - ((ViewParent)view).requestDisallowInterceptTouchEvent(true); - } - return super.onInterceptTouchEvent(event); + return false; } - public void setScrollabelViews(int[] views) { - this.scrollableViews = views; - } } diff --git a/astrid/src/com/todoroo/astrid/ui/WebServicesView.java b/astrid/src/com/todoroo/astrid/ui/WebServicesView.java index 5b53c2387..5751304c2 100644 --- a/astrid/src/com/todoroo/astrid/ui/WebServicesView.java +++ b/astrid/src/com/todoroo/astrid/ui/WebServicesView.java @@ -4,6 +4,7 @@ import greendroid.widget.AsyncImageView; import java.io.StringReader; import java.net.URLEncoder; +import java.util.ArrayList; import java.util.HashMap; import java.util.Map; @@ -27,7 +28,6 @@ import android.view.View; import android.view.ViewGroup; import android.widget.FrameLayout; import android.widget.HorizontalScrollView; -import android.widget.ImageView.ScaleType; import android.widget.LinearLayout; import android.widget.ProgressBar; import android.widget.TextView; @@ -59,6 +59,10 @@ public class WebServicesView extends LinearLayout { private LayoutInflater inflater; private Activity activity; + + private LinearLayout.LayoutParams rowParams; + + @Autowired RestClient restClient; @Autowired ExceptionService exceptionService; @@ -95,6 +99,11 @@ public class WebServicesView extends LinearLayout { inflater = (LayoutInflater) getContext().getSystemService( Context.LAYOUT_INFLATER_SERVICE); + rowParams = new LinearLayout.LayoutParams( + Math.round(metrics.widthPixels * 0.8f), + Math.round(ROW_HEIGHT * metrics.density)); + rowParams.rightMargin = Math.round(10 * metrics.density); + refresh(); } @@ -129,7 +138,7 @@ public class WebServicesView extends LinearLayout { params.put("Version", "2011-08-01"); params.put("Operation", "ItemSearch"); params.put("Availability", "Available"); - params.put("ResponseGroup", "Images"); + params.put("ResponseGroup", "Medium"); params.put("Keywords", task.getValue(Task.TITLE)); params.put("SearchIndex", "All"); params.put("AssociateTag", ASSOCIATE_TAG); @@ -151,16 +160,11 @@ public class WebServicesView extends LinearLayout { private final LinearLayout body; private final String searchResults; - private final MarginLayoutParams params; public AmazonSearchResultsProcessor(LinearLayout body, String searchResults) { this.body = body; this.searchResults = searchResults; - - params = new LinearLayout.LayoutParams( - LayoutParams.WRAP_CONTENT, LayoutParams.FILL_PARENT); - params.rightMargin = Math.round(10 * metrics.density); } @Override @@ -176,22 +180,52 @@ public class WebServicesView extends LinearLayout { xpp.setInput(new StringReader(searchResults)); int eventType = xpp.getEventType(); - String asin = null, image = null; + HashMap attributes = new HashMap(); + ArrayList authors = new ArrayList(); + while (eventType != XmlPullParser.END_DOCUMENT) { if(eventType == XmlPullParser.START_TAG) { - if("ASIN".equals(xpp.getName())) - asin = xpp.nextText(); + if("Title".equals(xpp.getName())) + attributes.put("title", xpp.nextText()); + else if("FormattedPrice".equals(xpp.getName())) + attributes.put("price", xpp.nextText()); + else if("DetailPageURL".equals(xpp.getName())) + attributes.put("url", xpp.nextText()); + else if("Brand".equals(xpp.getName()) || + "Studio".equals(xpp.getName()) || + "Label".equals(xpp.getName())) + attributes.put("subtitle", xpp.nextText()); + else if("Author".equals(xpp.getName()) || + "Director".equals(xpp.getName()) || + "Artist".equals(xpp.getName())) + authors.add(xpp.nextText()); else if("MediumImage".equals(xpp.getName())) { xpp.next(); - image = xpp.nextText(); - } else if("Error".equals(xpp.getName())) { + attributes.put("image", xpp.nextText()); + } + else if("LowestNewPrice".equals(xpp.getName())) { + xpp.next(); + attributes.put("lowestNew", xpp.nextText()); + } + else if("LowestUsedPrice".equals(xpp.getName())) { + xpp.next(); + attributes.put("lowestUsed", xpp.nextText()); + } + else if("TotalNew".equals(xpp.getName())) + attributes.put("totalNew", xpp.nextText()); + else if("TotalUsed".equals(xpp.getName())) + attributes.put("totalUsed", xpp.nextText()); + else if("Error".equals(xpp.getName())) { while(!"Message".equals(xpp.getName())) xpp.next(); throw new AmazonException(xpp.nextText()); } } else if(eventType == XmlPullParser.END_TAG) { - if("Item".equals(xpp.getName())) - renderItem(asin, image); + if("Item".equals(xpp.getName())) { + renderItem(attributes, authors); + attributes.clear(); + authors.clear(); + } } eventType = xpp.next(); } @@ -200,8 +234,7 @@ public class WebServicesView extends LinearLayout { String url = String.format("http://www.amazon.com/s/?field-keywords=%s&tag=%s", URLEncoder.encode(task.getValue(Task.TITLE), "UTF-8"), ASSOCIATE_TAG); - View view = inflateTextRow(body, moreLabel, "", url); - view.setLayoutParams(params); + View view = inflateRow(body, null, moreLabel, "", url); view.setBackgroundColor(Color.rgb(200, 200, 200)); } catch (Exception e) { @@ -209,17 +242,66 @@ public class WebServicesView extends LinearLayout { } } - private void renderItem(String asin, String image) { - AsyncImageView imageView = new AsyncImageView(activity); - imageView.setDefaultImageResource(R.drawable.ic_contact_picture_2); - imageView.setUrl(image); - imageView.setLayoutParams(params); - imageView.setScaleType(ScaleType.FIT_CENTER); - imageView.setTag(String.format("http://www.amazon.com/dp/%s/?tag=%s", asin, ASSOCIATE_TAG)); - imageView.setOnClickListener(linkClickListener); + private void renderItem(HashMap attributes, + ArrayList authors) { + View view = inflater.inflate(R.layout.web_service_amazon_row, body, false); + + ((AsyncImageView)view.findViewById(R.id.image)).setUrl( + attributes.get("image")); + ((TextView)view.findViewById(R.id.title)).setText( + attributes.get("title")); + ((TextView)view.findViewById(R.id.price)).setText( + attributes.get("price")); + view.setTag(attributes.get("url")); + + String subtitle = attributes.get("subtitle"); + if(authors.size() > 0) { + StringBuilder sb = new StringBuilder(); + for(String author : authors) { + System.err.println("AUTHOR " + author); + sb.append(author).append(", "); + } + subtitle = sb.toString().substring(0, sb.length() - 2); + } + ((TextView)view.findViewById(R.id.subtitle)).setText(subtitle); + + String newAndUsed = null; + if(attributes.containsKey("lowestNew") || attributes.containsKey("lowestUsed")) { + int lowestNew = Integer.MAX_VALUE; + try { + lowestNew = attributes.containsKey("lowestNew") ? + Integer.parseInt(attributes.get("lowestNew")) : Integer.MAX_VALUE; + } catch (NumberFormatException e) { + // text, i.e. "too low to display" + } + int lowestUsed = Integer.MAX_VALUE; + try { + lowestUsed = attributes.containsKey("lowestUsed") ? + Integer.parseInt(attributes.get("lowestUsed")) : Integer.MAX_VALUE; + } catch (NumberFormatException e) { + // text, i.e. "too low to display" + } + + int lowest = Math.min(lowestNew, lowestUsed); + int total = 0; + if(attributes.containsKey("totalNew")) + total += Integer.parseInt(attributes.get("totalNew")); + if(attributes.containsKey("totalUsed")) + total += Integer.parseInt(attributes.get("totalUsed")); + + String price = String.format("$%.2f", lowest / 100f); + if(!price.equals(attributes.get("price"))) + newAndUsed = String.format("%d New & Used from %s", total, price); + } + ((TextView)view.findViewById(R.id.new_and_used)).setText(newAndUsed); + - body.addView(imageView); + view.setOnClickListener(linkClickListener); + view.setLayoutParams(rowParams); + + body.addView(view); } + } private class AmazonException extends Exception { @@ -275,25 +357,19 @@ public class WebServicesView extends LinearLayout { try { JSONArray results = searchResults.getJSONArray("results"); - LayoutParams params = new LinearLayout.LayoutParams( - Math.round(metrics.widthPixels * 0.8f), - Math.round(ROW_HEIGHT * metrics.density)); - params.rightMargin = Math.round(10 * metrics.density); for(int i = 0; i < results.length(); i++) { JSONObject result = results.getJSONObject(i); String title = StringEscapeUtils.unescapeHtml(result.getString("titleNoFormatting")); - View view = inflateTextRow(body, title, + inflateRow(body, null, title, result.getString("visibleUrl"), result.getString("url")); - view.setLayoutParams(params); } JSONObject cursor = searchResults.getJSONObject("cursor"); - String moreLabel = "Show moreresults"; + String moreLabel = "Show more results"; String url = cursor.getString("moreResultsUrl"); - View view = inflateTextRow(body, moreLabel, "", url); - view.setLayoutParams(params); + View view = inflateRow(body, null, moreLabel, "", url); view.setBackgroundColor(Color.rgb(200, 200, 200)); } catch (JSONException e) { @@ -302,13 +378,22 @@ public class WebServicesView extends LinearLayout { } } - protected View inflateTextRow(ViewGroup body, String title, String subtitle, + protected View inflateRow(ViewGroup body, String imageUrl, String title, String subtitle, String tag) { - View view = inflater.inflate(R.layout.web_service_text_row, body, false); + View view = inflater.inflate(R.layout.web_service_row, body, false); + AsyncImageView imageView = (AsyncImageView)view.findViewById(R.id.image); + + if(imageUrl == null) + imageView.setVisibility(View.GONE); + else + imageView.setUrl(imageUrl); + ((TextView)view.findViewById(R.id.title)).setText(title); - ((TextView)view.findViewById(R.id.url)).setText(subtitle); + ((TextView)view.findViewById(R.id.subtitle)).setText(subtitle); view.setOnClickListener(linkClickListener); view.setTag(tag); + view.setLayoutParams(rowParams); + body.addView(view); return view; } @@ -346,6 +431,7 @@ public class WebServicesView extends LinearLayout { scroll.setLayoutParams(new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.WRAP_CONTENT)); scroll.setId(id); + scroll.setScrollbarFadingEnabled(false); addView(scroll); LinearLayout body = new LinearLayout(getContext()); @@ -355,10 +441,12 @@ public class WebServicesView extends LinearLayout { ProgressBar progressBar = new ProgressBar(getContext()); progressBar.setIndeterminate(true); - LayoutParams layoutParams = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, - LayoutParams.FILL_PARENT); + LayoutParams layoutParams = new LinearLayout.LayoutParams(metrics.widthPixels, + Math.round(30 * metrics.density)); layoutParams.gravity = Gravity.CENTER; progressBar.setLayoutParams(layoutParams); + progressBar.setIndeterminateDrawable(getResources().getDrawable( + android.R.drawable.progress_indeterminate_horizontal)); body.addView(progressBar); return body; @@ -370,14 +458,13 @@ public class WebServicesView extends LinearLayout { LayoutParams mlp = new LinearLayout.LayoutParams(LayoutParams.FILL_PARENT, 1); mlp.setMargins(10, 20, 10, 20); 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); + textView.setTextAppearance(getContext(), R.style.TextAppearance_GEN_EditLabel); addView(textView); }