1 /* 2 * Copyright (C) 2017 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.cts.vpnfirewall; 18 19 import android.app.Activity; 20 import android.content.Intent; 21 import android.net.VpnService; 22 import android.os.Bundle; 23 24 public class VpnClient extends Activity { 25 26 public static final String ACTION_CONNECT_AND_FINISH = 27 "com.android.cts.vpnfirewall.action.CONNECT_AND_FINISH"; 28 29 private static final int REQUEST_CONNECT = 0; 30 private static final int REQUEST_CONNECT_AND_FINISH = 1; 31 32 @Override onCreate(Bundle savedInstanceState)33 public void onCreate(Bundle savedInstanceState) { 34 super.onCreate(savedInstanceState); 35 setContentView(R.layout.vpn_client); 36 37 if (ACTION_CONNECT_AND_FINISH.equals(getIntent().getAction())) { 38 prepareAndStart(REQUEST_CONNECT_AND_FINISH); 39 } 40 findViewById(R.id.connect).setOnClickListener(v -> prepareAndStart(REQUEST_CONNECT)); 41 } 42 43 @Override onActivityResult(int request, int result, Intent data)44 protected void onActivityResult(int request, int result, Intent data) { 45 if (result == RESULT_OK) { 46 startService(new Intent(this, ReflectorVpnService.class)); 47 } 48 if (request == REQUEST_CONNECT_AND_FINISH) { 49 finish(); 50 } 51 } 52 prepareAndStart(int requestCode)53 private void prepareAndStart(int requestCode) { 54 Intent intent = VpnService.prepare(VpnClient.this); 55 if (intent != null) { 56 startActivityForResult(intent, requestCode); 57 } else { 58 onActivityResult(requestCode, RESULT_OK, null); 59 } 60 } 61 } 62