This commit is contained in:
Michel Jautzy 2017-06-23 16:04:48 +02:00
parent 57afebfa4e
commit 294299aa97
2 changed files with 186 additions and 144 deletions

View File

@ -16,6 +16,8 @@ import android.annotation.TargetApi;
import android.app.Activity; import android.app.Activity;
import android.os.Build; import android.os.Build;
import android.util.Log; import android.util.Log;
import android.content.Context;
// //
import android.view.View; import android.view.View;
import android.os.Handler; import android.os.Handler;
@ -23,152 +25,182 @@ import android.os.Handler;
import java.lang.reflect.Method; import java.lang.reflect.Method;
public class NavigationBar extends CordovaPlugin { public class NavigationBar extends CordovaPlugin {
private static final String LOG_TAG = "NavigationBar"; private static final String LOG_TAG = "NavigationBar";
public int pixelHeight = 0;
public void initialize(CordovaInterface cordova, CordovaWebView webView) { public void initialize(CordovaInterface cordova, CordovaWebView webView) {
super.initialize(cordova, webView); super.initialize(cordova, webView);
} }
@Override @Override
public boolean execute(String action, JSONArray args,CallbackContext callbackContext) throws JSONException { public boolean execute(String action, JSONArray args,CallbackContext callbackContext) throws JSONException {
PluginResult result = null; PluginResult result = null;
//args.length() //args.length()
//args.getString(0) //args.getString(0)
//args.getString(1) //args.getString(1)
//args.getInt(0) //args.getInt(0)
//args.getInt(1) //args.getInt(1)
//args.getBoolean(0) //args.getBoolean(0)
//args.getBoolean(1) //args.getBoolean(1)
//JSONObject json = args.optJSONObject(0); //JSONObject json = args.optJSONObject(0);
//json.optString("adUnit") //json.optString("adUnit")
//json.optString("adUnitFullScreen") //json.optString("adUnitFullScreen")
//JSONObject inJson = json.optJSONObject("inJson"); //JSONObject inJson = json.optJSONObject("inJson");
if (action.equals("setUp")) { if (action.equals("setUp")) {
//Activity activity = cordova.getActivity(); //Activity activity = cordova.getActivity();
//webView //webView
// //
final boolean autoHideNavigationBar = args.getBoolean(0); final boolean autoHideNavigationBar = args.getBoolean(0);
final CallbackContext delayedCC = callbackContext; final CallbackContext delayedCC = callbackContext;
cordova.getActivity().runOnUiThread(new Runnable(){ cordova.getActivity().runOnUiThread(new Runnable(){
@Override @Override
public void run() { public void run() {
_setUp(autoHideNavigationBar); _setUp(autoHideNavigationBar);
PluginResult pr = new PluginResult(PluginResult.Status.OK); PluginResult pr = new PluginResult(PluginResult.Status.OK);
//pr.setKeepCallback(true); //pr.setKeepCallback(true);
delayedCC.sendPluginResult(pr); delayedCC.sendPluginResult(pr);
//PluginResult pr = new PluginResult(PluginResult.Status.ERROR); //PluginResult pr = new PluginResult(PluginResult.Status.ERROR);
//pr.setKeepCallback(true); //pr.setKeepCallback(true);
//delayedCC.sendPluginResult(pr); //delayedCC.sendPluginResult(pr);
} }
}); });
return true; return true;
} }
else if (action.equals("hideNavigationBar")) { else if (action.equals("hideNavigationBar")) {
//Activity activity=cordova.getActivity(); //Activity activity=cordova.getActivity();
//webView //webView
// //
final CallbackContext delayedCC = callbackContext; final CallbackContext delayedCC = callbackContext;
cordova.getActivity().runOnUiThread(new Runnable(){ cordova.getActivity().runOnUiThread(new Runnable(){
@Override @Override
public void run() { public void run() {
_hideNavigationBar(); _hideNavigationBar();
PluginResult pr = new PluginResult(PluginResult.Status.OK); PluginResult pr = new PluginResult(PluginResult.Status.OK);
//pr.setKeepCallback(true); //pr.setKeepCallback(true);
delayedCC.sendPluginResult(pr); delayedCC.sendPluginResult(pr);
//PluginResult pr = new PluginResult(PluginResult.Status.ERROR); //PluginResult pr = new PluginResult(PluginResult.Status.ERROR);
//pr.setKeepCallback(true); //pr.setKeepCallback(true);
//delayedCC.sendPluginResult(pr); //delayedCC.sendPluginResult(pr);
} }
}); });
return true; return true;
} }
else if (action.equals("softNavigationHeight")) {
return false; // Returning false results in a "MethodNotFound" error.
}
//-------------------------------------
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void _setUp(boolean autoHideNavigationBar){
if (autoHideNavigationBar) {
Activity activity=cordova.getActivity();
//http://stackoverflow.com/questions/21164836/immersive-mode-navigation-becomes-sticky-after-volume-press-or-minimise-restore
//http://www.youtube.com/watch?v=Xw9TIS_JsPM
//https://developer.android.com/training/system-ui/status.html
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION//
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
final CordovaInterface cordova_final = cordova;
//http://stackoverflow.com/questions/11762306/listen-for-first-touchevent-when-using-system-ui-flag-hide-navigation
//http://stackoverflow.com/questions/15103339/android-full-screen-modeics-first-touch-shows-the-navigation-bar
//http://developer.android.com/reference/android/view/View.OnSystemUiVisibilityChangeListener.html
//webView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener(){//cordova5 build error
getView(webView).setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener(){//fixed cordova5 build error
@Override
public void onSystemUiVisibilityChange(int vis) {
if(vis == 0){
//http://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Activity activity=cordova_final.getActivity();
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION//
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
}, 3000);//after ms
}
}
});
}
}
public static View getView(CordovaWebView webView) {
if(View.class.isAssignableFrom(CordovaWebView.class)) {
return (View) webView; final CallbackContext delayedCC = callbackContext;
} cordova.getActivity().runOnUiThread(new Runnable(){
@Override
try { public void run() {
Method getViewMethod = CordovaWebView.class.getMethod("getView", (Class<?>[]) null);
if(getViewMethod != null) {
Object[] args = {}; Activity mikeActivity=cordova.getActivity();
return (View) getViewMethod.invoke(webView, args); Context context=mikeActivity.getApplicationContext();
} int resourceId = mikeActivity.getResources().getIdentifier("navigation_bar_height", "dimen", "android");
}
catch (Exception e) {
} int pixelHeight = 0;
if (resourceId > 0) {
return null; pixelHeight = mikeActivity.getResources().getDimensionPixelSize(resourceId);
} }
@TargetApi(Build.VERSION_CODES.HONEYCOMB) PluginResult pr = new PluginResult(PluginResult.Status.OK, pixelHeight);
private void _hideNavigationBar(){ delayedCC.sendPluginResult(pr);
Activity activity=cordova.getActivity(); }
activity.getWindow().getDecorView().setSystemUiVisibility( });
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION return true;
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN }
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN return false; // Returning false results in a "MethodNotFound" error.
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY }
); //-------------------------------------
}
} @TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void _setUp(boolean autoHideNavigationBar){
if (autoHideNavigationBar) {
Activity activity=cordova.getActivity();
//http://stackoverflow.com/questions/21164836/immersive-mode-navigation-becomes-sticky-after-volume-press-or-minimise-restore
//http://www.youtube.com/watch?v=Xw9TIS_JsPM
//https://developer.android.com/training/system-ui/status.html
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION//
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
final CordovaInterface cordova_final = cordova;
//http://stackoverflow.com/questions/11762306/listen-for-first-touchevent-when-using-system-ui-flag-hide-navigation
//http://stackoverflow.com/questions/15103339/android-full-screen-modeics-first-touch-shows-the-navigation-bar
//http://developer.android.com/reference/android/view/View.OnSystemUiVisibilityChangeListener.html
//webView.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener(){//cordova5 build error
getView(webView).setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener(){//fixed cordova5 build error
@Override
public void onSystemUiVisibilityChange(int vis) {
if(vis == 0){
//http://stackoverflow.com/questions/3072173/how-to-call-a-method-after-a-delay-in-android
Handler handler = new Handler();
handler.postDelayed(new Runnable() {
@Override
public void run() {
Activity activity=cordova_final.getActivity();
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_HIDE_NAVIGATION//
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
//| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
//| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
}, 3000);//after ms
}
}
});
}
}
public static View getView(CordovaWebView webView) {
if(View.class.isAssignableFrom(CordovaWebView.class)) {
return (View) webView;
}
try {
Method getViewMethod = CordovaWebView.class.getMethod("getView", (Class<?>[]) null);
if(getViewMethod != null) {
Object[] args = {};
return (View) getViewMethod.invoke(webView, args);
}
}
catch (Exception e) {
}
return null;
}
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
private void _hideNavigationBar(){
Activity activity=cordova.getActivity();
activity.getWindow().getDecorView().setSystemUiVisibility(
View.SYSTEM_UI_FLAG_LAYOUT_STABLE
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION
| View.SYSTEM_UI_FLAG_FULLSCREEN
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
);
}
}

View File

@ -17,5 +17,15 @@ module.exports = {
'hideNavigationBar', 'hideNavigationBar',
[] []
); );
},
softNavigationHeight: function(successCallback, errorCallback) {
cordova.exec(
successCallback,
errorCallback,
'NavigationBar',
'softNavigationHeight',
[]
);
} }
}; };