Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- public class ShakeActivity extends AppCompatActivity
- {
- private static final int VPN_REQUEST_CODE = 0x0F;
- private boolean waitingForVPNStart;
- private BroadcastReceiver vpnStateReceiver = new BroadcastReceiver()
- {
- @Override
- public void onReceive(Context context, Intent intent)
- {
- if (LocalVPNService.BROADCAST_VPN_STATE.equals(intent.getAction()))
- {
- if (intent.getBooleanExtra("Berjalan", false))
- waitingForVPNStart = false;
- }
- }
- };
- @Override
- protected void onCreate(Bundle savedInstanceState)
- {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.shake_vpn);
- final Button vpnButton = (Button)findViewById(R.id.vpn);
- vpnButton.setOnClickListener(new View.OnClickListener()
- {
- @Override
- public void onClick(View v)
- {
- startVPN();
- }
- });
- waitingForVPNStart = false;
- LocalBroadcastManager.getInstance(this).registerReceiver(vpnStateReceiver,
- new IntentFilter(LocalVPNService.BROADCAST_VPN_STATE));
- }
- private void startVPN()
- {
- Intent vpnIntent = VpnService.prepare(this);
- if (vpnIntent != null)
- startActivityForResult(vpnIntent, VPN_REQUEST_CODE);
- else
- onActivityResult(VPN_REQUEST_CODE, RESULT_OK, null);
- }
- @Override
- protected void onActivityResult(int requestCode, int resultCode, Intent data)
- {
- super.onActivityResult(requestCode, resultCode, data);
- if (requestCode == VPN_REQUEST_CODE && resultCode == RESULT_OK)
- {
- waitingForVPNStart = true;
- startService(new Intent(this, LocalVPNService.class));
- enableButton(false);
- }
- }
- @Override
- protected void onResume() {
- super.onResume();
- enableButton(!waitingForVPNStart && !LocalVPNService.isRunning());
- }
- private void enableButton(boolean enable)
- {
- final Button vpnButton = (Button) findViewById(R.id.vpn);
- if (enable)
- {
- vpnButton.setEnabled(true);
- vpnButton.setText(R.string.start_vpn);
- }
- else
- {
- vpnButton.setEnabled(false);
- vpnButton.setText(R.string.stop_vpn);
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement