/
ConnectionBuddyConfiguration.java
178 lines (135 loc) · 5.74 KB
/
ConnectionBuddyConfiguration.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
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
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
package com.zplesac.connectionbuddy;
import com.zplesac.connectionbuddy.models.ConnectivityStrength;
import android.content.Context;
import android.net.ConnectivityManager;
/**
* Created by Željko Plesac on 09/10/15.
* Configuration class for ConnectionBuddy instance. Use this to customize the library behaviour.
*/
public class ConnectionBuddyConfiguration {
public static final int SIGNAL_STRENGTH_NUMBER_OF_LEVELS = 3;
public static final int DEFAULT_NETWORK_EXECUTOR_THREAD_SIZE = 4;
private Context context;
private boolean registeredForWiFiChanges;
private boolean registeredForMobileNetworkChanges;
private ConnectivityStrength minimumSignalStrength;
private ConnectionBuddyCache networkEventsCache;
private boolean notifyImmediately;
private ConnectivityManager connectivityManager;
private boolean notifyOnlyReliableEvents;
private int testNetworkRequestExecutorSize;
private ConnectionBuddyConfiguration(Builder builder) {
this.context = builder.context;
this.registeredForMobileNetworkChanges = builder.registerForMobileNetworkChanges;
this.registeredForWiFiChanges = builder.registerForWiFiChanges;
this.minimumSignalStrength = builder.minimumSignalStrength;
this.notifyImmediately = builder.notifyImmediately;
this.notifyOnlyReliableEvents = builder.notifyOnlyReliableEvents;
this.connectivityManager = (ConnectivityManager) getContext().getSystemService(Context.CONNECTIVITY_SERVICE);
this.testNetworkRequestExecutorSize = builder.testNetworkRequestExecutorSize;
if (builder.cache != null) {
this.networkEventsCache = builder.cache;
} else {
this.networkEventsCache = new LruConnectionBuddyCache();
}
}
public Context getContext() {
return context;
}
public boolean isRegisteredForWiFiChanges() {
return registeredForWiFiChanges;
}
public boolean isRegisteredForMobileNetworkChanges() {
return registeredForMobileNetworkChanges;
}
public ConnectivityStrength getMinimumSignalStrength() {
return minimumSignalStrength;
}
public ConnectionBuddyCache getNetworkEventsCache() {
return networkEventsCache;
}
public boolean isNotifyImmediately() {
return notifyImmediately;
}
public ConnectivityManager getConnectivityManager() {
return connectivityManager;
}
public boolean isNotifyOnlyReliableEvents() {
return notifyOnlyReliableEvents;
}
public int getTestNetworkRequestExecutorSize() {
return testNetworkRequestExecutorSize;
}
public static class Builder {
private Context context;
/**
* Boolean value which defines should we register for WiFi network changes.
* Default value is set to true.
*/
private boolean registerForWiFiChanges = true;
/**
* Boolean value which defines should we register for mobile network changes.
* Default value is set to true.
*/
private boolean registerForMobileNetworkChanges = true;
/**
* Define minimum signal strength for which we should call callback listener.
* Default is set to ConnectivityStrength.UNDEFINED.
*/
private ConnectivityStrength minimumSignalStrength = new ConnectivityStrength(ConnectivityStrength.UNDEFINED);
/**
* Boolean value which defines do we want to notify the listener about current network connection state
* immediately after the listener has been registered.
* Default is set to true.
*/
private boolean notifyImmediately = true;
/**
* Cache which is used for storing network events.
*/
private ConnectionBuddyCache cache;
/**
* Boolean value which defines do we want to use reliable network events. This means that if we have active internet connection,
* it will try to execute test network request to determine if user is capable of any network operation.
* Default is set to false.
*/
private boolean notifyOnlyReliableEvents = false;
/**
* Default network request executor service size.
*/
private int testNetworkRequestExecutorSize = DEFAULT_NETWORK_EXECUTOR_THREAD_SIZE;
public Builder(Context context) {
this.context = context.getApplicationContext();
}
public Builder registerForWiFiChanges(boolean shouldRegister) {
this.registerForWiFiChanges = shouldRegister;
return this;
}
public Builder registerForMobileNetworkChanges(boolean shouldRegister) {
this.registerForMobileNetworkChanges = shouldRegister;
return this;
}
public Builder setMinimumSignalStrength(ConnectivityStrength minimumSignalStrength) {
this.minimumSignalStrength = minimumSignalStrength;
return this;
}
public Builder setNotifyImmediately(boolean shouldNotify) {
this.notifyImmediately = shouldNotify;
return this;
}
public Builder notifyOnlyReliableEvents(boolean shouldNotify) {
this.notifyOnlyReliableEvents = shouldNotify;
return this;
}
public Builder setNetworkEventsCache(ConnectionBuddyCache cache) {
this.cache = cache;
return this;
}
public Builder setTestNetworkRequestExecutorSize(int testNetworkRequestExecutorSize) {
this.testNetworkRequestExecutorSize = testNetworkRequestExecutorSize;
return this;
}
public ConnectionBuddyConfiguration build() {
return new ConnectionBuddyConfiguration(this);
}
}
}