Commit 6520a4df authored by Ricki Hirner's avatar Ricki Hirner

Add app-wide HTTP proxy setting

parent 9e62c44b
......@@ -49,7 +49,13 @@ public class App extends Application {
public static final String
DISTRUST_SYSTEM_CERTIFICATES = "distrustSystemCerts",
LOG_TO_EXTERNAL_STORAGE = "logToExternalStorage";
LOG_TO_EXTERNAL_STORAGE = "logToExternalStorage",
OVERRIDE_PROXY = "overrideProxy",
OVERRIDE_PROXY_HOST = "overrideProxyHost",
OVERRIDE_PROXY_PORT = "overrideProxyPort";
public static final String OVERRIDE_PROXY_HOST_DEFAULT = "localhost";
public static final int OVERRIDE_PROXY_PORT_DEFAULT = 8118;
@Getter
private CustomCertManager certManager;
......
......@@ -10,11 +10,14 @@ package at.bitfire.davdroid;
import android.accounts.Account;
import android.content.Context;
import android.database.sqlite.SQLiteOpenHelper;
import android.os.Build;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.Locale;
......@@ -23,6 +26,8 @@ import java.util.logging.Level;
import java.util.logging.Logger;
import at.bitfire.dav4android.BasicDigestAuthHandler;
import at.bitfire.davdroid.model.ServiceDB;
import at.bitfire.davdroid.model.Settings;
import okhttp3.Interceptor;
import okhttp3.OkHttpClient;
import okhttp3.Request;
......@@ -85,6 +90,28 @@ public class HttpClient {
// don't allow redirects, because it would break PROPFIND handling
builder.followRedirects(false);
// custom proxy support
if (context != null) {
SQLiteOpenHelper dbHelper = new ServiceDB.OpenHelper(context);
try {
Settings settings = new Settings(dbHelper.getReadableDatabase());
if (settings.getBoolean(App.OVERRIDE_PROXY, false)) {
InetSocketAddress address = new InetSocketAddress(
settings.getString(App.OVERRIDE_PROXY_HOST, App.OVERRIDE_PROXY_HOST_DEFAULT),
settings.getInt(App.OVERRIDE_PROXY_PORT, App.OVERRIDE_PROXY_PORT_DEFAULT)
);
Proxy proxy = new Proxy(Proxy.Type.HTTP, address);
builder.proxy(proxy);
App.log.log(Level.INFO, "Using proxy", proxy);
}
} catch(IllegalArgumentException|NullPointerException e) {
App.log.log(Level.SEVERE, "Can't set proxy, ignoring", e);
} finally {
dbHelper.close();
}
}
// add User-Agent to every request
builder.addNetworkInterceptor(userAgentInterceptor);
......
......@@ -11,6 +11,7 @@ package at.bitfire.davdroid.model;
import android.content.ContentValues;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.support.annotation.Nullable;
import lombok.Cleanup;
......@@ -22,10 +23,11 @@ public class Settings {
this.db = db;
}
public boolean getBoolean(String name, boolean defaultValue) {
@Cleanup Cursor cursor = db.query(ServiceDB.Settings._TABLE, new String[] { ServiceDB.Settings.VALUE },
ServiceDB.Settings.NAME + "=?", new String[] { name }, null, null, null);
if (cursor.moveToNext())
if (cursor.moveToNext() && !cursor.isNull(0))
return cursor.getInt(0) != 0;
else
return defaultValue;
......@@ -38,6 +40,42 @@ public class Settings {
db.insertWithOnConflict(ServiceDB.Settings._TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
}
public int getInt(String name, int defaultValue) {
@Cleanup Cursor cursor = db.query(ServiceDB.Settings._TABLE, new String[] { ServiceDB.Settings.VALUE },
ServiceDB.Settings.NAME + "=?", new String[] { name }, null, null, null);
if (cursor.moveToNext() && !cursor.isNull(0))
return cursor.isNull(0) ? defaultValue : cursor.getInt(0);
else
return defaultValue;
}
public void putInt(String name, int value) {
ContentValues values = new ContentValues(2);
values.put(ServiceDB.Settings.NAME, name);
values.put(ServiceDB.Settings.VALUE, value);
db.insertWithOnConflict(ServiceDB.Settings._TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
}
@Nullable
public String getString(String name, @Nullable String defaultValue) {
@Cleanup Cursor cursor = db.query(ServiceDB.Settings._TABLE, new String[] { ServiceDB.Settings.VALUE },
ServiceDB.Settings.NAME + "=?", new String[] { name }, null, null, null);
if (cursor.moveToNext())
return cursor.getString(0);
else
return defaultValue;
}
public void putString(String name, @Nullable String value) {
ContentValues values = new ContentValues(2);
values.put(ServiceDB.Settings.NAME, name);
values.put(ServiceDB.Settings.VALUE, value);
db.insertWithOnConflict(ServiceDB.Settings._TABLE, null, values, SQLiteDatabase.CONFLICT_REPLACE);
}
public void remove(String name) {
db.delete(ServiceDB.Settings._TABLE, ServiceDB.Settings.NAME + "=?", new String[] { name });
}
......
......@@ -12,10 +12,14 @@ import android.content.Intent;
import android.os.Bundle;
import android.support.design.widget.Snackbar;
import android.support.v7.app.AppCompatActivity;
import android.support.v7.preference.EditTextPreference;
import android.support.v7.preference.Preference;
import android.support.v7.preference.PreferenceFragmentCompat;
import android.support.v7.preference.SwitchPreferenceCompat;
import java.net.URI;
import java.net.URISyntaxException;
import at.bitfire.davdroid.App;
import at.bitfire.davdroid.R;
import at.bitfire.davdroid.model.ServiceDB;
......@@ -43,9 +47,14 @@ public class AppSettingsActivity extends AppCompatActivity {
prefResetHints,
prefResetCertificates;
SwitchPreferenceCompat
prefOverrideProxy,
prefDistrustSystemCerts,
prefLogToExternalStorage;
EditTextPreference
prefProxyHost,
prefProxyPort;
@Override
public void onCreate(Bundle savedInstanceState) {
dbHelper = new ServiceDB.OpenHelper(getContext());
......@@ -66,6 +75,56 @@ public class AppSettingsActivity extends AppCompatActivity {
prefResetHints = findPreference("reset_hints");
prefOverrideProxy = (SwitchPreferenceCompat)findPreference("override_proxy");
prefOverrideProxy.setChecked(settings.getBoolean(App.OVERRIDE_PROXY, false));
prefOverrideProxy.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
settings.putBoolean(App.OVERRIDE_PROXY, (boolean)newValue);
return true;
}
});
prefProxyHost = (EditTextPreference)findPreference("proxy_host");
String proxyHost = settings.getString(App.OVERRIDE_PROXY_HOST, App.OVERRIDE_PROXY_HOST_DEFAULT);
prefProxyHost.setText(proxyHost);
prefProxyHost.setSummary(proxyHost);
prefProxyHost.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
String host = (String)newValue;
try {
URI uri = new URI(null, host, null, null);
} catch(URISyntaxException e) {
Snackbar.make(getView(), e.getLocalizedMessage(), Snackbar.LENGTH_LONG).show();
return false;
}
settings.putString(App.OVERRIDE_PROXY_HOST, host);
prefProxyHost.setSummary(host);
return true;
}
});
prefProxyPort = (EditTextPreference)findPreference("proxy_port");
String proxyPort = settings.getString(App.OVERRIDE_PROXY_PORT, String.valueOf(App.OVERRIDE_PROXY_PORT_DEFAULT));
prefProxyPort.setText(proxyPort);
prefProxyPort.setSummary(proxyPort);
prefProxyPort.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {
@Override
public boolean onPreferenceChange(Preference preference, Object newValue) {
int port;
try {
port = Integer.parseInt((String)newValue);
} catch(NumberFormatException e) {
port = App.OVERRIDE_PROXY_PORT_DEFAULT;
}
settings.putInt(App.OVERRIDE_PROXY_PORT, port);
prefProxyPort.setText(String.valueOf(port));
prefProxyPort.setSummary(String.valueOf(port));
return true;
}
});
prefDistrustSystemCerts = (SwitchPreferenceCompat)findPreference("distrust_system_certs");
prefDistrustSystemCerts.setChecked(settings.getBoolean(App.DISTRUST_SYSTEM_CERTIFICATES, false));
......
......@@ -56,6 +56,12 @@
<string name="app_settings_reset_hints">Hinweise zurücksetzen</string>
<string name="app_settings_reset_hints_summary">Hinweise, die deaktiviert wurden, wieder anzeigen</string>
<string name="app_settings_reset_hints_success">Alle Hinweise werden wieder angezeigt</string>
<string name="app_settings_connection">Verbindung</string>
<string name="app_settings_override_proxy">Proxy-Einstellungen überschreiben</string>
<string name="app_settings_override_proxy_on">Eigene Proxy-Einstellungen werden verwendet</string>
<string name="app_settings_override_proxy_off">System-Proxy-Einstellungen werden verwendet</string>
<string name="app_settings_override_proxy_host">HTTP-Proxy-Rechnername</string>
<string name="app_settings_override_proxy_port">HTTP-Proxy-Port</string>
<string name="app_settings_security">Sicherheit</string>
<string name="app_settings_distrust_system_certs">Systemzertifikaten nicht vertrauen</string>
<string name="app_settings_distrust_system_certs_on">System- und installierten CAs wird nicht vertraut</string>
......
......@@ -72,6 +72,12 @@
<string name="app_settings_reset_hints">Reset hints</string>
<string name="app_settings_reset_hints_summary">Re-enables hints which have been dismissed previously</string>
<string name="app_settings_reset_hints_success">All hints will be shown again</string>
<string name="app_settings_connection">Connection</string>
<string name="app_settings_override_proxy">Override proxy settings</string>
<string name="app_settings_override_proxy_on">Use custom proxy settings</string>
<string name="app_settings_override_proxy_off">Use system default proxy settings</string>
<string name="app_settings_override_proxy_host">HTTP proxy host name</string>
<string name="app_settings_override_proxy_port">HTTP proxy port</string>
<string name="app_settings_security">Security</string>
<string name="app_settings_distrust_system_certs">Distrust system certificates</string>
<string name="app_settings_distrust_system_certs_on">System and user-added CAs won\'t be trusted</string>
......
......@@ -10,30 +10,59 @@
<PreferenceScreen xmlns:android="http://schemas.android.com/apk/res/android">
<PreferenceCategory android:title="@string/app_settings_user_interface">
<Preference
android:key="reset_hints"
android:title="@string/app_settings_reset_hints"
android:summary="@string/app_settings_reset_hints_summary"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/app_settings_connection">
<SwitchPreferenceCompat
android:key="override_proxy"
android:title="@string/app_settings_override_proxy"
android:summaryOn="@string/app_settings_override_proxy_on"
android:summaryOff="@string/app_settings_override_proxy_off"/>
<EditTextPreference
android:key="proxy_host"
android:dependency="override_proxy"
android:title="@string/app_settings_override_proxy_host"
android:inputType="textUri"/>
<EditTextPreference
android:key="proxy_port"
android:dependency="override_proxy"
android:title="@string/app_settings_override_proxy_port"
android:inputType="number"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/app_settings_security">
<SwitchPreferenceCompat
android:key="distrust_system_certs"
android:title="@string/app_settings_distrust_system_certs"
android:summaryOn="@string/app_settings_distrust_system_certs_on"
android:summaryOff="@string/app_settings_distrust_system_certs_off"/>
<Preference
android:key="reset_certificates"
android:title="@string/app_settings_reset_certificates"
android:summary="@string/app_settings_reset_certificates_summary"/>
</PreferenceCategory>
<PreferenceCategory android:title="@string/app_settings_debug">
<SwitchPreferenceCompat
android:key="log_to_external_storage"
android:title="@string/app_settings_log_to_external_storage"
android:summaryOn="@string/app_settings_log_to_external_storage_on"
android:summaryOff="@string/app_settings_log_to_external_storage_off"/>
<Preference
android:title="@string/app_settings_show_debug_info"
android:summary="@string/app_settings_show_debug_info_details">
......@@ -41,6 +70,7 @@
android:targetPackage="at.bitfire.davdroid"
android:targetClass="at.bitfire.davdroid.ui.DebugInfoActivity"/>
</Preference>
</PreferenceCategory>
</PreferenceScreen>
\ No newline at end of file
Subproject commit 1e326a6a729f633504db4410aeb4269199fe999e
Subproject commit 439f727777292f52bd041b4a145c3ae348df1120
Subproject commit 32e2966b5af1146f9fd5758c7dc6d19297345005
Subproject commit bc41e36cc75c25ae49106120572cb9fecaf7430f
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment