Skip to content

Commit

Permalink
fixing night css path
Browse files Browse the repository at this point in the history
  • Loading branch information
thekirankumar committed Jan 6, 2018
1 parent d277b30 commit 08e936e
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 10 deletions.
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -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;
Expand All @@ -60,14 +61,15 @@


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;
public static final String GOOGLE_AUTOSUGGEST_URL_BASE = "http://suggestqueries.google.com/complete/search?client=chrome&q=";
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;
Expand Down Expand Up @@ -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);
Expand Down Expand Up @@ -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();
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -525,7 +536,6 @@ private void toggleToolbarAnimation() {
}
}


private boolean isRecordAudioGranted() {
int result = ContextCompat.checkSelfPermission(getContext(), Manifest.permission.RECORD_AUDIO);
return result == PackageManager.PERMISSION_GRANTED;
Expand All @@ -541,7 +551,6 @@ private String getSearchUrlBase() {
return null;
}


@Override
public void onStart() {
super.onStart();
Expand Down Expand Up @@ -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() {
Expand All @@ -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) {
Expand All @@ -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
Expand Down
File renamed without changes.

0 comments on commit 08e936e

Please sign in to comment.