Skip to content

Commit

Permalink
Converted activity to kt code & improved custom context syntax
Browse files Browse the repository at this point in the history
  • Loading branch information
cioccarellia committed Oct 23, 2019
1 parent e03fc23 commit 4cb8367
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 74 deletions.
1 change: 1 addition & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ dependencies {
implementation "org.jetbrains.kotlin:kotlin-stdlib-jdk7:$kotlin_version"
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
implementation "org.jetbrains.kotlinx:kotlinx-coroutines-android:1.3.2"
implementation 'com.android.support:design:28.0.0'
implementation "com.android.support:support-v4:28.0.0"
implementation project(path: ':library')
Expand Down

This file was deleted.

Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
package com.andreacioccarelli.billingprotectorsample

import android.annotation.SuppressLint
import android.os.Bundle
import android.app.Activity
import android.content.Context
import android.os.Vibrator
import android.support.design.widget.FloatingActionButton
import android.widget.TextView
import android.widget.Toast

import com.andreacioccarelli.billingprotector.*
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import java.util.concurrent.TimeUnit
import kotlin.coroutines.CoroutineContext
import kotlin.system.measureTimeMillis

class DetectionActivity : Activity() {

private lateinit var bp: BillingProtector

@SuppressLint("SetTextI18n")
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity)

bp = BillingProtector(this)
runRefresh()

val fab = findViewById<FloatingActionButton>(R.id.fab)
fab.setOnClickListener {
val vib = getSystemService(Context.VIBRATOR_SERVICE) as Vibrator
vib.vibrate(100)

runRefresh()
}
}

private fun runRefresh() = CoroutineScope(Dispatchers.Main).launch {
val millis = measureTimeMillis { updateData() }
val seconds = TimeUnit.MILLISECONDS.toSeconds(millis)

runOnUiThread {
Toast.makeText(this@DetectionActivity, "Re-computed. time: ${seconds}s", Toast.LENGTH_SHORT).show()
}
}

private var i = 0
@SuppressLint("SetTextI18n")
private suspend fun updateData() {
val mxp = findViewById<TextView>(R.id.mxp)
mxp.text = "i=${i++}\nisRootInstalled: ${bp.isRootInstalled()}\npirateAppsList: ${bp.getPirateAppsListAsync()}"
}
}
2 changes: 1 addition & 1 deletion app/src/main/res/layout/activity.xml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

<android.support.v7.widget.Toolbar
android:id="@+id/toolbar"
app:title="Java activity"
app:title="Kt activity"
android:layout_width="match_parent"
android:layout_height="?attr/actionBarSize"
android:background="?attr/colorPrimary"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import com.andreacioccarelli.billingprotector.data.PirateApp
import com.andreacioccarelli.billingprotector.data.SelectionCriteria
import com.andreacioccarelli.billingprotector.utils.RootUtils
import com.andreacioccarelli.billingprotector.utils.assembleAppList
import kotlinx.coroutines.CoroutineDispatcher
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
Expand All @@ -18,7 +19,8 @@ import kotlinx.coroutines.withContext
*/
class BillingProtector(
private val context: Context,
private val simulateSafeEnvironment: Boolean = false
private val simulateSafeEnvironment: Boolean = false,
private val coroutineContext: CoroutineDispatcher = Dispatchers.Default
) {
/**
* Lazily-evaluated and statically-generated pirate apps list
Expand Down Expand Up @@ -49,7 +51,7 @@ class BillingProtector(
* */
suspend fun getPirateAppsListAsync(): List<PirateApp> {
if (simulateSafeEnvironment) return emptyList()
return withContext(CoroutineScope(Dispatchers.Default).coroutineContext) { startScan() }
return withContext(coroutineContext) { startScan() }
}

/**
Expand Down

0 comments on commit 4cb8367

Please sign in to comment.