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

@ -7,6 +7,7 @@
<uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/> <uses-permission android:name="android.permission.ACCESS_NETWORK_STATE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/> <uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<application <application
android:name=".App"
android:allowBackup="true" android:allowBackup="true"
android:icon="@mipmap/ic_launcher" android:icon="@mipmap/ic_launcher"
android:logo="@mipmap/ic_launcher" android:logo="@mipmap/ic_launcher"

View File

@ -0,0 +1,32 @@
package org.schabi.newpipe;
import android.app.Application;
import android.content.SharedPreferences;
import android.preference.PreferenceManager;
import info.guardianproject.netcipher.NetCipher;
import info.guardianproject.netcipher.proxy.OrbotHelper;
public class App extends Application {
@Override
public void onCreate() {
super.onCreate();
// if Orbot is installed, then default to using Tor, the user can still override
if (OrbotHelper.requestStartTor(this)) {
SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
configureTor(prefs.getBoolean(getString(R.string.useTor), true));
}
}
/**
* Set the proxy settings based on whether Tor should be enabled or not.
*/
static void configureTor(boolean useTor) {
if (useTor) {
NetCipher.useTor();
} else {
NetCipher.setProxy(null);
}
}
}

View File

@ -41,7 +41,6 @@ public class Downloader {
* @param language the language (usually a 2-character code) to set as the preferred language * @param language the language (usually a 2-character code) to set as the preferred language
* @return the contents of the specified text file*/ * @return the contents of the specified text file*/
public static String download(String siteUrl, String language) { public static String download(String siteUrl, String language) {
NetCipher.useTor();
String ret = ""; String ret = "";
try { try {
URL url = new URL(siteUrl); URL url = new URL(siteUrl);
@ -86,7 +85,6 @@ public class Downloader {
* @return the contents of the specified text file*/ * @return the contents of the specified text file*/
public static String download(String siteUrl) { public static String download(String siteUrl) {
String ret = ""; String ret = "";
NetCipher.useTor();
try { try {
URL url = new URL(siteUrl); URL url = new URL(siteUrl);

View File

@ -3,7 +3,6 @@ package org.schabi.newpipe;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.SharedPreferences.OnSharedPreferenceChangeListener;
import android.content.pm.ActivityInfo; import android.content.pm.ActivityInfo;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.media.MediaPlayer; import android.media.MediaPlayer;
@ -26,8 +25,6 @@ import android.widget.Button;
import android.widget.MediaController; import android.widget.MediaController;
import android.widget.ProgressBar; import android.widget.ProgressBar;
import android.widget.VideoView; import android.widget.VideoView;
import info.guardianproject.netcipher.NetCipher;
import info.guardianproject.netcipher.proxy.OrbotHelper;
/** /**
* Copyright (C) Christian Schabesberger 2015 <chris.schabesberger@mailbox.org> * 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/>. * 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 //// 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) { if(prefs.getBoolean(PREF_IS_LANDSCAPE, false) && !isLandscape) {
toggleOrientation(); toggleOrientation();
} }
setTorPreference(prefs);
prefs.registerOnSharedPreferenceChangeListener(this);
} }
@Override @Override
@ -197,7 +191,6 @@ public class PlayVideoActivity extends AppCompatActivity implements OnSharedPref
protected void onDestroy() { protected void onDestroy() {
super.onDestroy(); super.onDestroy();
prefs = getPreferences(Context.MODE_PRIVATE); prefs = getPreferences(Context.MODE_PRIVATE);
prefs.unregisterOnSharedPreferenceChangeListener(this);
} }
@Override @Override
@ -361,21 +354,4 @@ public class PlayVideoActivity extends AppCompatActivity implements OnSharedPref
editor.putBoolean(PREF_IS_LANDSCAPE, isLandscape); editor.putBoolean(PREF_IS_LANDSCAPE, isLandscape);
editor.apply(); 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);
}
}
} }

View File

@ -2,6 +2,8 @@ package org.schabi.newpipe;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.os.Bundle; import android.os.Bundle;
import android.preference.CheckBoxPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.support.annotation.LayoutRes; import android.support.annotation.LayoutRes;
@ -13,6 +15,8 @@ import android.view.MenuItem;
import android.view.View; import android.view.View;
import android.view.ViewGroup; import android.view.ViewGroup;
import info.guardianproject.netcipher.proxy.OrbotHelper;
/** /**
* Created by Christian Schabesberger on 31.08.15. * Created by Christian Schabesberger on 31.08.15.
* *
@ -52,10 +56,25 @@ public class SettingsActivity extends PreferenceActivity {
} }
public static class SettingsFragment extends PreferenceFragment { public static class SettingsFragment extends PreferenceFragment {
private CheckBoxPreference useTorCheckBox;
@Override @Override
public void onCreate(Bundle savedInstanceState) { public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
addPreferencesFromResource(R.xml.settings_screen); 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;
}
});
} }
} }

View File

@ -75,8 +75,7 @@
<CheckBoxPreference <CheckBoxPreference
android:key="@string/useTor" android:key="@string/useTor"
android:title="@string/useTor" android:title="@string/useTor"
android:summary="@string/useTorSummary" android:summary="@string/useTorSummary" />
android:defaultValue="false"/>
</PreferenceCategory> </PreferenceCategory>
</PreferenceScreen> </PreferenceScreen>