Show portal highlighters in action bar

This commit is contained in:
fkloft 2013-09-18 16:43:12 +02:00
parent 6290ef56aa
commit daaef1575e
3 changed files with 104 additions and 13 deletions

View File

@ -11,14 +11,26 @@ window.addPortalHighlighter = function(name, callback) {
_highlighters = {}; _highlighters = {};
} }
_highlighters[name] = callback; _highlighters[name] = callback;
if (typeof android !== 'undefined' && android && android.addPortalHighlighter)
android.addPortalHighlighter(name);
if(localStorage.portal_highlighter === undefined) { if(localStorage.portal_highlighter === undefined) {
_current_highlighter = name; _current_highlighter = name;
if (typeof android !== 'undefined' && android && android.setActiveHighlighter)
android.setActiveHighlighter(name);
localStorage.portal_highlighter = name; localStorage.portal_highlighter = name;
} }
portalHighlighterControl(); portalHighlighterControl();
} }
window.portalHighlighterControl = function() { window.portalHighlighterControl = function() {
if (typeof android !== 'undefined' && android && android.addPortalHighlighter) {
$('#portal_highlight_select').remove();
return;
}
if(_highlighters !== null) { if(_highlighters !== null) {
if($('#portal_highlight_select').length === 0) { if($('#portal_highlight_select').length === 0) {
$("body").append("<select id='portal_highlight_select'></select>"); $("body").append("<select id='portal_highlight_select'></select>");
@ -46,6 +58,8 @@ window.portalHighlighterControl = function() {
window.changePortalHighlights = function(name) { window.changePortalHighlights = function(name) {
_current_highlighter = name; _current_highlighter = name;
if (typeof android !== 'undefined' && android && android.setActiveHighlighter)
android.setActiveHighlighter(name);
resetHighlightedPortals(); resetHighlightedPortals();
localStorage.portal_highlighter = name; localStorage.portal_highlighter = name;
} }

View File

@ -1,11 +1,13 @@
package com.cradle.iitc_mobile; package com.cradle.iitc_mobile;
import android.app.ActionBar; import android.app.ActionBar;
import android.app.ActionBar.OnNavigationListener;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.widget.ArrayAdapter;
import android.widget.Toast; import android.widget.Toast;
public class ActionBarHelper { public class ActionBarHelper implements OnNavigationListener {
/* /*
* Show/hide the up arrow on the left end * Show/hide the up arrow on the left end
* getActionBar().setDisplayHomeAsUpEnabled(enabled); * getActionBar().setDisplayHomeAsUpEnabled(enabled);
@ -20,10 +22,31 @@ public class ActionBarHelper {
* getActionBar().setHomeButtonEnabled(enabled); * getActionBar().setHomeButtonEnabled(enabled);
*/ */
private ActionBar mActionBar; private class HighlighterAdapter extends ArrayAdapter<String> {
private IITC_Mobile mIitc; public HighlighterAdapter() {
private SharedPreferences mPrefs; 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 mDesktopMode = false;
private boolean mFullscreen = false; private boolean mFullscreen = false;
private boolean mHideInFullscreen = false; private boolean mHideInFullscreen = false;
@ -33,58 +56,90 @@ public class ActionBarHelper {
mIitc = activity; mIitc = activity;
mActionBar = bar; mActionBar = bar;
mPrefs = PreferenceManager.getDefaultSharedPreferences(activity); mPrefs = PreferenceManager.getDefaultSharedPreferences(activity);
mHighlighters = new HighlighterAdapter();
mActionBar.setDisplayShowHomeEnabled(true); // show icon mActionBar.setDisplayShowHomeEnabled(true); // show icon
mActionBar.setListNavigationCallbacks(mHighlighters, this);
onPrefChanged(); // also calls updateActionBar() onPrefChanged(); // also calls updateActionBar()
} }
private void updateActionBar() { private void updateActionBar() {
boolean showHighlighter = true;
if (mDesktopMode) { if (mDesktopMode) {
mActionBar.setDisplayHomeAsUpEnabled(false); // Hide "up" indicator mActionBar.setDisplayHomeAsUpEnabled(false); // Hide "up" indicator
mActionBar.setHomeButtonEnabled(false); // Make icon unclickable mActionBar.setHomeButtonEnabled(false); // Make icon unclickable
mActionBar.setTitle(mIitc.getString(R.string.app_name)); mActionBar.setTitle(mIitc.getString(R.string.app_name));
} else { } else {
if (mPane != android.R.id.home) if (mPane != android.R.id.home) {
{
mActionBar.setDisplayHomeAsUpEnabled(true); // Show "up" indicator mActionBar.setDisplayHomeAsUpEnabled(true); // Show "up" indicator
mActionBar.setHomeButtonEnabled(true);// Make icon clickable mActionBar.setHomeButtonEnabled(true);// Make icon clickable
showHighlighter = false;
} }
else else {
{
mActionBar.setDisplayHomeAsUpEnabled(false); // Hide "up" indicator mActionBar.setDisplayHomeAsUpEnabled(false); // Hide "up" indicator
mActionBar.setHomeButtonEnabled(false); // Make icon unclickable mActionBar.setHomeButtonEnabled(false); // Make icon unclickable
} }
mActionBar.setTitle(IITC_Mobile.PANE_TITLES.get(mPane, mIitc.getString(R.string.app_name))); 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);
} else {
mActionBar.setNavigationMode(ActionBar.NAVIGATION_MODE_STANDARD);
mActionBar.setDisplayShowTitleEnabled(true); // Show title
}
if (mFullscreen && mHideInFullscreen) if (mFullscreen && mHideInFullscreen)
mActionBar.hide(); mActionBar.hide();
else else
mActionBar.show(); mActionBar.show();
} }
@Deprecated public void addPortalHighlighter(String name) {
public void goHome() { mHighlighters.add(name);
switchTo(android.R.id.home);
if (name.equals(mActiveHighlighter))
setActiveHighlighter(name);
updateActionBar();
} }
public boolean hideInFullscreen() { public boolean hideInFullscreen() {
return mHideInFullscreen; 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() { public void onPrefChanged() {
mDesktopMode = mPrefs.getBoolean("pref_force_desktop", false); mDesktopMode = mPrefs.getBoolean("pref_force_desktop", false);
mHideInFullscreen = mPrefs.getBoolean("pref_fullscreen_actionbar", false); mHideInFullscreen = mPrefs.getBoolean("pref_fullscreen_actionbar", false);
updateActionBar(); updateActionBar();
} }
public void reset() public void reset() {
{ mHighlighters.clear();
mPane = android.R.id.home; mPane = android.R.id.home;
updateActionBar(); updateActionBar();
} }
public void setActiveHighlighter(String name) {
int position = mHighlighters.getPosition(name);
if (position >= 0)
mActionBar.setSelectedNavigationItem(position);
mActiveHighlighter = name;
}
public void setFullscreen(boolean fullscreen) { public void setFullscreen(boolean fullscreen) {
mFullscreen = fullscreen; mFullscreen = fullscreen;
if (mFullscreen && mHideInFullscreen) { if (mFullscreen && mHideInFullscreen) {

View File

@ -230,6 +230,28 @@ public class IITC_JSInterface {
showMultiSelection(); showMultiSelection();
} }
@JavascriptInterface
public void addPortalHighlighter(final String name) {
final IITC_Mobile iitc = ((IITC_Mobile) mContext);
iitc.runOnUiThread(new Runnable() {
@Override
public void run() {
iitc.getActionBarHelper().addPortalHighlighter(name);
}
});
}
@JavascriptInterface
public void setActiveHighlighter(final String name) {
final IITC_Mobile iitc = ((IITC_Mobile) mContext);
iitc.runOnUiThread(new Runnable() {
@Override
public void run() {
iitc.getActionBarHelper().setActiveHighlighter(name);
}
});
}
// show all overlay layers in a multi selection list dialog // show all overlay layers in a multi selection list dialog
private void showMultiSelection() { private void showMultiSelection() {
// build the layer chooser dialog // build the layer chooser dialog