Added simple adapter for trusted certificates (to be used with a Spinner widget)
[strongswan.git] / src / frontends / android / src / org / strongswan / android / ui / MainActivity.java
index d2a7eec..65131fa 100644 (file)
 
 package org.strongswan.android.ui;
 
+import org.strongswan.android.R;
+import org.strongswan.android.data.VpnProfile;
+import org.strongswan.android.data.VpnProfileDataSource;
+import org.strongswan.android.logic.CharonVpnService;
 import org.strongswan.android.logic.TrustedCertificateManager;
+import org.strongswan.android.ui.VpnProfileListFragment.OnVpnProfileSelectedListener;
 
 import android.app.ActionBar;
 import android.app.Activity;
 import android.content.Intent;
 import android.net.VpnService;
 import android.os.Bundle;
+import android.view.Menu;
+import android.view.MenuInflater;
+import android.view.MenuItem;
+import android.view.Window;
 
-public class MainActivity extends Activity
+public class MainActivity extends Activity implements OnVpnProfileSelectedListener
 {
+       private static final int PREPARE_VPN_SERVICE = 0;
+       private VpnProfile activeProfile;
+
        @Override
        public void onCreate(Bundle savedInstanceState)
        {
                super.onCreate(savedInstanceState);
+               requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
                setContentView(R.layout.main);
-               startVpnService();
 
                ActionBar bar = getActionBar();
                bar.setDisplayShowTitleEnabled(false);
 
                /* load CA certificates in a background thread */
+               setProgressBarIndeterminateVisibility(true);
                new Thread(new Runnable() {
                        @Override
                        public void run()
                        {
                                TrustedCertificateManager.getInstance().load();
+                               runOnUiThread(new Runnable() {
+                                       @Override
+                                       public void run()
+                                       {
+                                               setProgressBarIndeterminateVisibility(false);
+                                       }
+                               });
                        }
                }).start();
        }
 
-       private void startVpnService()
+       @Override
+       public boolean onCreateOptionsMenu(Menu menu)
+       {
+               MenuInflater inflater = getMenuInflater();
+               inflater.inflate(R.menu.main, menu);
+               return true;
+       }
+
+       @Override
+       public boolean onOptionsItemSelected(MenuItem item)
+       {
+               switch (item.getItemId())
+               {
+                       case R.id.menu_reload_certs:
+                               setProgressBarIndeterminateVisibility(true);
+                               new Thread(new Runnable() {
+                                       @Override
+                                       public void run()
+                                       {
+                                               TrustedCertificateManager.getInstance().reload();
+                                               runOnUiThread(new Runnable() {
+                                                       @Override
+                                                       public void run()
+                                                       {
+                                                               setProgressBarIndeterminateVisibility(false);
+                                                       }
+                                               });
+                                       }
+                               }).start();
+                               return true;
+                       default:
+                               return super.onOptionsItemSelected(item);
+               }
+       }
+
+       /**
+        * Prepare the VpnService. If this succeeds the current VPN profile is
+        * started.
+        */
+       protected void prepareVpnService()
        {
                Intent intent = VpnService.prepare(this);
                if (intent != null)
                {
-                       startActivityForResult(intent, 0);
+                       startActivityForResult(intent, PREPARE_VPN_SERVICE);
                }
                else
                {
-                       onActivityResult(0, RESULT_OK, null);
+                       onActivityResult(PREPARE_VPN_SERVICE, RESULT_OK, null);
                }
        }
 
        @Override
        protected void onActivityResult(int requestCode, int resultCode, Intent data)
        {
-               if (resultCode == RESULT_OK)
+               switch (requestCode)
                {
-                       Intent intent = new Intent(this, CharonVpnService.class);
-                       startService(intent);
+                       case PREPARE_VPN_SERVICE:
+                               if (resultCode == RESULT_OK && activeProfile != null)
+                               {
+                                       Intent intent = new Intent(this, CharonVpnService.class);
+                                       intent.putExtra(VpnProfileDataSource.KEY_ID, activeProfile.getId());
+                                       this.startService(intent);
+                               }
+                               break;
+                       default:
+                               super.onActivityResult(requestCode, resultCode, data);
                }
        }
+
+       @Override
+       public void onVpnProfileSelected(VpnProfile profile)
+       {
+               activeProfile = profile;
+               prepareVpnService();
+       }
 }