From d3879a03982f3e8da4831704fb0ef280b884405f Mon Sep 17 00:00:00 2001 From: Hans-Christoph Steiner Date: Fri, 1 Jan 2016 22:16:41 +0100 Subject: [PATCH] setup Tor at app start, and config immediately when pref is changed This adds an Application subclass to get the onCreate() method, which is called once at the first start up of the app, before any Activity starts. Tor is configured there to ensure it is setup before anything happens. This also moves the "Use Tor" pref listener to a more appropriate place. --- app/src/main/AndroidManifest.xml | 1 + app/src/main/java/org/schabi/newpipe/App.java | 32 +++++++++++++++++++ .../java/org/schabi/newpipe/Downloader.java | 2 -- .../org/schabi/newpipe/PlayVideoActivity.java | 26 +-------------- .../org/schabi/newpipe/SettingsActivity.java | 19 +++++++++++ app/src/main/res/xml/settings_screen.xml | 3 +- 6 files changed, 54 insertions(+), 29 deletions(-) create mode 100644 app/src/main/java/org/schabi/newpipe/App.java diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index d028886b1..ef84c57b3 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -7,6 +7,7 @@ @@ -47,7 +44,7 @@ import info.guardianproject.netcipher.proxy.OrbotHelper; * along with NewPipe. If not, see . */ -public class PlayVideoActivity extends AppCompatActivity implements OnSharedPreferenceChangeListener { +public class PlayVideoActivity extends AppCompatActivity { //// TODO: 11.09.15 add "choose stream" menu @@ -173,9 +170,6 @@ public class PlayVideoActivity extends AppCompatActivity implements OnSharedPref if(prefs.getBoolean(PREF_IS_LANDSCAPE, false) && !isLandscape) { toggleOrientation(); } - - setTorPreference(prefs); - prefs.registerOnSharedPreferenceChangeListener(this); } @Override @@ -197,7 +191,6 @@ public class PlayVideoActivity extends AppCompatActivity implements OnSharedPref protected void onDestroy() { super.onDestroy(); prefs = getPreferences(Context.MODE_PRIVATE); - prefs.unregisterOnSharedPreferenceChangeListener(this); } @Override @@ -361,21 +354,4 @@ public class PlayVideoActivity extends AppCompatActivity implements OnSharedPref editor.putBoolean(PREF_IS_LANDSCAPE, isLandscape); editor.apply(); } - - private void setTorPreference(SharedPreferences prefs) { - // if Orbot is installed, then default to using Tor, the user can still override - if(prefs.getBoolean(getString(R.string.useTor), OrbotHelper.isOrbotInstalled(this))) { - NetCipher.useTor(); - } else { - NetCipher.setProxy(null); - } - } - - @Override - public void onSharedPreferenceChanged(SharedPreferences prefs, String key) { - if(key.equals(getString(R.string.useTor))) { - setTorPreference(prefs); - } - } - } diff --git a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java index c8a548ab6..c8c089a98 100644 --- a/app/src/main/java/org/schabi/newpipe/SettingsActivity.java +++ b/app/src/main/java/org/schabi/newpipe/SettingsActivity.java @@ -2,6 +2,8 @@ package org.schabi.newpipe; import android.content.res.Configuration; import android.os.Bundle; +import android.preference.CheckBoxPreference; +import android.preference.Preference; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.support.annotation.LayoutRes; @@ -13,6 +15,8 @@ import android.view.MenuItem; import android.view.View; import android.view.ViewGroup; +import info.guardianproject.netcipher.proxy.OrbotHelper; + /** * Created by Christian Schabesberger on 31.08.15. * @@ -52,10 +56,25 @@ public class SettingsActivity extends PreferenceActivity { } public static class SettingsFragment extends PreferenceFragment { + private CheckBoxPreference useTorCheckBox; + @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); addPreferencesFromResource(R.xml.settings_screen); + + // if Orbot is installed, then default to using Tor, the user can still override + useTorCheckBox = (CheckBoxPreference) findPreference(getString(R.string.useTor)); + boolean useTor = OrbotHelper.isOrbotInstalled(getActivity()); + useTorCheckBox.setDefaultValue(useTor); + useTorCheckBox.setChecked(useTor); + useTorCheckBox.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() { + @Override + public boolean onPreferenceChange(Preference preference, Object useTor) { + App.configureTor((Boolean) useTor); + return true; + } + }); } } diff --git a/app/src/main/res/xml/settings_screen.xml b/app/src/main/res/xml/settings_screen.xml index f7ab99a53..4d21b457b 100644 --- a/app/src/main/res/xml/settings_screen.xml +++ b/app/src/main/res/xml/settings_screen.xml @@ -75,8 +75,7 @@ + android:summary="@string/useTorSummary" />