blob: 1c62a6e516a3773202b8b6dec2d29d9e65a7ffd5 (
plain)
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
|
package de.luhmer.owncloudnewsreader.di;
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.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
OkHttpClient provideOkHttpClient(Cache cache) {
// setCache(cache);
return new OkHttpClient();
}
@Provides
@Singleton
PostDelayHandler providePostDelayHandler() {
return new PostDelayHandler(mApplication);
}
/*
@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);
}
}
|