diff --git a/mobile/AndroidManifest.xml b/mobile/AndroidManifest.xml index fda15a23..f23c1d2b 100644 --- a/mobile/AndroidManifest.xml +++ b/mobile/AndroidManifest.xml @@ -18,8 +18,7 @@ android:allowBackup="true" android:icon="@drawable/ic_iitcm" android:label="@string/app_name" - android:theme="@style/AppTheme" - android:uiOptions="splitActionBarWhenNarrow"> + android:theme="@style/AppTheme"> + - - + - \ No newline at end of file + + + + + + + \ No newline at end of file diff --git a/mobile/res/layout/list_item_selectable.xml b/mobile/res/layout/list_item_selectable.xml new file mode 100644 index 00000000..a82df550 --- /dev/null +++ b/mobile/res/layout/list_item_selectable.xml @@ -0,0 +1,11 @@ + + diff --git a/mobile/res/menu/main.xml b/mobile/res/menu/main.xml index 479e4446..a998cfe7 100644 --- a/mobile/res/menu/main.xml +++ b/mobile/res/menu/main.xml @@ -5,46 +5,6 @@ android:orderInCategory="10" android:showAsAction="ifRoom|collapseActionView" android:actionViewClass="android.widget.SearchView" /> - - - - - - - - - - - - - - - - + android:title="@string/action_settings"> Share using… Copy to clipboard Settings + Show navigation menu + Hide navigation menu Reload IITC Print Version Toggle fullscreen @@ -81,13 +83,6 @@ IITC source Load IITC main script from url or use local script. Currently used source: - Chat - Full - Compact - Public - Faction - Info - Debug Clear Cookies Search Choose account to login diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_ActionBarHelper.java b/mobile/src/com/cradle/iitc_mobile/IITC_ActionBarHelper.java deleted file mode 100644 index 01b6ebe7..00000000 --- a/mobile/src/com/cradle/iitc_mobile/IITC_ActionBarHelper.java +++ /dev/null @@ -1,158 +0,0 @@ -package com.cradle.iitc_mobile; - -import android.app.ActionBar; -import android.app.ActionBar.OnNavigationListener; -import android.content.SharedPreferences; -import android.preference.PreferenceManager; -import android.widget.ArrayAdapter; -import android.widget.Toast; - -public class IITC_ActionBarHelper implements OnNavigationListener { - // Show/hide the up arrow on the very left - // 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 class HighlighterAdapter extends ArrayAdapter { - public HighlighterAdapter() { - super(mIitc, android.R.layout.simple_list_item_1); - clear(); - } - - @Override - public void add(String object) { - super.remove(object); // to avoid duplicates - super.add(object); - } - - @Override - public void clear() { - super.clear(); - add("No Highlights");// Probably must be the same as window._no_highlighter - } - } - - private IITC_Mobile mIitc; - private ActionBar mActionBar; - private SharedPreferences mPrefs; - private HighlighterAdapter mHighlighters; - - private String mActiveHighlighter = null; - private boolean mDesktopMode = false; - private boolean mFullscreen = false; - private boolean mHideInFullscreen = false; - private int mPane = android.R.id.home; - - public IITC_ActionBarHelper(IITC_Mobile activity, ActionBar bar) { - mIitc = activity; - mActionBar = bar; - mPrefs = PreferenceManager.getDefaultSharedPreferences(activity); - mHighlighters = new HighlighterAdapter(); - - mActionBar.setDisplayShowHomeEnabled(true); // show icon - mActionBar.setListNavigationCallbacks(mHighlighters, this); - - onPrefChanged(); // also calls updateActionBar() - } - - private void updateActionBar() { - boolean showHighlighter = true; - - 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 - showHighlighter = false; - } - 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 (mHighlighters.getCount() < 2) // there should always be "No Highlights" - showHighlighter = false; - - if (showHighlighter) { - mActionBar.setDisplayShowTitleEnabled(false); // Hide title - mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); - setActiveHighlighter(mActiveHighlighter); - } else { - mActionBar.setDisplayShowTitleEnabled(true); // Show title - mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); - } - if (mFullscreen && mHideInFullscreen) - mActionBar.hide(); - else - mActionBar.show(); - } - - public void addPortalHighlighter(String name) { - mHighlighters.add(name); - - if (name.equals(mActiveHighlighter)) - setActiveHighlighter(name); - - updateActionBar(); - } - - public boolean hideInFullscreen() { - return mHideInFullscreen; - } - - @Override - public boolean onNavigationItemSelected(int position, long itemId) { - String name = mHighlighters.getItem(position); - mIitc.getWebView().loadUrl("javascript: window.changePortalHighlights('" + name + "')"); - return true; - } - - public void onPrefChanged() { - mDesktopMode = mPrefs.getBoolean("pref_force_desktop", false); - mHideInFullscreen = mPrefs.getBoolean("pref_fullscreen_actionbar", false); - updateActionBar(); - } - - public void reset() { - mHighlighters.clear(); - mPane = android.R.id.home; - updateActionBar(); - } - - public void setActiveHighlighter(String name) { - mActiveHighlighter = name; - - if (mActionBar.getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST) { - int position = mHighlighters.getPosition(mActiveHighlighter); - if (position >= 0 && position < mActionBar.getNavigationItemCount()) - mActionBar.setSelectedNavigationItem(position); - } - } - - 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 cd3cbf94..a2cab2dc 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_JSInterface.java @@ -18,12 +18,14 @@ import android.widget.AdapterView; import android.widget.ListView; import android.widget.Toast; +import com.cradle.iitc_mobile.IITC_NavigationHelper.Pane; import com.cradle.iitc_mobile.share.ShareActivity; import org.json.JSONArray; import org.json.JSONException; import java.util.HashMap; +import java.util.Locale; // provide communication between IITC script and android app public class IITC_JSInterface { @@ -102,14 +104,14 @@ public class IITC_JSInterface { iitcm.runOnUiThread(new Runnable() { @Override public void run() { - IITC_ActionBarHelper actionbar = iitcm.getActionBarHelper(); - Integer button = IITC_Mobile.PANES.get(id); + Pane pane; + try { + pane = Pane.valueOf(id.toUpperCase(Locale.getDefault())); + } catch (IllegalArgumentException e) { + pane = Pane.MAP; + } - if (button == null) - button = android.R.id.home; - - actionbar.switchTo(button); - iitcm.backStackUpdate(button); + iitcm.setCurrentPane(pane); } }); } @@ -132,8 +134,7 @@ public class IITC_JSInterface { iitc.runOnUiThread(new Runnable() { @Override public void run() { - iitc.findViewById(R.id.iitc_webview).setVisibility(View.VISIBLE); - iitc.findViewById(R.id.imageLoading).setVisibility(View.GONE); + iitc.setLoadingState(false); } }); } @@ -236,7 +237,7 @@ public class IITC_JSInterface { iitc.runOnUiThread(new Runnable() { @Override public void run() { - iitc.getActionBarHelper().addPortalHighlighter(name); + iitc.getNavigationHelper().addPortalHighlighter(name); } }); } @@ -247,7 +248,7 @@ public class IITC_JSInterface { iitc.runOnUiThread(new Runnable() { @Override public void run() { - iitc.getActionBarHelper().setActiveHighlighter(name); + iitc.getNavigationHelper().setActiveHighlighter(name); } }); } diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java index b8d84c83..ed0d646a 100644 --- a/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java +++ b/mobile/src/com/cradle/iitc_mobile/IITC_Mobile.java @@ -17,8 +17,8 @@ import android.net.Uri; import android.os.Bundle; import android.os.Handler; import android.preference.PreferenceManager; +import android.support.v4.widget.DrawerLayout; import android.util.Log; -import android.util.SparseArray; import android.view.Menu; import android.view.MenuItem; import android.view.View; @@ -27,13 +27,16 @@ import android.view.WindowManager; import android.webkit.CookieManager; import android.webkit.WebSettings; import android.webkit.WebView; +import android.widget.ListView; import android.widget.SearchView; import android.widget.Toast; +import com.cradle.iitc_mobile.IITC_NavigationHelper.Pane; + import java.io.IOException; import java.net.URISyntaxException; -import java.util.ArrayList; -import java.util.HashMap; +import java.util.Locale; +import java.util.Stack; public class IITC_Mobile extends Activity { @@ -52,37 +55,16 @@ public class IITC_Mobile extends Activity { private boolean mDesktopMode = false; private boolean mAdvancedMenu = false; private boolean mReloadNeeded = false; - private final ArrayList mDialogStack = new ArrayList(); + private final Stack mDialogStack = new Stack(); private SharedPreferences mSharedPrefs; - private IITC_ActionBarHelper mActionBarHelper; + private IITC_NavigationHelper mNavigationHelper; // Used for custom back stack handling - private final ArrayList mBackStack = new ArrayList(); + private final Stack mBackStack = new Stack(); private boolean mBackStackPush = true; - private int mCurrentPane = android.R.id.home; + private Pane mCurrentPane = Pane.MAP; 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); @@ -92,9 +74,10 @@ public class IITC_Mobile extends Activity { setContentView(R.layout.activity_main); mIitcWebView = (IITC_WebView) findViewById(R.id.iitc_webview); - // pass ActionBar to helper because we deprecated getActionBar - mActionBarHelper = new IITC_ActionBarHelper(this, super.getActionBar()); + mNavigationHelper = new IITC_NavigationHelper(this, super.getActionBar(), + (ListView) findViewById(R.id.left_drawer), + (DrawerLayout) findViewById(R.id.drawer_layout)); // do something if user changed something in the settings mSharedPrefs = PreferenceManager.getDefaultSharedPreferences(this); @@ -104,18 +87,19 @@ public class IITC_Mobile extends Activity { SharedPreferences sharedPreferences, String key) { if (key.equals("pref_force_desktop")) { mDesktopMode = sharedPreferences.getBoolean("pref_force_desktop", false); - mActionBarHelper.onPrefChanged(); + mNavigationHelper.onPrefChanged(); invalidateOptionsMenu(); } if (key.equals("pref_user_loc")) mIsLocEnabled = sharedPreferences.getBoolean("pref_user_loc", false); if (key.equals("pref_fullscreen_actionbar")) { - mActionBarHelper.onPrefChanged(); + mNavigationHelper.onPrefChanged(); return; } if (key.equals("pref_advanced_menu")) { mAdvancedMenu = sharedPreferences.getBoolean("pref_advanced_menu", false); + mNavigationHelper.setDebugMode(mAdvancedMenu); invalidateOptionsMenu(); // no reload needed return; @@ -224,8 +208,8 @@ public class IITC_Mobile extends Activity { (SearchView) mSearchMenuItem.getActionView(); searchView.setQuery(query, false); searchView.clearFocus(); - mActionBarHelper.switchTo(android.R.id.home); - backStackUpdate(android.R.id.home); + + switchToPane(Pane.MAP); mIitcWebView.loadUrl("javascript:search('" + query + "');"); return; } @@ -318,18 +302,25 @@ public class IITC_Mobile extends Activity { public void onConfigurationChanged(Configuration newConfig) { super.onConfigurationChanged(newConfig); + mNavigationHelper.onConfigurationChanged(newConfig); + Log.d("iitcm", "configuration changed...restoring...reset idleTimer"); mIitcWebView.loadUrl("javascript: window.idleTime = 0"); mIitcWebView.loadUrl("javascript: window.renderUpdateStatus()"); } + @Override + protected void onPostCreate(Bundle savedInstanceState) { + super.onPostCreate(savedInstanceState); + mNavigationHelper.onPostCreate(savedInstanceState); + } + // we want a self defined behavior for the back button @Override public void onBackPressed() { // first kill all open iitc dialogs if (!mDialogStack.isEmpty()) { - int last = mDialogStack.size() - 1; - String id = mDialogStack.get(last); + String id = mDialogStack.pop(); mIitcWebView.loadUrl("javascript: " + "var selector = $(window.DIALOGS['" + id + "']); " + "selector.dialog('close'); " + @@ -338,7 +329,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() || mActionBarHelper.hideInFullscreen())) { + if (mFullscreenMode && (mBackStack.isEmpty() || mNavigationHelper.hideInFullscreen())) { this.toggleFullscreen(); } else if (!mBackStack.isEmpty()) { // Pop last item from backstack and pretend the relevant menu item was clicked @@ -364,31 +355,30 @@ public class IITC_Mobile extends Activity { // 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 - mActionBarHelper.switchTo(android.R.id.home); - mIitcWebView.loadUrl("javascript: window.show('map');"); - return; + mBackStack.push(Pane.MAP); } - int index = mBackStack.size() - 1; - int itemId = mBackStack.remove(index); + + Pane pane = mBackStack.pop(); mBackStackPush = false; - handleMenuItemSelected(itemId); + switchToPane(pane); } - public void backStackUpdate(int itemId) { + public void setCurrentPane(Pane pane) { // ensure no double adds - if (itemId == mCurrentPane) return; - if (itemId == android.R.id.home) { - mBackStack.clear(); - mBackStackPush = true; - } else { - if (mBackStackPush) - mBackStack.add(mCurrentPane); - else - mBackStackPush = true; - } + if (pane == mCurrentPane) return; - mCurrentPane = itemId; + if (mBackStackPush) + mBackStack.push(mCurrentPane); + else + mBackStackPush = true; + + mCurrentPane = pane; + mNavigationHelper.switchTo(pane); + } + + public void switchToPane(Pane pane) { + String name = pane.name().toLowerCase(Locale.getDefault()); + mIitcWebView.loadUrl("javascript: window.show('" + name + "');"); } @Override @@ -397,30 +387,32 @@ public class IITC_Mobile extends Activity { getMenuInflater().inflate(R.menu.main, menu); // Get the SearchView and set the searchable configuration SearchManager searchManager = (SearchManager) getSystemService(Context.SEARCH_SERVICE); - this.mSearchMenuItem = menu.findItem(R.id.menu_search); + mSearchMenuItem = menu.findItem(R.id.menu_search); final SearchView searchView = (SearchView) mSearchMenuItem.getActionView(); // Assumes current activity is the searchable activity searchView.setSearchableInfo(searchManager.getSearchableInfo(getComponentName())); searchView.setIconifiedByDefault(false); // Do not iconify the widget; expand it by default + // enable/disable mDesktopMode menu - enableDesktopUI(menu); - enableAdvancedMenu(menu); + MenuItem item = menu.findItem(R.id.menu_clear_cookies); + item.setVisible(mAdvancedMenu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { + if (mNavigationHelper.onOptionsItemSelected(item)) + return true; + // Handle item selection final int itemId = item.getItemId(); - boolean result = handleMenuItemSelected(itemId); - return result || super.onOptionsItemSelected(item); - } - public boolean handleMenuItemSelected(int itemId) { switch (itemId) { case android.R.id.home: - mIitcWebView.loadUrl("javascript: window.show('map');"); + mBackStack.clear(); + mBackStackPush = false; + switchToPane(Pane.MAP); return true; case R.id.reload_button: reloadIITC(); @@ -430,13 +422,13 @@ public class IITC_Mobile extends Activity { return true; case R.id.layer_chooser: // Force map view to handle potential issue with back stack - if (!mBackStack.isEmpty() && mCurrentPane != android.R.id.home) - mIitcWebView.loadUrl("javascript: window.show('map');"); + if (!mBackStack.isEmpty() && mCurrentPane != Pane.MAP) + switchToPane(Pane.MAP); // the getLayers function calls the setLayers method of IITC_JSInterface mIitcWebView.loadUrl("javascript: window.layerChooser.getLayers()"); return true; case R.id.locate: // get the users current location and focus it on map - mIitcWebView.loadUrl("javascript: window.show('map');"); + switchToPane(Pane.MAP); // get location from network by default if (!mIsLocEnabled) { mIitcWebView.loadUrl("javascript: " + @@ -455,24 +447,6 @@ public class IITC_Mobile extends Activity { .getIITCVersion()); startActivity(intent); return true; - case R.id.menu_info: - mIitcWebView.loadUrl("javascript: window.show('info');"); - return true; - case R.id.menu_full: - mIitcWebView.loadUrl("javascript: window.show('full');"); - return true; - case R.id.menu_compact: - mIitcWebView.loadUrl("javascript: window.show('compact');"); - return true; - case R.id.menu_public: - mIitcWebView.loadUrl("javascript: window.show('public');"); - return true; - case R.id.menu_faction: - mIitcWebView.loadUrl("javascript: window.show('faction');"); - return true; - case R.id.menu_debug: - mIitcWebView.loadUrl("javascript: window.show('debug')"); - return true; case R.id.menu_clear_cookies: CookieManager cm = CookieManager.getInstance(); cm.removeAllCookie(); @@ -483,10 +457,11 @@ public class IITC_Mobile extends Activity { } public void reloadIITC() { - mActionBarHelper.reset(); + mNavigationHelper.reset(); mBackStack.clear(); + mBackStackPush = true; // iitc starts on map after reload - mCurrentPane = android.R.id.home; + mCurrentPane = Pane.MAP; loadUrl(mIntelUrl); mReloadNeeded = false; } @@ -512,7 +487,7 @@ public class IITC_Mobile extends Activity { // inject the iitc-script and load the intel url // plugins are injected onPageFinished public void loadUrl(String url) { - showSplashScreen(); + setLoadingState(true); url = addUrlParam(url); loadIITC(); mIitcWebView.loadUrl(url); @@ -534,7 +509,7 @@ public class IITC_Mobile extends Activity { public void toggleFullscreen() { mFullscreenMode = !mFullscreenMode; - mActionBarHelper.setFullscreen(mFullscreenMode); + mNavigationHelper.setFullscreen(mFullscreenMode); // toggle notification bar WindowManager.LayoutParams attrs = getWindow().getAttributes(); @@ -584,18 +559,7 @@ public class IITC_Mobile extends Activity { public void loginSucceeded() { // garbage collection mLogin = null; - showSplashScreen(); - } - - // disable/enable some menu buttons... - public void enableDesktopUI(Menu menu) { - MenuItem item; - item = menu.findItem(R.id.menu_chat); - item.setVisible(!mDesktopMode); - item = menu.findItem(R.id.menu_info); - item.setVisible(!mDesktopMode); - item = menu.findItem(R.id.menu_debug); - item.setVisible(!mDesktopMode); + setLoadingState(true); } // remove dialog and add it back again @@ -604,37 +568,35 @@ public class IITC_Mobile extends Activity { public void setFocusedDialog(String id) { Log.d("iitcm", "Dialog " + id + " focused"); mDialogStack.remove(id); - mDialogStack.add(id); + mDialogStack.push(id); } // called by the javascript interface public void dialogOpened(String id, boolean open) { if (open) { Log.d("iitcm", "Dialog " + id + " added"); - mDialogStack.add(id); + mDialogStack.push(id); } else { Log.d("iitcm", "Dialog " + id + " closed"); mDialogStack.remove(id); } } - public void showSplashScreen() { - if (!mSharedPrefs.getBoolean("pref_disable_splash", false)) { + public void setLoadingState(boolean isLoading) { + mNavigationHelper.setLoadingState(isLoading); + + if (isLoading && !mSharedPrefs.getBoolean("pref_disable_splash", false)) { findViewById(R.id.iitc_webview).setVisibility(View.GONE); findViewById(R.id.imageLoading).setVisibility(View.VISIBLE); + } else { + findViewById(R.id.iitc_webview).setVisibility(View.VISIBLE); + findViewById(R.id.imageLoading).setVisibility(View.GONE); } } - public void enableAdvancedMenu(Menu menu) { - MenuItem item; - item = menu.findItem(R.id.menu_debug); - item.setVisible(mAdvancedMenu); - item = menu.findItem(R.id.menu_clear_cookies); - item.setVisible(mAdvancedMenu); - } - /** - * @deprecated ActionBar related stuff should be handled by ActionBarHelper + * @deprecated ActionBar related stuff should be handled by IITC_NavigationHelper + * @see getNavigationHelper() */ @Deprecated @Override @@ -642,7 +604,7 @@ public class IITC_Mobile extends Activity { return super.getActionBar(); } - public IITC_ActionBarHelper getActionBarHelper() { - return mActionBarHelper; + public IITC_NavigationHelper getNavigationHelper() { + return mNavigationHelper; } } diff --git a/mobile/src/com/cradle/iitc_mobile/IITC_NavigationHelper.java b/mobile/src/com/cradle/iitc_mobile/IITC_NavigationHelper.java new file mode 100644 index 00000000..3f152dde --- /dev/null +++ b/mobile/src/com/cradle/iitc_mobile/IITC_NavigationHelper.java @@ -0,0 +1,315 @@ +package com.cradle.iitc_mobile; + +import android.app.ActionBar; +import android.app.ActionBar.OnNavigationListener; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.preference.PreferenceManager; +import android.support.v4.app.ActionBarDrawerToggle; +import android.support.v4.widget.DrawerLayout; +import android.view.View; +import android.view.ViewGroup; +import android.widget.AdapterView; +import android.widget.AdapterView.OnItemClickListener; +import android.widget.ArrayAdapter; +import android.widget.ListView; +import android.widget.TextView; +import android.widget.Toast; + +public class IITC_NavigationHelper extends ActionBarDrawerToggle implements OnNavigationListener, OnItemClickListener { + // Show/hide the up arrow on the very left + // 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 class HighlighterAdapter extends ArrayAdapter { + public HighlighterAdapter() { + super(mIitc, android.R.layout.simple_list_item_1); + clear(); + } + + @Override + public void add(String object) { + super.remove(object); // to avoid duplicates + super.add(object); + } + + @Override + public void clear() { + super.clear(); + add("No Highlights");// Probably must be the same as window._no_highlighter + } + }; + + private class NavigationAdapter extends ArrayAdapter { + public NavigationAdapter() { + super(mIitc, R.layout.list_item_selectable); + + add(Pane.MAP); + add(Pane.INFO); + add(Pane.FULL); + add(Pane.COMPACT); + add(Pane.PUBLIC); + add(Pane.FACTION); + + if (mPrefs.getBoolean("pref_advanced_menu", false)) + add(Pane.DEBUG); + } + + @Override + public View getView(int position, View convertView, ViewGroup parent) { + TextView view = (TextView) super.getView(position, convertView, parent); + Pane item = getItem(position); + if (item == Pane.MAP) + view.setText("Map"); + else + view.setText(getPaneTitle(item)); + + int icon = 0; + switch (item) + { + case MAP: + icon = R.drawable.location_map; + break; + case INFO: + icon = R.drawable.action_about; + break; + case FULL: + case COMPACT: + case PUBLIC: + case FACTION: + icon = R.drawable.social_group; + break; + } + + if (icon != 0) + view.setCompoundDrawablesWithIntrinsicBounds(icon, 0, 0, 0); + + return view; + } + } + + public static enum Pane { + COMPACT, DEBUG, FACTION, FULL, INFO, MAP, PUBLIC + } + + private IITC_Mobile mIitc; + private ActionBar mActionBar; + private SharedPreferences mPrefs; + private HighlighterAdapter mHighlighters; + private NavigationAdapter mNavigationAdapter; + private DrawerLayout mDrawerLayout; + private ListView mDrawerList; + + private String mActiveHighlighter = null; + private boolean mDesktopMode = false; + private boolean mDrawerOpened; + private boolean mFullscreen = false; + private boolean mIsLoading; + private boolean mHideInFullscreen = false; + private Pane mPane = Pane.MAP; + + public IITC_NavigationHelper(IITC_Mobile activity, ActionBar bar, ListView drawerList, DrawerLayout drawerLayout) { + super(activity, drawerLayout, R.drawable.ic_drawer, R.string.drawer_open, R.string.drawer_close); + + mIitc = activity; + mActionBar = bar; + mDrawerList = drawerList; + mDrawerLayout = drawerLayout; + + mPrefs = PreferenceManager.getDefaultSharedPreferences(activity); + mHighlighters = new HighlighterAdapter(); + + mActionBar.setDisplayShowHomeEnabled(true); // show icon + mActionBar.setListNavigationCallbacks(mHighlighters, this); + + mNavigationAdapter = new NavigationAdapter(); + mDrawerList.setAdapter(mNavigationAdapter); + mDrawerList.setOnItemClickListener(this); + mDrawerList.setItemChecked(0, true); + mDrawerLayout.setDrawerListener(this); + + onPrefChanged(); // also calls updateActionBar() + } + + private void updateActionBar() { + boolean showHighlighter = true; + + int position = mNavigationAdapter.getPosition(mPane); + if (position >= 0 && position < mNavigationAdapter.getCount()) + mDrawerList.setItemChecked(position, true); + + if (mDesktopMode) { + mActionBar.setDisplayHomeAsUpEnabled(false); // Hide "up" indicator + mActionBar.setHomeButtonEnabled(false); // Make icon unclickable + mActionBar.setTitle(mIitc.getString(R.string.app_name)); + mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); + setDrawerIndicatorEnabled(false); + } else { + if (mIsLoading) { + mActionBar.setDisplayHomeAsUpEnabled(false); // Hide "up" indicator + mActionBar.setHomeButtonEnabled(false);// Make icon unclickable + mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED); + setDrawerIndicatorEnabled(false); + } else { + mActionBar.setDisplayHomeAsUpEnabled(true); // Show "up" indicator + mActionBar.setHomeButtonEnabled(true);// Make icon clickable + mDrawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED); + + if (mPane != Pane.MAP) + setDrawerIndicatorEnabled(false); + else + setDrawerIndicatorEnabled(true); + } + + if (mPane != Pane.MAP) + showHighlighter = false; + + mActionBar.setTitle(getPaneTitle(mPane)); + } + + if (mHighlighters.getCount() < 2) // there should always be "No Highlights" + showHighlighter = false; + + if(mDrawerOpened) + showHighlighter = false; + + if (showHighlighter) { + mActionBar.setDisplayShowTitleEnabled(false); // Hide title + mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_LIST); + setActiveHighlighter(mActiveHighlighter); + } else { + mActionBar.setDisplayShowTitleEnabled(true); // Show title + mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD); + } + if (mFullscreen && mHideInFullscreen) + mActionBar.hide(); + else + mActionBar.show(); + } + + public void addPortalHighlighter(String name) { + mHighlighters.add(name); + + if (name.equals(mActiveHighlighter)) + setActiveHighlighter(name); + + updateActionBar(); + } + + public String getPaneTitle(Pane pane) + { + switch (pane) { + case INFO: + return "Info"; + case FULL: + return "Full"; + case COMPACT: + return "Compact"; + case PUBLIC: + return "Public"; + case FACTION: + return "Faction"; + case DEBUG: + return "Debug"; + default: + return mIitc.getString(R.string.app_name); + } + } + + public boolean hideInFullscreen() { + return mHideInFullscreen; + } + + @Override + public void onDrawerClosed(View drawerView) { + // TODO change menu? (via invalidateOptionsMenu) + super.onDrawerClosed(drawerView); + mDrawerOpened = false; + updateActionBar(); + } + + @Override + public void onDrawerOpened(View drawerView) { + // TODO change menu? (via invalidateOptionsMenu) + super.onDrawerOpened(drawerView); + mDrawerOpened=true; + updateActionBar(); + } + + @Override + public void onItemClick(AdapterView parent, View view, int position, long id) { + Pane item = mNavigationAdapter.getItem(position); + mIitc.switchToPane(item); + mDrawerLayout.closeDrawer(mDrawerList); + } + + @Override + public boolean onNavigationItemSelected(int position, long itemId) { + String name = mHighlighters.getItem(position); + mIitc.getWebView().loadUrl("javascript: window.changePortalHighlights('" + name + "')"); + + return true; + } + + public void onPostCreate(Bundle savedInstanceState) { + // Sync the toggle state after onRestoreInstanceState has occurred. + syncState(); + } + + public void onPrefChanged() { + mDesktopMode = mPrefs.getBoolean("pref_force_desktop", false); + mHideInFullscreen = mPrefs.getBoolean("pref_fullscreen_actionbar", false); + updateActionBar(); + } + + public void reset() { + mHighlighters.clear(); + mPane = Pane.MAP; + updateActionBar(); + } + + public void setActiveHighlighter(String name) { + mActiveHighlighter = name; + + if (mActionBar.getNavigationMode() == ActionBar.NAVIGATION_MODE_LIST) { + int position = mHighlighters.getPosition(mActiveHighlighter); + if (position >= 0 && position < mActionBar.getNavigationItemCount()) + mActionBar.setSelectedNavigationItem(position); + } + } + + public void setDebugMode(boolean enabled) { + mNavigationAdapter.remove(Pane.DEBUG); // avoid duplicates + if (enabled) + mNavigationAdapter.add(Pane.DEBUG); + } + + 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 setLoadingState(boolean isLoading) { + mIsLoading = isLoading; + updateActionBar(); + } + + public void switchTo(Pane pane) { + mPane = pane; + + updateActionBar(); + } +}