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.
This commit is contained in:
Hans-Christoph Steiner 2016-01-01 22:16:41 +01:00
parent 6bd2468d44
commit d3879a0398
6 changed files with 54 additions and 29 deletions

View file

@ -3,7 +3,6 @@ package org.schabi.newpipe;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.media.MediaPlayer;
@ -26,8 +25,6 @@ import android.widget.Button;
import android.widget.MediaController;
import android.widget.ProgressBar;
import android.widget.VideoView;
import info.guardianproject.netcipher.NetCipher;
import info.guardianproject.netcipher.proxy.OrbotHelper;
/**
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org>
@ -47,7 +44,7 @@ import info.guardianproject.netcipher.proxy.OrbotHelper;
* along with NewPipe. If not, see <http://www.gnu.org/licenses/>.
*/
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);
}
}
}