From eee9ce78e014629fdac65735a02519a0fb997cf7 Mon Sep 17 00:00:00 2001 From: fkloft Date: Wed, 18 Sep 2013 15:31:50 +0200 Subject: [PATCH] But ActionBar related stuff into new class ActionBarHelper --- .../cradle/iitc_mobile/ActionBarHelper.java | 102 ++++++++++++++ .../cradle/iitc_mobile/IITC_JSInterface.java | 53 ++------ .../com/cradle/iitc_mobile/IITC_Mobile.java | 124 ++++++++---------- 3 files changed, 174 insertions(+), 105 deletions(-) create mode 100644 mobile/src/com/cradle/iitc_mobile/ActionBarHelper.java diff --git a/mobile/src/com/cradle/iitc_mobile/ActionBarHelper.java b/mobile/src/com/cradle/iitc_mobile/ActionBarHelper.java new file mode 100644 index 00000000..912957e8 --- /dev/null +++ b/mobile/src/com/cradle/iitc_mobile/ActionBarHelper.java @@ -0,0 +1,102 @@ +package com.cradle.iitc_mobile; + +import android.app.ActionBar; +import android.content.SharedPreferences; +import android.preference.PreferenceManager; +import android.widget.Toast; + +public class ActionBarHelper { + /* + * Show/hide the up arrow on the left end + * getActionBar().setDisplayHomeAsUpEnabled(enabled); + * + * Show/hide the activity icon/logo + * getActionBar().setDisplayShowHomeEnabled(enabled); + * + * Show/hide the activity title + * getActionBar().setDisplayShowTitleEnabled(enabled); + * + * Makes the icon/title clickable + * getActionBar().setHomeButtonEnabled(enabled); + */ + + private ActionBar mActionBar; + private IITC_Mobile mIitc; + private SharedPreferences mPrefs; + + private boolean mDesktopMode = false; + private boolean mFullscreen = false; + private boolean mHideInFullscreen = false; + private int mPane = android.R.id.home; + + public ActionBarHelper(IITC_Mobile activity, ActionBar bar) { + mIitc = activity; + mActionBar = bar; + mPrefs = PreferenceManager.getDefaultSharedPreferences(activity); + + mActionBar.setDisplayShowHomeEnabled(true); // show icon + + onPrefChanged(); // also calls updateActionBar() + } + + private void updateActionBar() { + if (mDesktopMode) { + mActionBar.setDisplayHomeAsUpEnabled(false); // Hide "up" indicator + mActionBar.setHomeButtonEnabled(false); // Make icon unclickable + mActionBar.setTitle(mIitc.getString(R.string.app_name)); + } else { + if (mPane != android.R.id.home) + { + mActionBar.setDisplayHomeAsUpEnabled(true); // Show "up" indicator + mActionBar.setHomeButtonEnabled(true);// Make icon clickable + } + else + { + mActionBar.setDisplayHomeAsUpEnabled(false); // Hide "up" indicator + mActionBar.setHomeButtonEnabled(false); // Make icon unclickable + } + mActionBar.setTitle(IITC_Mobile.PANE_TITLES.get(mPane, mIitc.getString(R.string.app_name))); + } + + if (mFullscreen && mHideInFullscreen) + mActionBar.hide(); + else + mActionBar.show(); + } + + @Deprecated + public void goHome() { + switchTo(android.R.id.home); + } + + public boolean hideInFullscreen() { + return mHideInFullscreen; + } + + public void onPrefChanged() { + mDesktopMode = mPrefs.getBoolean("pref_force_desktop", false); + mHideInFullscreen = mPrefs.getBoolean("pref_fullscreen_actionbar", false); + updateActionBar(); + } + + public void reset() + { + mPane = android.R.id.home; + updateActionBar(); + } + + public void setFullscreen(boolean fullscreen) { + mFullscreen = fullscreen; + if (mFullscreen && mHideInFullscreen) { + // show a toast with instructions to exit the fullscreen mode again + Toast.makeText(mIitc, "Press back button to exit fullscreen", Toast.LENGTH_SHORT).show(); + } + + updateActionBar(); + } + + public void switchTo(int button) { + mPane = button; + updateActionBar(); + } +} diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java b/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java index e6a84364..66eecbad 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java @@ -96,46 +96,20 @@ public class IITC_JSInterface { } @JavascriptInterface - public void switchToPane(String id) { - + public void switchToPane(final String id) { final IITC_Mobile iitcm = (IITC_Mobile) mContext; - final int button_id; - final String title; - if (id.equals("map")) { - button_id = android.R.id.home; - title = iitcm.getString(R.string.app_name); - } else if (id.equals("info")) { - button_id = R.id.menu_info; - title = "Info"; - } else if (id.equals("full")) { - button_id = R.id.menu_full; - title = "Full"; - } else if (id.equals("compact")) { - button_id = R.id.menu_compact; - title = "Compact"; - } else if (id.equals("public")) { - button_id = R.id.menu_public; - title = "Public"; - } else if (id.equals("faction")) { - button_id = R.id.menu_faction; - title = "Faction"; - } else if (id.equals("debug")) { - button_id = R.id.menu_debug; - title = "Debug"; - } - // default - else { - button_id = android.R.id.home; - title = iitcm.getString(R.string.app_name); - } - - Log.d("iitcm", "switch to pane " + id); iitcm.runOnUiThread(new Runnable() { @Override public void run() { - iitcm.getActionBar().setTitle(title); - iitcm.backStackUpdate(button_id); + ActionBarHelper actionbar = iitcm.getActionBarHelper(); + Integer button = IITC_Mobile.PANES.get(id); + + if (button == null) + button = android.R.id.home; + + actionbar.switchTo(button); + iitcm.backStackUpdate(button); } }); } @@ -169,10 +143,10 @@ public class IITC_JSInterface { public void setLayers(String base_layer, String overlay_layer) { /* - * the layer strings have a form like: - * [{"layerId":27,"name":"MapQuest OSM","active":true}, - * {"layerId":28,"name":"Default Ingress Map","active":false}] - * Put it in a JSONArray and parse it + * the layer strings have a form like: + * [{"layerId":27,"name":"MapQuest OSM","active":true}, + * {"layerId":28,"name":"Default Ingress Map","active":false}] + * Put it in a JSONArray and parse it */ JSONArray base_layersJSON = null; JSONArray overlay_layersJSON = null; @@ -287,6 +261,7 @@ public class IITC_JSInterface { final ListView list = dialog.getListView(); list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { boolean disable = false; + @Override public boolean onItemLongClick(AdapterView adapterView, View view, int i, long l) { int j = 0; diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index 63278c8b..42794353 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -1,5 +1,10 @@ package com.cradle.iitc_mobile; +import java.io.IOException; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.HashMap; + import android.app.ActionBar; import android.app.Activity; import android.app.AlertDialog; @@ -16,11 +21,11 @@ import android.location.LocationManager; import android.net.ConnectivityManager; import android.net.NetworkInfo; import android.net.Uri; -import android.os.Build; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; import android.util.Log; +import android.util.SparseArray; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -32,10 +37,6 @@ import android.webkit.WebView; import android.widget.SearchView; import android.widget.Toast; -import java.io.IOException; -import java.net.URISyntaxException; -import java.util.ArrayList; - public class IITC_Mobile extends Activity { private static final int REQUEST_LOGIN = 1; @@ -48,8 +49,6 @@ public class IITC_Mobile extends Activity { private LocationManager mLocMngr = null; private LocationListener mLocListener = null; private boolean mFullscreenMode = false; - private boolean mFullscreenActionbar = false; - private ActionBar mActionBar; private IITC_DeviceAccountLogin mLogin; private MenuItem mSearchMenuItem; private boolean mDesktopMode = false; @@ -57,6 +56,7 @@ public class IITC_Mobile extends Activity { private boolean mReloadNeeded = false; private final ArrayList mDialogStack = new ArrayList(); private SharedPreferences mSharedPrefs; + private ActionBarHelper mActionBarHelper; // Used for custom back stack handling private final ArrayList mBackStack = new ArrayList(); @@ -64,6 +64,27 @@ public class IITC_Mobile extends Activity { private int mCurrentPane = android.R.id.home; private boolean mBackButtonPressed = false; + public static final SparseArray PANE_TITLES = new SparseArray(); + public static final HashMap PANES = new HashMap(); + + static { + PANES.put("map", android.R.id.home); + PANES.put("info", R.id.menu_info); + PANES.put("full", R.id.menu_full); + PANES.put("compact", R.id.menu_compact); + PANES.put("public", R.id.menu_public); + PANES.put("faction", R.id.menu_faction); + PANES.put("debug", R.id.menu_debug); + + // No need to declare android.R.id.home - that title is default + PANE_TITLES.append(R.id.menu_info, "Info"); + PANE_TITLES.append(R.id.menu_full, "Full"); + PANE_TITLES.append(R.id.menu_compact, "Compact"); + PANE_TITLES.append(R.id.menu_public, "Public"); + PANE_TITLES.append(R.id.menu_faction, "Faction"); + PANE_TITLES.append(R.id.menu_debug, "Debug"); + } + @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -74,13 +95,8 @@ public class IITC_Mobile extends Activity { setContentView(R.layout.activity_main); mIitcWebView = (IITC_WebView) findViewById(R.id.iitc_webview); - // fetch actionbar, set display flags, title and enable home button - mActionBar = this.getActionBar(); - mActionBar.setDisplayOptions(ActionBar.DISPLAY_SHOW_HOME - | ActionBar.DISPLAY_USE_LOGO | ActionBar.DISPLAY_SHOW_TITLE); - mActionBar.setTitle(getString(R.string.app_name)); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) - mActionBar.setHomeButtonEnabled(true); + // pass ActionBar to helper because we deprecated getActionBar + mActionBarHelper = new ActionBarHelper(this, super.getActionBar()); // do something if user changed something in the settings mSharedPrefs = PreferenceManager @@ -91,21 +107,14 @@ public class IITC_Mobile extends Activity { SharedPreferences sharedPreferences, String key) { if (key.equals("pref_force_desktop")) { mDesktopMode = sharedPreferences.getBoolean("pref_force_desktop", false); - if (mDesktopMode) { - setActionBarHomeEnabledWithUp(false); - mActionBar.setTitle(getString(R.string.app_name)); - } else mActionBar.setHomeButtonEnabled(true); + mActionBarHelper.onPrefChanged(); invalidateOptionsMenu(); } if (key.equals("pref_user_loc")) mIsLocEnabled = sharedPreferences.getBoolean("pref_user_loc", false); if (key.equals("pref_fullscreen_actionbar")) { - mFullscreenActionbar = sharedPreferences.getBoolean("pref_fullscreen_actionbar", - false); - if (mFullscreenMode) - IITC_Mobile.this.getActionBar().hide(); - // no iitc reload needed here + mActionBarHelper.onPrefChanged(); return; } if (key.equals("pref_advanced_menu")) { @@ -142,8 +151,7 @@ public class IITC_Mobile extends Activity { mLastLocation = location; } - public void onStatusChanged(String provider, int status, - Bundle extras) { + public void onStatusChanged(String provider, int status, Bundle extras) { } public void onProviderEnabled(String provider) { @@ -163,11 +171,8 @@ public class IITC_Mobile extends Activity { mLocListener); } - mFullscreenActionbar = mSharedPrefs.getBoolean("pref_fullscreen_actionbar", false); - // Clear the back stack mBackStack.clear(); - setActionBarHomeEnabledWithUp(false); handleIntent(getIntent(), true); } @@ -222,7 +227,7 @@ public class IITC_Mobile extends Activity { (SearchView) mSearchMenuItem.getActionView(); searchView.setQuery(query, false); searchView.clearFocus(); - mActionBar.setTitle(getString(R.string.app_name)); + mActionBarHelper.switchTo(android.R.id.home); backStackUpdate(android.R.id.home); mIitcWebView.loadUrl("javascript:search('" + query + "');"); return; @@ -345,7 +350,7 @@ public class IITC_Mobile extends Activity { } // exit fullscreen mode if it is enabled and action bar is disabled // or the back stack is empty - if (mFullscreenMode && (mBackStack.isEmpty() || mFullscreenActionbar)) { + if (mFullscreenMode && (mBackStack.isEmpty() || mActionBarHelper.hideInFullscreen())) { this.toggleFullscreen(); } else if (!mBackStack.isEmpty()) { // Pop last item from backstack and pretend the relevant menu item was clicked @@ -367,19 +372,12 @@ public class IITC_Mobile extends Activity { } } - private void setActionBarHomeEnabledWithUp(boolean enabled) { - mActionBar.setDisplayHomeAsUpEnabled(enabled); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.ICE_CREAM_SANDWICH) - mActionBar.setHomeButtonEnabled(enabled); - } - public void backStackPop() { // shouldn't be called when back stack is empty // catch wrong usage if (mBackStack.isEmpty()) { // Empty back stack means we should be at home (ie map) screen - setActionBarHomeEnabledWithUp(false); - mActionBar.setTitle(getString(R.string.app_name)); + mActionBarHelper.switchTo(android.R.id.home); mIitcWebView.loadUrl("javascript: window.show('map');"); return; } @@ -403,13 +401,6 @@ public class IITC_Mobile extends Activity { } mCurrentPane = itemId; - if (mBackStack.size() >= 1) { - setActionBarHomeEnabledWithUp(true); - } else { - // if we popped our last item from stack...illustrate it on home button - // Empty back stack means we should be at home (ie map) screen - setActionBarHomeEnabledWithUp(false); - } } @Override @@ -456,14 +447,13 @@ public class IITC_Mobile extends Activity { // the getLayers function calls the setLayers method of IITC_JSInterface mIitcWebView.loadUrl("javascript: window.layerChooser.getLayers()"); return true; - // get the users current location and focus it on map - case R.id.locate: + case R.id.locate: // get the users current location and focus it on map mIitcWebView.loadUrl("javascript: window.show('map');"); // get location from network by default if (!mIsLocEnabled) { mIitcWebView.loadUrl("javascript: " + "window.map.locate({setView : true, maxZoom: 15});"); - // if gps location is displayed we can use a better location without any costs + // if gps location is displayed we can use a better location without any costs } else { if (mLastLocation != null) mIitcWebView.loadUrl("javascript: window.map.setView(new L.LatLng(" + @@ -471,8 +461,7 @@ public class IITC_Mobile extends Activity { mLastLocation.getLongitude() + "), 15);"); } return true; - // start settings activity - case R.id.action_settings: + case R.id.action_settings: // start settings activity Intent intent = new Intent(this, IITC_PreferenceActivity.class); intent.putExtra("iitc_version", mIitcWebView.getWebViewClient() .getIITCVersion()); @@ -506,9 +495,8 @@ public class IITC_Mobile extends Activity { } public void reloadIITC() { - mActionBar.setTitle(getString(R.string.app_name)); + mActionBarHelper.reset(); mBackStack.clear(); - setActionBarHomeEnabledWithUp(false); // iitc starts on map after reload mCurrentPane = android.R.id.home; this.loadUrl(mIntelUrl); @@ -556,19 +544,9 @@ public class IITC_Mobile extends Activity { } public void toggleFullscreen() { - if (mFullscreenMode) { - if (mFullscreenActionbar) - this.getActionBar().show(); - this.mFullscreenMode = false; - } else { - if (mFullscreenActionbar) { - this.getActionBar().hide(); - // show a toast with instructions to exit the fc mode again - Toast.makeText(this, "Press back button to exit fullscreen", - Toast.LENGTH_SHORT).show(); - } - this.mFullscreenMode = true; - } + mFullscreenMode = !mFullscreenMode; + mActionBarHelper.setFullscreen(mFullscreenMode); + // toggle notification bar WindowManager.LayoutParams attrs = getWindow().getAttributes(); attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN; @@ -604,7 +582,7 @@ public class IITC_Mobile extends Activity { * called by IITC_WebViewClient when the Google login form is opened. */ public void onReceivedLoginRequest(IITC_WebViewClient client, WebView view, - String realm, String account, String args) { + String realm, String account, String args) { Log.d("iitcm", "logging in...set caching mode to default"); mIitcWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); mLogin = new IITC_DeviceAccountLogin(this, view, client); @@ -665,4 +643,18 @@ public class IITC_Mobile extends Activity { item = menu.findItem(R.id.menu_clear_cookies); item.setVisible(mAdvancedMenu); } + + /** + * @deprecated ActionBar related stuff should be handled by ActionBarHelper + */ + @Deprecated + @Override + public ActionBar getActionBar() { + return super.getActionBar(); + } + + public ActionBarHelper getActionBarHelper() + { + return mActionBarHelper; + } }