summaryrefslogtreecommitdiff
path: root/app/src/main/java/se/leap
diff options
context:
space:
mode:
authorcyberta <cyberta@riseup.net>2019-07-12 10:38:00 -0700
committercyberta <cyberta@riseup.net>2019-07-12 10:38:00 -0700
commit6c9a29082a8543991b8485c8613e7c586fed636f (patch)
tree3e6730b27eff129a54f6104c2dd2f487ffea2887 /app/src/main/java/se/leap
parent144244cab15014d495985f396c618729209a8f8a (diff)
parent50667074dd6729eb47400273f4275ceba2457575 (diff)
Merge branch 'improve_blocking_vpn' into 'master'
Improve blocking vpn See merge request leap/bitmask_android!91
Diffstat (limited to 'app/src/main/java/se/leap')
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/Constants.java3
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/EipFragment.java9
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java5
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java5
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/EIP.java6
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java5
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java14
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java80
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/fragments/AlwaysOnDialog.java32
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/fragments/LogFragment.java114
-rw-r--r--app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java56
11 files changed, 100 insertions, 229 deletions
diff --git a/app/src/main/java/se/leap/bitmaskclient/Constants.java b/app/src/main/java/se/leap/bitmaskclient/Constants.java
index f9ee8fcf..42df6d1d 100644
--- a/app/src/main/java/se/leap/bitmaskclient/Constants.java
+++ b/app/src/main/java/se/leap/bitmaskclient/Constants.java
@@ -11,6 +11,8 @@ public interface Constants {
String SHARED_PREFERENCES = "LEAPPreferences";
String PREFERENCES_APP_VERSION = "bitmask version";
String ALWAYS_ON_SHOW_DIALOG = "DIALOG.ALWAYS_ON_SHOW_DIALOG";
+ String CLEARLOG = "clearlogconnect";
+ String LAST_USED_PROFILE = "last_used_profile";
//////////////////////////////////////////////
@@ -68,6 +70,7 @@ public interface Constants {
String PROVIDER_CONFIGURED = "Constants.PROVIDER_CONFIGURED";
String PROVIDER_EIP_DEFINITION = "Constants.EIP_DEFINITION";
String PROVIDER_PROFILE_UUID = "Constants.PROVIDER_PROFILE_UUID";
+ String PROVIDER_PROFILE = "Constants.PROVIDER_PROFILE";
//////////////////////////////////////////////
// CREDENTIAL CONSTANTS
diff --git a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
index 11ad8da3..269bf61f 100644
--- a/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
+++ b/app/src/main/java/se/leap/bitmaskclient/EipFragment.java
@@ -50,10 +50,9 @@ import java.util.Observer;
import butterknife.ButterKnife;
import butterknife.InjectView;
import butterknife.OnClick;
-import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.IOpenVPNServiceInternal;
import de.blinkt.openvpn.core.OpenVPNService;
-import de.blinkt.openvpn.core.ProfileManager;
+import de.blinkt.openvpn.core.VpnStatus;
import se.leap.bitmaskclient.eip.EipCommand;
import se.leap.bitmaskclient.eip.EipStatus;
import se.leap.bitmaskclient.fragments.DonationReminderDialog;
@@ -537,9 +536,9 @@ public class EipFragment extends Fragment implements Observer {
private void setVpnRouteText() {
String vpnRouteString = provider.getName();
- VpnProfile vpnProfile = ProfileManager.getLastConnectedVpn();
- if (vpnProfile != null && !TextUtils.isEmpty(vpnProfile.mName)) {
- vpnRouteString += " (" + vpnProfile.mName + ")";
+ String profileName = VpnStatus.getLastConnectedVpnName();
+ if (!TextUtils.isEmpty(profileName)) {
+ vpnRouteString += " (" + profileName + ")";
}
vpnRoute.setText(vpnRouteString);
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java b/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java
index ff94fe82..a8aa2dfb 100644
--- a/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java
+++ b/app/src/main/java/se/leap/bitmaskclient/EipSetupObserver.java
@@ -35,6 +35,7 @@ import static se.leap.bitmaskclient.Constants.EIP_ACTION_START;
import static se.leap.bitmaskclient.Constants.EIP_ACTION_START_ALWAYS_ON_VPN;
import static se.leap.bitmaskclient.Constants.EIP_REQUEST;
import static se.leap.bitmaskclient.Constants.PROVIDER_KEY;
+import static se.leap.bitmaskclient.Constants.PROVIDER_PROFILE;
import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES;
import static se.leap.bitmaskclient.ProviderAPI.CORRECTLY_DOWNLOADED_EIP_SERVICE;
import static se.leap.bitmaskclient.ProviderAPI.CORRECTLY_UPDATED_INVALID_VPN_CERTIFICATE;
@@ -186,7 +187,7 @@ class EipSetupObserver extends BroadcastReceiver implements VpnStatus.StateListe
finishGatewaySetup(true);
}
- VpnProfile vpnProfile = (VpnProfile) event.getSerializableExtra(LaunchVPN.EXTRA_TEMP_VPN_PROFILE);
+ VpnProfile vpnProfile = (VpnProfile) event.getSerializableExtra(PROVIDER_PROFILE);
if (vpnProfile == null) {
Log.e(TAG, "Tried to setup non existing vpn profile.");
return;
@@ -204,7 +205,7 @@ class EipSetupObserver extends BroadcastReceiver implements VpnStatus.StateListe
intent.setAction(Intent.ACTION_MAIN);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
intent.putExtra(LaunchVPN.EXTRA_HIDELOG, true);
- intent.putExtra(LaunchVPN.EXTRA_TEMP_VPN_PROFILE, vpnProfile);
+ intent.putExtra(PROVIDER_PROFILE, vpnProfile);
intent.putExtra(Gateway.KEY_N_CLOSEST_GATEWAY, setupNClosestGateway.get());
context.startActivity(intent);
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java b/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java
index f33bc27e..2efce9e4 100644
--- a/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java
+++ b/app/src/main/java/se/leap/bitmaskclient/OnBootReceiver.java
@@ -6,6 +6,9 @@ import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
+import de.blinkt.openvpn.core.VpnStatus;
+import se.leap.bitmaskclient.utils.PreferenceHelper;
+
import static android.content.Intent.ACTION_BOOT_COMPLETED;
import static se.leap.bitmaskclient.Constants.APP_ACTION_CONFIGURE_ALWAYS_ON_PROFILE;
import static se.leap.bitmaskclient.Constants.EIP_IS_ALWAYS_ON;
@@ -27,7 +30,7 @@ public class OnBootReceiver extends BroadcastReceiver {
preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
boolean providerConfigured = !preferences.getString(PROVIDER_VPN_CERTIFICATE, "").isEmpty();
boolean startOnBoot = preferences.getBoolean(EIP_RESTART_ON_BOOT, false);
- boolean isAlwaysOnConfigured = preferences.getBoolean(EIP_IS_ALWAYS_ON, false);
+ boolean isAlwaysOnConfigured = VpnStatus.isAlwaysOn();
Log.d("OpenVPN", "OpenVPN onBoot intent received. Provider configured? " + providerConfigured + " Start on boot? " + startOnBoot + " isAlwaysOn feature configured: " + isAlwaysOnConfigured);
if (providerConfigured) {
if (isAlwaysOnConfigured) {
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
index 29e2199f..a5434871 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java
@@ -46,7 +46,6 @@ import java.util.concurrent.LinkedBlockingQueue;
import de.blinkt.openvpn.core.ConnectionStatus;
import de.blinkt.openvpn.core.IOpenVPNServiceInternal;
import de.blinkt.openvpn.core.OpenVPNService;
-import de.blinkt.openvpn.core.ProfileManager;
import de.blinkt.openvpn.core.VpnStatus;
import se.leap.bitmaskclient.OnBootReceiver;
import se.leap.bitmaskclient.R;
@@ -54,7 +53,6 @@ import se.leap.bitmaskclient.R;
import static android.app.Activity.RESULT_CANCELED;
import static android.app.Activity.RESULT_OK;
import static android.content.Intent.CATEGORY_DEFAULT;
-import static de.blinkt.openvpn.LaunchVPN.EXTRA_TEMP_VPN_PROFILE;
import static se.leap.bitmaskclient.Constants.BROADCAST_EIP_EVENT;
import static se.leap.bitmaskclient.Constants.BROADCAST_GATEWAY_SETUP_OBSERVER_EVENT;
import static se.leap.bitmaskclient.Constants.BROADCAST_RESULT_CODE;
@@ -70,6 +68,7 @@ import static se.leap.bitmaskclient.Constants.EIP_N_CLOSEST_GATEWAY;
import static se.leap.bitmaskclient.Constants.EIP_RECEIVER;
import static se.leap.bitmaskclient.Constants.EIP_REQUEST;
import static se.leap.bitmaskclient.Constants.EIP_RESTART_ON_BOOT;
+import static se.leap.bitmaskclient.Constants.PROVIDER_PROFILE;
import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE;
import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES;
import static se.leap.bitmaskclient.MainActivityErrorDialog.DOWNLOAD_ERRORS.ERROR_INVALID_VPN_CERTIFICATE;
@@ -243,7 +242,7 @@ public final class EIP extends JobIntentService implements Observer {
*/
private void launchActiveGateway(@NonNull Gateway gateway, int nClosestGateway) {
Intent intent = new Intent(BROADCAST_GATEWAY_SETUP_OBSERVER_EVENT);
- intent.putExtra(EXTRA_TEMP_VPN_PROFILE, gateway.getProfile());
+ intent.putExtra(PROVIDER_PROFILE, gateway.getProfile());
intent.putExtra(Gateway.KEY_N_CLOSEST_GATEWAY, nClosestGateway);
LocalBroadcastManager.getInstance(this).sendBroadcast(intent);
@@ -401,7 +400,6 @@ public final class EIP extends JobIntentService implements Observer {
return false;
}
- ProfileManager.setConntectedVpnProfileDisconnected(this);
try {
return openVpnServiceConnection.getService().stopVPN(false);
} catch (RemoteException e) {
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java
index fc07c521..64904816 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/EipStatus.java
@@ -25,9 +25,7 @@ import java.util.Observable;
import de.blinkt.openvpn.core.ConnectionStatus;
import de.blinkt.openvpn.core.LogItem;
-import de.blinkt.openvpn.core.ProfileManager;
import de.blinkt.openvpn.core.VpnStatus;
-import se.leap.bitmaskclient.Provider;
/**
* EipStatus is a Singleton that represents a reduced set of a vpn's ConnectionStatus.
@@ -37,6 +35,7 @@ import se.leap.bitmaskclient.Provider;
public class EipStatus extends Observable implements VpnStatus.StateListener {
public static String TAG = EipStatus.class.getSimpleName();
private static EipStatus currentStatus;
+
public enum EipLevel {
CONNECTING,
DISCONNECTING,
@@ -99,7 +98,7 @@ public class EipStatus extends Observable implements VpnStatus.StateListener {
currentEipLevel = EipLevel.CONNECTED;
break;
case LEVEL_VPNPAUSED:
- if (ProfileManager.getLastConnectedVpn() != null && ProfileManager.getLastConnectedVpn().mPersistTun) {
+ if (VpnStatus.getLastConnectedVpnProfile() != null && VpnStatus.getLastConnectedVpnProfile().mPersistTun) {
//if persistTun is enabled, treat EipLevel as connecting as it *shouldn't* allow passing traffic in the clear...
currentEipLevel = EipLevel.CONNECTING;
} else {
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java
index 317a91bd..55ade1ae 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/Gateway.java
@@ -132,4 +132,18 @@ public class Gateway {
public String toString() {
return new Gson().toJson(this, Gateway.class);
}
+
+ @Override
+ public boolean equals(Object obj) {
+ return obj instanceof Gateway &&
+ (this.mVpnProfile != null &&
+ ((Gateway) obj).mVpnProfile != null &&
+ this.mVpnProfile.mConnections != null &&
+ ((Gateway) obj).mVpnProfile != null &&
+ this.mVpnProfile.mConnections.length > 0 &&
+ ((Gateway) obj).mVpnProfile.mConnections.length > 0 &&
+ this.mVpnProfile.mConnections[0].mServerName != null &&
+ this.mVpnProfile.mConnections[0].mServerName.equals(((Gateway) obj).mVpnProfile.mConnections[0].mServerName)) ||
+ this.mVpnProfile == null && ((Gateway) obj).mVpnProfile == null;
+ }
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java
index 003cef7d..060843fd 100644
--- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java
+++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java
@@ -1,5 +1,5 @@
/**
- * Copyright (c) 2013, 2014, 2015 LEAP Encryption Access Project and contributers
+ * Copyright (c) 2013 - 2019 LEAP Encryption Access Project and contributors
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
@@ -18,7 +18,6 @@ package se.leap.bitmaskclient.eip;
import android.content.Context;
import android.content.SharedPreferences;
-import android.util.Log;
import com.google.gson.Gson;
import com.google.gson.reflect.TypeToken;
@@ -29,13 +28,8 @@ import org.json.JSONObject;
import java.lang.reflect.Type;
import java.util.ArrayList;
-import java.util.Collection;
-import java.util.Iterator;
import java.util.List;
-import de.blinkt.openvpn.VpnProfile;
-import de.blinkt.openvpn.core.Connection;
-import de.blinkt.openvpn.core.ProfileManager;
import se.leap.bitmaskclient.Provider;
import se.leap.bitmaskclient.utils.PreferenceHelper;
@@ -52,13 +46,11 @@ public class GatewaysManager {
private Context context;
private SharedPreferences preferences;
private List<Gateway> gateways = new ArrayList<>();
- private ProfileManager profileManager;
private Type listType = new TypeToken<ArrayList<Gateway>>() {}.getType();
GatewaysManager(Context context, SharedPreferences preferences) {
this.context = context;
this.preferences = preferences;
- profileManager = ProfileManager.getInstance(context);
}
/**
@@ -102,7 +94,7 @@ public class GatewaysManager {
if (isOpenVpnGateway(gw)) {
JSONObject secrets = secretsConfiguration();
Gateway aux = new Gateway(eipDefinition, secrets, gw);
- if (!containsProfileWithSecrets(aux.getProfile())) {
+ if (!gateways.contains(aux)) {
addGateway(aux);
}
}
@@ -139,85 +131,19 @@ public class GatewaysManager {
return result;
}
- private boolean containsProfileWithSecrets(VpnProfile profile) {
- boolean result = false;
- Collection<VpnProfile> profiles = profileManager.getProfiles();
- for (VpnProfile aux : profiles) {
- result = result || sameConnections(profile.mConnections, aux.mConnections)
- && profile.mClientCertFilename.equalsIgnoreCase(aux.mClientCertFilename)
- && profile.mClientKeyFilename.equalsIgnoreCase(aux.mClientKeyFilename);
- }
- return result;
- }
-
- void clearGatewaysAndProfiles() {
+ void clearGateways() {
gateways.clear();
- ArrayList<VpnProfile> profiles = new ArrayList<>(profileManager.getProfiles());
- for (VpnProfile profile : profiles) {
- profileManager.removeProfile(context, profile);
- }
}
private void addGateway(Gateway gateway) {
- removeDuplicatedGateway(gateway);
gateways.add(gateway);
-
- VpnProfile profile = gateway.getProfile();
- profileManager.addProfile(profile);
- }
-
- private void removeDuplicatedGateway(Gateway gateway) {
- Iterator<Gateway> it = gateways.iterator();
- List<Gateway> gatewaysToRemove = new ArrayList<>();
- while (it.hasNext()) {
- Gateway aux = it.next();
- if (sameConnections(aux.getProfile().mConnections, gateway.getProfile().mConnections)) {
- gatewaysToRemove.add(aux);
- }
- }
- gateways.removeAll(gatewaysToRemove);
- removeDuplicatedProfiles(gateway.getProfile());
- }
-
- private void removeDuplicatedProfiles(VpnProfile original) {
- Collection<VpnProfile> profiles = profileManager.getProfiles();
- List<VpnProfile> removeList = new ArrayList<>();
- for (VpnProfile aux : profiles) {
- if (sameConnections(original.mConnections, aux.mConnections)) {
- removeList.add(aux);
- }
- }
- for (VpnProfile profile : removeList) {
- profileManager.removeProfile(context, profile);
- }
- }
-
- /**
- * check if all connections in c1 are also in c2
- * @param c1 array of connections
- * @param c2 array of connections
- * @return true if all connections of c1 exist in c2 and vice versa
- */
- private boolean sameConnections(Connection[] c1, Connection[] c2) {
- int sameConnections = 0;
- for (Connection c1_aux : c1) {
- for (Connection c2_aux : c2)
- if (c2_aux.mServerName.equals(c1_aux.mServerName)) {
- sameConnections++;
- break;
- }
- }
- return c1.length == c2.length && c1.length == sameConnections;
}
/**
* read EipServiceJson from preferences and set gateways
*/
void configureFromPreferences() {
- //TODO: THIS IS A QUICK FIX - it deletes all profiles in ProfileManager, thus it's possible
- // to add all gateways from prefs without duplicates, but this should be refactored.
- clearGatewaysAndProfiles();
fromEipServiceJson(
PreferenceHelper.getEipDefinitionFromPreferences(preferences)
);
diff --git a/app/src/main/java/se/leap/bitmaskclient/fragments/AlwaysOnDialog.java b/app/src/main/java/se/leap/bitmaskclient/fragments/AlwaysOnDialog.java
index e3d004f5..09817b78 100644
--- a/app/src/main/java/se/leap/bitmaskclient/fragments/AlwaysOnDialog.java
+++ b/app/src/main/java/se/leap/bitmaskclient/fragments/AlwaysOnDialog.java
@@ -1,13 +1,14 @@
package se.leap.bitmaskclient.fragments;
import android.app.Dialog;
-import android.content.DialogInterface;
import android.content.Intent;
+import android.os.Build;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v7.app.AlertDialog;
import android.support.v7.app.AppCompatDialogFragment;
+import android.support.v7.widget.AppCompatTextView;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.CheckBox;
@@ -36,6 +37,10 @@ public class AlwaysOnDialog extends AppCompatDialogFragment {
@InjectView(R.id.user_message)
IconTextView userMessage;
+ @InjectView(R.id.block_vpn_user_message)
+ AppCompatTextView blockVpnUserMessage;
+
+
@Override
public void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
@@ -51,22 +56,21 @@ public class AlwaysOnDialog extends AppCompatDialogFragment {
userMessage.setIcon(R.drawable.ic_settings);
userMessage.setText(getString(R.string.always_on_vpn_user_message));
+
+ if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
+ blockVpnUserMessage.setVisibility(View.VISIBLE);
+ }
+
builder.setView(view)
- .setPositiveButton(android.R.string.ok, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- if (doNotShowAgainCheckBox.isChecked()) {
- saveShowAlwaysOnDialog(getContext(), false);
- }
- Intent intent = new Intent("android.net.vpn.SETTINGS");
- intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
- startActivity(intent);
+ .setPositiveButton(android.R.string.ok, (dialog, id) -> {
+ if (doNotShowAgainCheckBox.isChecked()) {
+ saveShowAlwaysOnDialog(getContext(), false);
}
+ Intent intent = new Intent("android.net.vpn.SETTINGS");
+ intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+ startActivity(intent);
})
- .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
- public void onClick(DialogInterface dialog, int id) {
- dialog.cancel();
- }
- });
+ .setNegativeButton(R.string.cancel, (dialog, id) -> dialog.cancel());
return builder.create();
}
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/fragments/LogFragment.java b/app/src/main/java/se/leap/bitmaskclient/fragments/LogFragment.java
index 268d326d..64d199dc 100644
--- a/app/src/main/java/se/leap/bitmaskclient/fragments/LogFragment.java
+++ b/app/src/main/java/se/leap/bitmaskclient/fragments/LogFragment.java
@@ -8,16 +8,12 @@ package se.leap.bitmaskclient.fragments;
import android.animation.Animator;
import android.animation.AnimatorListenerAdapter;
import android.animation.ObjectAnimator;
-import android.app.Activity;
-import android.app.AlertDialog;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context;
-import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Resources;
import android.database.DataSetObserver;
-import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.Handler;
import android.os.Handler.Callback;
@@ -27,7 +23,6 @@ import android.support.annotation.Nullable;
import android.support.v4.app.ListFragment;
import android.text.SpannableString;
import android.text.format.DateFormat;
-import android.text.style.ImageSpan;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuInflater;
@@ -37,7 +32,6 @@ import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.CheckBox;
-import android.widget.CompoundButton;
import android.widget.LinearLayout;
import android.widget.ListAdapter;
import android.widget.ListView;
@@ -52,17 +46,16 @@ import java.util.Date;
import java.util.Locale;
import java.util.Vector;
-import de.blinkt.openvpn.LaunchVPN;
import de.blinkt.openvpn.VpnProfile;
import de.blinkt.openvpn.core.ConnectionStatus;
import de.blinkt.openvpn.core.LogItem;
import de.blinkt.openvpn.core.OpenVPNManagement;
import de.blinkt.openvpn.core.OpenVPNService;
import de.blinkt.openvpn.core.Preferences;
-import de.blinkt.openvpn.core.ProfileManager;
import de.blinkt.openvpn.core.VpnStatus;
import de.blinkt.openvpn.core.VpnStatus.LogListener;
import de.blinkt.openvpn.core.VpnStatus.StateListener;
+import se.leap.bitmaskclient.Constants;
import se.leap.bitmaskclient.R;
import static de.blinkt.openvpn.core.OpenVPNService.humanReadableByteCount;
@@ -70,7 +63,6 @@ import static de.blinkt.openvpn.core.OpenVPNService.humanReadableByteCount;
public class LogFragment extends ListFragment implements StateListener, SeekBar.OnSeekBarChangeListener, RadioGroup.OnCheckedChangeListener, VpnStatus.ByteCountListener {
public static final String TAG = LogFragment.class.getSimpleName();
private static final String LOGTIMEFORMAT = "logtimeformat";
- private static final int START_VPN_CONFIG = 0;
private static final String VERBOSITYLEVEL = "verbositylevel";
@@ -242,7 +234,6 @@ public class LogFragment extends ListFragment implements StateListener, SeekBar.
SpannableString t = new SpannableString(msg);
- //t.setSpan(getSpanImage(le,(int)v.getTextSize()),spanStart,spanStart+1, Spanned.SPAN_INCLUSIVE_INCLUSIVE);
v.setText(t);
return v;
}
@@ -264,34 +255,6 @@ public class LogFragment extends ListFragment implements StateListener, SeekBar.
}
- private ImageSpan getSpanImage(LogItem li, int imageSize) {
- int imageRes = android.R.drawable.ic_menu_call;
-
- switch (li.getLogLevel()) {
- case ERROR:
- imageRes = android.R.drawable.ic_notification_clear_all;
- break;
- case INFO:
- imageRes = android.R.drawable.ic_menu_compass;
- break;
- case VERBOSE:
- imageRes = android.R.drawable.ic_menu_info_details;
- break;
- case WARNING:
- imageRes = android.R.drawable.ic_menu_camera;
- break;
- }
-
- Drawable d = getResources().getDrawable(imageRes);
-
-
- //d.setBounds(0, 0, d.getIntrinsicWidth(), d.getIntrinsicHeight());
- d.setBounds(0, 0, imageSize, imageSize);
- ImageSpan span = new ImageSpan(d, ImageSpan.ALIGN_BOTTOM);
-
- return span;
- }
-
@Override
public int getItemViewType(int position) {
return 0;
@@ -488,39 +451,6 @@ public class LogFragment extends ListFragment implements StateListener, SeekBar.
}
@Override
- public void onActivityResult(int requestCode, int resultCode, Intent data) {
- if (requestCode == START_VPN_CONFIG && resultCode == Activity.RESULT_OK) {
- String configuredVPN = data.getStringExtra(VpnProfile.EXTRA_PROFILEUUID);
-
- final VpnProfile profile = ProfileManager.get(getActivity(), configuredVPN);
- ProfileManager.getInstance(getActivity()).saveProfile(getActivity(), profile);
- // Name could be modified, reset List adapter
-
- AlertDialog.Builder dialog = new AlertDialog.Builder(getActivity());
- dialog.setTitle(R.string.configuration_changed);
- dialog.setMessage(R.string.restart_vpn_after_change);
-
-
- dialog.setPositiveButton(R.string.restart,
- new DialogInterface.OnClickListener() {
- @Override
- public void onClick(DialogInterface dialog, int which) {
- Intent intent = new Intent(getActivity(), LaunchVPN.class);
- intent.putExtra(LaunchVPN.EXTRA_KEY, profile.getUUIDString());
- intent.setAction(Intent.ACTION_MAIN);
- startActivity(intent);
- }
-
-
- });
- dialog.setNegativeButton(R.string.ignore, null);
- dialog.create().show();
- }
- super.onActivityResult(requestCode, resultCode, data);
- }
-
-
- @Override
public void onStop() {
super.onStop();
VpnStatus.removeStateListener(this);
@@ -566,7 +496,7 @@ public class LogFragment extends ListFragment implements StateListener, SeekBar.
setListAdapter(ladapter);
- mTimeRadioGroup = (RadioGroup) v.findViewById(R.id.timeFormatRadioGroup);
+ mTimeRadioGroup = v.findViewById(R.id.timeFormatRadioGroup);
mTimeRadioGroup.setOnCheckedChangeListener(this);
if (ladapter.mTimeFormat == LogWindowListAdapter.TIME_FORMAT_ISO) {
@@ -577,19 +507,15 @@ public class LogFragment extends ListFragment implements StateListener, SeekBar.
mTimeRadioGroup.check(R.id.radioShort);
}
- mClearLogCheckBox = (CheckBox) v.findViewById(R.id.clearlogconnect);
- mClearLogCheckBox.setChecked(PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean(LaunchVPN.CLEARLOG, true));
- mClearLogCheckBox.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
- @Override
- public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
- Preferences.getDefaultSharedPreferences(getActivity()).edit().putBoolean(LaunchVPN.CLEARLOG, isChecked).apply();
- }
- });
+ mClearLogCheckBox = v.findViewById(R.id.clearlogconnect);
+ mClearLogCheckBox.setChecked(PreferenceManager.getDefaultSharedPreferences(getActivity()).getBoolean(Constants.CLEARLOG, true));
+ mClearLogCheckBox.setOnCheckedChangeListener((buttonView, isChecked) ->
+ Preferences.getDefaultSharedPreferences(getActivity()).edit().putBoolean(Constants.CLEARLOG, isChecked).apply());
- mSpeedView = (TextView) v.findViewById(R.id.speed);
+ mSpeedView = v.findViewById(R.id.speed);
- mOptionsLayout = (LinearLayout) v.findViewById(R.id.logOptionsLayout);
- mLogLevelSlider = (SeekBar) v.findViewById(R.id.LogLevelSlider);
+ mOptionsLayout = v.findViewById(R.id.logOptionsLayout);
+ mLogLevelSlider = v.findViewById(R.id.LogLevelSlider);
mLogLevelSlider.setMax(VpnProfile.MAXLOGLEVEL - 1);
mLogLevelSlider.setProgress(logLevel - 1);
@@ -598,9 +524,9 @@ public class LogFragment extends ListFragment implements StateListener, SeekBar.
if (getResources().getBoolean(R.bool.logSildersAlwaysVisible))
mOptionsLayout.setVisibility(View.VISIBLE);
- mUpStatus = (TextView) v.findViewById(R.id.speedUp);
- mDownStatus = (TextView) v.findViewById(R.id.speedDown);
- mConnectStatus = (TextView) v.findViewById(R.id.speedStatus);
+ mUpStatus = v.findViewById(R.id.speedUp);
+ mDownStatus = v.findViewById(R.id.speedDown);
+ mConnectStatus = v.findViewById(R.id.speedStatus);
if (mShowOptionsLayout)
mOptionsLayout.setVisibility(View.VISIBLE);
return v;
@@ -635,17 +561,13 @@ public class LogFragment extends ListFragment implements StateListener, SeekBar.
if (isAdded()) {
final String cleanLogMessage = VpnStatus.getLastCleanLogMessage(getActivity());
- getActivity().runOnUiThread(new Runnable() {
-
- @Override
- public void run() {
- if (isAdded()) {
- if (mSpeedView != null) {
- mSpeedView.setText(cleanLogMessage);
- }
- if (mConnectStatus != null)
- mConnectStatus.setText(cleanLogMessage);
+ getActivity().runOnUiThread(() -> {
+ if (isAdded()) {
+ if (mSpeedView != null) {
+ mSpeedView.setText(cleanLogMessage);
}
+ if (mConnectStatus != null)
+ mConnectStatus.setText(cleanLogMessage);
}
});
}
diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
index 44831049..f5cf590b 100644
--- a/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
+++ b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java
@@ -16,10 +16,13 @@ import java.util.List;
import java.util.Locale;
import java.util.Map;
+import de.blinkt.openvpn.VpnProfile;
import se.leap.bitmaskclient.Provider;
+import static android.content.Context.MODE_PRIVATE;
import static se.leap.bitmaskclient.Constants.ALWAYS_ON_SHOW_DIALOG;
import static se.leap.bitmaskclient.Constants.DEFAULT_SHARED_PREFS_BATTERY_SAVER;
+import static se.leap.bitmaskclient.Constants.LAST_USED_PROFILE;
import static se.leap.bitmaskclient.Constants.PREFERENCES_APP_VERSION;
import static se.leap.bitmaskclient.Constants.PROVIDER_CONFIGURED;
import static se.leap.bitmaskclient.Constants.PROVIDER_EIP_DEFINITION;
@@ -140,6 +143,27 @@ public class PreferenceHelper {
apply();
}
+ /**
+ * Sets the profile that is connected (to connect if the service restarts)
+ */
+ public static void setLastUsedVpnProfile(Context context, VpnProfile connectedProfile) {
+ SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
+ SharedPreferences.Editor prefsedit = prefs.edit();
+ prefsedit.putString(LAST_USED_PROFILE, connectedProfile.toJson());
+ prefsedit.apply();
+ }
+
+ /**
+ * Returns the profile that was last connected (to connect if the service restarts)
+ */
+ public static VpnProfile getLastConnectedVpnProfile(Context context) {
+ SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
+ String lastConnectedProfileJson = preferences.getString(LAST_USED_PROFILE, null);
+ return VpnProfile.fromJson(lastConnectedProfileJson);
+ }
+
+
+
public static void clearDataOfLastProvider(SharedPreferences preferences) {
clearDataOfLastProvider(preferences, false);
@@ -206,7 +230,7 @@ public class PreferenceHelper {
if (context == null) {
return;
}
- SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
+ SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
preferences.edit().putBoolean(ALWAYS_ON_SHOW_DIALOG, showAlwaysOnDialog).apply();
}
@@ -214,7 +238,7 @@ public class PreferenceHelper {
if (context == null) {
return true;
}
- SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
+ SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
return preferences.getBoolean(ALWAYS_ON_SHOW_DIALOG, true);
}
@@ -232,36 +256,14 @@ public class PreferenceHelper {
return result;
}
- /*public static void saveLastProfile(Context context, String uuid) {
- if (context == null) {
- return;
- }
- putString(context, PROVIDER_PROFILE_UUID, uuid);
- }
-
- public static void clearLastProfile(Context context) {
- if (context == null) {
- return;
- }
- SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
- preferences.edit().remove(PROVIDER_PROFILE_UUID).apply();
- }
-
- public static String getLastProfile(Context context){
- return getString(context, PROVIDER_PROFILE_UUID, null);
- }
-
- public static void saveLastGatewayNumber(Context context, int number) {
-
- }
-*/
public static String getString(Context context, String key, String defValue) {
- SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
+ SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
return preferences.getString(key, defValue);
}
public static void putString(Context context, String key, String value){
- SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, Context.MODE_PRIVATE);
+ SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE);
preferences.edit().putString(key, value).apply();
}
+
}