-
-
Notifications
You must be signed in to change notification settings - Fork 30
/
IndicatorService.kt
412 lines (365 loc) 路 15.3 KB
/
IndicatorService.kt
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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
package com.nitish.privacyindicator.services
import android.Manifest
import android.accessibilityservice.AccessibilityService
import android.app.NotificationChannel
import android.app.NotificationManager
import android.app.PendingIntent
import android.content.ComponentName
import android.content.Intent
import android.content.pm.PackageManager
import android.graphics.Color
import android.graphics.PixelFormat
import android.hardware.camera2.CameraManager
import android.hardware.camera2.CameraManager.AvailabilityCallback
import android.location.GnssStatus
import android.location.LocationListener
import android.location.LocationManager
import android.media.AudioManager
import android.media.AudioManager.AudioRecordingCallback
import android.media.AudioRecordingConfiguration
import android.os.Build
import android.os.VibrationEffect
import android.os.Vibrator
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.WindowManager
import android.view.accessibility.AccessibilityEvent
import android.view.animation.Animation
import android.view.animation.ScaleAnimation
import androidx.core.app.ActivityCompat
import androidx.core.app.NotificationCompat
import androidx.core.app.NotificationManagerCompat
import com.nitish.privacyindicator.BuildConfig
import com.nitish.privacyindicator.R
import com.nitish.privacyindicator.databinding.IndicatorsLayoutBinding
import com.nitish.privacyindicator.db.AccessLogsDatabase
import com.nitish.privacyindicator.helpers.*
import com.nitish.privacyindicator.models.AccessLog
import com.nitish.privacyindicator.models.IndicatorType
import com.nitish.privacyindicator.repository.AccessLogsRepo
import com.nitish.privacyindicator.repository.SharedPrefManager
import com.nitish.privacyindicator.ui.home.HomeActivity
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.GlobalScope
import kotlinx.coroutines.launch
class IndicatorService : AccessibilityService() {
private lateinit var binding: IndicatorsLayoutBinding
private var cameraManager: CameraManager? = null
private var cameraCallback: AvailabilityCallback? = null
private var locationManager: LocationManager? = null
private var locationCallback: GnssStatus.Callback? = null
private var audioManager: AudioManager? = null
private var micCallback: AudioRecordingCallback? = null
private lateinit var sharedPrefManager: SharedPrefManager
private lateinit var layoutParams: WindowManager.LayoutParams
private lateinit var windowManager: WindowManager
private lateinit var accessLogsRepo: AccessLogsRepo
private val notification_channel_id = "PRIVACY_INDICATORS_NOTIFICATION"
private var notifManager: NotificationManagerCompat? = null
private var notificationBuilder: NotificationCompat.Builder? = null
private val notificationID = 256
private var isCameraOn = false
private var isMicOn = false
private var isLocationOn = false
private var currentAppId = BuildConfig.APPLICATION_ID
override fun onCreate() {
super.onCreate()
fetchData()
}
override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
return super.onStartCommand(intent, flags, startId)
}
override fun onServiceConnected() {
createOverlay()
setUpInnerViews()
startCallBacks()
}
private fun fetchData() {
sharedPrefManager = SharedPrefManager.getInstance(applicationContext)
accessLogsRepo = AccessLogsRepo(AccessLogsDatabase(this))
}
private fun startCallBacks() {
if (cameraManager == null) cameraManager = getSystemService(CAMERA_SERVICE) as CameraManager
cameraManager!!.registerAvailabilityCallback(getCameraCallback(), null)
if (audioManager == null) audioManager = getSystemService(AUDIO_SERVICE) as AudioManager
audioManager!!.registerAudioRecordingCallback(getMicCallback(), null)
registerLocationCallback()
}
//This feature is EXPERIMENTAL
private fun registerLocationCallback() {
/*if (ActivityCompat.checkSelfPermission(this, Manifest.permission.ACCESS_FINE_LOCATION) == PackageManager.PERMISSION_GRANTED) {
if(locationManager==null) locationManager = getSystemService(LOCATION_SERVICE) as LocationManager
locationManager!!.registerGnssStatusCallback(getLocationCallback())
val locationListener = LocationListener { }
locationManager!!.requestLocationUpdates(LocationManager.GPS_PROVIDER, 100, 8.4f,locationListener)
locationManager!!.removeUpdates(locationListener)
}else{
sharedPrefManager.isLocationEnabled = false
}*/
}
private fun getCameraCallback(): AvailabilityCallback {
cameraCallback = object : AvailabilityCallback() {
override fun onCameraAvailable(cameraId: String) {
super.onCameraAvailable(cameraId)
isCameraOn = false
hideCam()
dismissNotification()
}
override fun onCameraUnavailable(cameraId: String) {
super.onCameraUnavailable(cameraId)
isCameraOn = true
showCam()
triggerVibration()
showNotification()
}
}
return cameraCallback as AvailabilityCallback
}
private fun getMicCallback(): AudioRecordingCallback {
micCallback = object : AudioRecordingCallback() {
override fun onRecordingConfigChanged(configs: List<AudioRecordingConfiguration>) {
if (configs.size > 0) {
isMicOn = true
showMic()
triggerVibration()
showNotification()
} else {
isMicOn = false
hideMic()
dismissNotification()
}
}
}
return micCallback as AudioRecordingCallback
}
private fun getLocationCallback(): GnssStatus.Callback {
locationCallback = object : GnssStatus.Callback() {
override fun onStarted() {
super.onStarted()
isLocationOn = true
showLocation()
triggerVibration()
}
override fun onStopped() {
super.onStopped()
isLocationOn = false
hideLocation()
}
}
return locationCallback as GnssStatus.Callback
}
private fun triggerVibration() {
if (sharedPrefManager.isVibrationEnabled) {
val v = getSystemService(VIBRATOR_SERVICE) as Vibrator
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
v.vibrate(VibrationEffect.createOneShot(500, VibrationEffect.DEFAULT_AMPLITUDE))
} else {
//deprecated in API 26
v.vibrate(500)
}
}
}
private fun setUpInnerViews() {
setViewColors()
showInitialAnimation(true)
}
private fun showInitialAnimation(isEnabled: Boolean) {
val delay = if (isEnabled) 1000 else 0
binding.ivLoc.postDelayed({
binding.ivLoc.visibility = View.GONE
binding.ivCam.visibility = View.GONE
binding.ivMic.visibility = View.GONE
}, delay.toLong())
}
private fun createOverlay() {
windowManager = getSystemService(WINDOW_SERVICE) as WindowManager
layoutParams = WindowManager.LayoutParams()
layoutParams.apply {
type = WindowManager.LayoutParams.TYPE_ACCESSIBILITY_OVERLAY
format = PixelFormat.TRANSLUCENT
flags = WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
width = WindowManager.LayoutParams.WRAP_CONTENT
height = WindowManager.LayoutParams.WRAP_CONTENT
gravity = layoutGravity
}
binding = IndicatorsLayoutBinding.inflate(LayoutInflater.from(this))
windowManager.addView(binding.root, layoutParams)
}
private fun updateLayoutGravity() {
layoutParams.gravity = layoutGravity
windowManager.updateViewLayout(binding.root, layoutParams)
}
private val layoutGravity: Int
get() = sharedPrefManager.indicatorPosition.layoutGravity
private fun makeLog(indicatorType: IndicatorType) {
if(isLogEligible(currentAppId)){
val log = AccessLog(System.currentTimeMillis(), currentAppId, getAppName(currentAppId), indicatorType)
GlobalScope.launch(Dispatchers.IO) {
accessLogsRepo.save(log)
}
}
}
private fun getAppName(packageName: String): String {
val packageManager = applicationContext.packageManager
return try{
packageManager.getApplicationLabel(packageManager.getApplicationInfo(packageName, PackageManager.GET_META_DATA)) as String
}catch (exp: PackageManager.NameNotFoundException){
packageName
}
}
private fun isLogEligible(currentAppId: String): Boolean {
return currentAppId != BuildConfig.APPLICATION_ID
}
private fun showMic() {
if (sharedPrefManager.isMicIndicatorEnabled) {
updateIndicatorProperties()
binding.ivMic.visibility = View.VISIBLE
makeLog(IndicatorType.MICROPHONE)
}
}
private fun hideMic() {
binding.ivMic.visibility = View.GONE
}
private fun showCam() {
if (sharedPrefManager.isCameraIndicatorEnabled) {
updateIndicatorProperties()
binding.ivCam.visibility = View.VISIBLE
makeLog(IndicatorType.CAMERA)
}
}
private fun hideCam() {
binding.ivCam.visibility = View.GONE
}
private fun showLocation() {
if (sharedPrefManager.isLocationEnabled) {
updateIndicatorProperties()
binding.ivLoc.visibility = View.VISIBLE
makeLog(IndicatorType.LOCATION)
}
}
private fun hideLocation() {
binding.ivLoc.visibility = View.GONE
}
private fun updateIndicatorProperties() {
updateLayoutGravity()
updateIndicatorsSize()
updateIndicatorsOpacity()
setViewColors()
}
private fun setViewColors() {
val dotsTint = sharedPrefManager.indicatorColor
val indicatorBackground = sharedPrefManager.indicatorBackgroundColor
binding.ivCam.setViewTint(dotsTint)
binding.ivMic.setViewTint(dotsTint)
binding.ivLoc.setViewTint(dotsTint)
binding.llBackground.setBackgroundColor(Color.parseColor(indicatorBackground))
}
private fun updateIndicatorsOpacity() {
binding.root.updateOpacity(sharedPrefManager.indicatorOpacity.opacity)
}
private fun updateIndicatorsSize() {
val size = sharedPrefManager.indicatorSize.size
binding.cvIndicators.radius = (size / 2).toFloat()
binding.ivCam.updateSize(size)
binding.ivMic.updateSize(size)
binding.ivLoc.updateSize(size)
}
fun upScaleView(view: View) {
val fade_in = ScaleAnimation(0f, 1f, 0f, 1f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
fade_in.duration = 350
fade_in.fillAfter = true
view.startAnimation(fade_in)
}
fun downScaleView(view: View) {
val fade_in = ScaleAnimation(1f, 0f, 1f, 0f, Animation.RELATIVE_TO_SELF, 0.5f, Animation.RELATIVE_TO_SELF, 0.5f)
fade_in.duration = 350
fade_in.fillAfter = true
view.startAnimation(fade_in)
}
private fun setupNotification() {
createNotificationChannel()
notificationBuilder = NotificationCompat.Builder(applicationContext, notification_channel_id)
.setSmallIcon(R.drawable.camera_indicator2)
.setContentTitle(notificationTitle)
.setContentText(notificationDescription)
.setContentIntent(pendingIntent)
.setOngoing(true)
.setPriority(NotificationCompat.PRIORITY_HIGH)
notifManager = NotificationManagerCompat.from(applicationContext)
}
private val notificationTitle: String
get() {
if (isCameraOn && isMicOn) return "Your Camera and Mic is ON"
if (isCameraOn && !isMicOn) return "Your Camera is ON"
return if (!isCameraOn && isMicOn) "Your MIC is ON" else "Your Camera or Mic is ON"
}
private val notificationDescription: String
get() {
if (isCameraOn && isMicOn) return "A third-party app is using your Camera and Microphone"
if (isCameraOn && !isMicOn) return "A third-party app is using your Camera"
return if (!isCameraOn && isMicOn) "A third-party app is using your Microphone" else "A third-party app is using your Camera or Microphone"
}
private fun showNotification() {
if (sharedPrefManager.isNotificationEnabled) {
setupNotification()
if (notifManager != null) notifManager!!.notify(notificationID, notificationBuilder!!.build())
}
}
private fun dismissNotification() {
if (isCameraOn || isMicOn) {
showNotification()
} else {
if (notifManager != null) notifManager!!.cancel(notificationID)
}
}
private val pendingIntent: PendingIntent
get() {
val intent = Intent(applicationContext, HomeActivity::class.java)
return PendingIntent.getActivity(applicationContext, 1, intent, PendingIntent.FLAG_UPDATE_CURRENT)
}
private fun createNotificationChannel() {
val notificationChannel = "Notifications for Privacy Indicators"
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
val importance = NotificationManager.IMPORTANCE_LOW
val channel = NotificationChannel(notification_channel_id, notificationChannel, importance)
val description = getString(R.string.notification_alert_summary)
channel.description = description
channel.lightColor = Color.RED
val notificationManager = applicationContext.getSystemService(NOTIFICATION_SERVICE) as NotificationManager
notificationManager.createNotificationChannel(channel)
}
}
override fun onInterrupt() {}
override fun onAccessibilityEvent(accessibilityEvent: AccessibilityEvent) {
if(accessibilityEvent.eventType == AccessibilityEvent.TYPE_WINDOW_STATE_CHANGED
&& accessibilityEvent.packageName != null){
val componentName = ComponentName(accessibilityEvent.packageName.toString(), accessibilityEvent.className.toString())
currentAppId = componentName.packageName
}
}
private fun unRegisterCameraCallBack() {
if (cameraManager != null
&& cameraCallback != null) {
cameraManager!!.unregisterAvailabilityCallback(cameraCallback!!)
}
}
private fun unRegisterMicCallback() {
if (audioManager != null
&& micCallback != null) {
audioManager!!.unregisterAudioRecordingCallback(micCallback!!)
}
}
private fun unRegisterLocationCallback() {
if (locationManager != null
&& locationCallback != null) {
locationManager!!.unregisterGnssStatusCallback(locationCallback!!)
}
}
override fun onDestroy() {
unRegisterCameraCallBack()
unRegisterMicCallback()
unRegisterLocationCallback()
super.onDestroy()
}
}