Advertisement
Guest User

Untitled

a guest
Nov 12th, 2018
159
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Kotlin 6.09 KB | None | 0 0
  1. class MainActivity : MvpAppCompatActivity(), MainView {
  2.     @InjectPresenter
  3.     lateinit var mMainPresenter: MainPresenter
  4.  
  5.     lateinit var coordinator: CoordinatorLayout
  6.     lateinit var fabButton: FloatingActionButton
  7.  
  8.     private val prefs = Injection.getProvider().prefs
  9.     private val cache = Injection.getProvider().cache
  10.  
  11.     private var dialogVpn: AlertDialog? = null
  12.    
  13.     override fun onCreate(savedInstanceState: Bundle?) {
  14.         super.onCreate(savedInstanceState)
  15.  
  16.         setContentView(R.layout.activity_main)
  17.         mMainPresenter.context = this
  18.         configViews()
  19.         configVpn()
  20.         configRules()
  21.     }
  22.  
  23.     private fun configVpn() {
  24.         prefs.putEnabled(true)
  25.         val alwaysOn = Settings.Secure.getString(contentResolver, "always_on_vpn_app")
  26.         Log.i(TAG, "Always-on=$alwaysOn")
  27.         if (!TextUtils.isEmpty(alwaysOn))
  28.             if (packageName == alwaysOn) {
  29.                 if (prefs.filter) {
  30.                     val lockdown = Settings.Secure.getInt(contentResolver, "always_on_vpn_lockdown", 0)
  31.                     Log.i(TAG, "Lockdown=$lockdown")
  32.                     if (lockdown != 0) {
  33.                         Toast.makeText(this@MainActivity, R.string.msg_always_on_lockdown, Toast.LENGTH_LONG).show()
  34.                         return
  35.                     }
  36.                 }
  37.             } else {
  38.                 Toast.makeText(this@MainActivity, R.string.msg_always_on, Toast.LENGTH_LONG).show()
  39.                 return
  40.             }
  41.  
  42.         var dns_mode: String? = Settings.Global.getString(contentResolver, "private_dns_mode")
  43.         if ("off" != dns_mode) {
  44.             Toast.makeText(this@MainActivity, R.string.msg_private_dns, Toast.LENGTH_LONG).show()
  45.             return
  46.         }
  47.  
  48.         try {
  49.             val prepare = VpnService.prepare(this@MainActivity)
  50.             if (prepare == null) {
  51.                 Log.i(TAG, "Prepare done")
  52.                 onActivityResult(REQUEST_VPN, Activity.RESULT_OK, null)
  53.             } else {
  54.                 // Show dialog
  55.                 val inflater = LayoutInflater.from(this@MainActivity)
  56.                 val view = inflater.inflate(R.layout.vpn, null, false)
  57.                 dialogVpn = AlertDialog.Builder(this@MainActivity)
  58.                         .setView(view)
  59.                         .setCancelable(false)
  60.                         .setPositiveButton(android.R.string.yes) { _, _ ->
  61.                             Log.i(TAG, "Start intent=$prepare")
  62.                             try {
  63.                                 // com.android.vpndialogs.ConfirmDialog required
  64.                                 startActivityForResult(prepare, REQUEST_VPN)
  65.                             } catch (ex: Throwable) {
  66.                                 logEx(TAG, ex)
  67.                                 onActivityResult(REQUEST_VPN, Activity.RESULT_CANCELED, null)
  68.                                 prefs.putEnabled(false)
  69.                             }
  70.                         }
  71.                         .setOnDismissListener { dialogVpn = null }
  72.                         .create()
  73.                 dialogVpn!!.show()
  74.             }
  75.         } catch (ex: Throwable) {
  76.             // Prepare failed
  77.             logEx(TAG, ex)
  78.             prefs.putEnabled(false)
  79.         }
  80.  
  81.  
  82.         GlobalScope.launch {
  83.             Rule.getRules(true, this@MainActivity, prefs, cache)
  84.         }.start()
  85.         ReceiverAutostart.upgrade(prefs.initialized, this@MainActivity, null)
  86.     }
  87.  
  88.     private fun configViews() {
  89.         coordinator = find(R.id.content)
  90.  
  91.         fabButton = find(R.id.fabButton)
  92.         fabButton.setOnClickListener {
  93.             if (find<ToggleButton>(R.id.tbMain).isChecked) {
  94.                              // TEST ON WATSAPP
  95.                 setupRules(listOf(Company("com.whatsapp", 0, "com.whatsapp", false)))
  96.             }
  97.         }
  98.     }
  99.  
  100.     private fun configRules() {
  101.         val allRules = Rule.getRules(true, this@MainActivity, prefs, cache)
  102.  
  103.         for(i in 0 until allRules.size) {
  104.             GlobalScope.launch {
  105.                 updateRule(AppEx.instance!!, allRules[i], true, allRules)
  106.             }.start()
  107.         }
  108.     }
  109.  
  110.     override fun onDestroy() {
  111.         dialogVpn?.dismiss()
  112.         dialogVpn = null
  113.        
  114.         super.onDestroy()
  115.     }
  116.  
  117.     override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
  118.         Log.i(TAG, "onActivityResult request=$requestCode result=$requestCode ok=${resultCode == Activity.RESULT_OK}")
  119.         Logger.logExtras(data)
  120.  
  121.         if (requestCode == REQUEST_VPN) {
  122.             // Handle VPN approval
  123.             prefs.putEnabled(resultCode == Activity.RESULT_OK)
  124.             if (resultCode == Activity.RESULT_OK) {
  125.                 VpnServiceExt.start(IVpnService.REASON_PREPARED, this)
  126.  
  127.                 val on = Toast.makeText(this@MainActivity, R.string.msg_on, Toast.LENGTH_LONG)
  128.                 on.setGravity(Gravity.CENTER, 0, 0)
  129.                 on.show()
  130.             } else if (resultCode == Activity.RESULT_CANCELED) {
  131.                 Toast.makeText(this, R.string.msg_vpn_cancelled, Toast.LENGTH_LONG).show()
  132.             }
  133.         } else if (requestCode == REQUEST_INVITE) {
  134.             // Do nothing
  135.         } else if (requestCode == REQUEST_LOGCAT) {
  136.             // Send logcat by e-mail
  137.             if (resultCode == Activity.RESULT_OK) {
  138.                 val target = if (data!!.hasExtra("org.openintents.extra.DIR_PATH")) {
  139.                     Uri.parse(data.data!!.toString() + "/logcat.txt")
  140.                 } else {
  141.                     data.data
  142.                 }
  143.                 Log.i(TAG, "Export URI=" + target!!)
  144.                 Util.sendLogcat(target, this)
  145.             }
  146.         } else {
  147.             Log.w(TAG, "Unknown activity result request=$requestCode")
  148.             super.onActivityResult(requestCode, resultCode, data)
  149.         }
  150.     }
  151.  
  152.     companion object {
  153.         private const val TAG = "MainActivity"
  154.  
  155.         private const val REQUEST_VPN = 1
  156.         private const val REQUEST_INVITE = 2
  157.         private const val REQUEST_LOGCAT = 3
  158.         const val REQUEST_ROAMING = 4
  159.     }
  160. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement