-
-
Notifications
You must be signed in to change notification settings - Fork 263
Expand file tree
/
Copy pathApiModule.java
More file actions
147 lines (124 loc) · 4.56 KB
/
Copy pathApiModule.java
File metadata and controls
147 lines (124 loc) · 4.56 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
package de.luhmer.owncloudnewsreader.di;
import static de.luhmer.owncloudnewsreader.helper.NextcloudGlideModuleKt.CACHE_SIZE;
import static de.luhmer.owncloudnewsreader.helper.NextcloudGlideModuleKt.MB;
import android.app.Application;
import android.content.Context;
import android.content.SharedPreferences;
import com.google.gson.FieldNamingPolicy;
import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import javax.inject.Named;
import javax.inject.Singleton;
import dagger.Module;
import dagger.Provides;
import de.luhmer.owncloudnewsreader.SettingsActivity;
import de.luhmer.owncloudnewsreader.helper.PostDelayHandler;
import de.luhmer.owncloudnewsreader.helper.ThemeChooser;
import de.luhmer.owncloudnewsreader.ssl.MemorizingTrustManager;
import okhttp3.Cache;
import okhttp3.OkHttpClient;
/**
* Created by david on 22.05.17.
*/
@Module
public class ApiModule {
private final Application mApplication;
public ApiModule(Application application) {
this.mApplication = application;
}
// Dagger will only look for methods annotated with @Provides
@Provides
@Singleton
// Application reference must come from AppModule.class
SharedPreferences providesSharedPreferences() {
//return PreferenceManager.getDefaultSharedPreferences(mApplication);
SharedPreferences mPrefs = mApplication.getSharedPreferences(providesSharedPreferencesFileName(), Context.MODE_PRIVATE);
ThemeChooser.init(mPrefs);
return mPrefs;
}
// Dagger will only look for methods annotated with @Provides
@Provides
@Named("sharedPreferencesFileName")
public String providesSharedPreferencesFileName() {
//return PreferenceManager.getDefaultSharedPreferencesName(mApplication);
return mApplication.getPackageName() + "_preferences";
}
// Dagger will only look for methods annotated with @Provides
@Provides
@Named("databaseFileName")
public String providesDatabaseFileName() {
//return PreferenceManager.getDefaultSharedPreferencesName(mApplication);
return "OwncloudNewsReaderOrm.db";
}
/*
@Provides
@Singleton
NextcloudAPI providexNextcloudAPI() {
return new NextcloudAPI("");
}*/
/*
@Provides
@Singleton
Cache provideOkHttpCache(Application application) {
int cacheSize = 10 * 1024 * 1024; // 10 MiB
Cache cache = new Cache(application.getCacheDir(), cacheSize);
return cache;
}*/
@Provides
@Singleton
Gson provideGson() {
GsonBuilder gsonBuilder = new GsonBuilder();
gsonBuilder.setFieldNamingPolicy(FieldNamingPolicy.LOWER_CASE_WITH_UNDERSCORES);
return gsonBuilder.create();
}
@Provides
@Singleton
PostDelayHandler providePostDelayHandler() {
return new PostDelayHandler(mApplication);
}
@Provides
@Singleton
OkHttpClient provideOkHttpClient(SharedPreferences prefs) {
String cacheSize = prefs.getString(SettingsActivity.SP_MAX_CACHE_SIZE, String.valueOf(CACHE_SIZE));
Long diskCacheSizeBytes = Long.valueOf(cacheSize) * MB;
OkHttpClient.Builder client = new OkHttpClient.Builder()
.cache(new Cache(mApplication.getApplicationContext().getCacheDir(), diskCacheSizeBytes));
/*
.addInterceptor(Interceptor { chain: Interceptor.Chain ->
val response = chain.proceed(chain.request())
if (response.cacheResponse != null) {
Log.d("NextcloudGlideModule", "cached response: " + response.request.url)
} else if (response.networkResponse != null) {
Log.d("NextcloudGlideModule", "network response: " + response.request.url)
for (h in response.request.headers) {
Log.d("NextcloudGlideModule", "request headers: $h")
}
}
response
})
*/
return client.build();
}
/*
@Provides
@Singleton
Retrofit provideRetrofit(String baseUrl, Gson gson, OkHttpClient okHttpClient) {
Retrofit retrofit = new Retrofit.Builder()
.addConverterFactory(GsonConverterFactory.create(gson))
.baseUrl(baseUrl)
.client(okHttpClient)
.build();
return retrofit;
}
*/
@Provides
@Singleton
MemorizingTrustManager provideMTM() {
return new MemorizingTrustManager(mApplication);
}
@Provides
@Singleton
ApiProvider provideAPI(MemorizingTrustManager mtm, SharedPreferences sp) {
return new ApiProvider(mtm, sp, mApplication);
}
}