Add support for app-wide proxy

Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
Mario Danic 2017-10-25 21:38:16 +02:00
parent c231edf4a9
commit 3fe8976818
10 changed files with 202 additions and 25 deletions

View file

@ -38,7 +38,7 @@ import com.nextcloud.talk.api.NcApi;
import com.nextcloud.talk.api.helpers.api.ApiHelper;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.controllers.base.BaseController;
import com.nextcloud.talk.utils.BundleKeys;
import com.nextcloud.talk.utils.bundle.BundleKeys;
import com.nextcloud.talk.utils.database.user.UserUtils;
import javax.inject.Inject;

View file

@ -42,7 +42,7 @@ import com.bluelinelabs.conductor.Router;
import com.bluelinelabs.conductor.RouterTransaction;
import com.nextcloud.talk.R;
import com.nextcloud.talk.controllers.base.BaseController;
import com.nextcloud.talk.utils.BundleBuilder;
import com.nextcloud.talk.utils.bundle.BundleBuilder;
import butterknife.BindView;

View file

@ -39,8 +39,8 @@ import com.nextcloud.talk.api.helpers.api.ApiHelper;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.controllers.base.BaseController;
import com.nextcloud.talk.models.LoginData;
import com.nextcloud.talk.utils.BundleBuilder;
import com.nextcloud.talk.utils.BundleKeys;
import com.nextcloud.talk.utils.bundle.BundleBuilder;
import com.nextcloud.talk.utils.bundle.BundleKeys;
import com.nextcloud.talk.utils.database.user.UserUtils;
import java.net.URLDecoder;

View file

