From 7cd685e486e735c3994d59c01df98ac2f82dc0b2 Mon Sep 17 00:00:00 2001 From: Fup Duck Date: Tue, 19 Dec 2017 15:09:40 +0100 Subject: refactor dashboard --- app/src/main/java/se/leap/bitmaskclient/eip/EIP.java | 18 ++++++++++++------ .../bitmaskclient/eip/VpnCertificateValidator.java | 9 ++++++--- .../se/leap/bitmaskclient/eip/VpnConfigGenerator.java | 9 ++++++--- 3 files changed, 24 insertions(+), 12 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/eip') 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 a84ab941..998d2c87 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -16,15 +16,21 @@ */ package se.leap.bitmaskclient.eip; -import android.app.*; -import android.content.*; -import android.os.*; +import android.app.Activity; +import android.app.IntentService; +import android.content.Context; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; +import android.os.ResultReceiver; import android.util.Log; -import org.json.*; +import org.json.JSONException; +import org.json.JSONObject; -import de.blinkt.openvpn.*; -import se.leap.bitmaskclient.*; +import de.blinkt.openvpn.LaunchVPN; +import se.leap.bitmaskclient.OnBootReceiver; +import se.leap.bitmaskclient.VpnFragment; import static se.leap.bitmaskclient.Constants.EIP_ACTION_CHECK_CERT_VALIDITY; import static se.leap.bitmaskclient.Constants.EIP_ACTION_IS_RUNNING; diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java index 28099f06..197a080b 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnCertificateValidator.java @@ -16,10 +16,13 @@ */ package se.leap.bitmaskclient.eip; -import java.security.cert.*; -import java.util.*; +import java.security.cert.CertificateExpiredException; +import java.security.cert.CertificateNotYetValidException; +import java.security.cert.X509Certificate; +import java.util.Calendar; +import java.util.Date; -import se.leap.bitmaskclient.*; +import se.leap.bitmaskclient.ConfigHelper; public class VpnCertificateValidator { public final static String TAG = VpnCertificateValidator.class.getSimpleName(); diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index 54563ec4..b1318def 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -16,11 +16,14 @@ */ package se.leap.bitmaskclient.eip; -import org.json.*; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; -import java.util.*; +import java.util.Iterator; -import se.leap.bitmaskclient.*; +import se.leap.bitmaskclient.Constants; +import se.leap.bitmaskclient.Provider; public class VpnConfigGenerator { -- cgit v1.2.3 From 5d5d6ae8aeafbba407b9a4cf5985a1cdc1cf2904 Mon Sep 17 00:00:00 2001 From: Fup Duck Date: Thu, 11 Jan 2018 19:41:28 +0100 Subject: rename variables to CamelCase --- .../main/java/se/leap/bitmaskclient/eip/EIP.java | 58 ++++++++++---------- .../leap/bitmaskclient/eip/VpnConfigGenerator.java | 64 +++++++++++----------- 2 files changed, 61 insertions(+), 61 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/eip') 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 998d2c87..a2ac9d66 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/EIP.java @@ -63,8 +63,8 @@ public final class EIP extends IntentService { private static ResultReceiver mReceiver; private static SharedPreferences preferences; - private static JSONObject eip_definition; - private static GatewaysManager gateways_manager = new GatewaysManager(); + private static JSONObject eipDefinition; + private static GatewaysManager gatewaysManager = new GatewaysManager(); private static Gateway gateway; public EIP() { @@ -76,8 +76,8 @@ public final class EIP extends IntentService { super.onCreate(); context = getApplicationContext(); preferences = getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); - eip_definition = eipDefinitionFromPreferences(); - if (gateways_manager.isEmpty()) + eipDefinition = eipDefinitionFromPreferences(); + if (gatewaysManager.isEmpty()) gatewaysFromPreferences(); } @@ -106,13 +106,13 @@ public final class EIP extends IntentService { * It also sets up early routes. */ private void startEIP() { - if (gateways_manager.isEmpty()) + if (gatewaysManager.isEmpty()) updateEIPService(); if (!EipStatus.getInstance().isBlockingVpnEstablished()) { earlyRoutes(); } - gateway = gateways_manager.select(); + gateway = gatewaysManager.select(); if (gateway != null && gateway.getProfile() != null) { mReceiver = VpnFragment.getReceiver(); launchActiveGateway(); @@ -128,10 +128,10 @@ public final class EIP extends IntentService { private void startAlwaysOnEIP() { Log.d(TAG, "startAlwaysOnEIP vpn"); - if (gateways_manager.isEmpty()) + if (gatewaysManager.isEmpty()) updateEIPService(); - gateway = gateways_manager.select(); + gateway = gatewaysManager.select(); if (gateway != null && gateway.getProfile() != null) { //mReceiver = VpnFragment.getReceiver(); @@ -147,9 +147,9 @@ public final class EIP extends IntentService { * VpnService is started properly. */ private void earlyRoutes() { - Intent void_vpn_launcher = new Intent(context, VoidVpnLauncher.class); - void_vpn_launcher.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); - startActivity(void_vpn_launcher); + Intent voidVpnLauncher = new Intent(context, VoidVpnLauncher.class); + voidVpnLauncher.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK); + startActivity(voidVpnLauncher); } private void launchActiveGateway() { @@ -162,12 +162,12 @@ public final class EIP extends IntentService { } private void stopEIP() { - EipStatus eip_status = EipStatus.getInstance(); - int result_code = Activity.RESULT_CANCELED; - if (eip_status.isConnected() || eip_status.isConnecting()) - result_code = Activity.RESULT_OK; + EipStatus eipStatus = EipStatus.getInstance(); + int resultCode = Activity.RESULT_CANCELED; + if (eipStatus.isConnected() || eipStatus.isConnecting()) + resultCode = Activity.RESULT_OK; - tellToReceiver(EIP_ACTION_STOP, result_code); + tellToReceiver(EIP_ACTION_STOP, resultCode); } /** @@ -176,8 +176,8 @@ public final class EIP extends IntentService { * request if it's not connected, Activity.RESULT_OK otherwise. */ private void isRunning() { - EipStatus eip_status = EipStatus.getInstance(); - int resultCode = (eip_status.isConnected()) ? + EipStatus eipStatus = EipStatus.getInstance(); + int resultCode = (eipStatus.isConnected()) ? Activity.RESULT_OK : Activity.RESULT_CANCELED; tellToReceiver(EIP_ACTION_IS_RUNNING, resultCode); @@ -188,8 +188,8 @@ public final class EIP extends IntentService { * TODO Implement API call to refresh eip-service.json from the provider */ private void updateEIPService() { - eip_definition = eipDefinitionFromPreferences(); - if (eip_definition.length() > 0) + eipDefinition = eipDefinitionFromPreferences(); + if (eipDefinition.length() > 0) updateGateways(); tellToReceiver(EIP_ACTION_UPDATE, Activity.RESULT_OK); } @@ -197,9 +197,9 @@ public final class EIP extends IntentService { private JSONObject eipDefinitionFromPreferences() { JSONObject result = new JSONObject(); try { - String eip_definition_string = preferences.getString(PROVIDER_KEY, ""); - if (!eip_definition_string.isEmpty()) { - result = new JSONObject(eip_definition_string); + String eipDefinitionString = preferences.getString(PROVIDER_KEY, ""); + if (!eipDefinitionString.isEmpty()) { + result = new JSONObject(eipDefinitionString); } } catch (JSONException e) { // TODO Auto-generated catch block @@ -209,20 +209,20 @@ public final class EIP extends IntentService { } private void updateGateways() { - gateways_manager.clearGatewaysAndProfiles(); - gateways_manager.fromEipServiceJson(eip_definition); + gatewaysManager.clearGatewaysAndProfiles(); + gatewaysManager.fromEipServiceJson(eipDefinition); gatewaysToPreferences(); } private void gatewaysFromPreferences() { - String gateways_string = preferences.getString(Gateway.TAG, ""); - gateways_manager = new GatewaysManager(context, preferences); - gateways_manager.addFromString(gateways_string); + String gatewaysString = preferences.getString(Gateway.TAG, ""); + gatewaysManager = new GatewaysManager(context, preferences); + gatewaysManager.addFromString(gatewaysString); preferences.edit().remove(Gateway.TAG).apply(); } private void gatewaysToPreferences() { - String gateways_string = gateways_manager.toString(); + String gateways_string = gatewaysManager.toString(); preferences.edit().putString(Gateway.TAG, gateways_string).commit(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index b1318def..e9c16dda 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -32,7 +32,7 @@ public class VpnConfigGenerator { private JSONObject secrets; public final static String TAG = VpnConfigGenerator.class.getSimpleName(); - private final String new_line = System.getProperty("line.separator"); // Platform new line + private final String newLine = System.getProperty("line.separator"); // Platform new line public VpnConfigGenerator(JSONObject general_configuration, JSONObject secrets, JSONObject gateway) { this.general_configuration = general_configuration; @@ -43,25 +43,25 @@ public class VpnConfigGenerator { public String generate() { return generalConfiguration() - + new_line + + newLine + gatewayConfiguration() - + new_line + + newLine + secretsConfiguration() - + new_line + + newLine + androidCustomizations(); } private String generalConfiguration() { - String common_options = ""; + String commonOptions = ""; try { Iterator keys = general_configuration.keys(); while (keys.hasNext()) { String key = keys.next().toString(); - common_options += key + " "; + commonOptions += key + " "; for (String word : String.valueOf(general_configuration.get(key)).split(" ")) - common_options += word + " "; - common_options += new_line; + commonOptions += word + " "; + commonOptions += newLine; } } catch (JSONException e) { @@ -69,31 +69,31 @@ public class VpnConfigGenerator { e.printStackTrace(); } - common_options += "client"; + commonOptions += "client"; - return common_options; + return commonOptions; } private String gatewayConfiguration() { String remotes = ""; - String ip_address_keyword = "ip_address"; - String remote_keyword = "remote"; - String ports_keyword = "ports"; - String protocol_keyword = "protocols"; - String capabilities_keyword = "capabilities"; + String ipAddressKeyword = "ip_address"; + String remoteKeyword = "remote"; + String portsKeyword = "ports"; + String protocolKeyword = "protocols"; + String capabilitiesKeyword = "capabilities"; try { - String ip_address = gateway.getString(ip_address_keyword); - JSONObject capabilities = gateway.getJSONObject(capabilities_keyword); - JSONArray ports = capabilities.getJSONArray(ports_keyword); + String ip_address = gateway.getString(ipAddressKeyword); + JSONObject capabilities = gateway.getJSONObject(capabilitiesKeyword); + JSONArray ports = capabilities.getJSONArray(portsKeyword); for (int i = 0; i < ports.length(); i++) { String port_specific_remotes = ""; int port = ports.getInt(i); - JSONArray protocols = capabilities.getJSONArray(protocol_keyword); + JSONArray protocols = capabilities.getJSONArray(protocolKeyword); for (int j = 0; j < protocols.length(); j++) { String protocol = protocols.optString(j); - String new_remote = remote_keyword + " " + ip_address + " " + port + " " + protocol + new_line; + String new_remote = remoteKeyword + " " + ip_address + " " + port + " " + protocol + newLine; port_specific_remotes += new_remote; } @@ -103,8 +103,8 @@ public class VpnConfigGenerator { // TODO Auto-generated catch block e.printStackTrace(); } - if (remotes.endsWith(new_line)) { - remotes = remotes.substring(0, remotes.lastIndexOf(new_line)); + if (remotes.endsWith(newLine)) { + remotes = remotes.substring(0, remotes.lastIndexOf(newLine)); } return remotes; } @@ -113,26 +113,26 @@ public class VpnConfigGenerator { try { String ca = "" - + new_line + + newLine + secrets.getString(Provider.CA_CERT) - + new_line + + newLine + ""; String key = "" - + new_line + + newLine + secrets.getString(Constants.PROVIDER_PRIVATE_KEY) - + new_line + + newLine + ""; - String openvpn_cert = + String openvpnCert = "" - + new_line + + newLine + secrets.getString(Constants.PROVIDER_VPN_CERTIFICATE) - + new_line + + newLine + ""; - return ca + new_line + key + new_line + openvpn_cert; + return ca + newLine + key + newLine + openvpnCert; } catch (JSONException e) { e.printStackTrace(); return ""; @@ -142,9 +142,9 @@ public class VpnConfigGenerator { private String androidCustomizations() { return "remote-cert-tls server" - + new_line + + newLine + "persist-tun" - + new_line + + newLine + "auth-retry nointeract"; } } -- cgit v1.2.3 From 153e35707ed2854d89c61fdb9f82a6e9ec0db9cc Mon Sep 17 00:00:00 2001 From: Fup Duck Date: Sat, 13 Jan 2018 17:03:14 +0100 Subject: more static constant import and import cleanup --- app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java | 8 +++++--- .../main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/eip') 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 43c16d00..0b330ed9 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/GatewaysManager.java @@ -36,9 +36,11 @@ 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.Constants; import se.leap.bitmaskclient.Provider; +import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY; +import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; + /** * @author parmegv */ @@ -124,8 +126,8 @@ public class GatewaysManager { JSONObject result = new JSONObject(); try { result.put(Provider.CA_CERT, preferences.getString(Provider.CA_CERT, "")); - result.put(Constants.PROVIDER_PRIVATE_KEY, preferences.getString(Constants.PROVIDER_PRIVATE_KEY, "")); - result.put(Constants.PROVIDER_VPN_CERTIFICATE, preferences.getString(Constants.PROVIDER_VPN_CERTIFICATE, "")); + result.put(PROVIDER_PRIVATE_KEY, preferences.getString(PROVIDER_PRIVATE_KEY, "")); + result.put(PROVIDER_VPN_CERTIFICATE, preferences.getString(PROVIDER_VPN_CERTIFICATE, "")); } catch (JSONException e) { e.printStackTrace(); } diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java index e9c16dda..6f0ccf18 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VpnConfigGenerator.java @@ -22,9 +22,11 @@ import org.json.JSONObject; import java.util.Iterator; -import se.leap.bitmaskclient.Constants; import se.leap.bitmaskclient.Provider; +import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY; +import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; + public class VpnConfigGenerator { private JSONObject general_configuration; @@ -121,14 +123,14 @@ public class VpnConfigGenerator { String key = "" + newLine - + secrets.getString(Constants.PROVIDER_PRIVATE_KEY) + + secrets.getString(PROVIDER_PRIVATE_KEY) + newLine + ""; String openvpnCert = "" + newLine - + secrets.getString(Constants.PROVIDER_VPN_CERTIFICATE) + + secrets.getString(PROVIDER_VPN_CERTIFICATE) + newLine + ""; -- cgit v1.2.3 From 76884123b22dbdd7538add0d931c5f2d8e660ed2 Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 16 Jan 2018 13:59:33 +0100 Subject: #8788 implement VpnNotificationManager to handle notifications from VoidVPN and OpenVPN --- .../se/leap/bitmaskclient/eip/VoidVpnService.java | 139 +++++---------------- 1 file changed, 33 insertions(+), 106 deletions(-) (limited to 'app/src/main/java/se/leap/bitmaskclient/eip') diff --git a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java index 792de2cb..6d49d83d 100644 --- a/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java +++ b/app/src/main/java/se/leap/bitmaskclient/eip/VoidVpnService.java @@ -1,19 +1,27 @@ +/** + * Copyright (c) 2013 LEAP Encryption Access Project and contributers + * + * 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 + * the Free Software Foundation, either version 3 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ package se.leap.bitmaskclient.eip; -import android.annotation.TargetApi; import android.app.Notification; -import android.app.NotificationChannel; -import android.app.NotificationManager; -import android.app.PendingIntent; -import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; -import android.graphics.Color; import android.net.VpnService; import android.os.Build; import android.os.ParcelFileDescriptor; -import android.support.v4.app.NotificationCompat; -import android.support.v4.app.NotificationManagerCompat; import android.util.Log; import java.io.IOException; @@ -22,10 +30,9 @@ import java.util.Observer; import de.blinkt.openvpn.core.ConnectionStatus; import de.blinkt.openvpn.core.VpnStatus; -import se.leap.bitmaskclient.Dashboard; import se.leap.bitmaskclient.R; +import se.leap.bitmaskclient.VpnNotificationManager; -import static android.os.Build.VERSION_CODES.O; import static se.leap.bitmaskclient.Constants.EIP_ACTION_START_ALWAYS_ON_EIP; import static se.leap.bitmaskclient.Constants.EIP_ACTION_START_BLOCKING_VPN; import static se.leap.bitmaskclient.Constants.EIP_ACTION_STOP_BLOCKING_VPN; @@ -33,7 +40,7 @@ import static se.leap.bitmaskclient.Constants.EIP_IS_ALWAYS_ON; import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; -public class VoidVpnService extends VpnService implements Observer { +public class VoidVpnService extends VpnService implements Observer, VpnNotificationManager.VpnServiceCallback { static final String TAG = VoidVpnService.class.getSimpleName(); static ParcelFileDescriptor fd; @@ -42,16 +49,15 @@ public class VoidVpnService extends VpnService implements Observer { private static final String STATE_ESTABLISH = "ESTABLISHVOIDVPN"; public static final String NOTIFICATION_CHANNEL_NEWSTATUS_ID = "bitmask_void_vpn_news"; private EipStatus eipStatus; - NotificationManager notificationManager; - NotificationManagerCompat compatNotificationManager; + private VpnNotificationManager notificationManager; @Override public void onCreate() { super.onCreate(); - notificationManager = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - compatNotificationManager = NotificationManagerCompat.from(this); eipStatus = EipStatus.getInstance(); eipStatus.addObserver(this); + notificationManager = new VpnNotificationManager(this, this); + notificationManager.createVoidVpnNotificationChannel(); } @Override @@ -91,24 +97,9 @@ public class VoidVpnService extends VpnService implements Observer { closeFd(); } - @TargetApi(O) - private void createNotificationChannel() { - - // Connection status change messages - CharSequence name = getString(R.string.channel_name_status); - NotificationChannel mChannel = new NotificationChannel(VoidVpnService.NOTIFICATION_CHANNEL_NEWSTATUS_ID, - name, NotificationManagerCompat.IMPORTANCE_DEFAULT); - - mChannel.setDescription(getString(R.string.channel_description_status)); - mChannel.enableLights(true); - - mChannel.setLightColor(Color.BLUE); - notificationManager.createNotificationChannel(mChannel); - } - - private void stop() { - stopNotifications(); + notificationManager.stopNotifications(NOTIFICATION_CHANNEL_NEWSTATUS_ID); + notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_NEWSTATUS_ID); if (thread != null) { thread.interrupt(); } @@ -174,87 +165,23 @@ public class VoidVpnService extends VpnService implements Observer { } if (eipStatus.isBlockingVpnEstablished()) { - showNotification(getString(eipStatus.getLocalizedResId()), - getString(eipStatus.getLocalizedResId()), eipStatus.getLevel()); + notificationManager.buildVoidVpnNotification( + getString(eipStatus.getLocalizedResId()), + getString(eipStatus.getLocalizedResId()), + eipStatus.getLevel()); } else { - stopNotifications(); + notificationManager.stopNotifications(NOTIFICATION_CHANNEL_NEWSTATUS_ID); } } - private void stopNotifications() { - stopForeground(true); - compatNotificationManager.cancel(NOTIFICATION_CHANNEL_NEWSTATUS_ID.hashCode()); - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O && - notificationManager.getNotificationChannel(NOTIFICATION_CHANNEL_NEWSTATUS_ID) != null) { - notificationManager.deleteNotificationChannel(NOTIFICATION_CHANNEL_NEWSTATUS_ID); - } - } - - /** - * @param msg - * @param tickerText - * @param status - */ - private void showNotification(final String msg, String tickerText, ConnectionStatus status) { - if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { - createNotificationChannel(); - } - - int icon = getIconByConnectionStatus(status); - NotificationCompat.Builder nCompatBuilder = new NotificationCompat.Builder(this, NOTIFICATION_CHANNEL_NEWSTATUS_ID); - - nCompatBuilder.setContentTitle(getString(R.string.notifcation_title_bitmask, getString(R.string.void_vpn_title))); - nCompatBuilder.setCategory(NotificationCompat.CATEGORY_SERVICE); - nCompatBuilder.setLocalOnly(true); - nCompatBuilder.setContentText(msg); - nCompatBuilder.setOnlyAlertOnce(true); - nCompatBuilder.setSmallIcon(icon); - if (tickerText != null && !tickerText.equals("")) { - nCompatBuilder.setTicker(tickerText); - } - - nCompatBuilder.setContentIntent(getDashboardIntent()); - //TODO: implement extra Dashboard.ACTION_ASK_TO_CANCEL_BLOCKING_VPN - NotificationCompat.Action.Builder builder = new NotificationCompat.Action.Builder(R.drawable.ic_menu_close_clear_cancel, getString(R.string.vpn_button_turn_off_blocking), getStopVoidVpnIntent()); - nCompatBuilder.addAction(builder.build()); - - Notification notification = nCompatBuilder.build(); - int notificationId = NOTIFICATION_CHANNEL_NEWSTATUS_ID.hashCode(); - compatNotificationManager.notify(notificationId, notification); + @Override + public void onNotificationBuild(int notificationId, Notification notification) { startForeground(notificationId, notification); } - private PendingIntent getDashboardIntent() { - Intent startDashboard = new Intent(this, Dashboard.class); - startDashboard.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | - Intent.FLAG_ACTIVITY_SINGLE_TOP); - return PendingIntent.getActivity(this, 0, startDashboard, PendingIntent.FLAG_CANCEL_CURRENT); - } - - private PendingIntent getStopVoidVpnIntent() { - Intent stopVoidVpnIntent = new Intent (this, VoidVpnService.class); - stopVoidVpnIntent.setAction(EIP_ACTION_STOP_BLOCKING_VPN); - return PendingIntent.getService(this, 0, stopVoidVpnIntent, PendingIntent.FLAG_CANCEL_CURRENT); + @Override + public void onNotificationStop() { + stopForeground(true); } - //TODO: replace with getIconByEipLevel(EipLevel level) - private int getIconByConnectionStatus(ConnectionStatus level) { - switch (level) { - case LEVEL_CONNECTED: - return R.drawable.ic_stat_vpn; - case LEVEL_AUTH_FAILED: - case LEVEL_NONETWORK: - case LEVEL_NOTCONNECTED: - return R.drawable.ic_stat_vpn_offline; - case LEVEL_CONNECTING_NO_SERVER_REPLY_YET: - case LEVEL_WAITING_FOR_USER_INPUT: - case LEVEL_CONNECTING_SERVER_REPLIED: - return R.drawable.ic_stat_vpn_outline; - case LEVEL_BLOCKING: - return R.drawable.ic_stat_vpn_blocking; - case UNKNOWN_LEVEL: - default: - return R.drawable.ic_stat_vpn_offline; - } - } } -- cgit v1.2.3