Advertisement
Zacharee1

Untitled

Nov 7th, 2018
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 3.56 KB | None | 0 0
  1. //"..." means redacted
  2.  
  3. class Service : Service() {
  4.     private val checker by lazy {
  5.         piracyChecker {
  6.             enableSigningCertificate("...")
  7.             enableGooglePlayLicensing("...")
  8.  
  9.             callback {
  10.                 doNotAllow { error, _ ->
  11.                     sendResult(false, "PiracyChecker doNotAllow: $error")
  12.                 }
  13.  
  14.                 onError {
  15.                     sendResult(false, "PiracyChecker onError: $it")
  16.                 }
  17.  
  18.                 allow {
  19.                     sendResult(true, "PiracyChecker valid")
  20.                 }
  21.             }
  22.         }
  23.     }
  24.     private val prefs by lazy { PreferenceManager.getDefaultSharedPreferences(this) }
  25.  
  26.     override fun onBind(intent: Intent?): IBinder? {
  27.         return null
  28.     }
  29.  
  30.     override fun onStartCommand(intent: Intent?, flags: Int, startId: Int): Int {
  31.         if (Build.VERSION.SDK_INT > Build.VERSION_CODES.N_MR1) {
  32.             val nm = getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager
  33.             nm.createNotificationChannel(
  34.                     NotificationChannel("...", resources.getString(R.string.app_name), NotificationManager.IMPORTANCE_LOW))
  35.             nm.getNotificationChannel("...").importance = NotificationManager.IMPORTANCE_LOW
  36.         }
  37.  
  38.         startForeground(100, NotificationCompat.Builder(this, "...")
  39.                 .setContentTitle(resources.getText(R.string.notif_text))
  40.                 .setSmallIcon(R.mipmap.ic_launcher)
  41.                 .setPriority(NotificationCompat.PRIORITY_LOW)
  42.                 .build())
  43.  
  44.         checkOnline {
  45.             if (it) {
  46.                 clearDate()
  47.                 checker.start()
  48.             } else {
  49.                 logDate()
  50.                 if (tooLongWithoutNet()) sendResult(false, "User has been offline for more than 5 days")
  51.                 else sendResult(savedStatus(), "User is offline, but within the 5-day limit; sending saved status")
  52.             }
  53.         }
  54.  
  55.         return super.onStartCommand(intent, flags, startId)
  56.     }
  57.  
  58.     override fun onDestroy() {
  59.         super.onDestroy()
  60.  
  61.         stopForeground(true)
  62.         checker.destroy()
  63.     }
  64.  
  65.     private fun sendResult(valid: Boolean, reason: String) {
  66.         saveStatus(valid)
  67.  
  68.         val intent = Intent("...")
  69.         intent.putExtra("valid", valid)
  70.         intent.putExtra("msg", reason)
  71.  
  72.         sendBroadcast(intent)
  73.  
  74.         stopSelf()
  75.     }
  76.  
  77.     private fun checkOnline(action: (online: Boolean) -> Unit) {
  78.         Thread {
  79.             action.invoke(isOnlineAsync())
  80.         }.start()
  81.     }
  82.  
  83.     private fun isOnlineAsync(): Boolean {
  84.         try {
  85.             return Runtime.getRuntime().exec("ping -c 3 8.8.8.8").waitFor() == 0
  86.                     || Runtime.getRuntime().exec("ping -c 3 208.67.222.222").waitFor() == 0
  87.         } catch (e: IOException) {
  88.         } catch (e: InterruptedException) {}
  89.  
  90.         return false
  91.     }
  92.  
  93.     private fun tooLongWithoutNet(): Boolean {
  94.         val date = prefs.getLong("offline_date", -1)
  95.         return if (date == -1L) false
  96.         else TimeUnit.MILLISECONDS.toDays(System.currentTimeMillis() - date) > 5L
  97.     }
  98.  
  99.     private fun logDate() {
  100.         if (!prefs.contains("offline_date")) {
  101.             prefs.edit().putLong("offline_date", System.currentTimeMillis()).apply()
  102.         }
  103.     }
  104.  
  105.     private fun clearDate() = prefs.edit().remove("offline_date").apply()
  106.  
  107.     private fun savedStatus(): Boolean = prefs.getBoolean("prem", false)
  108.  
  109.     private fun saveStatus(valid: Boolean) = prefs.edit().putBoolean("prem", valid).apply()
  110. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement