Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- class MainActivity : MvpAppCompatActivity(), MainView {
- @InjectPresenter
- lateinit var mMainPresenter: MainPresenter
- lateinit var coordinator: CoordinatorLayout
- lateinit var fabButton: FloatingActionButton
- private val prefs = Injection.getProvider().prefs
- private val cache = Injection.getProvider().cache
- private var dialogVpn: AlertDialog? = null
- override fun onCreate(savedInstanceState: Bundle?) {
- super.onCreate(savedInstanceState)
- setContentView(R.layout.activity_main)
- mMainPresenter.context = this
- configViews()
- configVpn()
- configRules()
- }
- private fun configVpn() {
- prefs.putEnabled(true)
- val alwaysOn = Settings.Secure.getString(contentResolver, "always_on_vpn_app")
- Log.i(TAG, "Always-on=$alwaysOn")
- if (!TextUtils.isEmpty(alwaysOn))
- if (packageName == alwaysOn) {
- if (prefs.filter) {
- val lockdown = Settings.Secure.getInt(contentResolver, "always_on_vpn_lockdown", 0)
- Log.i(TAG, "Lockdown=$lockdown")
- if (lockdown != 0) {
- Toast.makeText(this@MainActivity, R.string.msg_always_on_lockdown, Toast.LENGTH_LONG).show()
- return
- }
- }
- } else {
- Toast.makeText(this@MainActivity, R.string.msg_always_on, Toast.LENGTH_LONG).show()
- return
- }
- var dns_mode: String? = Settings.Global.getString(contentResolver, "private_dns_mode")
- if ("off" != dns_mode) {
- Toast.makeText(this@MainActivity, R.string.msg_private_dns, Toast.LENGTH_LONG).show()
- return
- }
- try {
- val prepare = VpnService.prepare(this@MainActivity)
- if (prepare == null) {
- Log.i(TAG, "Prepare done")
- onActivityResult(REQUEST_VPN, Activity.RESULT_OK, null)
- } else {
- // Show dialog
- val inflater = LayoutInflater.from(this@MainActivity)
- val view = inflater.inflate(R.layout.vpn, null, false)
- dialogVpn = AlertDialog.Builder(this@MainActivity)
- .setView(view)
- .setCancelable(false)
- .setPositiveButton(android.R.string.yes) { _, _ ->
- Log.i(TAG, "Start intent=$prepare")
- try {
- // com.android.vpndialogs.ConfirmDialog required
- startActivityForResult(prepare, REQUEST_VPN)
- } catch (ex: Throwable) {
- logEx(TAG, ex)
- onActivityResult(REQUEST_VPN, Activity.RESULT_CANCELED, null)
- prefs.putEnabled(false)
- }
- }
- .setOnDismissListener { dialogVpn = null }
- .create()
- dialogVpn!!.show()
- }
- } catch (ex: Throwable) {
- // Prepare failed
- logEx(TAG, ex)
- prefs.putEnabled(false)
- }
- GlobalScope.launch {
- Rule.getRules(true, this@MainActivity, prefs, cache)
- }.start()
- ReceiverAutostart.upgrade(prefs.initialized, this@MainActivity, null)
- }
- private fun configViews() {
- coordinator = find(R.id.content)
- fabButton = find(R.id.fabButton)
- fabButton.setOnClickListener {
- if (find<ToggleButton>(R.id.tbMain).isChecked) {
- // TEST ON WATSAPP
- setupRules(listOf(Company("com.whatsapp", 0, "com.whatsapp", false)))
- }
- }
- }
- private fun configRules() {
- val allRules = Rule.getRules(true, this@MainActivity, prefs, cache)
- for(i in 0 until allRules.size) {
- GlobalScope.launch {
- updateRule(AppEx.instance!!, allRules[i], true, allRules)
- }.start()
- }
- }
- override fun onDestroy() {
- dialogVpn?.dismiss()
- dialogVpn = null
- super.onDestroy()
- }
- override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
- Log.i(TAG, "onActivityResult request=$requestCode result=$requestCode ok=${resultCode == Activity.RESULT_OK}")
- Logger.logExtras(data)
- if (requestCode == REQUEST_VPN) {
- // Handle VPN approval
- prefs.putEnabled(resultCode == Activity.RESULT_OK)
- if (resultCode == Activity.RESULT_OK) {
- VpnServiceExt.start(IVpnService.REASON_PREPARED, this)
- val on = Toast.makeText(this@MainActivity, R.string.msg_on, Toast.LENGTH_LONG)
- on.setGravity(Gravity.CENTER, 0, 0)
- on.show()
- } else if (resultCode == Activity.RESULT_CANCELED) {
- Toast.makeText(this, R.string.msg_vpn_cancelled, Toast.LENGTH_LONG).show()
- }
- } else if (requestCode == REQUEST_INVITE) {
- // Do nothing
- } else if (requestCode == REQUEST_LOGCAT) {
- // Send logcat by e-mail
- if (resultCode == Activity.RESULT_OK) {
- val target = if (data!!.hasExtra("org.openintents.extra.DIR_PATH")) {
- Uri.parse(data.data!!.toString() + "/logcat.txt")
- } else {
- data.data
- }
- Log.i(TAG, "Export URI=" + target!!)
- Util.sendLogcat(target, this)
- }
- } else {
- Log.w(TAG, "Unknown activity result request=$requestCode")
- super.onActivityResult(requestCode, resultCode, data)
- }
- }
- companion object {
- private const val TAG = "MainActivity"
- private const val REQUEST_VPN = 1
- private const val REQUEST_INVITE = 2
- private const val REQUEST_LOGCAT = 3
- const val REQUEST_ROAMING = 4
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement