Skip to content

Commit

Permalink
Minor improvements to night mode and address bar
Browse files Browse the repository at this point in the history
  • Loading branch information
alandau committed Apr 14, 2020
1 parent fcf1523 commit 5b52b8c
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 7 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "landau.sweb"
minSdkVersion 21
targetSdkVersion 26
versionCode 3
versionName "1.2"
versionCode 4
versionName "1.3"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
40 changes: 35 additions & 5 deletions app/src/main/java/landau/sweb/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.DrawableContainer;
import android.net.Uri;
import android.net.http.SslCertificate;
import android.net.http.SslError;
Expand Down Expand Up @@ -118,7 +119,7 @@ private static class Tab {

static final String searchUrl = "https://www.google.com/search?q=%s";
static final String searchCompleteUrl = "https://www.google.com/complete/search?client=firefox&q=%s";
static final String desktopUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/64.0.3282.167 Safari/537.36";
static final String desktopUA = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/80.0.3987.163 Safari/537.36";

static final String[] adblockRulesList = {
"https://easylist.to/easylist/easylist.txt",
Expand Down Expand Up @@ -337,6 +338,8 @@ public void onPageStarted(WebView view, String url, Bitmap favicon) {
progressBar.setVisibility(View.VISIBLE);
if (view == getCurrentWebView()) {
et.setText(url);
et.setSelection(0);
view.requestFocus();
}
injectCSS(view);
}
Expand All @@ -346,7 +349,10 @@ public void onPageFinished(WebView view, String url) {
if (view == getCurrentWebView()) {
// Don't use the argument url here since navigation to that URL might have been
// cancelled due to SSL error
et.setText(view.getUrl());
if (et.getSelectionStart() == 0 && et.getSelectionEnd() == 0 && et.getText().toString().equals(view.getUrl())) {
// If user haven't started typing anything, focus on webview
view.requestFocus();
}
}
injectCSS(view);
}
Expand Down Expand Up @@ -647,7 +653,8 @@ public void uncaughtException(Thread t, Throwable e) {

// setup edit text
et.setSelected(false);
et.setText(getUrlFromIntent(getIntent()));
String initialUrl = getUrlFromIntent(getIntent());
et.setText(initialUrl.isEmpty() ? "about:blank" : initialUrl);
et.setAdapter(new SearchAutocompleteAdapter(this, text -> {
et.setText(text);
et.setSelection(text.length());
Expand Down Expand Up @@ -1353,13 +1360,27 @@ private void injectCSS(WebView webview) {
final String styleElementId = "night_mode_style_4398357"; // should be unique
String js;
if (isNightMode) {
js = "if (document.head && !document.getElementById('" + styleElementId + "')) {" +
" var style = document.createElement('style');" +
js = "if (document.head) {" +
"if (!window.night_mode_id_list) night_mode_id_list = new Set();" +
"var newset = new Set();" +
" for (var n of document.querySelectorAll(':not(a)')) { " +
" if (!n.id) n.id = 'night_mode_id_' + (night_mode_id_list.size + newset.size);" +
" if (!night_mode_id_list.has(n.id)) newset.add(n.id); " +
" }" +
"for (var item of newset) night_mode_id_list.add(item);" +
"var style = document.getElementById('" + styleElementId + "');" +
"if (!style) {" +
" style = document.createElement('style');" +
" style.id = '" + styleElementId + "';" +
" style.type = 'text/css';" +
" style.innerHTML = '" + css + "';" +
" document.head.appendChild(style);" +
"}" +
" var css2 = ' ';" +
" for (var nid of newset) css2 += ('#' + nid + '#' + nid + ',');" +
" css2 += '#nonexistent {background-color: #161a1e !important; color: #61615f !important; border-color: #212a32 !important; background-image:none !important; outline-color: #161a1e !important; z-index: 1 !important}';" +
" style.innerHTML += css2;" +
"}" +
"var iframes = document.getElementsByTagName('iframe');" +
"for (var i = 0; i < iframes.length; i++) {" +
" var fr = iframes[i];" +
Expand All @@ -1373,6 +1394,7 @@ private void injectCSS(WebView webview) {
js = "if (document.head && document.getElementById('" + styleElementId + "')) {" +
" var style = document.getElementById('" + styleElementId + "');" +
" document.head.removeChild(style);" +
" window.night_mode_id_list = undefined;" +
"}" +
"var iframes = document.getElementsByTagName('iframe');" +
"for (var i = 0; i < iframes.length; i++) {" +
Expand Down Expand Up @@ -1611,6 +1633,7 @@ public long getItemId(int position) {
return position;
}

@SuppressLint("ClickableViewAccessibility")
@Override
@SuppressWarnings("ConstantConditions")
public View getView(final int position, View convertView, ViewGroup parent) {
Expand All @@ -1636,6 +1659,13 @@ public View getView(final int position, View convertView, ViewGroup parent) {
}
return false;
});
//noinspection AndroidLintClickableViewAccessibility
parent.setOnTouchListener((dropdown, event) -> {
if (event.getX() > dropdown.getWidth() - size * 2) {
return true;
}
return false;
});
return convertView;
}

Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/layout/activity_main.xml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
android:inputType="textUri"
android:paddingBottom="10dp"
android:paddingTop="10dp"
android:paddingLeft="10dp"
android:paddingRight="10dp"
android:selectAllOnFocus="true"
android:singleLine="true" />

Expand Down

0 comments on commit 5b52b8c

Please sign in to comment.