Home | History | Annotate | Download | only in toyvpn
      1 /*
      2  * Copyright (C) 2011 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.example.android.toyvpn;
     18 
     19 import android.app.Activity;
     20 import android.content.Intent;
     21 import android.net.VpnService;
     22 import android.os.Bundle;
     23 import android.util.Log;
     24 import android.view.View;
     25 import android.widget.TextView;
     26 import android.widget.Button;
     27 
     28 public class ToyVpnClient extends Activity implements View.OnClickListener {
     29     private TextView mServerAddress;
     30     private TextView mServerPort;
     31     private TextView mSharedSecret;
     32 
     33     @Override
     34     public void onCreate(Bundle savedInstanceState) {
     35         super.onCreate(savedInstanceState);
     36         setContentView(R.layout.form);
     37 
     38         mServerAddress = (TextView) findViewById(R.id.address);
     39         mServerPort = (TextView) findViewById(R.id.port);
     40         mSharedSecret = (TextView) findViewById(R.id.secret);
     41 
     42         findViewById(R.id.connect).setOnClickListener(this);
     43     }
     44 
     45     @Override
     46     public void onClick(View v) {
     47         Intent intent = VpnService.prepare(this);
     48         if (intent != null) {
     49             startActivityForResult(intent, 0);
     50         } else {
     51             onActivityResult(0, RESULT_OK, null);
     52         }
     53     }
     54 
     55     @Override
     56     protected void onActivityResult(int request, int result, Intent data) {
     57         if (result == RESULT_OK) {
     58             String prefix = getPackageName();
     59             Intent intent = new Intent(this, ToyVpnService.class)
     60                     .putExtra(prefix + ".ADDRESS", mServerAddress.getText().toString())
     61                     .putExtra(prefix + ".PORT", mServerPort.getText().toString())
     62                     .putExtra(prefix + ".SECRET", mSharedSecret.getText().toString());
     63             startService(intent);
     64         }
     65     }
     66 }
     67