From 5839821f8f22b6639df51529f51cd65fb6676902 Mon Sep 17 00:00:00 2001 From: Akinwale Ariwodola Date: Fri, 22 May 2020 03:54:24 +0100 Subject: [PATCH] show URL suggestions and data network (DHT) settings --- .../java/io/lbry/browser/MainActivity.java | 37 +++++++++++++------ .../browser/ui/other/SettingsFragment.java | 29 +++++++++++++++ app/src/main/res/values/strings.xml | 2 +- 3 files changed, 55 insertions(+), 13 deletions(-) diff --git a/app/src/main/java/io/lbry/browser/MainActivity.java b/app/src/main/java/io/lbry/browser/MainActivity.java index 0224f35b..d8ead83e 100644 --- a/app/src/main/java/io/lbry/browser/MainActivity.java +++ b/app/src/main/java/io/lbry/browser/MainActivity.java @@ -245,7 +245,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener // preference keys public static final String PREFERENCE_KEY_DARK_MODE = "io.lbry.browser.preference.userinterface.DarkMode"; public static final String PREFERENCE_KEY_SHOW_MATURE_CONTENT = "io.lbry.browser.preference.userinterface.ShowMatureContent"; - public static final String PREFERENCE_KEY_NOTIFICATION_URL_SUGGESTIONS = "io.lbry.browser.preference.userinterface.UrlSuggestions"; + public static final String PREFERENCE_KEY_SHOW_URL_SUGGESTIONS = "io.lbry.browser.preference.userinterface.UrlSuggestions"; public static final String PREFERENCE_KEY_NOTIFICATION_SUBSCRIPTIONS = "io.lbry.browser.preference.notifications.Subscriptions"; public static final String PREFERENCE_KEY_NOTIFICATION_REWARDS = "io.lbry.browser.preference.notifications.Rewards"; public static final String PREFERENCE_KEY_NOTIFICATION_CONTENT_INTERESTS = "io.lbry.browser.preference.notifications.ContentInterests"; @@ -366,13 +366,6 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener 0, insets.getSystemWindowInsetBottom())); } }); - /*ViewCompat.setOnApplyWindowInsetsListener(findViewById(R.id.url_suggestions_container), new OnApplyWindowInsetsListener() { - @Override - public WindowInsetsCompat onApplyWindowInsets(View v, WindowInsetsCompat insets) { - return ViewCompat.onApplyWindowInsets(v, - insets.replaceSystemWindowInsets(0, 0,0, insets.getSystemWindowInsetBottom())); - } - });*/ // register receivers registerRequestsReceiver(); @@ -789,6 +782,12 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener stopExoplayer(); nowPlayingClaim = null; nowPlayingClaimUrl = null; + appStarted = false; + + if (!keepSdkBackground()) { + sendBroadcast(new Intent(LbrynetService.ACTION_STOP_SERVICE)); + } + super.onDestroy(); } @@ -915,6 +914,17 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener return Helper.getScaledValue(value, scale); } + + public boolean canShowUrlSuggestions() { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); + return sp.getBoolean(MainActivity.PREFERENCE_KEY_SHOW_URL_SUGGESTIONS, false); + } + + public boolean keepSdkBackground() { + SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(this); + return sp.getBoolean(MainActivity.PREFERENCE_KEY_KEEP_SDK_BACKGROUND, true); + } + private void setupUriBar() { findViewById(R.id.wunderbar_close).setOnClickListener(new View.OnClickListener() { @Override @@ -931,9 +941,12 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener InputMethodManager imm = (InputMethodManager) getSystemService(Context.INPUT_METHOD_SERVICE); imm.showSoftInput(view, 0); } - toggleUrlSuggestions(hasFocus); - if (hasFocus && Helper.isNullOrEmpty(Helper.getValue(((EditText) view).getText()))) { - displayUrlSuggestionsForNoInput(); + + if (canShowUrlSuggestions()) { + toggleUrlSuggestions(hasFocus); + if (hasFocus && Helper.isNullOrEmpty(Helper.getValue(((EditText) view).getText()))) { + displayUrlSuggestionsForNoInput(); + } } } }); @@ -946,7 +959,7 @@ public class MainActivity extends AppCompatActivity implements SdkStatusListener @Override public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) { - if (charSequence != null) { + if (charSequence != null && canShowUrlSuggestions()) { handleUriInputChanged(charSequence.toString().trim()); } } diff --git a/app/src/main/java/io/lbry/browser/ui/other/SettingsFragment.java b/app/src/main/java/io/lbry/browser/ui/other/SettingsFragment.java index 7f5cb690..6cf4deaf 100644 --- a/app/src/main/java/io/lbry/browser/ui/other/SettingsFragment.java +++ b/app/src/main/java/io/lbry/browser/ui/other/SettingsFragment.java @@ -2,6 +2,7 @@ package io.lbry.browser.ui.other; import android.content.Context; import android.content.SharedPreferences; +import android.os.AsyncTask; import android.os.Bundle; import androidx.appcompat.app.ActionBar; @@ -9,9 +10,14 @@ import androidx.appcompat.app.AppCompatDelegate; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.PreferenceManager; +import java.io.FileOutputStream; +import java.io.PrintStream; + import io.lbry.browser.MainActivity; import io.lbry.browser.R; +import io.lbry.browser.utils.Helper; import io.lbry.browser.utils.LbryAnalytics; +import io.lbry.lbrysdk.Utils; public class SettingsFragment extends PreferenceFragmentCompat implements SharedPreferences.OnSharedPreferenceChangeListener { @Override @@ -69,6 +75,29 @@ public class SettingsFragment extends PreferenceFragmentCompat implements Shared if (key.equalsIgnoreCase(MainActivity.PREFERENCE_KEY_DARK_MODE)) { boolean darkMode = sp.getBoolean(MainActivity.PREFERENCE_KEY_DARK_MODE, false); AppCompatDelegate.setDefaultNightMode(darkMode ? AppCompatDelegate.MODE_NIGHT_YES : AppCompatDelegate.MODE_NIGHT_NO); + } else if (key.equalsIgnoreCase(MainActivity.PREFERENCE_KEY_PARTICIPATE_DATA_NETWORK)) { + boolean dhtEnabled = sp.getBoolean(MainActivity.PREFERENCE_KEY_PARTICIPATE_DATA_NETWORK, false); + updateDHTFileSetting(dhtEnabled); } } + + private void updateDHTFileSetting(final boolean enabled) { + Context context = getContext(); + (new AsyncTask() { + protected Void doInBackground(Void... params) { + PrintStream out = null; + try { + String fileContent = enabled ? "on" : "off"; + String path = String.format("%s/%s", Utils.getAppInternalStorageDir(context), "dht"); + out = new PrintStream(new FileOutputStream(path)); + out.print(fileContent); + } catch (Exception ex) { + // pass + } finally { + Helper.closeCloseable(out); + } + return null; + } + }).executeOnExecutor(AsyncTask.THREAD_POOL_EXECUTOR); + } } diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 09916496..2c981540 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -215,7 +215,7 @@ Content Interests Keep the LBRY service running in the background for improved wallet and network performance - Participate in the data network + Participate in the data network (requires app and background service restart) %1$s - Search