From 0e76e955ba4848b18ee458cb9f53cc8e64671146 Mon Sep 17 00:00:00 2001 From: Arne Schwabe Date: Sun, 23 Jun 2013 17:09:52 +0200 Subject: Fix issues found by Code Analysis --- src/de/blinkt/openvpn/ConfigConverter.java | 2 -- src/de/blinkt/openvpn/FileProvider.java | 6 ++---- src/de/blinkt/openvpn/LogWindow.java | 8 ++++---- src/de/blinkt/openvpn/MainActivity.java | 2 +- src/de/blinkt/openvpn/VPNPreferences.java | 2 +- src/de/blinkt/openvpn/VpnProfile.java | 8 ++++---- src/de/blinkt/openvpn/api/APIVpnProfile.java | 5 +---- src/de/blinkt/openvpn/api/ExternalAppDatabase.java | 5 ++--- .../blinkt/openvpn/api/ExternalOpenVPNService.java | 7 ++++--- src/de/blinkt/openvpn/core/ConfigParser.java | 6 +++--- src/de/blinkt/openvpn/core/OpenVPN.java | 2 +- src/de/blinkt/openvpn/core/OpenVPNThread.java | 10 +++------- src/de/blinkt/openvpn/core/OpenVpnService.java | 20 +++++++++----------- src/de/blinkt/openvpn/core/X509Utils.java | 2 +- src/de/blinkt/openvpn/fragments/AboutFragment.java | 6 ------ src/de/blinkt/openvpn/fragments/FaqFragment.java | 6 ------ .../openvpn/fragments/FileSelectionFragment.java | 5 +---- src/de/blinkt/openvpn/fragments/GeneralSettings.java | 6 ++---- .../openvpn/fragments/Settings_Authentication.java | 2 +- src/de/blinkt/openvpn/fragments/Settings_Basic.java | 7 +------ src/de/blinkt/openvpn/fragments/Settings_IP.java | 4 +--- .../blinkt/openvpn/fragments/ShowConfigFragment.java | 4 ++-- 22 files changed, 44 insertions(+), 81 deletions(-) (limited to 'src/de/blinkt') diff --git a/src/de/blinkt/openvpn/ConfigConverter.java b/src/de/blinkt/openvpn/ConfigConverter.java index ae0385da..22109f81 100644 --- a/src/de/blinkt/openvpn/ConfigConverter.java +++ b/src/de/blinkt/openvpn/ConfigConverter.java @@ -383,8 +383,6 @@ public class ConfigConverter extends ListActivity { } } } - - return; } private void log(String logmessage) { diff --git a/src/de/blinkt/openvpn/FileProvider.java b/src/de/blinkt/openvpn/FileProvider.java index c5cadc12..671d6446 100644 --- a/src/de/blinkt/openvpn/FileProvider.java +++ b/src/de/blinkt/openvpn/FileProvider.java @@ -94,8 +94,7 @@ implements PipeDataWriter { openPipeHelper(uri, null, null, is, this), 0, dumpfile.length()); } catch (IOException e) { - FileNotFoundException fnf = new FileNotFoundException("Unable to open minidump " + uri); - throw fnf; + throw new FileNotFoundException("Unable to open minidump " + uri); } } @@ -110,8 +109,7 @@ implements PipeDataWriter { if (!path.matches("^[0-9a-z-.]*(dmp|dmp.log)$")) throw new FileNotFoundException("url not in expect format " + uri); File cachedir = getContext().getCacheDir(); - File dumpfile = new File(cachedir,path); - return dumpfile; + return new File(cachedir,path); } @Override diff --git a/src/de/blinkt/openvpn/LogWindow.java b/src/de/blinkt/openvpn/LogWindow.java index 1d75c154..e06dbe6c 100644 --- a/src/de/blinkt/openvpn/LogWindow.java +++ b/src/de/blinkt/openvpn/LogWindow.java @@ -23,6 +23,7 @@ import de.blinkt.openvpn.core.OpenVpnService.LocalBinder; import de.blinkt.openvpn.core.ProfileManager; import java.text.SimpleDateFormat; +import java.util.Collections; import java.util.Date; import java.util.Locale; import java.util.Vector; @@ -58,7 +59,7 @@ public class LogWindow extends ListActivity implements StateListener { private static final int MESSAGE_CLEARLOG = 1; - private static final int MESSAGE_NEWTS = 1; + private static final int MESSAGE_NEWTS = 2; private Vector myEntries=new Vector(); @@ -83,9 +84,7 @@ public class LogWindow extends ListActivity implements StateListener { private void initLogBuffer() { myEntries.clear(); - for (LogItem litem : OpenVPN.getlogbuffer()) { - myEntries.add(litem); - } + Collections.addAll(myEntries, OpenVPN.getlogbuffer()); } String getLogStr() { @@ -189,6 +188,7 @@ public class LogWindow extends ListActivity implements StateListener { @Override public void newLog(LogItem logmessage) { Message msg = Message.obtain(); + assert (msg!=null); msg.what=MESSAGE_NEWLOG; Bundle mbundle=new Bundle(); mbundle.putParcelable("logmessage", logmessage); diff --git a/src/de/blinkt/openvpn/MainActivity.java b/src/de/blinkt/openvpn/MainActivity.java index db578b19..afe08e2e 100644 --- a/src/de/blinkt/openvpn/MainActivity.java +++ b/src/de/blinkt/openvpn/MainActivity.java @@ -41,7 +41,7 @@ public class MainActivity extends Activity { bar.addTab(sendDump); } - }; + } protected class TabListener implements ActionBar.TabListener { diff --git a/src/de/blinkt/openvpn/VPNPreferences.java b/src/de/blinkt/openvpn/VPNPreferences.java index 71cd5448..3f24728a 100644 --- a/src/de/blinkt/openvpn/VPNPreferences.java +++ b/src/de/blinkt/openvpn/VPNPreferences.java @@ -29,7 +29,7 @@ public class VPNPreferences extends PreferenceActivity { @Override protected void onStop() { super.onStop(); - }; + } @Override protected void onSaveInstanceState(Bundle outState) { diff --git a/src/de/blinkt/openvpn/VpnProfile.java b/src/de/blinkt/openvpn/VpnProfile.java index e6ebae90..190cdf9a 100644 --- a/src/de/blinkt/openvpn/VpnProfile.java +++ b/src/de/blinkt/openvpn/VpnProfile.java @@ -449,7 +449,7 @@ public class VpnProfile implements Serializable{ } private boolean nonNull(String val) { - if(val == null || val.equals("")) + if(val == null || val.equals("")) return false; else return true; @@ -531,7 +531,7 @@ public class VpnProfile implements Serializable{ args.add(cacheDir.getAbsolutePath() + "/" + OVPNCONFIGFILE); - return (String[]) args.toArray(new String[args.size()]); + return args.toArray(new String[args.size()]); } public Intent prepareIntent(Context context) { @@ -584,11 +584,11 @@ public class VpnProfile implements Serializable{ try { Certificate cacert = X509Utils.getCertificateFromFile(mCaFilename); X509Certificate[] newcachain = new X509Certificate[cachain.length+1]; - for(int i=0;i getExtAppList() { - SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); - Set allowedapps = prefs.getStringSet(PREFERENCES_KEY, new HashSet()); - return allowedapps; + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); + return prefs.getStringSet(PREFERENCES_KEY, new HashSet()); } void addApp(String packagename) diff --git a/src/de/blinkt/openvpn/api/ExternalOpenVPNService.java b/src/de/blinkt/openvpn/api/ExternalOpenVPNService.java index 866fb698..35374c77 100644 --- a/src/de/blinkt/openvpn/api/ExternalOpenVPNService.java +++ b/src/de/blinkt/openvpn/api/ExternalOpenVPNService.java @@ -76,7 +76,8 @@ public class ExternalOpenVPNService extends Service implements StateListener { } private final IOpenVPNAPIService.Stub mBinder = new IOpenVPNAPIService.Stub() { - private boolean checkOpenVPNPermission() throws SecurityRemoteException{ + + private void checkOpenVPNPermission() throws SecurityRemoteException{ PackageManager pm = getPackageManager(); for (String apppackage:mExtAppDb.getExtAppList()) { @@ -84,7 +85,7 @@ public class ExternalOpenVPNService extends Service implements StateListener { try { app = pm.getApplicationInfo(apppackage, 0); if (Binder.getCallingUid() == app.uid) { - return true; + return; } } catch (NameNotFoundException e) { // App not found. Remove it from the list @@ -299,7 +300,7 @@ public class ExternalOpenVPNService extends Service implements StateListener { { broadcastItem.newStatus(um.vpnUUID, um.state, um.logmessage, um.level.name()); } - }; + } } \ No newline at end of file diff --git a/src/de/blinkt/openvpn/core/ConfigParser.java b/src/de/blinkt/openvpn/core/ConfigParser.java index 3c3b37a9..1a37f7cd 100644 --- a/src/de/blinkt/openvpn/core/ConfigParser.java +++ b/src/de/blinkt/openvpn/core/ConfigParser.java @@ -3,6 +3,7 @@ package de.blinkt.openvpn.core; import java.io.BufferedReader; import java.io.IOException; import java.io.Reader; +import java.util.Collections; import java.util.HashMap; import java.util.Locale; import java.util.Vector; @@ -64,8 +65,7 @@ public class ConfigParser { String meta = line.split("#\\sOVPN_ACCESS_SERVER_", 2)[1]; String[] parts = meta.split("=",2); Vector rval = new Vector(); - for(String p:parts) - rval.add(p); + Collections.addAll(rval, parts); return rval; } @@ -359,7 +359,7 @@ public class ConfigParser { Vector proto = getOption("proto", 1,1); if(proto!=null){ - np.mUseUdp=isUdpProto(proto.get(1));; + np.mUseUdp=isUdpProto(proto.get(1)); } // Parse remote config diff --git a/src/de/blinkt/openvpn/core/OpenVPN.java b/src/de/blinkt/openvpn/core/OpenVPN.java index 66d985bd..af090204 100644 --- a/src/de/blinkt/openvpn/core/OpenVPN.java +++ b/src/de/blinkt/openvpn/core/OpenVPN.java @@ -354,7 +354,7 @@ public class OpenVPN { // The stoned way of java to return an array from a vector // brought to you by eclipse auto complete - return (LogItem[]) logbuffer.toArray(new LogItem[logbuffer.size()]); + return logbuffer.toArray(new LogItem[logbuffer.size()]); } diff --git a/src/de/blinkt/openvpn/core/OpenVPNThread.java b/src/de/blinkt/openvpn/core/OpenVPNThread.java index 2cfde13d..100990bb 100644 --- a/src/de/blinkt/openvpn/core/OpenVPNThread.java +++ b/src/de/blinkt/openvpn/core/OpenVPNThread.java @@ -8,10 +8,7 @@ import de.blinkt.openvpn.core.OpenVPN.LogItem; import java.io.*; import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.LinkedList; -import java.util.Locale; -import java.util.Map; +import java.util.*; import java.util.Map.Entry; public class OpenVPNThread implements Runnable { @@ -83,9 +80,8 @@ public class OpenVPNThread implements Runnable { private void startOpenVPNThreadArgs(String[] argv, Map env) { LinkedList argvlist = new LinkedList(); - - for(String arg:argv) - argvlist.add(arg); + + Collections.addAll(argvlist, argv); ProcessBuilder pb = new ProcessBuilder(argvlist); // Hack O rama diff --git a/src/de/blinkt/openvpn/core/OpenVpnService.java b/src/de/blinkt/openvpn/core/OpenVpnService.java index 230ddbaa..0bfc150a 100644 --- a/src/de/blinkt/openvpn/core/OpenVpnService.java +++ b/src/de/blinkt/openvpn/core/OpenVpnService.java @@ -68,7 +68,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac private static boolean mNotificationAlwaysVisible =false; private final IBinder mBinder = new LocalBinder(); - private boolean mOvpn3; + private boolean mOvpn3 = false; private OpenVPNManagement mManagement; @@ -269,14 +269,14 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac OpenVPN.addStateListener(this); OpenVPN.addByteCountListener(this); - if(intent != null && intent.getAction() !=null &&intent.getAction().equals(PAUSE_VPN)) + if(intent != null && PAUSE_VPN.equals(intent.getAction())) { if(mDeviceStateReceiver!=null) mDeviceStateReceiver.userPause(true); return START_NOT_STICKY; } - if(intent != null && intent.getAction() !=null &&intent.getAction().equals(RESUME_VPN)) + if(intent != null && RESUME_VPN.equals(intent.getAction())) { if(mDeviceStateReceiver!=null) mDeviceStateReceiver.userPause(false); @@ -284,12 +284,13 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac } - if(intent != null && intent.getAction() !=null &&intent.getAction().equals(START_SERVICE)) + if(intent != null && START_SERVICE.equals(intent.getAction())) return START_NOT_STICKY; - if(intent != null && intent.getAction() !=null &&intent.getAction().equals(START_SERVICE_STICKY)) { + if(intent != null && START_SERVICE_STICKY.equals(intent.getAction())) { return START_REDELIVER_INTENT; } + assert(intent!=null); // Extract information from the intent. String prefix = getPackageName(); @@ -312,6 +313,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac try { Thread.sleep(1000); } catch (InterruptedException e) { + e.printStackTrace(); } @@ -320,6 +322,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac try { Thread.sleep(1000); } catch (InterruptedException e) { + e.printStackTrace(); } } // An old running VPN should now be exited @@ -512,11 +515,6 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac } } - - public void addRoute(CIDRIP route) - { - mRoutes.add(route ); - } public void addRoute(String dest, String mask) { CIDRIP route = new CIDRIP(dest, mask); if(route.len == 32 && !mask.equals("255.255.255.255")) { @@ -537,7 +535,7 @@ public class OpenVpnService extends VpnService implements StateListener, Callbac mMtu=mtu; } - public void setLocalIP(CIDRIP cdrip) + public void setLocalIP(CIDRIP cdrip) { mLocalIP=cdrip; } diff --git a/src/de/blinkt/openvpn/core/X509Utils.java b/src/de/blinkt/openvpn/core/X509Utils.java index 48434b22..ff1e932f 100644 --- a/src/de/blinkt/openvpn/core/X509Utils.java +++ b/src/de/blinkt/openvpn/core/X509Utils.java @@ -26,7 +26,7 @@ public class X509Utils { return certFact.generateCertificate(inStream); } - public static PemObject readPemObjectFromFile (String keyfilename) throws CertificateException, IOException { + public static PemObject readPemObjectFromFile (String keyfilename) throws IOException { Reader inStream; diff --git a/src/de/blinkt/openvpn/fragments/AboutFragment.java b/src/de/blinkt/openvpn/fragments/AboutFragment.java index a0ee9928..54bd3667 100644 --- a/src/de/blinkt/openvpn/fragments/AboutFragment.java +++ b/src/de/blinkt/openvpn/fragments/AboutFragment.java @@ -15,12 +15,6 @@ import de.blinkt.openvpn.R; public class AboutFragment extends Fragment { - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { diff --git a/src/de/blinkt/openvpn/fragments/FaqFragment.java b/src/de/blinkt/openvpn/fragments/FaqFragment.java index 459f2369..3aa84fd7 100644 --- a/src/de/blinkt/openvpn/fragments/FaqFragment.java +++ b/src/de/blinkt/openvpn/fragments/FaqFragment.java @@ -12,12 +12,6 @@ import de.blinkt.openvpn.R; public class FaqFragment extends Fragment { - @Override - public void onCreate(Bundle savedInstanceState) { - super.onCreate(savedInstanceState); - - } - @Override public View onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { diff --git a/src/de/blinkt/openvpn/fragments/FileSelectionFragment.java b/src/de/blinkt/openvpn/fragments/FileSelectionFragment.java index 864f24ce..e0c97620 100644 --- a/src/de/blinkt/openvpn/fragments/FileSelectionFragment.java +++ b/src/de/blinkt/openvpn/fragments/FileSelectionFragment.java @@ -59,7 +59,7 @@ public class FileSelectionFragment extends ListFragment { mInlineImport = (CheckBox) v.findViewById(R.id.doinline); - if(mHideImport== true) { + if(mHideImport) { mInlineImport.setVisibility(View.GONE); mInlineImport.setChecked(false); } @@ -95,9 +95,6 @@ public class FileSelectionFragment extends ListFragment { mClearButton.setEnabled(false); } - - - return v; } diff --git a/src/de/blinkt/openvpn/fragments/GeneralSettings.java b/src/de/blinkt/openvpn/fragments/GeneralSettings.java index bf391d29..a78c31f6 100644 --- a/src/de/blinkt/openvpn/fragments/GeneralSettings.java +++ b/src/de/blinkt/openvpn/fragments/GeneralSettings.java @@ -76,10 +76,8 @@ public class GeneralSettings extends PreferenceFragment implements OnPreferenceC private boolean isTunModuleAvailable() { // Check if the tun module exists on the file system - if(new File("/system/lib/modules/tun.ko").length() > 10) - return true; - return false; - } + return new File("/system/lib/modules/tun.ko").length() > 10; + } @Override public boolean onPreferenceClick(Preference preference) { diff --git a/src/de/blinkt/openvpn/fragments/Settings_Authentication.java b/src/de/blinkt/openvpn/fragments/Settings_Authentication.java index 9ec77f35..592adcb1 100644 --- a/src/de/blinkt/openvpn/fragments/Settings_Authentication.java +++ b/src/de/blinkt/openvpn/fragments/Settings_Authentication.java @@ -92,7 +92,7 @@ public class Settings_Authentication extends OpenVpnPreferencesFragment implemen if(mTLSAuthDirection.getValue()==null) mProfile.mTLSAuthDirection=null; else - mProfile.mTLSAuthDirection = mTLSAuthDirection.getValue().toString(); + mProfile.mTLSAuthDirection = mTLSAuthDirection.getValue(); if(mCipher.getText()==null) mProfile.mCipher=null; diff --git a/src/de/blinkt/openvpn/fragments/Settings_Basic.java b/src/de/blinkt/openvpn/fragments/Settings_Basic.java index ad9a79ad..05e4a2a8 100644 --- a/src/de/blinkt/openvpn/fragments/Settings_Basic.java +++ b/src/de/blinkt/openvpn/fragments/Settings_Basic.java @@ -144,12 +144,7 @@ public class Settings_Basic extends Fragment implements View.OnClickListener, On } - @Override - public void onActivityCreated(Bundle savedInstanceState) { - super.onActivityCreated(savedInstanceState); - } - - @Override + @Override public void onItemSelected(AdapterView parent, View view, int position, long id) { if (parent == mType) { changeType(position); diff --git a/src/de/blinkt/openvpn/fragments/Settings_IP.java b/src/de/blinkt/openvpn/fragments/Settings_IP.java index 483397d0..674a09d4 100644 --- a/src/de/blinkt/openvpn/fragments/Settings_IP.java +++ b/src/de/blinkt/openvpn/fragments/Settings_IP.java @@ -113,10 +113,8 @@ public class Settings_IP extends OpenVpnPreferencesFragment implements OnPrefere mOverrideDNS.setEnabled(mUsePull.isChecked()); if(!mUsePull.isChecked()) enabled =true; - else if (mOverrideDNS.isChecked()) - enabled = true; else - enabled = false; + enabled = mOverrideDNS.isChecked(); mDNS1.setEnabled(enabled); mDNS2.setEnabled(enabled); diff --git a/src/de/blinkt/openvpn/fragments/ShowConfigFragment.java b/src/de/blinkt/openvpn/fragments/ShowConfigFragment.java index 3c76a383..ad077633 100644 --- a/src/de/blinkt/openvpn/fragments/ShowConfigFragment.java +++ b/src/de/blinkt/openvpn/fragments/ShowConfigFragment.java @@ -53,9 +53,9 @@ public class ShowConfigFragment extends Fragment { }); - }; + } }.start(); - }; + } @Override public void onCreate(Bundle savedInstanceState) { -- cgit v1.2.3