diff --git a/app/src/main/java/com/nextcloud/talk/controllers/AccountVerificationController.java b/app/src/main/java/com/nextcloud/talk/controllers/AccountVerificationController.java index d4aba1be1..9445009f4 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/AccountVerificationController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/AccountVerificationController.java @@ -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; diff --git a/app/src/main/java/com/nextcloud/talk/controllers/BottomNavigationController.java b/app/src/main/java/com/nextcloud/talk/controllers/BottomNavigationController.java index 6b3bd5e11..b876974f1 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/BottomNavigationController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/BottomNavigationController.java @@ -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; diff --git a/app/src/main/java/com/nextcloud/talk/controllers/WebViewLoginController.java b/app/src/main/java/com/nextcloud/talk/controllers/WebViewLoginController.java index dfe6380b4..6945e2ebf 100644 --- a/app/src/main/java/com/nextcloud/talk/controllers/WebViewLoginController.java +++ b/app/src/main/java/com/nextcloud/talk/controllers/WebViewLoginController.java @@ -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; diff --git a/app/src/main/java/com/nextcloud/talk/dagger/modules/DatabaseModule.java b/app/src/main/java/com/nextcloud/talk/dagger/modules/DatabaseModule.java index 83279c31c..76219ecd2 100644 --- a/app/src/main/java/com/nextcloud/talk/dagger/modules/DatabaseModule.java +++ b/app/src/main/java/com/nextcloud/talk/dagger/modules/DatabaseModule.java @@ -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); } } diff --git a/app/src/main/java/com/nextcloud/talk/dagger/modules/RestModule.java b/app/src/main/java/com/nextcloud/talk/dagger/modules/RestModule.java index c810b8bb1..40061232f 100644 --- a/app/src/main/java/com/nextcloud/talk/dagger/modules/RestModule.java +++ b/app/src/main/java/com/nextcloud/talk/dagger/modules/RestModule.java @@ -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(); diff --git a/app/src/main/java/com/nextcloud/talk/utils/BundleBuilder.java b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleBuilder.java similarity index 86% rename from app/src/main/java/com/nextcloud/talk/utils/BundleBuilder.java rename to app/src/main/java/com/nextcloud/talk/utils/bundle/BundleBuilder.java index f1fea96f7..d0f8edce6 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/BundleBuilder.java +++ b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleBuilder.java @@ -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 . */ -package com.nextcloud.talk.utils; +package com.nextcloud.talk.utils.bundle; import android.os.Bundle; import android.os.Parcelable; diff --git a/app/src/main/java/com/nextcloud/talk/utils/BundleKeys.java b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java similarity index 91% rename from app/src/main/java/com/nextcloud/talk/utils/BundleKeys.java rename to app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java index 33921e887..b33a0b9a8 100644 --- a/app/src/main/java/com/nextcloud/talk/utils/BundleKeys.java +++ b/app/src/main/java/com/nextcloud/talk/utils/bundle/BundleKeys.java @@ -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 . */ -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"; - } diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java new file mode 100644 index 000000000..eda4147bd --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/AppPreferences.java @@ -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 . + */ + +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(); +} diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/json/ProxyPrefs.java b/app/src/main/java/com/nextcloud/talk/utils/preferences/json/ProxyPrefs.java new file mode 100644 index 000000000..ea330cbb1 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/json/ProxyPrefs.java @@ -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 . + */ + +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; +} diff --git a/app/src/main/java/com/nextcloud/talk/utils/preferences/json/ProxyTypeAdapter.java b/app/src/main/java/com/nextcloud/talk/utils/preferences/json/ProxyTypeAdapter.java new file mode 100644 index 000000000..cf2a30875 --- /dev/null +++ b/app/src/main/java/com/nextcloud/talk/utils/preferences/json/ProxyTypeAdapter.java @@ -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 . + */ + +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 { + + 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(); + } +}