@@ -8,13 +8,46 @@ import androidx.annotation.DrawableRes
8
8
import androidx.localbroadcastmanager.content.LocalBroadcastManager
9
9
import io.matthewnelson.topl_service.model.ServiceNotification
10
10
import io.matthewnelson.topl_service.service.TorService
11
- import io.matthewnelson.topl_service.service.ServiceActions .ServiceAction
11
+ import io.matthewnelson.topl_service.service.ActionConsts .ServiceAction
12
12
import androidx.core.app.NotificationCompat.NotificationVisibility
13
13
import io.matthewnelson.topl_android_settings.TorConfigFiles
14
14
import io.matthewnelson.topl_android_settings.TorSettings
15
15
16
16
class TorServiceController private constructor() {
17
17
18
+ /* *
19
+ * The [TorServiceController.Builder] is where you get to customize how [TorService] works
20
+ * for your application. Call it in `Application.onCreate` and follow along.
21
+ *
22
+ * A note about the [TorSettings] you send this. Those are the default settings which
23
+ * [TorService] will fall back on if [io.matthewnelson.topl_service_prefs.TorServicePrefs]
24
+ * has nothing in it for that particular [io.matthewnelson.topl_service_prefs.PrefsKeys].
25
+ * The settings get written to the `torrc` file everytime Tor is started (I plan to make this
26
+ * less sledgehammer-ish in the future).
27
+ *
28
+ * To update settings while your application is running you need only to instantiate
29
+ * [io.matthewnelson.topl_service_prefs.TorServicePrefs] and save the data using the
30
+ * appropriately annotated method and [io.matthewnelson.topl_service_prefs.PrefsKeys], then
31
+ * restart Tor (for now... ;-D).
32
+ *
33
+ * I plan to implement a
34
+ * [android.content.SharedPreferences.OnSharedPreferenceChangeListener] that will do this
35
+ * immediately for the settings that don't require a restart, but a stable release comes first).
36
+ *
37
+ * You can see how the [TorSettings] sent here are used in [TorService] by looking at
38
+ * [io.matthewnelson.topl_service_settings.TorServiceSettings] and [TorService.onCreate].
39
+ *
40
+ * @param [context] Context
41
+ * @param [buildConfigVersion] send [BuildConfig.VERSION_CODE]. Mitigates copying of geoip
42
+ * files to app updates only.
43
+ * @param [torSettings] [TorSettings] used to create your torrc file on startup.
44
+ * @param [geoipAssetPath] The path to where you have your geoip file located (ex: in
45
+ * assets/common directory, send this variable "common/geoip").
46
+ * @param [geoip6AssetPath] The path to where you have your geoip6 file located (ex: in
47
+ * assets/common directory, send this variable "common/geoip6").
48
+ *
49
+ * @sample [io.matthewnelson.sampleapp.App.setupTorServices]
50
+ * */
18
51
class Builder (
19
52
private val context : Context ,
20
53
private val buildConfigVersion : Int ,
@@ -25,11 +58,36 @@ class TorServiceController private constructor() {
25
58
26
59
private lateinit var torConfigFiles: TorConfigFiles
27
60
61
+ /* *
62
+ * If you wish to customize the file structure of how Tor is installed in your app,
63
+ * you can do so by instantiating your own [TorConfigFiles] and customizing it via
64
+ * the [TorConfigFiles.Builder], or overridden method [TorConfigFiles.createConfig].
65
+ *
66
+ * By default, [TorService] will call [TorConfigFiles.createConfig] using your
67
+ * [Context.getApplicationContext] to set up a standard directory hierarchy for Tor
68
+ * to operate with.
69
+ *
70
+ * See [Builder] for code samples.
71
+ *
72
+ * @return [Builder]
73
+ * @see [Builder.build]
74
+ * */
28
75
fun useCustomTorConfigFiles (torConfigFiles : TorConfigFiles ): Builder {
29
76
this .torConfigFiles = torConfigFiles
30
77
return this
31
78
}
32
79
80
+ /* *
81
+ * Customize the service notification to your application.
82
+ *
83
+ * See [Builder] for code samples.
84
+ *
85
+ * @param [channelName] Your notification channel's name.
86
+ * @param [channelID] Your notification channel's ID.
87
+ * @param [channelDescription] Your notification channel's description.
88
+ * @param [notificationID] Your foreground notification's ID
89
+ * @return [NotificationBuilder]
90
+ * */
33
91
@Throws(IllegalArgumentException ::class )
34
92
fun customizeNotification (
35
93
channelName : String ,
@@ -41,7 +99,7 @@ class TorServiceController private constructor() {
41
99
channelName.isNotEmpty() &&
42
100
channelID.isNotEmpty() &&
43
101
channelDescription.isNotEmpty()
44
- ) { " channelName & channelID must not be empty." }
102
+ ) { " channelName, channelID, & channelDescription must not be empty." }
45
103
46
104
return NotificationBuilder (
47
105
this ,
@@ -52,6 +110,11 @@ class TorServiceController private constructor() {
52
110
)
53
111
}
54
112
113
+ /* *
114
+ * Where you get to customize how your foreground notification will look/function.
115
+ *
116
+ * See [Builder] for code samples.
117
+ * */
55
118
class NotificationBuilder (
56
119
private val builder : Builder ,
57
120
channelName : String ,
@@ -68,6 +131,16 @@ class TorServiceController private constructor() {
68
131
notificationID
69
132
)
70
133
134
+ /* *
135
+ * For when your user taps the TorService notification.
136
+ *
137
+ * See [Builder] for code samples.
138
+ *
139
+ * @param [clazz] The Activity to be opened when tapped.
140
+ * @param [intentExtrasKey]? The key for if you with to add extras in the PendingIntent.
141
+ * @param [intentExtras]? The extras that will be sent in the PendingIntent.
142
+ * @param [intentRequestCode]? The request code - Defaults to 0 if not set.
143
+ * */
71
144
fun setActivityToBeOpenedOnTap (
72
145
clazz : Class <* >,
73
146
intentExtrasKey : String? ,
@@ -81,54 +154,147 @@ class TorServiceController private constructor() {
81
154
return this
82
155
}
83
156
157
+ /* *
158
+ * Defaults to Orbot/TorBrowser's icon.
159
+ *
160
+ * The small icon you wish to display when Tor's State is
161
+ * [io.matthewnelson.topl_android_settings.TorStates.TorState.ON].
162
+ *
163
+ * See [Builder] for code samples.
164
+ *
165
+ * @param [drawableRes] Drawable resource id.
166
+ * @return [NotificationBuilder]
167
+ * */
84
168
fun setImageTorOn (@DrawableRes drawableRes : Int ): NotificationBuilder {
85
169
serviceNotification.imageOn = drawableRes
86
170
return this
87
171
}
88
172
173
+ /* *
174
+ * Defaults to Orbot/TorBrowser's icon.
175
+ *
176
+ * The small icon you wish to display when Tor's State is
177
+ * [io.matthewnelson.topl_android_settings.TorStates.TorState.OFF].
178
+ *
179
+ * See [Builder] for code samples.
180
+ *
181
+ * @param [drawableRes] Drawable resource id.
182
+ * @return [NotificationBuilder]
183
+ * */
89
184
fun setImageTorOff (@DrawableRes drawableRes : Int ): NotificationBuilder {
90
185
serviceNotification.imageOff = drawableRes
91
186
return this
92
187
}
93
188
189
+ /* *
190
+ * Defaults to Orbot/TorBrowser's icon.
191
+ *
192
+ * The small icon you wish to display when bandwidth is being used.
193
+ *
194
+ * See [Builder] for code samples.
195
+ *
196
+ * @param [drawableRes] Drawable resource id.
197
+ * @return [NotificationBuilder]
198
+ * */
94
199
fun setImageTorDataTransfer (@DrawableRes drawableRes : Int ): NotificationBuilder {
95
200
serviceNotification.imageData = drawableRes
96
201
return this
97
202
}
98
203
204
+ /* *
205
+ * Defaults to Orbot/TorBrowser's icon.
206
+ *
207
+ * The small icon you wish to display when Tor is having problems.
208
+ *
209
+ * See [Builder] for code samples.
210
+ *
211
+ * @param [drawableRes] Drawable resource id.
212
+ * @return [NotificationBuilder]
213
+ * */
99
214
fun setImageTorErrors (@DrawableRes drawableRes : Int ): NotificationBuilder {
100
215
serviceNotification.imageError = drawableRes
101
216
return this
102
217
}
103
218
219
+ /* *
220
+ * Defaults to white
221
+ *
222
+ * The color you wish to display when Tor's State is
223
+ * [io.matthewnelson.topl_android_settings.TorStates.TorState.ON].
224
+ *
225
+ * See [Builder] for code samples.
226
+ *
227
+ * @param [colorRes] Color resource id.
228
+ * @return [NotificationBuilder]
229
+ * */
104
230
fun setColorWhenTorOn (@ColorRes colorRes : Int ): NotificationBuilder {
105
- serviceNotification.colorRes = colorRes
231
+ serviceNotification.colorWhenOn = colorRes
106
232
return this
107
233
}
108
234
235
+ /* *
236
+ * Defaults to NotificationVisibility.VISIBILITY_SECRET
237
+ *
238
+ * The visibility of your notification on the user's lock screen.
239
+ *
240
+ * See [Builder] for code samples.
241
+ *
242
+ * @return [NotificationBuilder]
243
+ * */
109
244
fun setVisibility (@NotificationVisibility visibility : Int ): NotificationBuilder {
110
245
if (visibility in - 1 .. 1 )
111
246
serviceNotification.visibility = visibility
112
247
return this
113
248
}
114
249
250
+ /* *
251
+ * Disabled by Default
252
+ *
253
+ * Enable on the notification the ability to *restart* Tor.
254
+ *
255
+ * See [Builder] for code samples.
256
+ *
257
+ * @return [NotificationBuilder]
258
+ * */
115
259
fun enableTorRestartButton (): NotificationBuilder {
116
260
serviceNotification.enableRestartButton = true
117
261
return this
118
262
}
119
263
264
+ /* *
265
+ * Disabled by Default
266
+ *
267
+ * Enable on the notification the ability to *stop* Tor.
268
+ *
269
+ * See [Builder] for code samples.
270
+ *
271
+ * @return [NotificationBuilder]
272
+ * */
120
273
fun enableTorStopButton (): NotificationBuilder {
121
274
serviceNotification.enableStopButton = true
122
275
return this
123
276
}
124
277
125
- fun applySettings (): Builder {
278
+ /* *
279
+ * Initialize settings.
280
+ *
281
+ * See [Builder] for code samples.
282
+ *
283
+ * @return [Builder]
284
+ * */
285
+ fun applyNotificationSettings (): Builder {
126
286
ServiceNotification .initialize(serviceNotification)
127
287
return builder
128
288
}
129
289
130
290
}
131
291
292
+ /* *
293
+ * Initializes [TorService] setup and enables the ability to call methods in the
294
+ * [Companion] object.
295
+ *
296
+ * See [Builder] for code samples.
297
+ * */
132
298
fun build () {
133
299
val torConfigFiles =
134
300
if (::torConfigFiles.isInitialized) {
@@ -151,6 +317,9 @@ class TorServiceController private constructor() {
151
317
}
152
318
}
153
319
320
+ /* *
321
+ * Where everything needed to interact with [TorService] resides.
322
+ * */
154
323
companion object {
155
324
156
325
private lateinit var appContext: Context
0 commit comments