Skip to content
This repository was archived by the owner on Dec 18, 2022. It is now read-only.

Commit 0f88af0

Browse files
committed
code cleanup and documentation
1 parent 5a996fa commit 0f88af0

File tree

9 files changed

+228
-19
lines changed

9 files changed

+228
-19
lines changed

sampleapp/src/main/java/io/matthewnelson/sampleapp/App.kt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,9 +24,9 @@ class App: Application() {
2424
"TorService Channel",
2525
"Tor Channel",
2626
"My Sample Application",
27-
6156)
27+
615)
2828
.setActivityToBeOpenedOnTap(MainActivity::class.java, null, null, null)
29-
.applySettings()
29+
.applyNotificationSettings()
3030
.build()
3131
}
3232
}

sampleapp/src/main/java/io/matthewnelson/sampleapp/MyTorSettings.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,9 @@ package io.matthewnelson.sampleapp
22

33
import io.matthewnelson.topl_android_settings.TorSettings
44

5+
/**
6+
* See [TorSettings] for comments on what is what.
7+
* */
58
class MyTorSettings: TorSettings() {
69

710
override val disableNetwork: Boolean

topl-android-settings/src/main/java/io/matthewnelson/topl_android_settings/TorSettings.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ package io.matthewnelson.topl_android_settings
1616
*
1717
* https://2019.www.torproject.org/docs/tor-manual.html.en
1818
*
19-
* @sample [io.matthewnelson.topl_service_settings.TestTorSettings]
19+
* @sample [io.matthewnelson.sampleapp.MyTorSettings]
2020
* */
2121
abstract class TorSettings: SettingsConsts() {
2222

topl-service/src/main/java/io/matthewnelson/topl_service/TorServiceController.kt

Lines changed: 173 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,46 @@ import androidx.annotation.DrawableRes
88
import androidx.localbroadcastmanager.content.LocalBroadcastManager
99
import io.matthewnelson.topl_service.model.ServiceNotification
1010
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
1212
import androidx.core.app.NotificationCompat.NotificationVisibility
1313
import io.matthewnelson.topl_android_settings.TorConfigFiles
1414
import io.matthewnelson.topl_android_settings.TorSettings
1515

1616
class TorServiceController private constructor() {
1717

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+
* */
1851
class Builder(
1952
private val context: Context,
2053
private val buildConfigVersion: Int,
@@ -25,11 +58,36 @@ class TorServiceController private constructor() {
2558

2659
private lateinit var torConfigFiles: TorConfigFiles
2760

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+
* */
2875
fun useCustomTorConfigFiles(torConfigFiles: TorConfigFiles): Builder {
2976
this.torConfigFiles = torConfigFiles
3077
return this
3178
}
3279

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+
* */
3391
@Throws(IllegalArgumentException::class)
3492
fun customizeNotification(
3593
channelName: String,
@@ -41,7 +99,7 @@ class TorServiceController private constructor() {
4199
channelName.isNotEmpty() &&
42100
channelID.isNotEmpty() &&
43101
channelDescription.isNotEmpty()
44-
) { "channelName & channelID must not be empty." }
102+
) { "channelName, channelID, & channelDescription must not be empty." }
45103

46104
return NotificationBuilder(
47105
this,
@@ -52,6 +110,11 @@ class TorServiceController private constructor() {
52110
)
53111
}
54112

113+
/**
114+
* Where you get to customize how your foreground notification will look/function.
115+
*
116+
* See [Builder] for code samples.
117+
* */
55118
class NotificationBuilder(
56119
private val builder: Builder,
57120
channelName: String,
@@ -68,6 +131,16 @@ class TorServiceController private constructor() {
68131
notificationID
69132
)
70133

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+
* */
71144
fun setActivityToBeOpenedOnTap(
72145
clazz: Class<*>,
73146
intentExtrasKey: String?,
@@ -81,54 +154,147 @@ class TorServiceController private constructor() {
81154
return this
82155
}
83156

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+
* */
84168
fun setImageTorOn(@DrawableRes drawableRes: Int): NotificationBuilder {
85169
serviceNotification.imageOn = drawableRes
86170
return this
87171
}
88172

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+
* */
89184
fun setImageTorOff(@DrawableRes drawableRes: Int): NotificationBuilder {
90185
serviceNotification.imageOff = drawableRes
91186
return this
92187
}
93188

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+
* */
94199
fun setImageTorDataTransfer(@DrawableRes drawableRes: Int): NotificationBuilder {
95200
serviceNotification.imageData = drawableRes
96201
return this
97202
}
98203

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+
* */
99214
fun setImageTorErrors(@DrawableRes drawableRes: Int): NotificationBuilder {
100215
serviceNotification.imageError = drawableRes
101216
return this
102217
}
103218

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+
* */
104230
fun setColorWhenTorOn(@ColorRes colorRes: Int): NotificationBuilder {
105-
serviceNotification.colorRes = colorRes
231+
serviceNotification.colorWhenOn = colorRes
106232
return this
107233
}
108234

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+
* */
109244
fun setVisibility(@NotificationVisibility visibility: Int): NotificationBuilder {
110245
if (visibility in -1..1)
111246
serviceNotification.visibility = visibility
112247
return this
113248
}
114249

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+
* */
115259
fun enableTorRestartButton(): NotificationBuilder {
116260
serviceNotification.enableRestartButton = true
117261
return this
118262
}
119263

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+
* */
120273
fun enableTorStopButton(): NotificationBuilder {
121274
serviceNotification.enableStopButton = true
122275
return this
123276
}
124277

125-
fun applySettings(): Builder {
278+
/**
279+
* Initialize settings.
280+
*
281+
* See [Builder] for code samples.
282+
*
283+
* @return [Builder]
284+
* */
285+
fun applyNotificationSettings(): Builder {
126286
ServiceNotification.initialize(serviceNotification)
127287
return builder
128288
}
129289

130290
}
131291

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+
* */
132298
fun build() {
133299
val torConfigFiles =
134300
if (::torConfigFiles.isInitialized) {
@@ -151,6 +317,9 @@ class TorServiceController private constructor() {
151317
}
152318
}
153319

320+
/**
321+
* Where everything needed to interact with [TorService] resides.
322+
* */
154323
companion object {
155324

156325
private lateinit var appContext: Context

topl-service/src/main/java/io/matthewnelson/topl_service/model/ServiceNotification.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,11 @@ import androidx.core.app.NotificationCompat.NotificationVisibility
1313
import io.matthewnelson.topl_service.R
1414
import io.matthewnelson.topl_service.service.TorService
1515

16+
/**
17+
* Everything to do with [TorService]'s notification.
18+
*
19+
* See [io.matthewnelson.topl_service.TorServiceController.Builder.NotificationBuilder]
20+
* */
1621
internal class ServiceNotification(
1722
private val channelName: String,
1823
private val channelID: String,

topl-service/src/main/java/io/matthewnelson/topl_service/onionproxy/OnionProxyEventBroadcaster.kt

Lines changed: 20 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,32 @@
11
package io.matthewnelson.topl_service.onionproxy
22

3-
import android.content.Context
43
import android.util.Log
54
import androidx.localbroadcastmanager.content.LocalBroadcastManager
65
import io.matthewnelson.topl_android.broadcaster.DefaultEventBroadcaster
7-
import io.matthewnelson.topl_android_settings.TorSettings
8-
9-
class OnionProxyEventBroadcaster(
10-
private val context: Context,
11-
private val torSettings: TorSettings
6+
import io.matthewnelson.topl_service.service.TorService
7+
import io.matthewnelson.topl_service_settings.TorServiceSettings
8+
9+
/**
10+
* [io.matthewnelson.topl_android.OnionProxyManager] utilizes this customized class for
11+
* broadcasting things while it is operating (such as Tor's State, operation errors,
12+
* debugging, etc).
13+
*
14+
* [OnionProxyEventListener] utilizes this class by sending it what Tor is spitting out
15+
* (selectively curated, ofc).
16+
*
17+
* @param [torService] [TorService] for context.
18+
* @param [torSettings] [TorServiceSettings]
19+
* */
20+
internal class OnionProxyEventBroadcaster(
21+
private val torService: TorService,
22+
private val torSettings: TorServiceSettings
1223
): DefaultEventBroadcaster(torSettings) {
1324

14-
private companion object {
15-
const val TAG = "EventBroadcaster"
25+
companion object {
26+
private const val TAG = "EventBroadcaster"
1627
}
1728

18-
private val broadcastManager = LocalBroadcastManager.getInstance(context)
29+
private val broadcastManager = LocalBroadcastManager.getInstance(torService.applicationContext)
1930

2031
override fun broadcastBandwidth(upload: Long, download: Long, written: Long, read: Long) {
2132
Log.d(TAG, "BANDWIDTH__upload: $upload, download: $download, written: $written, read: $read")

topl-service/src/main/java/io/matthewnelson/topl_service/onionproxy/OnionProxyEventListener.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,18 @@ import io.matthewnelson.topl_android.listener.BaseEventListener
55
import io.matthewnelson.topl_service.service.TorService
66
import net.freehaven.tor.control.TorControlCommands
77

8+
/**
9+
* [io.matthewnelson.topl_android.OnionProxyManager] registers this class in it's
10+
* [io.matthewnelson.topl_android.OnionProxyManager.start] method such that messages from
11+
* Tor get funneled here. They get modify as needed, then shipped to it's final destination.
12+
*
13+
* TODO: Decide whether or not I wish to ship it directly to
14+
* [io.matthewnelson.topl_service.TorServiceController.Companion] as a Flow so library
15+
* users can easily hook in, or if the EventBroadcaster should... (Might be too confusing).
16+
*
17+
* @param [torService] [TorService] for context.
18+
* @param [eventBroadcaster] [OnionProxyEventBroadcaster] for broadcasting data.
19+
* */
820
internal class OnionProxyEventListener(
921
private val torService: TorService,
1022
private val eventBroadcaster: OnionProxyEventBroadcaster

0 commit comments

Comments
 (0)