-
Notifications
You must be signed in to change notification settings - Fork 1
/
Injection.java
89 lines (66 loc) · 2.44 KB
/
Injection.java
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
package com.mytestedapp;
import android.support.annotation.NonNull;
import android.support.annotation.VisibleForTesting;
import android.util.Log;
import com.mytestedapp.rest.RestService;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.SynchronousQueue;
import java.util.concurrent.ThreadFactory;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.atomic.AtomicInteger;
import okhttp3.Dispatcher;
import okhttp3.OkHttpClient;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;
public class Injection {
private static Injection sInjection;
public static synchronized Injection getInstance() {
if (sInjection == null) {
sInjection = new Injection();
}
return sInjection;
}
public RestService provideRestService() {
OkHttpClient okHttpClient = provideOkHttpClient();
Retrofit retrofit = new Retrofit.Builder()
.baseUrl("http://10.0.2.2:9000")
.addConverterFactory(GsonConverterFactory.create())
.client(okHttpClient)
.build();
return retrofit.create(RestService.class);
}
public OkHttpClient provideOkHttpClient() {
ExecutorService executorService = provideExecutor();
Dispatcher dispatcher = new Dispatcher(executorService);
return new OkHttpClient.Builder()
.dispatcher(dispatcher)
.build();
}
public ExecutorService provideExecutor() {
ThreadFactory threadFactory = new ThreadFactory() {
AtomicInteger seq = new AtomicInteger();
@Override
public Thread newThread(@NonNull final Runnable r) {
String name = "MyThread-" + seq.getAndIncrement();
return new Thread(r, name);
}
};
return new ThreadPoolExecutor(
0,
Integer.MAX_VALUE,
5L,
TimeUnit.SECONDS,
new SynchronousQueue<Runnable>(),
threadFactory) {
@Override
protected void beforeExecute(Thread t, Runnable r) {
Log.d("MyTestedApp", "Will be executing on " + t.getName() + " runnable " + r);
}
};
}
@VisibleForTesting
public static synchronized void setInstance(Injection injection) {
sInjection = injection;
}
}