But ActionBar related stuff into new class ActionBarHelper

This commit is contained in:
fkloft 2013-09-18 15:31:50 +02:00
parent cdc98de350
commit eee9ce78e0
3 changed files with 174 additions and 105 deletions

View File

@ -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();
}
}

View File

@ -96,46 +96,20 @@ public class IITC_JSInterface {
} }
@JavascriptInterface @JavascriptInterface
public void switchToPane(String id) { public void switchToPane(final String id) {
final IITC_Mobile iitcm = (IITC_Mobile) mContext; 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() { iitcm.runOnUiThread(new Runnable() {
@Override @Override
public void run() { public void run() {
iitcm.getActionBar().setTitle(title); ActionBarHelper actionbar = iitcm.getActionBarHelper();
iitcm.backStackUpdate(button_id); 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) { public void setLayers(String base_layer, String overlay_layer) {
/* /*
* the layer strings have a form like: * the layer strings have a form like:
* [{"layerId":27,"name":"MapQuest OSM","active":true}, * [{"layerId":27,"name":"MapQuest OSM","active":true},
* {"layerId":28,"name":"Default Ingress Map","active":false}] * {"layerId":28,"name":"Default Ingress Map","active":false}]
* Put it in a JSONArray and parse it * Put it in a JSONArray and parse it
*/ */
JSONArray base_layersJSON = null; JSONArray base_layersJSON = null;
JSONArray overlay_layersJSON = null; JSONArray overlay_layersJSON = null;
@ -287,6 +261,7 @@ public class IITC_JSInterface {
final ListView list = dialog.getListView(); final ListView list = dialog.getListView();
list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() { list.setOnItemLongClickListener(new AdapterView.OnItemLongClickListener() {
boolean disable = false; boolean disable = false;
@Override @Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) { public boolean onItemLongClick(AdapterView<?> adapterView, View view, int i, long l) {
int j = 0; int j = 0;

View File

@ -1,5 +1,10 @@
package com.cradle.iitc_mobile; 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.ActionBar;
import android.app.Activity; import android.app.Activity;
import android.app.AlertDialog; import android.app.AlertDialog;
@ -16,11 +21,11 @@ import android.location.LocationManager;
import android.net.ConnectivityManager; import android.net.ConnectivityManager;
import android.net.NetworkInfo; import android.net.NetworkInfo;
import android.net.Uri; import android.net.Uri;
import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.util.Log; import android.util.Log;
import android.util.SparseArray;
import android.view.Menu; import android.view.Menu;
import android.view.MenuItem; import android.view.MenuItem;
import android.view.View; import android.view.View;
@ -32,10 +37,6 @@ import android.webkit.WebView;
import android.widget.SearchView; import android.widget.SearchView;
import android.widget.Toast; import android.widget.Toast;
import java.io.IOException;
import java.net.URISyntaxException;
import java.util.ArrayList;
public class IITC_Mobile extends Activity { public class IITC_Mobile extends Activity {
private static final int REQUEST_LOGIN = 1; private static final int REQUEST_LOGIN = 1;
@ -48,8 +49,6 @@ public class IITC_Mobile extends Activity {
private LocationManager mLocMngr = null; private LocationManager mLocMngr = null;
private LocationListener mLocListener = null; private LocationListener mLocListener = null;
private boolean mFullscreenMode = false; private boolean mFullscreenMode = false;
private boolean mFullscreenActionbar = false;
private ActionBar mActionBar;
private IITC_DeviceAccountLogin mLogin; private IITC_DeviceAccountLogin mLogin;
private MenuItem mSearchMenuItem; private MenuItem mSearchMenuItem;
private boolean mDesktopMode = false; private boolean mDesktopMode = false;
@ -57,6 +56,7 @@ public class IITC_Mobile extends Activity {
private boolean mReloadNeeded = false; private boolean mReloadNeeded = false;
private final ArrayList<String> mDialogStack = new ArrayList<String>(); private final ArrayList<String> mDialogStack = new ArrayList<String>();
private SharedPreferences mSharedPrefs; private SharedPreferences mSharedPrefs;
private ActionBarHelper mActionBarHelper;
// Used for custom back stack handling // Used for custom back stack handling
private final ArrayList<Integer> mBackStack = new ArrayList<Integer>(); private final ArrayList<Integer> mBackStack = new ArrayList<Integer>();
@ -64,6 +64,27 @@ public class IITC_Mobile extends Activity {
private int mCurrentPane = android.R.id.home; private int mCurrentPane = android.R.id.home;
private boolean mBackButtonPressed = false; private boolean mBackButtonPressed = false;
public static final SparseArray<String> PANE_TITLES = new SparseArray<String>();
public static final HashMap<String, Integer> PANES = new HashMap<String, Integer>();
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 @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
@ -74,13 +95,8 @@ public class IITC_Mobile extends Activity {
setContentView(R.layout.activity_main); setContentView(R.layout.activity_main);
mIitcWebView = (IITC_WebView) findViewById(R.id.iitc_webview); mIitcWebView = (IITC_WebView) findViewById(R.id.iitc_webview);
// fetch actionbar, set display flags, title and enable home button // pass ActionBar to helper because we deprecated getActionBar
mActionBar = this.getActionBar(); mActionBarHelper = new ActionBarHelper(this, super.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);
// do something if user changed something in the settings // do something if user changed something in the settings
mSharedPrefs = PreferenceManager mSharedPrefs = PreferenceManager
@ -91,21 +107,14 @@ public class IITC_Mobile extends Activity {
SharedPreferences sharedPreferences, String key) { SharedPreferences sharedPreferences, String key) {
if (key.equals("pref_force_desktop")) { if (key.equals("pref_force_desktop")) {
mDesktopMode = sharedPreferences.getBoolean("pref_force_desktop", false); mDesktopMode = sharedPreferences.getBoolean("pref_force_desktop", false);
if (mDesktopMode) { mActionBarHelper.onPrefChanged();
setActionBarHomeEnabledWithUp(false);
mActionBar.setTitle(getString(R.string.app_name));
} else mActionBar.setHomeButtonEnabled(true);
invalidateOptionsMenu(); invalidateOptionsMenu();
} }
if (key.equals("pref_user_loc")) if (key.equals("pref_user_loc"))
mIsLocEnabled = sharedPreferences.getBoolean("pref_user_loc", mIsLocEnabled = sharedPreferences.getBoolean("pref_user_loc",
false); false);
if (key.equals("pref_fullscreen_actionbar")) { if (key.equals("pref_fullscreen_actionbar")) {
mFullscreenActionbar = sharedPreferences.getBoolean("pref_fullscreen_actionbar", mActionBarHelper.onPrefChanged();
false);
if (mFullscreenMode)
IITC_Mobile.this.getActionBar().hide();
// no iitc reload needed here
return; return;
} }
if (key.equals("pref_advanced_menu")) { if (key.equals("pref_advanced_menu")) {
@ -142,8 +151,7 @@ public class IITC_Mobile extends Activity {
mLastLocation = location; mLastLocation = location;
} }
public void onStatusChanged(String provider, int status, public void onStatusChanged(String provider, int status, Bundle extras) {
Bundle extras) {
} }
public void onProviderEnabled(String provider) { public void onProviderEnabled(String provider) {
@ -163,11 +171,8 @@ public class IITC_Mobile extends Activity {
mLocListener); mLocListener);
} }
mFullscreenActionbar = mSharedPrefs.getBoolean("pref_fullscreen_actionbar", false);
// Clear the back stack // Clear the back stack
mBackStack.clear(); mBackStack.clear();
setActionBarHomeEnabledWithUp(false);
handleIntent(getIntent(), true); handleIntent(getIntent(), true);
} }
@ -222,7 +227,7 @@ public class IITC_Mobile extends Activity {
(SearchView) mSearchMenuItem.getActionView(); (SearchView) mSearchMenuItem.getActionView();
searchView.setQuery(query, false); searchView.setQuery(query, false);
searchView.clearFocus(); searchView.clearFocus();
mActionBar.setTitle(getString(R.string.app_name)); mActionBarHelper.switchTo(android.R.id.home);
backStackUpdate(android.R.id.home); backStackUpdate(android.R.id.home);
mIitcWebView.loadUrl("javascript:search('" + query + "');"); mIitcWebView.loadUrl("javascript:search('" + query + "');");
return; return;
@ -345,7 +350,7 @@ public class IITC_Mobile extends Activity {
} }
// exit fullscreen mode if it is enabled and action bar is disabled // exit fullscreen mode if it is enabled and action bar is disabled
// or the back stack is empty // or the back stack is empty
if (mFullscreenMode && (mBackStack.isEmpty() || mFullscreenActionbar)) { if (mFullscreenMode && (mBackStack.isEmpty() || mActionBarHelper.hideInFullscreen())) {
this.toggleFullscreen(); this.toggleFullscreen();
} else if (!mBackStack.isEmpty()) { } else if (!mBackStack.isEmpty()) {
// Pop last item from backstack and pretend the relevant menu item was clicked // 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() { public void backStackPop() {
// shouldn't be called when back stack is empty // shouldn't be called when back stack is empty
// catch wrong usage // catch wrong usage
if (mBackStack.isEmpty()) { if (mBackStack.isEmpty()) {
// Empty back stack means we should be at home (ie map) screen // Empty back stack means we should be at home (ie map) screen
setActionBarHomeEnabledWithUp(false); mActionBarHelper.switchTo(android.R.id.home);
mActionBar.setTitle(getString(R.string.app_name));
mIitcWebView.loadUrl("javascript: window.show('map');"); mIitcWebView.loadUrl("javascript: window.show('map');");
return; return;
} }
@ -403,13 +401,6 @@ public class IITC_Mobile extends Activity {
} }
mCurrentPane = itemId; 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 @Override
@ -456,14 +447,13 @@ public class IITC_Mobile extends Activity {
// the getLayers function calls the setLayers method of IITC_JSInterface // the getLayers function calls the setLayers method of IITC_JSInterface
mIitcWebView.loadUrl("javascript: window.layerChooser.getLayers()"); mIitcWebView.loadUrl("javascript: window.layerChooser.getLayers()");
return true; return true;
// get the users current location and focus it on map case R.id.locate: // get the users current location and focus it on map
case R.id.locate:
mIitcWebView.loadUrl("javascript: window.show('map');"); mIitcWebView.loadUrl("javascript: window.show('map');");
// get location from network by default // get location from network by default
if (!mIsLocEnabled) { if (!mIsLocEnabled) {
mIitcWebView.loadUrl("javascript: " + mIitcWebView.loadUrl("javascript: " +
"window.map.locate({setView : true, maxZoom: 15});"); "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 { } else {
if (mLastLocation != null) if (mLastLocation != null)
mIitcWebView.loadUrl("javascript: window.map.setView(new L.LatLng(" + mIitcWebView.loadUrl("javascript: window.map.setView(new L.LatLng(" +
@ -471,8 +461,7 @@ public class IITC_Mobile extends Activity {
mLastLocation.getLongitude() + "), 15);"); mLastLocation.getLongitude() + "), 15);");
} }
return true; return true;
// start settings activity case R.id.action_settings: // start settings activity
case R.id.action_settings:
Intent intent = new Intent(this, IITC_PreferenceActivity.class); Intent intent = new Intent(this, IITC_PreferenceActivity.class);
intent.putExtra("iitc_version", mIitcWebView.getWebViewClient() intent.putExtra("iitc_version", mIitcWebView.getWebViewClient()
.getIITCVersion()); .getIITCVersion());
@ -506,9 +495,8 @@ public class IITC_Mobile extends Activity {
} }
public void reloadIITC() { public void reloadIITC() {
mActionBar.setTitle(getString(R.string.app_name)); mActionBarHelper.reset();
mBackStack.clear(); mBackStack.clear();
setActionBarHomeEnabledWithUp(false);
// iitc starts on map after reload // iitc starts on map after reload
mCurrentPane = android.R.id.home; mCurrentPane = android.R.id.home;
this.loadUrl(mIntelUrl); this.loadUrl(mIntelUrl);
@ -556,19 +544,9 @@ public class IITC_Mobile extends Activity {
} }
public void toggleFullscreen() { public void toggleFullscreen() {
if (mFullscreenMode) { mFullscreenMode = !mFullscreenMode;
if (mFullscreenActionbar) mActionBarHelper.setFullscreen(mFullscreenMode);
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;
}
// toggle notification bar // toggle notification bar
WindowManager.LayoutParams attrs = getWindow().getAttributes(); WindowManager.LayoutParams attrs = getWindow().getAttributes();
attrs.flags ^= WindowManager.LayoutParams.FLAG_FULLSCREEN; 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. * called by IITC_WebViewClient when the Google login form is opened.
*/ */
public void onReceivedLoginRequest(IITC_WebViewClient client, WebView view, 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"); Log.d("iitcm", "logging in...set caching mode to default");
mIitcWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT); mIitcWebView.getSettings().setCacheMode(WebSettings.LOAD_DEFAULT);
mLogin = new IITC_DeviceAccountLogin(this, view, client); 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 = menu.findItem(R.id.menu_clear_cookies);
item.setVisible(mAdvancedMenu); 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;
}
} }