@ -25,11 +25,10 @@ import android.support.annotation.NonNull;
import com.nextcloud.talk.R;
import com.nextcloud.talk.persistence.entities.Models;
import com.nextcloud.talk.utils.preferences.AppPreferences;
import net.orange_box.storebox.StoreBox;
import java.util.prefs.Preferences;
import javax.inject.Singleton;
import dagger.Module;
@ -58,7 +57,7 @@ public class DatabaseModule {
@Provides
@Singleton
public Preferences providePreferences(@NonNull final Context poContext) {
return StoreBox.create(poContext, Preferences.class);
public AppPreferences providePreferences(@NonNull final Context poContext) {
return StoreBox.create(poContext, AppPreferences.class);
}
}

View file

@ -21,14 +21,20 @@
package com.nextcloud.talk.dagger.modules;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.text.TextUtils;
import com.github.aurae.retrofit2.LoganSquareConverterFactory;
import com.nextcloud.talk.BuildConfig;
import com.nextcloud.talk.api.NcApi;
import com.nextcloud.talk.api.helpers.api.ApiHelper;
import com.nextcloud.talk.application.NextcloudTalkApplication;
import com.nextcloud.talk.utils.preferences.AppPreferences;
import com.nextcloud.talk.utils.preferences.json.ProxyPrefs;
import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Proxy;
import javax.inject.Singleton;
@ -44,7 +50,7 @@ import okhttp3.logging.HttpLoggingInterceptor;
import retrofit2.Retrofit;
import retrofit2.adapter.rxjava2.RxJava2CallAdapterFactory;
@Module
@Module(includes = DatabaseModule.class)
public class RestModule {
@Provides
@ -53,6 +59,19 @@ public class RestModule {
return retrofit.create(NcApi.class);
}
@Provides
@Singleton
@Nullable
Proxy provideProxy(AppPreferences appPreferences) {
ProxyPrefs proxyPrefs = appPreferences.getProxyServer();
if (!TextUtils.isEmpty(proxyPrefs.getProxyHost())) {
return (new Proxy(Proxy.Type.valueOf(proxyPrefs.getProxyType()),
new InetSocketAddress(proxyPrefs.getProxyHost(), proxyPrefs.getProxyPort())));
} else {
return null;
}
}
@Provides
@Singleton
Retrofit provideRetrofit(OkHttpClient httpClient) {
@ -67,7 +86,7 @@ public class RestModule {
@Provides
@Singleton
OkHttpClient provideHttpClient() {
OkHttpClient provideHttpClient(@Nullable Proxy proxy) {
OkHttpClient.Builder httpClient = new OkHttpClient.Builder();
int cacheSize = 128 * 1024 * 1024; // 128 MB
@ -80,6 +99,11 @@ public class RestModule {
httpClient.addInterceptor(loggingInterceptor);
}
if (proxy != null) {
httpClient.proxy(proxy);
}
httpClient.addInterceptor(new HeadersInterceptor());
return httpClient.build();

View file

@ -1,22 +1,23 @@
/*
* Nextcloud Talk application
*
* @author BlueLine Labs, Inc.
* Copyright (C) 2016 BlueLine Labs, Inc.
* @author Mario Danic
* Copyright (C) 2017 Mario Danic
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* http://www.apache.org/licenses/LICENSE-2.0
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils;
package com.nextcloud.talk.utils.bundle;
import android.os.Bundle;
import android.os.Parcelable;

View file

@ -2,7 +2,7 @@
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017 Mario Danic (mario@lovelyhq.com)
* Copyright (C) 2017 Mario Danic
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@ -18,11 +18,10 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils;
package com.nextcloud.talk.utils.bundle;
public class BundleKeys {
public static final String KEY_USERNAME = "KEY_USERNAME";
public static final String KEY_TOKEN = "KEY_TOKEN";
public static final String KEY_BASE_URL = "KEY_BASE_URL";
}

View file

@ -0,0 +1,50 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017 Mario Danic
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils.preferences;
import com.nextcloud.talk.utils.preferences.json.ProxyPrefs;
import com.nextcloud.talk.utils.preferences.json.ProxyTypeAdapter;
import net.orange_box.storebox.annotations.method.ClearMethod;
import net.orange_box.storebox.annotations.method.KeyByString;
import net.orange_box.storebox.annotations.method.RemoveMethod;
import net.orange_box.storebox.annotations.method.TypeAdapter;
import net.orange_box.storebox.annotations.option.SaveOption;
import net.orange_box.storebox.enums.SaveMode;
@SaveOption(SaveMode.APPLY)
public interface AppPreferences {
@KeyByString("proxy_server")
@TypeAdapter(ProxyTypeAdapter.class)
ProxyPrefs getProxyServer();
@KeyByString("proxy_server")
@TypeAdapter(ProxyTypeAdapter.class)
void setProxyServer(ProxyPrefs proxyPrefsServer);
@KeyByString("proxy_server")
@RemoveMethod
void removeProxyServer();
@ClearMethod
void clear();
}

View file

@ -0,0 +1,43 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017 Mario Danic
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils.preferences.json;
import com.bluelinelabs.logansquare.annotation.JsonField;
import com.bluelinelabs.logansquare.annotation.JsonObject;
import org.parceler.Parcel;
import lombok.Data;
@Data
@Parcel
@JsonObject
public class ProxyPrefs {
@JsonField(name = "proxy_host")
String proxyHost;
@JsonField(name = "proxy_port")
int proxyPort;
@JsonField(name = "proxy_type")
String proxyType;
}

View file

@ -0,0 +1,61 @@
/*
* Nextcloud Talk application
*
* @author Mario Danic
* Copyright (C) 2017 Mario Danic
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package com.nextcloud.talk.utils.preferences.json;
import android.support.annotation.Nullable;
import android.util.Log;
import com.bluelinelabs.logansquare.LoganSquare;
import net.orange_box.storebox.adapters.base.BaseStringTypeAdapter;
import java.io.IOException;
public class ProxyTypeAdapter extends BaseStringTypeAdapter<ProxyPrefs> {
private static final String TAG = "ProxyTypeAdapter";
@Nullable
@Override
public String adaptForPreferences(@Nullable ProxyPrefs value) {
if (value != null) {
try {
return LoganSquare.serialize(value);
} catch (IOException e) {
Log.d(TAG, "Failed to serialize proxy from preferences");
}
}
return "";
}
@Nullable
@Override
public ProxyPrefs adaptFromPreferences(@Nullable String value) {
if (value != null) {
try {
return LoganSquare.parse(value, ProxyPrefs.class);
} catch (IOException e) {
Log.d(TAG, "Failed to parse proxy from preferences");
}
}
return new ProxyPrefs();
}
}