Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //"..." means redacted
- class Service : Service() {
- private val checker by lazy {
- piracyChecker {
- enableSigningCertificate("...")
- enableGooglePlayLicensing("...")
- callback {
- doNotAllow { error, _ ->
- sendResult(false, "PiracyChecker doNotAllow: $error")
- }
- onError {
- sendResult(false, "PiracyChecker onError: $it")
- }
- allow {
- sendResult(true, "PiracyChecker valid")
- }
- }
- }
- }
- private val prefs by lazy { PreferenceManager.getDefaultSharedPreferences(this) }
- override fun onBind(intent: Intent?): IBinder? {
- return null
- }
- override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
- if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
- val nm = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
- nm.createNotificationChannel(
- NotificationChannel("...", resources.getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW))
- nm.getNotificationChannel("...").importance = NotificationManager.IMPORTANCE_LOW
- }
- startForeground(100, NotificationCompat.Builder(this, "...")
- .setContentTitle(resources.getText(R.string.notif_text))
- .setSmallIcon(R.mipmap.ic_launcher)
- .setPriority(NotificationCompat.PRIORITY_LOW)
- .build())
- checkOnline {
- if (it) {
- clearDate()
- checker.start()
- } else {
- logDate()
- if (tooLongWithoutNet()) sendResult(false, "User has been offline for more than 5 days")
- else sendResult(savedStatus(), "User is offline, but within the 5-day limit; sending saved status")
- }
- }
- return super.onStartCommand(intent, flags, startId)
- }
- override fun onDestroy() {
- super.onDestroy()
- stopForeground(true)
- checker.destroy()
- }
- private fun sendResult(valid: Boolean, reason: String) {
- saveStatus(valid)
- val intent = Intent("...")
- intent.putExtra("valid", valid)
- intent.putExtra("msg", reason)
- sendBroadcast(intent)
- stopSelf()
- }
- private fun checkOnline(action: (online: Boolean) -> Unit) {
- Thread {
- action.invoke(isOnlineAsync())
- }.start()
- }
- private fun isOnlineAsync(): Boolean {
- try {
- return Runtime.getRuntime().exec("ping -c 3 8.8.8.8").waitFor() == 0
- || Runtime.getRuntime().exec("ping -c 3 208.67.222.222").waitFor() == 0
- } catch (e: IOException) {
- } catch (e: InterruptedException) {}
- return false
- }
- private fun tooLongWithoutNet(): Boolean {
- val date = prefs.getLong("offline_date", -1)
- return if (date == -1L) false
- else TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - date) > 5L
- }
- private fun logDate() {
- if (!prefs.contains("offline_date")) {
- prefs.edit().putLong("offline_date", System.currentTimeMillis()).apply()
- }
- }
- private fun clearDate() = prefs.edit().remove("offline_date").apply()
- private fun savedStatus(): Boolean = prefs.getBoolean("prem", false)
- private fun saveStatus(valid: Boolean) = prefs.edit().putBoolean("prem", valid).apply()
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement