diff --git a/app/build.gradle b/app/build.gradle index 03afea0..8a00a28 100755 --- a/app/build.gradle +++ b/app/build.gradle @@ -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') diff --git a/app/src/main/java/com/andreacioccarelli/billingprotectorsample/DetectionActivity.java b/app/src/main/java/com/andreacioccarelli/billingprotectorsample/DetectionActivity.java deleted file mode 100755 index 252541f..0000000 --- a/app/src/main/java/com/andreacioccarelli/billingprotectorsample/DetectionActivity.java +++ /dev/null @@ -1,71 +0,0 @@ -package com.andreacioccarelli.billingprotectorsample; - -import android.annotation.SuppressLint; -import android.os.Bundle; -import android.app.Activity; -import android.os.Vibrator; -import android.support.design.widget.FloatingActionButton; -import android.view.View; -import android.widget.TextView; -import android.widget.Toast; - -import com.andreacioccarelli.billingprotector.*; -import java.util.concurrent.TimeUnit; - -public class DetectionActivity extends Activity { - - BillingProtector bp; - private int i = 0; - - @SuppressLint("SetTextI18n") - @Override - protected void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - setContentView(R.layout.activity); - - bp = new BillingProtector(this, false); - runRefresh(); - - FloatingActionButton fab = findViewById(R.id.fab); - fab.setOnClickListener(new View.OnClickListener() { - @Override - public void onClick(View v) { - Vibrator vib = (Vibrator) getSystemService(VIBRATOR_SERVICE); - vib.vibrate(100); - - runRefresh(); - } - }); - } - - void runRefresh() { - new Thread(new Runnable() { - @Override - public void run() { - long mills = System.currentTimeMillis(); - updateData(); - - long millsAfter = System.currentTimeMillis(); - long diff = millsAfter - mills; - final long seconds = TimeUnit.MILLISECONDS.toSeconds(diff); - - runOnUiThread(new Runnable() { - @Override - public void run() { - Toast.makeText(DetectionActivity.this, "Re-computed. time: " + seconds + "s", Toast.LENGTH_SHORT).show(); - } - }); - } - }).run(); - } - - @SuppressLint("SetTextI18n") - void updateData() { - final TextView mxp = findViewById(R.id.mxp); - mxp.setText( - "i=" + i++ + - "isRootInstalled: " + bp.isRootInstalled() + - "\n\npirateAppsList: " + bp.getPirateAppsList() - ); - } -} diff --git a/app/src/main/java/com/andreacioccarelli/billingprotectorsample/DetectionActivity.kt b/app/src/main/java/com/andreacioccarelli/billingprotectorsample/DetectionActivity.kt new file mode 100755 index 0000000..4ee899c --- /dev/null +++ b/app/src/main/java/com/andreacioccarelli/billingprotectorsample/DetectionActivity.kt @@ -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(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(R.id.mxp) + mxp.text = "i=${i++}\nisRootInstalled: ${bp.isRootInstalled()}\npirateAppsList: ${bp.getPirateAppsListAsync()}" + } +} diff --git a/app/src/main/res/layout/activity.xml b/app/src/main/res/layout/activity.xml index 9effebc..3873e4c 100755 --- a/app/src/main/res/layout/activity.xml +++ b/app/src/main/res/layout/activity.xml @@ -14,7 +14,7 @@ { if (simulateSafeEnvironment) return emptyList() - return withContext(CoroutineScope(Dispatchers.Default).coroutineContext) { startScan() } + return withContext(coroutineContext) { startScan() } } /**