From 749f8ac1247e862c6daa7727136eb9267dc9a5fa Mon Sep 17 00:00:00 2001 From: "dkeen@dkeen-laptop" Date: Wed, 18 Jul 2012 10:31:06 -0700 Subject: [PATCH] Home button in action bar now links back to initial activity Fixed toasts for route selection errors --- .classpath | 1 + .../bart/AbstractRouteSelectionActivity.java | 6 +- .../dougkeen/bart/ViewDeparturesActivity.java | 9 +- src/com/dougkeen/bart/ViewMapActivity.java | 16 +++ .../bart/actionbarcompat/ActionBarHelper.java | 133 ++++++++++-------- .../actionbarcompat/ActionBarHelperBase.java | 8 ++ .../ActionBarHelperHoneycomb.java | 96 +++++++------ .../actionbarcompat/ActionBarHelperICS.java | 2 - 8 files changed, 161 insertions(+), 110 deletions(-) diff --git a/.classpath b/.classpath index 4b421f7..42ddf22 100644 --- a/.classpath +++ b/.classpath @@ -6,5 +6,6 @@ + diff --git a/src/com/dougkeen/bart/AbstractRouteSelectionActivity.java b/src/com/dougkeen/bart/AbstractRouteSelectionActivity.java index 7ac80a2..7a074f6 100644 --- a/src/com/dougkeen/bart/AbstractRouteSelectionActivity.java +++ b/src/com/dougkeen/bart/AbstractRouteSelectionActivity.java @@ -43,20 +43,20 @@ public abstract class AbstractRouteSelectionActivity extends Activity { if (origin == null) { Toast.makeText(v.getContext(), com.dougkeen.bart.R.string.error_null_origin, - Toast.LENGTH_LONG); + Toast.LENGTH_LONG).show(); return; } if (destination == null) { Toast.makeText(v.getContext(), com.dougkeen.bart.R.string.error_null_destination, - Toast.LENGTH_LONG); + Toast.LENGTH_LONG).show(); return; } if (origin.equals(destination)) { Toast.makeText( v.getContext(), com.dougkeen.bart.R.string.error_matching_origin_and_destination, - Toast.LENGTH_LONG); + Toast.LENGTH_LONG).show(); return; } onOkButtonClick(origin, destination); diff --git a/src/com/dougkeen/bart/ViewDeparturesActivity.java b/src/com/dougkeen/bart/ViewDeparturesActivity.java index 834aaf1..bed4509 100644 --- a/src/com/dougkeen/bart/ViewDeparturesActivity.java +++ b/src/com/dougkeen/bart/ViewDeparturesActivity.java @@ -128,6 +128,9 @@ public class ViewDeparturesActivity extends ActionBarListActivity { findViewById(R.id.missingDepartureText).setVisibility(View.VISIBLE); refreshBoardedDeparture(); + + getActionBarHelper().setHomeButtonEnabled(true); + getActionBarHelper().setDisplayHomeAsUpEnabled(true); } @Override @@ -534,7 +537,11 @@ public class ViewDeparturesActivity extends ActionBarListActivity { @Override public boolean onOptionsItemSelected(MenuItem item) { int itemId = item.getItemId(); - if (itemId == R.id.view_on_bart_site_button) { + if (itemId == android.R.id.home) { + startActivity(new Intent(Intent.ACTION_PICK, + Constants.FAVORITE_CONTENT_URI)); + return true; + } else if (itemId == R.id.view_on_bart_site_button) { startActivity(new Intent( Intent.ACTION_VIEW, Uri.parse("http://m.bart.gov/schedules/qp_results.aspx?type=departure&date=today&time=" diff --git a/src/com/dougkeen/bart/ViewMapActivity.java b/src/com/dougkeen/bart/ViewMapActivity.java index 00c3485..760c950 100644 --- a/src/com/dougkeen/bart/ViewMapActivity.java +++ b/src/com/dougkeen/bart/ViewMapActivity.java @@ -1,11 +1,14 @@ package com.dougkeen.bart; +import android.content.Intent; import android.os.Bundle; import android.view.Menu; import android.view.MenuInflater; +import android.view.MenuItem; import android.webkit.WebView; import com.dougkeen.bart.actionbarcompat.ActionBarActivity; +import com.dougkeen.bart.model.Constants; public class ViewMapActivity extends ActionBarActivity { @@ -20,6 +23,9 @@ public class ViewMapActivity extends ActionBarActivity { webview.getSettings().setSupportZoom(true); webview.loadUrl("file:///android_res/drawable/map.png"); + + getActionBarHelper().setHomeButtonEnabled(true); + getActionBarHelper().setDisplayHomeAsUpEnabled(true); } @Override @@ -29,4 +35,14 @@ public class ViewMapActivity extends ActionBarActivity { return true; } + @Override + public boolean onOptionsItemSelected(MenuItem item) { + if (item.getItemId() == android.R.id.home) { + startActivity(new Intent(Intent.ACTION_PICK, + Constants.FAVORITE_CONTENT_URI)); + return true; + } + return super.onOptionsItemSelected(item); + } + } diff --git a/src/com/dougkeen/bart/actionbarcompat/ActionBarHelper.java b/src/com/dougkeen/bart/actionbarcompat/ActionBarHelper.java index 73de5d9..f4a2ab4 100644 --- a/src/com/dougkeen/bart/actionbarcompat/ActionBarHelper.java +++ b/src/com/dougkeen/bart/actionbarcompat/ActionBarHelper.java @@ -23,75 +23,86 @@ import android.view.Menu; import android.view.MenuInflater; /** - * An abstract class that handles some common action bar-related functionality in the app. This - * class provides functionality useful for both phones and tablets, and does not require any Android - * 3.0-specific features, although it uses them if available. - * - * Two implementations of this class are {@link ActionBarHelperBase} for a pre-Honeycomb version of - * the action bar, and {@link ActionBarHelperHoneycomb}, which uses the built-in ActionBar features - * in Android 3.0 and later. + * An abstract class that handles some common action bar-related functionality + * in the app. This class provides functionality useful for both phones and + * tablets, and does not require any Android 3.0-specific features, although it + * uses them if available. + * + * Two implementations of this class are {@link ActionBarHelperBase} for a + * pre-Honeycomb version of the action bar, and {@link ActionBarHelperHoneycomb} + * , which uses the built-in ActionBar features in Android 3.0 and later. */ public abstract class ActionBarHelper { - protected Activity mActivity; + protected Activity mActivity; - /** - * Factory method for creating {@link ActionBarHelper} objects for a - * given activity. Depending on which device the app is running, either a basic helper or - * Honeycomb-specific helper will be returned. - */ - public static ActionBarHelper createInstance(Activity activity) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { - return new ActionBarHelperICS(activity); - } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { - return new ActionBarHelperHoneycomb(activity); - } else { - return new ActionBarHelperBase(activity); - } - } + /** + * Factory method for creating {@link ActionBarHelper} objects for a given + * activity. Depending on which device the app is running, either a basic + * helper or Honeycomb-specific helper will be returned. + */ + public static ActionBarHelper createInstance(Activity activity) { + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) { + return new ActionBarHelperICS(activity); + } else if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) { + return new ActionBarHelperHoneycomb(activity); + } else { + return new ActionBarHelperBase(activity); + } + } - protected ActionBarHelper(Activity activity) { - mActivity = activity; - } + protected ActionBarHelper(Activity activity) { + mActivity = activity; + } - /** - * Action bar helper code to be run in {@link Activity#onCreate(android.os.Bundle)}. - */ - public void onCreate(Bundle savedInstanceState) { - } + /** + * Action bar helper code to be run in + * {@link Activity#onCreate(android.os.Bundle)}. + */ + public void onCreate(Bundle savedInstanceState) { + } - /** - * Action bar helper code to be run in {@link Activity#onPostCreate(android.os.Bundle)}. - */ - public void onPostCreate(Bundle savedInstanceState) { - } + /** + * Action bar helper code to be run in + * {@link Activity#onPostCreate(android.os.Bundle)}. + */ + public void onPostCreate(Bundle savedInstanceState) { + } - /** - * Action bar helper code to be run in {@link Activity#onCreateOptionsMenu(android.view.Menu)}. - * - * NOTE: Setting the visibility of menu items in menu is not currently supported. - */ - public boolean onCreateOptionsMenu(Menu menu) { - return true; - } + /** + * Action bar helper code to be run in + * {@link Activity#onCreateOptionsMenu(android.view.Menu)}. + * + * NOTE: Setting the visibility of menu items in menu is not + * currently supported. + */ + public boolean onCreateOptionsMenu(Menu menu) { + return true; + } - /** - * Action bar helper code to be run in {@link Activity#onTitleChanged(CharSequence, int)}. - */ - protected void onTitleChanged(CharSequence title, int color) { - } + /** + * Action bar helper code to be run in + * {@link Activity#onTitleChanged(CharSequence, int)}. + */ + protected void onTitleChanged(CharSequence title, int color) { + } - /** - * Sets the indeterminate loading state of the item with ID {@link R.id.menu_refresh}. - * (where the item ID was menu_refresh). - */ - public abstract void setRefreshActionItemState(boolean refreshing); + /** + * Sets the indeterminate loading state of the item with ID + * {@link R.id.menu_refresh}. (where the item ID was menu_refresh). + */ + public abstract void setRefreshActionItemState(boolean refreshing); - /** - * Returns a {@link MenuInflater} for use when inflating menus. The implementation of this - * method in {@link ActionBarHelperBase} returns a wrapped menu inflater that can read - * action bar metadata from a menu resource pre-Honeycomb. - */ - public MenuInflater getMenuInflater(MenuInflater superMenuInflater) { - return superMenuInflater; - } + /** + * Returns a {@link MenuInflater} for use when inflating menus. The + * implementation of this method in {@link ActionBarHelperBase} returns a + * wrapped menu inflater that can read action bar metadata from a menu + * resource pre-Honeycomb. + */ + public MenuInflater getMenuInflater(MenuInflater superMenuInflater) { + return superMenuInflater; + } + + abstract public void setHomeButtonEnabled(boolean enabled); + + abstract public void setDisplayHomeAsUpEnabled(boolean enabled); } diff --git a/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperBase.java b/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperBase.java index 66812c9..d6d36ed 100644 --- a/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperBase.java +++ b/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperBase.java @@ -316,4 +316,12 @@ public class ActionBarHelperBase extends ActionBarHelper { } } + + @Override + public void setHomeButtonEnabled(boolean enabled) { + } + + @Override + public void setDisplayHomeAsUpEnabled(boolean enabled) { + } } diff --git a/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperHoneycomb.java b/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperHoneycomb.java index 2643f3f..8dbf070 100644 --- a/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperHoneycomb.java +++ b/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperHoneycomb.java @@ -26,55 +26,65 @@ import android.view.View; import com.dougkeen.bart.R; /** - * An extension of {@link ActionBarHelper} that provides Android 3.0-specific functionality for - * Honeycomb tablets. It thus requires API level 11. + * An extension of {@link ActionBarHelper} that provides Android 3.0-specific + * functionality for Honeycomb tablets. It thus requires API level 11. */ public class ActionBarHelperHoneycomb extends ActionBarHelper { - private Menu mOptionsMenu; - private View mRefreshIndeterminateProgressView = null; + private Menu mOptionsMenu; + private View mRefreshIndeterminateProgressView = null; - protected ActionBarHelperHoneycomb(Activity activity) { - super(activity); - } + protected ActionBarHelperHoneycomb(Activity activity) { + super(activity); + } - @Override - public boolean onCreateOptionsMenu(Menu menu) { - mOptionsMenu = menu; - return super.onCreateOptionsMenu(menu); - } + @Override + public boolean onCreateOptionsMenu(Menu menu) { + mOptionsMenu = menu; + return super.onCreateOptionsMenu(menu); + } - @Override - public void setRefreshActionItemState(boolean refreshing) { - // On Honeycomb, we can set the state of the refresh button by giving it a custom - // action view. - if (mOptionsMenu == null) { - return; - } + @Override + public void setRefreshActionItemState(boolean refreshing) { + // On Honeycomb, we can set the state of the refresh button by giving it + // a custom + // action view. + if (mOptionsMenu == null) { + return; + } - final MenuItem refreshItem = mOptionsMenu.findItem(R.id.menu_refresh); - if (refreshItem != null) { - if (refreshing) { - if (mRefreshIndeterminateProgressView == null) { - LayoutInflater inflater = (LayoutInflater) - getActionBarThemedContext().getSystemService( - Context.LAYOUT_INFLATER_SERVICE); - mRefreshIndeterminateProgressView = inflater.inflate( - R.layout.actionbar_indeterminate_progress, null); - } + final MenuItem refreshItem = mOptionsMenu.findItem(R.id.menu_refresh); + if (refreshItem != null) { + if (refreshing) { + if (mRefreshIndeterminateProgressView == null) { + LayoutInflater inflater = (LayoutInflater) getActionBarThemedContext() + .getSystemService(Context.LAYOUT_INFLATER_SERVICE); + mRefreshIndeterminateProgressView = inflater.inflate( + R.layout.actionbar_indeterminate_progress, null); + } - refreshItem.setActionView(mRefreshIndeterminateProgressView); - } else { - refreshItem.setActionView(null); - } - } - } + refreshItem.setActionView(mRefreshIndeterminateProgressView); + } else { + refreshItem.setActionView(null); + } + } + } - /** - * Returns a {@link Context} suitable for inflating layouts for the action bar. The - * implementation for this method in {@link ActionBarHelperICS} asks the action bar for a - * themed context. - */ - protected Context getActionBarThemedContext() { - return mActivity; - } + /** + * Returns a {@link Context} suitable for inflating layouts for the action + * bar. The implementation for this method in {@link ActionBarHelperICS} + * asks the action bar for a themed context. + */ + protected Context getActionBarThemedContext() { + return mActivity; + } + + @Override + public void setHomeButtonEnabled(boolean enabled) { + mActivity.getActionBar().setHomeButtonEnabled(enabled); + } + + @Override + public void setDisplayHomeAsUpEnabled(boolean enabled) { + mActivity.getActionBar().setDisplayHomeAsUpEnabled(enabled); + } } diff --git a/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperICS.java b/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperICS.java index 64abb53..d71b045 100644 --- a/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperICS.java +++ b/src/com/dougkeen/bart/actionbarcompat/ActionBarHelperICS.java @@ -18,8 +18,6 @@ package com.dougkeen.bart.actionbarcompat; import android.app.Activity; import android.content.Context; -import android.view.Menu; -import android.view.MenuItem; /** * An extension of {@link com.example.android.actionbarcompat.ActionBarHelper} that provides Android