diff --git a/CHANGELOG.md b/CHANGELOG.md index 7f1cf722..213f8d19 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,8 @@ ### v0.2.9 - Use opoc/Helpers +- Use opoc/AdBlock +- Added AMOLED mode +- Improve NavDrawer ### v0.2.7 (2017-05-26) - Small improvements diff --git a/app/src/main/java/com/github/dfa/diaspora_android/App.java b/app/src/main/java/com/github/dfa/diaspora_android/App.java index 2a7c45b2..e57c1a55 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/App.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/App.java @@ -34,6 +34,8 @@ import com.github.dfa.diaspora_android.util.AppLog; import com.github.dfa.diaspora_android.util.AppSettings; import com.github.dfa.diaspora_android.util.DiasporaUrlHelper; +import io.github.gsantner.opoc.util.AdBlock; + public class App extends Application { private volatile static App app; private AppSettings appSettings; @@ -69,6 +71,7 @@ public class App extends Application { } cookieManager.setAcceptCookie(true); AppCompatDelegate.setCompatVectorFromResourcesEnabled(true); + AdBlock.getInstance().loadHostsFromRawAssetsAsync(this); } public void resetPodData(@Nullable WebView webView) { diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java index 1e438d4b..204dc428 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/MainActivity.java @@ -25,6 +25,8 @@ import android.content.DialogInterface; import android.content.Intent; import android.content.IntentFilter; import android.content.pm.PackageManager; +import android.content.res.ColorStateList; +import android.graphics.Color; import android.graphics.drawable.LayerDrawable; import android.net.Uri; import android.os.Bundle; @@ -424,6 +426,14 @@ public class MainActivity extends ThemedActivity navheaderImage.setImageResource(R.drawable.ic_launcher_test); } updateNavigationViewEntryVisibilities(); + + if (appSettings.isAmoledColorMode()) { + navView.setItemTextColor(ColorStateList.valueOf(Color.LTGRAY)); + navView.setItemIconTintList(ColorStateList.valueOf(Color.LTGRAY)); + navView.setBackgroundColor(Color.BLACK); + navheaderTitle.setTextColor(Color.LTGRAY); + navheaderDescription.setTextColor(Color.GRAY); + } } protected void updateNavigationViewEntryVisibilities() { diff --git a/app/src/main/java/com/github/dfa/diaspora_android/activity/PodSelectionFragment.java b/app/src/main/java/com/github/dfa/diaspora_android/activity/PodSelectionFragment.java index c6c5ddb1..1db2d19f 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/activity/PodSelectionFragment.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/activity/PodSelectionFragment.java @@ -22,6 +22,7 @@ import android.content.BroadcastReceiver; import android.content.Context; import android.content.Intent; import android.content.IntentFilter; +import android.graphics.Color; import android.os.Build; import android.os.Bundle; import android.support.design.widget.Snackbar; @@ -176,10 +177,18 @@ public class PodSelectionFragment extends ThemedFragment implements SearchView.O listedPodsList.add(pod.getPodUrl().getHost()); } - listViewPodAdapter = new ArrayAdapter<>( + listViewPodAdapter = new ArrayAdapter( getContext(), android.R.layout.simple_list_item_1, - listedPodsList); + listedPodsList) { + @Override + public View getView(int position, View convertView, ViewGroup parent) { + View view = super.getView(position, convertView, parent); + TextView textView = (TextView) view.findViewById(android.R.id.text1); + textView.setTextColor(Color.BLACK); + return view; + } + }; // save index and top position int index = listViewPod.getFirstVisiblePosition(); diff --git a/app/src/main/java/com/github/dfa/diaspora_android/util/AppSettings.java b/app/src/main/java/com/github/dfa/diaspora_android/util/AppSettings.java index 3bfd9b60..74451ea7 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/util/AppSettings.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/util/AppSettings.java @@ -17,6 +17,7 @@ package com.github.dfa.diaspora_android.util; import android.annotation.SuppressLint; import android.content.Context; import android.content.SharedPreferences; +import android.graphics.Color; import com.github.dfa.diaspora_android.App; import com.github.dfa.diaspora_android.BuildConfig; @@ -394,8 +395,12 @@ public class AppSettings extends AppSettingsBase { @SuppressWarnings("ConstantConditions") public int getPrimaryColor() { - return getInt(prefApp, R.string.pref_key__primary_color_shade, rcolor( - BuildConfig.IS_TEST_BUILD ? R.color.md_brown_800 : R.color.primary)); + if (isAmoledColorMode()) { + return Color.BLACK; + } else { + return getInt(prefApp, R.string.pref_key__primary_color_shade, rcolor( + BuildConfig.IS_TEST_BUILD ? R.color.md_brown_800 : R.color.primary)); + } } public void setAccentColorSettings(int base, int shade) { @@ -417,4 +422,12 @@ public class AppSettings extends AppSettingsBase { public boolean isExtendedNotificationsActivated() { return getBool(prefApp, R.string.pref_key__extended_notifications, false); } + + public boolean isAmoledColorMode() { + return getBool(prefApp, R.string.pref_key__primary_color__amoled_mode, false); + } + + public boolean isAdBlockEnabled() { + return getBool(prefApp, R.string.pref_key__adblock_enable, true); + } } \ No newline at end of file diff --git a/app/src/main/java/com/github/dfa/diaspora_android/web/CustomWebViewClient.java b/app/src/main/java/com/github/dfa/diaspora_android/web/CustomWebViewClient.java index 7cb580f2..d605b386 100644 --- a/app/src/main/java/com/github/dfa/diaspora_android/web/CustomWebViewClient.java +++ b/app/src/main/java/com/github/dfa/diaspora_android/web/CustomWebViewClient.java @@ -18,9 +18,13 @@ */ package com.github.dfa.diaspora_android.web; +import android.annotation.TargetApi; import android.content.Intent; +import android.os.Build; +import android.provider.Settings; import android.support.v4.content.LocalBroadcastManager; import android.webkit.CookieManager; +import android.webkit.WebResourceResponse; import android.webkit.WebView; import android.webkit.WebViewClient; @@ -29,12 +33,16 @@ import com.github.dfa.diaspora_android.activity.MainActivity; import com.github.dfa.diaspora_android.data.DiasporaPodList; import com.github.dfa.diaspora_android.util.AppSettings; +import io.github.gsantner.opoc.util.AdBlock; + public class CustomWebViewClient extends WebViewClient { private final App app; private String lastLoadUrl = ""; + private boolean isAdBlockEnabled = false; public CustomWebViewClient(App app, WebView webView) { this.app = app; + isAdBlockEnabled = AppSettings.get().isAdBlockEnabled(); } //Open non-diaspora links in customtab/external browser @@ -77,4 +85,15 @@ public class CustomWebViewClient extends WebViewClient { } } + + @SuppressWarnings("deprecation") + @TargetApi(Build.VERSION_CODES.KITKAT_WATCH) + @Override + public WebResourceResponse shouldInterceptRequest(WebView view, String url) { + if (isAdBlockEnabled && AdBlock.getInstance().isAdHost(url)) { + return AdBlock.createEmptyResponse(); + } + return super.shouldInterceptRequest(view, url); + } + } diff --git a/app/src/main/java/io/github/gsantner/opoc/util/AdBlock.java b/app/src/main/java/io/github/gsantner/opoc/util/AdBlock.java new file mode 100644 index 00000000..db859064 --- /dev/null +++ b/app/src/main/java/io/github/gsantner/opoc/util/AdBlock.java @@ -0,0 +1,171 @@ +/* + * ---------------------------------------------------------------------------- * + * Gregor Santner wrote this file. You can do whatever + * you want with this stuff. If we meet some day, and you think this stuff is + * worth it, you can buy me a coke in return. Provided as is without any kind + * of warranty. No attribution required. - Gregor Santner + * + * License: Creative Commons Zero (CC0 1.0) + * http://creativecommons.org/publicdomain/zero/1.0/ + * ---------------------------------------------------------------------------- + */ + +/* + * Place adblock hosts file in raw: src/main/res/raw/adblock_domains__xyz.txt + * Always blocks both, www and non www + + * Load hosts (call e.g. via Application-Object) +AdBlock.getInstance().loadHostsFromRawAssetsAsync(context); + + * Override inside a WebViewClient: +public WebResourceResponse shouldInterceptRequest(WebView view, String url) { + return AdBlock.getInstance().isAdHost(url) + ? AdBlock.createEmptyResponse() + : super.shouldInterceptRequest(view, url); +} +*/ +package io.github.gsantner.opoc.util; + +import android.content.Context; +import android.util.Log; +import android.webkit.WebResourceResponse; + +import java.io.BufferedReader; +import java.io.ByteArrayInputStream; +import java.io.IOException; +import java.io.InputStreamReader; +import java.lang.reflect.Field; +import java.net.URI; +import java.net.URISyntaxException; +import java.util.ArrayList; +import java.util.HashSet; +import java.util.List; +import java.util.Set; + +import com.github.dfa.diaspora_android.R; + +/** + * Simple Host-Based AdBlocker + */ +public class AdBlock { + private static final AdBlock instance = new AdBlock(); + + public static AdBlock getInstance() { + return instance; + } + + //######################## + //## + //## Members + //## + //######################## + private final Set adblockHostsFromRaw = new HashSet<>(); + private final Set adblockHosts = new HashSet<>(); + private boolean isLoaded; + + //######################## + //## + //## Methods + //## + //######################## + private AdBlock() { + } + + public boolean isAdHost(String urlS) { + if (urlS != null && !urlS.isEmpty() && urlS.startsWith("http")) { + try { + URI url = new URI(urlS); + String host = url.getHost().trim(); + if (host.startsWith("www.") && host.length() >= 4) { + host = host.substring(4); + } + return adblockHosts.contains(host) || adblockHosts.contains("www." + host); + } catch (URISyntaxException e) { + e.printStackTrace(); + } + + } + return false; + } + + public AdBlock reset() { + adblockHosts.clear(); + adblockHosts.addAll(adblockHostsFromRaw); + return this; + } + + public static WebResourceResponse createEmptyResponse() { + return new WebResourceResponse("text/plain", "utf-8", new ByteArrayInputStream("".getBytes())); + } + + public void addBlockedHosts(String... hosts) { + for (String host : hosts) { + if (host != null) { + host = host.trim(); + if (host.startsWith("www.") && host.length() >= 4) { + host = host.substring(4); + } + if (!host.startsWith("#") && !host.startsWith("\"")) { + adblockHosts.add(host); + } + } + } + + } + + public void loadHostsFromRawAssetsAsync(final Context context) { + new Thread(new Runnable() { + @Override + public void run() { + try { + loadHostsFromRawAssets(context); + isLoaded = true; + } catch (IOException e) { + e.printStackTrace(); + } + } + }).start(); + } + + private void loadHostsFromRawAssets(Context context) throws IOException { + BufferedReader br = null; + String host; + + adblockHosts.clear(); + for (int rawId : getAdblockIdsInRaw()) { + try { + br = new BufferedReader(new InputStreamReader(context.getResources().openRawResource(rawId))); + while ((host = br.readLine()) != null) { + addBlockedHosts(host); + } + } catch (Exception e) { + Log.d(AdBlock.class.getName(), "Error: Cannot read adblock res " + rawId); + } finally { + if (br != null) { + try { + br.close(); + } catch (IOException ignored) { + } + } + } + } + adblockHostsFromRaw.clear(); + adblockHostsFromRaw.addAll(adblockHosts); + } + + private List getAdblockIdsInRaw() { + ArrayList adblockResIds = new ArrayList<>(); + Field[] fields = R.raw.class.getFields(); + for (Field field : fields) { + try { + int resId = field.getInt(field); + String resFilename = field.getName(); + if (resFilename.startsWith("adblock_domains__")) { + adblockResIds.add(resId); + } + } catch (IllegalAccessException ignored) { + } + } + return adblockResIds; + } +} diff --git a/app/src/main/res/layout/main__nav_header.xml b/app/src/main/res/layout/main__nav_header.xml index 39003d1e..b19e40fd 100644 --- a/app/src/main/res/layout/main__nav_header.xml +++ b/app/src/main/res/layout/main__nav_header.xml @@ -6,7 +6,8 @@ android:layout_height="110dp" android:gravity="bottom" android:orientation="vertical" - android:theme="@style/ThemeOverlay.AppCompat.Dark"> + android:theme="@style/ThemeOverlay.AppCompat.Dark" + tools:background="@color/primary"> Erweiterte Benachrichtigungen Erweitere die Benachrichtigungsglocke um ein Ausklappmenü mit verschiedenen Benachrichtigungskategorien @@ -85,4 +87,6 @@ Dies wird alle geänderten Einstellungen der Anwendung auf die Standardwerte zurücksetzen und alle Konten abmelden. Deine heruntergeladenen Medien bleiben unberührt. Bist du sicher, dass du fortfahren willst? + Einfachen Werbeblocker aktivieren. Werbung könnte z.B. in eingebetteten Anzeigen enthalten sein + Werbung blockieren diff --git a/app/src/main/res/values/strings-preferences.xml b/app/src/main/res/values/strings-preferences.xml index 9298bc69..654dc46f 100644 --- a/app/src/main/res/values/strings-preferences.xml +++ b/app/src/main/res/values/strings-preferences.xml @@ -19,6 +19,7 @@ pref_key__app_first_start_current_version + pref_key__primary_color__amoled_mode @string/pref_key__primary_color_shade pref_key_primary_color_base pref_key_primary_color_shade @@ -92,6 +93,8 @@ Color of the toolbars Accent Color Color of the progressbar + AMOLED Mode + Override colors with AMOLED display friendly black at many parts of the app. You need to restart to toggle this setting. To browse diaspora* in dark you also need to activate the Dark theme, which can be found in your personal diaspora* account settings. Extended Notifications @@ -213,8 +216,13 @@ pref_key__title__proxy pref_key__title__themes + pref_key__cat_themes pref_key__cat_nav_slider pref_key__cat_proxy pref_key__cat_debugging + + pref_key__adblock_enable + Enable basic AdBlocker. Ads may be included e.g. in embedded views + Block advertisements diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 7d9dd8b0..f686fcfa 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -4,7 +4,7 @@ diaspora* Diaspora for Android #dandelion - The community-run distributed social network + Your companion app for browsing diaspora* \n\n\n_________________________\n *via [dandelion*](/people?q=dandelion00%40diasp.org) client / #dandelíon* diff --git a/app/src/main/res/xml/preferences__master.xml b/app/src/main/res/xml/preferences__master.xml index 2feb957f..d6e0fed9 100644 --- a/app/src/main/res/xml/preferences__master.xml +++ b/app/src/main/res/xml/preferences__master.xml @@ -128,6 +128,13 @@ android:summary="@string/pref_desc__load_images" android:title="@string/pref_title__load_images"/> + + + + \ No newline at end of file