That fuck shit the fascists are using
at master 123 lines 3.5 kB view raw
1package org.tm.archive.service 2 3import android.content.ComponentName 4import android.content.Context 5import android.content.Intent 6import android.content.ServiceConnection 7import android.os.IBinder 8import org.signal.core.util.logging.Log 9import org.tm.archive.jobs.UnableToStartException 10import org.tm.archive.service.GenericForegroundService.Companion.stopForegroundTask 11import org.tm.archive.service.GenericForegroundService.LocalBinder 12import java.util.concurrent.atomic.AtomicReference 13import java.util.concurrent.locks.ReentrantLock 14import kotlin.concurrent.withLock 15 16class NotificationController internal constructor(private val context: Context, val id: Int) : AutoCloseable, ServiceConnection { 17 private val service = AtomicReference<GenericForegroundService?>() 18 private val lock = ReentrantLock() 19 20 private var progress = 0 21 private var progressMax = 0 22 private var indeterminate = false 23 private var percent: Int = -1 24 private var isBound: Boolean 25 26 companion object { 27 private val TAG = Log.tag(NotificationController::class.java) 28 } 29 30 init { 31 isBound = bindToService() 32 } 33 34 override fun onServiceConnected(name: ComponentName, service: IBinder) { 35 Log.i(TAG, "[onServiceConnected] Name: $name") 36 37 val binder = service as LocalBinder 38 val genericForegroundService = binder.service 39 40 this.service.set(genericForegroundService) 41 42 lock.withLock { 43 updateProgressOnService() 44 } 45 } 46 47 override fun onServiceDisconnected(name: ComponentName) { 48 Log.i(TAG, "[onServiceDisconnected] Name: $name") 49 service.set(null) 50 } 51 52 override fun close() { 53 try { 54 if (isBound) { 55 Log.d(TAG, "[close] Unbinding service.") 56 context.unbindService(this) 57 isBound = false 58 } else { 59 Log.w(TAG, "[close] Service was not bound at the time of close()...") 60 } 61 stopForegroundTask(context, id) 62 } catch (e: IllegalStateException) { 63 Log.w(TAG, "[close] Failed to unbind service...", e) 64 } catch (e: UnableToStartException) { 65 Log.w(TAG, "[close] Failed to unbind service...", e) 66 } 67 } 68 69 fun setIndeterminateProgress() { 70 lock.withLock { 71 setProgress( 72 newProgressMax = 0, 73 newProgress = 0, 74 indeterminant = true 75 ) 76 } 77 } 78 79 fun setProgress(newProgressMax: Long, newProgress: Long) { 80 lock.withLock { 81 setProgress( 82 newProgressMax = newProgressMax.toInt(), 83 newProgress = newProgress.toInt(), 84 indeterminant = false 85 ) 86 } 87 } 88 89 fun replaceTitle(title: String) { 90 lock.withLock { 91 service.get()?.replaceTitle(id, title) 92 ?: Log.w(TAG, "Tried to update the title, but the service was no longer bound!") 93 } 94 } 95 96 private fun bindToService(): Boolean { 97 return context.bindService(Intent(context, GenericForegroundService::class.java), this, Context.BIND_AUTO_CREATE) 98 } 99 100 private fun setProgress(newProgressMax: Int, newProgress: Int, indeterminant: Boolean) { 101 val newPercent = if (newProgressMax != 0) { 102 100 * newProgress / newProgressMax 103 } else { 104 -1 105 } 106 107 val same = newPercent == percent && indeterminate == indeterminant 108 109 percent = newPercent 110 progress = newProgress 111 progressMax = newProgressMax 112 indeterminate = indeterminant 113 114 if (!same) { 115 updateProgressOnService() 116 } 117 } 118 119 private fun updateProgressOnService() { 120 service.get()?.replaceProgress(id, progressMax, progress, indeterminate) 121 ?: Log.w(TAG, "Tried to update the progress, but the service was no longer bound!") 122 } 123}