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

@@ -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<String> mDialogStack = new ArrayList<String>();
private SharedPreferences mSharedPrefs;
private ActionBarHelper mActionBarHelper;
// Used for custom back stack handling
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 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
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;
}
}