From 08e936ecfc35d30326b76584231048a1d6708099 Mon Sep 17 00:00:00 2001 From: Kiran Kumar Date: Sat, 6 Jan 2018 11:58:07 +0530 Subject: [PATCH] fixing night css path --- .../youtubeauto/WebViewCarFragment.java | 68 ++++++++++++++++--- .../res/raw => night_css}/m.youtube.com.css | 0 2 files changed, 58 insertions(+), 10 deletions(-) rename {app/src/main/res/raw => night_css}/m.youtube.com.css (100%) diff --git a/app/src/main/java/com/thekirankumar/youtubeauto/WebViewCarFragment.java b/app/src/main/java/com/thekirankumar/youtubeauto/WebViewCarFragment.java index 50298d7..753f4e9 100644 --- a/app/src/main/java/com/thekirankumar/youtubeauto/WebViewCarFragment.java +++ b/app/src/main/java/com/thekirankumar/youtubeauto/WebViewCarFragment.java @@ -1,12 +1,12 @@ package com.thekirankumar.youtubeauto; -import android.app.UiModeManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.pm.PackageManager; import android.content.pm.ResolveInfo; import android.graphics.Bitmap; +import android.graphics.Color; import android.media.AudioAttributes; import android.media.AudioManager; import android.os.Bundle; @@ -50,6 +50,7 @@ import java.io.IOException; import java.io.UnsupportedEncodingException; +import java.net.URI; import java.net.URISyntaxException; import java.net.URLEncoder; import java.util.ArrayList; @@ -60,7 +61,7 @@ public class WebViewCarFragment extends CarFragment { - public static final String YOUTUBE_HOME_URL_BASE = "https://youtube.com"; + public static final String YOUTUBE_HOME_URL_BASE = "https://www.youtube.com"; public static final String YOUTUBE_SEARCH_URL_BASE = "https://www.youtube.com/results?search_query="; public static final String YOUTUBE_AUTOSUGGEST_URL_BASE = "http://suggestqueries.google.com/complete/search?client=firefox&ds=yt&q="; public static final int AUTOSUGGEST_DEBOUNCE_DELAY_MILLIS = 500; @@ -68,6 +69,7 @@ public class WebViewCarFragment extends CarFragment { public static final String GOOGLE_SEARCH_URL_BASE = "https://www.google.com/#q="; public static final String PREFS = "car"; public static final String HOME_URL = "home_url"; + public static final String NIGHT_CSS_PATH = "https://cdn.rawgit.com/thekirankumar/youtube-android-auto/d277b300/night_css/"; private static final String TAG = "WebViewCarFragment"; private HandlerThread handlerThread; private Runnable searchRunnable; @@ -134,11 +136,18 @@ public void run() { hideToolbar(); } }; + private boolean isNightMode = false; public WebViewCarFragment() { // Required empty public constructor } + public static String getDomainName(String url) throws URISyntaxException { + URI uri = new URI(url); + String domain = uri.getHost(); + return domain.startsWith("www.") ? domain.substring(4) : domain; + } + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); @@ -166,7 +175,7 @@ public View onCreateView(LayoutInflater inflater, ViewGroup container, @Override public void onViewCreated(final View view, @Nullable Bundle savedInstanceState) { super.onViewCreated(view, savedInstanceState); - boolean isNight = getResources().getBoolean(R.bool.isNight); + isNightMode = getResources().getBoolean(R.bool.isNight); //FirebaseAnalytics.getInstance(getContext()).logEvent("CarFragment_created", null); final MainCarActivity mainCarActivity = (MainCarActivity) getContext(); @@ -341,8 +350,10 @@ public void onClick(View v) { webView.setWebViewClient(new CustomWebViewClient()); final VideoEnabledWebChromeClient videoEnabledWebChromeClient = new VideoEnabledWebChromeClient(webViewContainer, fullScreenVideoView, new ProgressBar(getContext()), webView); webView.setWebChromeClient(videoEnabledWebChromeClient); - webView.getSettings().setUserAgentString("Mozilla/5.0 (Macintosh; Intel Mac OS X 10_12_0) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/63.0.3239.84 Safari/537.36"); webView.getSettings().setJavaScriptEnabled(true); + if (isNightMode) { + webView.setBackgroundColor(Color.BLACK); + } webView.getSettings().setTextSize(WebSettings.TextSize.LARGER); handler.post(new Runnable() { @Override @@ -525,7 +536,6 @@ private void toggleToolbarAnimation() { } } - private boolean isRecordAudioGranted() { int result = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.RECORD_AUDIO); return result == PackageManager.PERMISSION_GRANTED; @@ -541,7 +551,6 @@ private String getSearchUrlBase() { return null; } - @Override public void onStart() { super.onStart(); @@ -604,6 +613,8 @@ public void onPause() { } super.onPause(); loseAudioFocus(); + SharedPreferences car = getContext().getSharedPreferences(PREFS, Context.MODE_MULTI_PROCESS); + car.edit().putString(HOME_URL, webView.getUrl()).apply(); } public void onDetach() { @@ -612,12 +623,48 @@ public void onDetach() { @Override public void onDestroy() { - webView.destroy(); - handlerThread.quit(); - mediaSession.release(); + if (webView != null) { + webView.destroy(); + } + if (handlerThread != null) { + handlerThread.quit(); + } + if (mediaSession != null) { + mediaSession.release(); + } super.onDestroy(); } + @Override + public void onDestroyView() { + super.onDestroyView(); + + } + + private void injectNightModeCss() { + String domainName = null; + try { + domainName = getDomainName(webView.getUrl()); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + if (domainName != null && isNightMode) { + String injection = "var cssId = 'nightModeCss';\n" + + "if (!document.getElementById(cssId))\n" + + "{\n" + + " var head = document.getElementsByTagName('head')[0];\n" + + " var link = document.createElement('link');\n" + + " link.id = cssId;\n" + + " link.rel = 'stylesheet';\n" + + " link.type = 'text/css';\n" + + " link.href = " + NIGHT_CSS_PATH + domainName + ".css';\n" + + " link.media = 'all';\n" + + " head.appendChild(link);\n" + + "}"; + webView.loadUrl("javascript:" + injection); + } + } + private class CustomWebViewClient extends WebViewClient { @Override public void onPageStarted(WebView view, String url, Bitmap favicon) { @@ -628,9 +675,10 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) { @Override public void onPageFinished(WebView view, String url) { super.onPageFinished(view, url); + injectNightModeCss(); progressBar.setVisibility(View.GONE); SharedPreferences car = getContext().getSharedPreferences(PREFS, Context.MODE_MULTI_PROCESS); - car.edit().putString(HOME_URL, url).apply(); + car.edit().putString(HOME_URL, url).commit(); } @Override diff --git a/app/src/main/res/raw/m.youtube.com.css b/night_css/m.youtube.com.css similarity index 100% rename from app/src/main/res/raw/m.youtube.com.css rename to night_css/m.youtube.com.css