From e3152fa86cda600c3d993f8ddb43273f95aaa9f0 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sun, 20 May 2012 14:36:00 +0200 Subject: - Fix missing about - Fix vpn list saving to sharedpreferences (closes issue #27) - Version 0.5.5a --- src/de/blinkt/openvpn/ConfigConverter.java | 16 +++++ src/de/blinkt/openvpn/OpenVPNThread.java | 14 +---- src/de/blinkt/openvpn/OpenVpnService.java | 97 ++++++++++++++---------------- src/de/blinkt/openvpn/ProfileManager.java | 16 ++++- 4 files changed, 76 insertions(+), 67 deletions(-) (limited to 'src/de') diff --git a/src/de/blinkt/openvpn/ConfigConverter.java b/src/de/blinkt/openvpn/ConfigConverter.java index 952ab968..8aebc664 100644 --- a/src/de/blinkt/openvpn/ConfigConverter.java +++ b/src/de/blinkt/openvpn/ConfigConverter.java @@ -41,8 +41,11 @@ public class ConfigConverter extends ListActivity { log("Importing the config had error, cannot save it"); return true; } + Intent result = new Intent(); ProfileManager vpl = ProfileManager.getInstance(this); + + setUniqueProfileName(vpl); vpl.addProfile(mResult); vpl.saveProfile(this, mResult); vpl.saveProfileList(this); @@ -56,6 +59,19 @@ public class ConfigConverter extends ListActivity { } + + private void setUniqueProfileName(ProfileManager vpl) { + int i=1; + String newname = getString(R.string.converted_profile); + + while(vpl.getProfileByName(newname)!=null) { + i++; + newname = getString(R.string.converted_profile_i,i); + } + + mResult.mName=newname; + } + @Override public boolean onCreateOptionsMenu(Menu menu) { MenuInflater inflater = getMenuInflater(); diff --git a/src/de/blinkt/openvpn/OpenVPNThread.java b/src/de/blinkt/openvpn/OpenVPNThread.java index dcd5f4ec..503f4c4c 100644 --- a/src/de/blinkt/openvpn/OpenVPNThread.java +++ b/src/de/blinkt/openvpn/OpenVPNThread.java @@ -29,19 +29,7 @@ public class OpenVPNThread implements Runnable { @Override public void run() { try { - Log.i(TAG, "Starting openvpn"); - - // We try to create the tunnel for several times. The better way - // is to work with ConnectivityManager, such as trying only when - // the network is avaiable. Here we just use a counter to keep - // things simple. - //for (int attempt = 0; attempt < 10; ++attempt) { - mService.getHandler().sendEmptyMessage(R.string.connecting); - - // Log argv - - //OpenVPN.logMessage(0, "argv:" , Arrays.toString(mArgv)); - + Log.i(TAG, "Starting openvpn"); startOpenVPNThreadArgs(mArgv); diff --git a/src/de/blinkt/openvpn/OpenVpnService.java b/src/de/blinkt/openvpn/OpenVpnService.java index e2f7ba12..8aa39d89 100644 --- a/src/de/blinkt/openvpn/OpenVpnService.java +++ b/src/de/blinkt/openvpn/OpenVpnService.java @@ -19,19 +19,17 @@ package de.blinkt.openvpn; import java.io.IOException; import java.util.Vector; - +import android.app.Notification; +import android.app.NotificationManager; import android.app.PendingIntent; +import android.content.Context; import android.content.Intent; import android.net.LocalSocket; import android.net.LocalSocketAddress; import android.net.VpnService; -import android.os.Handler; -import android.os.Message; import android.os.ParcelFileDescriptor; -import android.widget.Toast; -public class OpenVpnService extends VpnService implements Handler.Callback { - Handler mHandler; +public class OpenVpnService extends VpnService { private Thread mServiceThread; private Vector mDnslist=new Vector(); @@ -50,9 +48,10 @@ public class OpenVpnService extends VpnService implements Handler.Callback { private Thread mSocketManagerThread; private int mMtu; private String mLocalIPv6=null; + private Notification mNotification=null; - - + private static final int HELLO_ID = 1; + @Override public void onRevoke() { OpenVpnManagementThread.stopOpenVPN(); @@ -60,8 +59,36 @@ public class OpenVpnService extends VpnService implements Handler.Callback { stopSelf(); }; + private void showNotification() { + String ns = Context.NOTIFICATION_SERVICE; + NotificationManager mNotificationManager = (NotificationManager) getSystemService(ns); + + + int icon = R.drawable.icon; + CharSequence tickerText = "Hello"; + long when = System.currentTimeMillis(); + + mNotification = new Notification(icon, tickerText, when); + + Context context = getApplicationContext(); + CharSequence contentTitle = "My notification"; + CharSequence contentText = "Hello World!"; + mNotification.setLatestEventInfo(context, contentTitle, contentText, getLogPendingIntent()); + + mNotificationManager.notify(HELLO_ID, mNotification); + + } + + PendingIntent getLogPendingIntent() { + // Let the configure Button show the Log + Intent intent = new Intent(getBaseContext(),LogWindow.class); + intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); + PendingIntent startLW = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); + intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); + return startLW; + } @@ -97,12 +124,8 @@ public class OpenVpnService extends VpnService implements Handler.Callback { String profileUUID = intent.getStringExtra(prefix + ".profileUUID"); mProfile = ProfileManager.get(profileUUID); - - // The handler is only used to show messages. - if (mHandler == null) { - mHandler = new Handler(this); - } - + //showNotification(); + // Stop the previous session by interrupting the thread. if(OpenVpnManagementThread.stopOpenVPN()){ // an old was asked to exit, wait 2s @@ -132,10 +155,7 @@ public class OpenVpnService extends VpnService implements Handler.Callback { //checkForRemainingMiniVpns(); } - - // Start a new session by creating a new thread. - OpenVPNThread serviceThread = new OpenVPNThread(this, argv,nativelibdir); mServiceThread = new Thread(serviceThread, "OpenVPNServiceThread"); @@ -164,34 +184,25 @@ public class OpenVpnService extends VpnService implements Handler.Callback { } } - @Override - public boolean handleMessage(Message message) { - if (message != null) { - Toast.makeText(this, message.what, Toast.LENGTH_SHORT).show(); - } - return true; - } - - public ParcelFileDescriptor openTun() { Builder builder = new Builder(); - + if(mLocalIP==null && mLocalIPv6==null) { OpenVPN.logMessage(0, "", getString(R.string.opentun_no_ipaddr)); return null; } - + if(mLocalIP!=null) { builder.addAddress(mLocalIP.mIp, mLocalIP.len); } - + if(mLocalIPv6!=null) { String[] ipv6parts = mLocalIPv6.split("/"); builder.addAddress(ipv6parts[0],Integer.parseInt(ipv6parts[1])); } - + for (String dns : mDnslist ) { builder.addDnsServer(dns); @@ -216,7 +227,7 @@ public class OpenVpnService extends VpnService implements Handler.Callback { OpenVPN.logMessage(0, "", getString(R.string.route_rejected) + v6route + " " + ia.getLocalizedMessage()); } } - + if(mDomain!=null) builder.addSearchDomain(mDomain); @@ -245,13 +256,9 @@ public class OpenVpnService extends VpnService implements Handler.Callback { mRoutesv6.clear(); mLocalIP=null; mLocalIPv6=null; - - // Let the configure Button show the Log - Intent intent = new Intent(getBaseContext(),LogWindow.class); - intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); - PendingIntent startLW = PendingIntent.getActivity(getApplicationContext(), 0, intent, 0); - intent.addFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT); - builder.setConfigureIntent(startLW); + + builder.setConfigureIntent(getLogPendingIntent()); + try { ParcelFileDescriptor pfd = builder.establish(); return pfd; @@ -305,7 +312,7 @@ public class OpenVpnService extends VpnService implements Handler.Callback { mRoutes.add(route); } - + public void addRoutev6(String extra) { mRoutesv6.add(extra); } @@ -329,20 +336,8 @@ public class OpenVpnService extends VpnService implements Handler.Callback { } } - - - - - - - public void setLocalIPv6(String ipv6addr) { mLocalIPv6 = ipv6addr; } - - public Handler getHandler() { - return mHandler; - } - } diff --git a/src/de/blinkt/openvpn/ProfileManager.java b/src/de/blinkt/openvpn/ProfileManager.java index d34bdbec..eb94505a 100644 --- a/src/de/blinkt/openvpn/ProfileManager.java +++ b/src/de/blinkt/openvpn/ProfileManager.java @@ -10,6 +10,8 @@ import java.util.HashMap; import java.util.HashSet; import java.util.Set; +import de.blinkt.openvpn.R.string; + import android.app.Activity; import android.content.Context; import android.content.SharedPreferences; @@ -23,6 +25,7 @@ public class ProfileManager { private static ProfileManager instance; private HashMap profiles=new HashMap(); + public static VpnProfile get(String key) { if(instance==null) return null; @@ -65,7 +68,14 @@ public class ProfileManager { SharedPreferences sharedprefs = context.getSharedPreferences(PREFS_NAME,Activity.MODE_PRIVATE); Editor editor = sharedprefs.edit(); editor.putStringSet("vpnlist", profiles.keySet()); - editor.commit(); + + // For reasing I do not understand at all + // Android saves my prefs file only one time + // if I remove the debug code below :( + int counter = sharedprefs.getInt("counter", 0); + editor.putInt("counter", counter+1); + editor.apply(); + } public void addProfile(VpnProfile profile) { @@ -99,8 +109,8 @@ public class ProfileManager { private void loadVPNList(Context context) { profiles = new HashMap(); - SharedPreferences settings =context.getSharedPreferences(PREFS_NAME,Activity.MODE_PRIVATE); - Set vlist = settings.getStringSet("vpnlist", null); + SharedPreferences listpref = context.getSharedPreferences(PREFS_NAME,Activity.MODE_PRIVATE); + Set vlist = listpref.getStringSet("vpnlist", null); Exception exp =null; if(vlist==null){ vlist = new HashSet(); -- cgit v1.2.3