mirror of
https://github.com/nextcloud/talk-android
synced 2025-02-02 04:32:02 +00:00
Start implementing different approach to retrofit
Signed-off-by: Mario Danic <mario@lovelyhq.com>
This commit is contained in:
parent
e5e33c6b62
commit
57c2858737
@ -48,7 +48,6 @@ import java.security.UnrecoverableKeyException;
|
|||||||
import java.security.cert.CertificateException;
|
import java.security.cert.CertificateException;
|
||||||
import java.util.concurrent.TimeUnit;
|
import java.util.concurrent.TimeUnit;
|
||||||
|
|
||||||
import javax.inject.Singleton;
|
|
||||||
import javax.net.ssl.KeyManagerFactory;
|
import javax.net.ssl.KeyManagerFactory;
|
||||||
import javax.net.ssl.X509KeyManager;
|
import javax.net.ssl.X509KeyManager;
|
||||||
|
|
||||||
@ -76,13 +75,11 @@ public class RestModule {
|
|||||||
private static final String TAG = "RestModule";
|
private static final String TAG = "RestModule";
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
|
||||||
NcApi provideNcApi(Retrofit retrofit) {
|
NcApi provideNcApi(Retrofit retrofit) {
|
||||||
return retrofit.create(NcApi.class);
|
return retrofit.create(NcApi.class);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
|
||||||
Proxy provideProxy(AppPreferences appPreferences) {
|
Proxy provideProxy(AppPreferences appPreferences) {
|
||||||
if (!TextUtils.isEmpty(appPreferences.getProxyType()) && !"No proxy".equals(appPreferences.getProxyType())
|
if (!TextUtils.isEmpty(appPreferences.getProxyType()) && !"No proxy".equals(appPreferences.getProxyType())
|
||||||
&& !TextUtils.isEmpty(appPreferences.getProxyHost())) {
|
&& !TextUtils.isEmpty(appPreferences.getProxyHost())) {
|
||||||
@ -102,7 +99,6 @@ public class RestModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
|
||||||
Retrofit provideRetrofit(OkHttpClient httpClient) {
|
Retrofit provideRetrofit(OkHttpClient httpClient) {
|
||||||
Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
|
Retrofit.Builder retrofitBuilder = new Retrofit.Builder()
|
||||||
.client(httpClient)
|
.client(httpClient)
|
||||||
@ -114,13 +110,11 @@ public class RestModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
|
||||||
MagicTrustManager provideMagicTrustManager() {
|
MagicTrustManager provideMagicTrustManager() {
|
||||||
return new MagicTrustManager();
|
return new MagicTrustManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
|
||||||
MagicKeyManager provideKeyManager(AppPreferences appPreferences, UserUtils userUtils) {
|
MagicKeyManager provideKeyManager(AppPreferences appPreferences, UserUtils userUtils) {
|
||||||
KeyStore keyStore = null;
|
KeyStore keyStore = null;
|
||||||
try {
|
try {
|
||||||
@ -146,27 +140,23 @@ public class RestModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
|
||||||
SSLSocketFactoryCompat provideSslSocketFactoryCompat(MagicKeyManager keyManager, MagicTrustManager
|
SSLSocketFactoryCompat provideSslSocketFactoryCompat(MagicKeyManager keyManager, MagicTrustManager
|
||||||
magicTrustManager) {
|
magicTrustManager) {
|
||||||
return new SSLSocketFactoryCompat(keyManager, magicTrustManager);
|
return new SSLSocketFactoryCompat(keyManager, magicTrustManager);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
|
||||||
CookieManager provideCookieManager() {
|
CookieManager provideCookieManager() {
|
||||||
return new CookieManager();
|
return new CookieManager();
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
|
||||||
Cache provideCache() {
|
Cache provideCache() {
|
||||||
int cacheSize = 128 * 1024 * 1024; // 128 MB
|
int cacheSize = 128 * 1024 * 1024; // 128 MB
|
||||||
return new Cache(NextcloudTalkApplication.getSharedApplication().getCacheDir(), cacheSize);
|
return new Cache(NextcloudTalkApplication.getSharedApplication().getCacheDir(), cacheSize);
|
||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
|
||||||
Dispatcher provideDispatcher() {
|
Dispatcher provideDispatcher() {
|
||||||
Dispatcher dispatcher = new Dispatcher();
|
Dispatcher dispatcher = new Dispatcher();
|
||||||
dispatcher.setMaxRequestsPerHost(100);
|
dispatcher.setMaxRequestsPerHost(100);
|
||||||
@ -175,7 +165,6 @@ public class RestModule {
|
|||||||
}
|
}
|
||||||
|
|
||||||
@Provides
|
@Provides
|
||||||
@Singleton
|
|
||||||
OkHttpClient provideHttpClient(Proxy proxy, AppPreferences appPreferences,
|
OkHttpClient provideHttpClient(Proxy proxy, AppPreferences appPreferences,
|
||||||
MagicTrustManager magicTrustManager,
|
MagicTrustManager magicTrustManager,
|
||||||
SSLSocketFactoryCompat sslSocketFactoryCompat, Cache cache,
|
SSLSocketFactoryCompat sslSocketFactoryCompat, Cache cache,
|
||||||
|
@ -108,6 +108,15 @@ public class UserUtils {
|
|||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public UserEntity getUserWithId(long id) {
|
||||||
|
Result findUserQueryResult = dataStore.select(User.class).where(UserEntity.ID.eq(id)
|
||||||
|
.and(UserEntity.SCHEDULED_FOR_DELETION.notEqual(true)))
|
||||||
|
.limit(1).get();
|
||||||
|
|
||||||
|
return (UserEntity) findUserQueryResult.firstOrNull();
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void disableAllUsersWithoutId(long userId) {
|
public void disableAllUsersWithoutId(long userId) {
|
||||||
Result findUserQueryResult = dataStore.select(User.class).where(UserEntity.ID.notEqual(userId)).get();
|
Result findUserQueryResult = dataStore.select(User.class).where(UserEntity.ID.notEqual(userId)).get();
|
||||||
|
|
||||||
|
@ -0,0 +1,71 @@
|
|||||||
|
/*
|
||||||
|
* Nextcloud Talk application
|
||||||
|
*
|
||||||
|
* @author Mario Danic
|
||||||
|
* Copyright (C) 2017-2018 Mario Danic <mario@lovelyhq.com>
|
||||||
|
*
|
||||||
|
* 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.singletons;
|
||||||
|
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.text.TextUtils;
|
||||||
|
|
||||||
|
import com.nextcloud.talk.api.NcApi;
|
||||||
|
import com.nextcloud.talk.application.NextcloudTalkApplication;
|
||||||
|
import com.nextcloud.talk.models.database.UserEntity;
|
||||||
|
import com.nextcloud.talk.utils.database.user.UserUtils;
|
||||||
|
|
||||||
|
import java.util.HashMap;
|
||||||
|
|
||||||
|
import javax.inject.Inject;
|
||||||
|
|
||||||
|
import autodagger.AutoInjector;
|
||||||
|
import retrofit2.Retrofit;
|
||||||
|
|
||||||
|
@AutoInjector(NextcloudTalkApplication.class)
|
||||||
|
public class ApiHolder {
|
||||||
|
private static final String TAG = "ApiHolder";
|
||||||
|
|
||||||
|
private HashMap<Long, NcApi> ncApiHashMap;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
UserUtils userUtils;
|
||||||
|
|
||||||
|
@Inject
|
||||||
|
Retrofit retrofit;
|
||||||
|
|
||||||
|
private static final ApiHolder holder = new ApiHolder();
|
||||||
|
|
||||||
|
public static ApiHolder getInstance() {
|
||||||
|
return holder;
|
||||||
|
}
|
||||||
|
|
||||||
|
public NcApi getNcApiInstanceForAccountId(long accountId, @Nullable String baseUrl) {
|
||||||
|
NextcloudTalkApplication.getSharedApplication().getComponentApplication().inject(this);
|
||||||
|
if (!ncApiHashMap.containsKey(accountId)) {
|
||||||
|
UserEntity userAccount = userUtils.getUserWithId(accountId);
|
||||||
|
if (userAccount == null || !TextUtils.isEmpty(baseUrl)) {
|
||||||
|
retrofit = retrofit.newBuilder().baseUrl(baseUrl).build();
|
||||||
|
return retrofit.create(NcApi.class);
|
||||||
|
} else {
|
||||||
|
retrofit = retrofit.newBuilder().baseUrl(userAccount.getBaseUrl()).build();
|
||||||
|
ncApiHashMap.put(accountId, retrofit.create(NcApi.class));
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return ncApiHashMap.get(accountId);
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user