Advertisement
BimoSora

Untitled

Jan 26th, 2020
235
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.42 KB | None | 0 0
  1. public class ShakeActivity extends AppCompatActivity
  2. {
  3. private static final int VPN_REQUEST_CODE = 0x0F;
  4.  
  5. private boolean waitingForVPNStart;
  6.  
  7. private BroadcastReceiver vpnStateReceiver = new BroadcastReceiver()
  8. {
  9. @Override
  10. public void onReceive(Context context, Intent intent)
  11. {
  12. if (LocalVPNService.BROADCAST_VPN_STATE.equals(intent.getAction()))
  13. {
  14. if (intent.getBooleanExtra("Berjalan", false))
  15. waitingForVPNStart = false;
  16. }
  17. }
  18. };
  19.  
  20. @Override
  21. protected void onCreate(Bundle savedInstanceState)
  22. {
  23. super.onCreate(savedInstanceState);
  24. setContentView(R.layout.shake_vpn);
  25.  
  26. final Button vpnButton = (Button)findViewById(R.id.vpn);
  27. vpnButton.setOnClickListener(new View.OnClickListener()
  28. {
  29. @Override
  30. public void onClick(View v)
  31. {
  32. startVPN();
  33. }
  34. });
  35. waitingForVPNStart = false;
  36. LocalBroadcastManager.getInstance(this).registerReceiver(vpnStateReceiver,
  37. new IntentFilter(LocalVPNService.BROADCAST_VPN_STATE));
  38. }
  39.  
  40. private void startVPN()
  41. {
  42. Intent vpnIntent = VpnService.prepare(this);
  43. if (vpnIntent != null)
  44. startActivityForResult(vpnIntent, VPN_REQUEST_CODE);
  45. else
  46. onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
  47. }
  48.  
  49. @Override
  50. protected void onActivityResult(int requestCode, int resultCode, Intent data)
  51. {
  52. super.onActivityResult(requestCode, resultCode, data);
  53. if (requestCode == VPN_REQUEST_CODE && resultCode == RESULT_OK)
  54. {
  55. waitingForVPNStart = true;
  56. startService(new Intent(this, LocalVPNService.class));
  57. enableButton(false);
  58. }
  59. }
  60.  
  61. @Override
  62. protected void onResume() {
  63. super.onResume();
  64.  
  65. enableButton(!waitingForVPNStart && !LocalVPNService.isRunning());
  66. }
  67.  
  68. private void enableButton(boolean enable)
  69. {
  70. final Button vpnButton = (Button) findViewById(R.id.vpn);
  71. if (enable)
  72. {
  73. vpnButton.setEnabled(true);
  74. vpnButton.setText(R.string.start_vpn);
  75. }
  76. else
  77. {
  78. vpnButton.setEnabled(false);
  79. vpnButton.setText(R.string.stop_vpn);
  80. }
  81. }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement