From fd9b8f8113772601508bea67102c498dd4668f9c Mon Sep 17 00:00:00 2001 From: "Warren R. Bank" Date: Tue, 1 Sep 2020 05:10:22 -0700 Subject: [PATCH] add checkbox to Setting: 'Enable Remote Debugger' when: * app setting is enabled * phone is connected to a remote computer by `adb` - either: * physically with a USB cable * wirelessly over a LAN user can: * inspect contents of WebView in DevTools with Chrome desktop at: chrome://inspect/#devices quick refresher: * # enable USB debugging * # connect by USB adb devices adb tcpip 5555 # disconnect USB * # connect by WiFi from any computer: adb connect 192.168.1.x:5555 adb devices --- .../webcast/webview/BrowserActivity.java | 8 ++++++ .../webcast/webview/BrowserDebugUtils.java | 26 ++++++++++++++++--- .../webcast/webview/BrowserUtils.java | 9 +++++++ .../WebCast/src/main/res/values/strings.xml | 12 +++++++-- .../WebCast/src/main/res/xml/preferences.xml | 7 +++++ android-studio-project/constants.gradle | 4 +-- 6 files changed, 59 insertions(+), 7 deletions(-) diff --git a/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserActivity.java b/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserActivity.java index 872b159..07358b6 100644 --- a/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserActivity.java +++ b/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserActivity.java @@ -138,6 +138,7 @@ public static boolean contains(ArrayList items, String uri) { private String current_page_url; private WebView webView; + private boolean shouldUpdateWebViewDebugConfigs; private boolean shouldClearWebView; private BrowserWebViewClient webViewClient; private BrowserDownloadListener downloadListener; @@ -239,6 +240,11 @@ public void onResume() { super.onResume(); WebCastApplication.activityResumed(); + if (shouldUpdateWebViewDebugConfigs) { + shouldUpdateWebViewDebugConfigs = false; + BrowserDebugUtils.configWebView(BrowserActivity.this); + } + webView.loadUrl(current_page_url); shouldClearWebView = true; } @@ -799,6 +805,7 @@ public void onClick_action_watch_all_videos(View view) { private void initWebView() { BrowserDebugUtils.configWebView(BrowserActivity.this); + shouldUpdateWebViewDebugConfigs = false; webViewClient = new BrowserWebViewClient(BrowserActivity.this) { @Override @@ -1022,6 +1029,7 @@ public boolean onOptionsItemSelected(MenuItem menuItem) { case R.id.action_settings: { Intent in = new Intent(BrowserActivity.this, SettingsActivity.class); startActivity(in); + shouldUpdateWebViewDebugConfigs = true; return true; } diff --git a/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserDebugUtils.java b/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserDebugUtils.java index 9966a92..5194055 100644 --- a/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserDebugUtils.java +++ b/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserDebugUtils.java @@ -10,17 +10,37 @@ public class BrowserDebugUtils { - public static void configWebView(Context context) { + private static boolean isWebContentsDebuggingEnabled = false; + + public static boolean configWebView(Context context) { + boolean didChange = false; + if (Build.VERSION.SDK_INT >= 19) { // Build.VERSION_CODES.KITKAT if ( (BuildConfig.DEBUG) || (0 != (context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE)) + || (BrowserUtils.getEnableRemoteDebuggerPreference(context)) ) { - WebView.setWebContentsDebuggingEnabled(true); + if (!isWebContentsDebuggingEnabled) { + isWebContentsDebuggingEnabled = true; + WebView.setWebContentsDebuggingEnabled(true); - Toast.makeText(context, "WebView remote debugging enabled", Toast.LENGTH_SHORT).show(); + Toast.makeText(context, "WebView remote debugging enabled", Toast.LENGTH_SHORT).show(); + didChange = true; + } + } + else { + if (isWebContentsDebuggingEnabled) { + isWebContentsDebuggingEnabled = false; + WebView.setWebContentsDebuggingEnabled(false); + + Toast.makeText(context, "WebView remote debugging disabled", Toast.LENGTH_SHORT).show(); + didChange = true; + } } } + + return didChange; } } diff --git a/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserUtils.java b/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserUtils.java index 1bb43f2..ed6469d 100644 --- a/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserUtils.java +++ b/android-studio-project/WebCast/src/main/java/com/github/warren_bank/webcast/webview/BrowserUtils.java @@ -46,6 +46,15 @@ public static String getVideoPlayerPreference(Context context) { return prefs.getString(pref_key, pref_default); } + public static boolean getEnableRemoteDebuggerPreference(Context context) { + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(context); + String pref_key = context.getString(R.string.pref_enableremotedebugger_key); + String pref_default = context.getString(R.string.pref_enableremotedebugger_default); + boolean val_default = "true".equals(pref_default); + + return prefs.getBoolean(pref_key, val_default); + } + public static int getIndexOfArray(Object needle, Object[] haystack) { for (int i=0; i < haystack.length; i++) { if ( diff --git a/android-studio-project/WebCast/src/main/res/values/strings.xml b/android-studio-project/WebCast/src/main/res/values/strings.xml index cdf003f..bd62de0 100644 --- a/android-studio-project/WebCast/src/main/res/values/strings.xml +++ b/android-studio-project/WebCast/src/main/res/values/strings.xml @@ -21,6 +21,7 @@ settings_key WebCast Settings + internal w/ Chromecast sender external @string/activity_name_exoairplayersender @@ -42,9 +43,16 @@ pref_videoplayer - Video Player - watch videos using.. @string/pref_videoplayer_array_values_1 + Video Player + watch videos using.. + + + pref_enableremotedebugger + false + Enable Remote Debugger + WebView can be inspected on LAN + WebView cannot be inspected on LAN pref_persistentcookies diff --git a/android-studio-project/WebCast/src/main/res/xml/preferences.xml b/android-studio-project/WebCast/src/main/res/xml/preferences.xml index d7b1019..f5fbed5 100644 --- a/android-studio-project/WebCast/src/main/res/xml/preferences.xml +++ b/android-studio-project/WebCast/src/main/res/xml/preferences.xml @@ -12,5 +12,12 @@ android:entryValues="@array/pref_videoplayer_array_values" android:defaultValue="@string/pref_videoplayer_default" /> + + diff --git a/android-studio-project/constants.gradle b/android-studio-project/constants.gradle index 2cffa4c..3e65442 100644 --- a/android-studio-project/constants.gradle +++ b/android-studio-project/constants.gradle @@ -1,6 +1,6 @@ project.ext { - releaseVersionCode = Integer.parseInt("004090516", 10) - releaseVersion = '004.09.05-16API' + releaseVersionCode = Integer.parseInt("004090616", 10) + releaseVersion = '004.09.06-16API' minSdkVersion = 16 targetSdkVersion = 28 compileSdkVersion = 28