From 6b032b751324a30120cfaabe88940f95171df11f Mon Sep 17 00:00:00 2001 From: cyBerta Date: Tue, 29 Dec 2020 00:54:08 +0100 Subject: new year cleanup: restructure messy project --- .../main/java/se/leap/bitmaskclient/utils/Cmd.java | 91 ------- .../se/leap/bitmaskclient/utils/ConfigHelper.java | 230 ----------------- .../se/leap/bitmaskclient/utils/DateHelper.java | 29 --- .../se/leap/bitmaskclient/utils/FileHelper.java | 46 ---- .../se/leap/bitmaskclient/utils/IPAddress.java | 102 -------- .../bitmaskclient/utils/InputStreamHelper.java | 21 -- .../leap/bitmaskclient/utils/KeyStoreHelper.java | 78 ------ .../leap/bitmaskclient/utils/PreferenceHelper.java | 273 --------------------- .../se/leap/bitmaskclient/utils/ViewHelper.java | 17 -- 9 files changed, 887 deletions(-) delete mode 100644 app/src/main/java/se/leap/bitmaskclient/utils/Cmd.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/utils/FileHelper.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/utils/IPAddress.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/utils/InputStreamHelper.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/utils/KeyStoreHelper.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java delete mode 100644 app/src/main/java/se/leap/bitmaskclient/utils/ViewHelper.java (limited to 'app/src/main/java/se/leap/bitmaskclient/utils') diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/Cmd.java b/app/src/main/java/se/leap/bitmaskclient/utils/Cmd.java deleted file mode 100644 index 7b97add2..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/utils/Cmd.java +++ /dev/null @@ -1,91 +0,0 @@ -/** - * Copyright (c) 2019 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.utils; - -import androidx.annotation.WorkerThread; - -import java.io.IOException; -import java.io.InputStreamReader; -import java.io.OutputStreamWriter; - -public class Cmd { - - private static final String TAG = Cmd.class.getSimpleName(); - - @WorkerThread - public static int runBlockingCmd(String[] cmds, StringBuilder log) throws Exception { - return runCmd(cmds, log, true); - } - - @WorkerThread - private static int runCmd(String[] cmds, StringBuilder log, - boolean waitFor) throws Exception { - - int exitCode = -1; - Process proc = Runtime.getRuntime().exec("sh"); - OutputStreamWriter out = new OutputStreamWriter(proc.getOutputStream()); - - try { - for (String cmd : cmds) { - out.write(cmd); - out.write("\n"); - } - - out.flush(); - out.write("exit\n"); - out.flush(); - } catch (IOException e) { - e.printStackTrace(); - } finally { - out.close(); - } - - if (waitFor) { - // Consume the "stdout" - InputStreamReader reader = new InputStreamReader(proc.getInputStream()); - readToLogString(reader, log); - - // Consume the "stderr" - reader = new InputStreamReader(proc.getErrorStream()); - readToLogString(reader, log); - - try { - exitCode = proc.waitFor(); - } catch (InterruptedException e) { - e.printStackTrace(); - } - } - - return exitCode; - } - - private static void readToLogString(InputStreamReader reader, StringBuilder log) throws IOException { - final char buf[] = new char[10]; - int read = 0; - try { - while ((read = reader.read(buf)) != -1) { - if (log != null) - log.append(buf, 0, read); - } - } catch (IOException e) { - reader.close(); - throw new IOException(e); - } - reader.close(); - } -} diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java deleted file mode 100644 index 5a142d90..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/utils/ConfigHelper.java +++ /dev/null @@ -1,230 +0,0 @@ -/** - * 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.utils; - -import android.content.Context; -import android.content.res.Resources; -import android.os.Build; -import android.os.Looper; -import androidx.annotation.NonNull; -import androidx.annotation.Nullable; -import androidx.annotation.StringRes; - -import org.json.JSONException; -import org.json.JSONObject; -import org.spongycastle.util.encoders.Base64; - -import java.io.ByteArrayInputStream; -import java.io.IOException; -import java.io.InputStream; -import java.math.BigInteger; -import java.security.KeyFactory; -import java.security.MessageDigest; -import java.security.NoSuchAlgorithmException; -import java.security.NoSuchProviderException; -import java.security.cert.CertificateEncodingException; -import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; -import java.security.interfaces.RSAPrivateKey; -import java.security.spec.InvalidKeySpecException; -import java.security.spec.PKCS8EncodedKeySpec; -import java.util.Calendar; - -import se.leap.bitmaskclient.BuildConfig; -import se.leap.bitmaskclient.ProviderAPI; -import se.leap.bitmaskclient.R; - -import static se.leap.bitmaskclient.Constants.DEFAULT_BITMASK; - -/** - * Stores constants, and implements auxiliary methods used across all Bitmask Android classes. - * Wraps BuildConfigFields for to support easier unit testing - * - * @author parmegv - * @author MeanderingCode - */ -public class ConfigHelper { - final public static String NG_1024 = - "eeaf0ab9adb38dd69c33f80afa8fc5e86072618775ff3c0b9ea2314c9c256576d674df7496ea81d3383b4813d692c6e0e0d5d8e250b98be48e495c1d6089dad15dc7d7b46154d6b6ce8ef4ad69b15d4982559b297bcf1885c529f566660e57ec68edbc3c05726cc02fd4cbf4976eaa9afd5138fe8376435b9fc61d2fc0eb06e3"; - final public static BigInteger G = new BigInteger("2"); - - public static boolean checkErroneousDownload(String downloadedString) { - try { - if (downloadedString == null || downloadedString.isEmpty() || new JSONObject(downloadedString).has(ProviderAPI.ERRORS) || new JSONObject(downloadedString).has(ProviderAPI.BACKEND_ERROR_KEY)) { - return true; - } else { - return false; - } - } catch (NullPointerException | JSONException e) { - return false; - } - } - - /** - * Treat the input as the MSB representation of a number, - * and lop off leading zero elements. For efficiency, the - * input is simply returned if no leading zeroes are found. - * - * @param in array to be trimmed - */ - public static byte[] trim(byte[] in) { - if (in.length == 0 || in[0] != 0) - return in; - - int len = in.length; - int i = 1; - while (in[i] == 0 && i < len) - ++i; - byte[] ret = new byte[len - i]; - System.arraycopy(in, i, ret, 0, len - i); - return ret; - } - - public static X509Certificate parseX509CertificateFromString(String certificateString) { - java.security.cert.Certificate certificate = null; - CertificateFactory cf; - try { - cf = CertificateFactory.getInstance("X.509"); - - certificateString = certificateString.replaceFirst("-----BEGIN CERTIFICATE-----", "").replaceFirst("-----END CERTIFICATE-----", "").trim(); - byte[] cert_bytes = Base64.decode(certificateString); - InputStream caInput = new ByteArrayInputStream(cert_bytes); - try { - certificate = cf.generateCertificate(caInput); - System.out.println("ca=" + ((X509Certificate) certificate).getSubjectDN()); - } finally { - caInput.close(); - } - } catch (NullPointerException | CertificateException | IOException | IllegalArgumentException e) { - return null; - } - return (X509Certificate) certificate; - } - - public static RSAPrivateKey parseRsaKeyFromString(String rsaKeyString) { - RSAPrivateKey key; - try { - KeyFactory kf; - if (Build.VERSION.SDK_INT < Build.VERSION_CODES.P) { - kf = KeyFactory.getInstance("RSA", "BC"); - } else { - kf = KeyFactory.getInstance("RSA"); - } - rsaKeyString = rsaKeyString.replaceFirst("-----BEGIN RSA PRIVATE KEY-----", "").replaceFirst("-----END RSA PRIVATE KEY-----", ""); - PKCS8EncodedKeySpec keySpec = new PKCS8EncodedKeySpec(Base64.decode(rsaKeyString)); - key = (RSAPrivateKey) kf.generatePrivate(keySpec); - } catch (InvalidKeySpecException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return null; - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - return null; - } catch (NullPointerException e) { - e.printStackTrace(); - return null; - } catch (NoSuchProviderException e) { - e.printStackTrace(); - return null; - } - - return key; - } - - private static String byteArrayToHex(byte[] input) { - int readBytes = input.length; - StringBuffer hexData = new StringBuffer(); - int onebyte; - for (int i = 0; i < readBytes; i++) { - onebyte = ((0x000000ff & input[i]) | 0xffffff00); - hexData.append(Integer.toHexString(onebyte).substring(6)); - } - return hexData.toString(); - } - - /** - * Calculates the hexadecimal representation of a sha256/sha1 fingerprint of a certificate - * - * @param certificate - * @param encoding - * @return - * @throws NoSuchAlgorithmException - * @throws CertificateEncodingException - */ - @NonNull - public static String getFingerprintFromCertificate(X509Certificate certificate, String encoding) throws NoSuchAlgorithmException, CertificateEncodingException /*, UnsupportedEncodingException*/ { - byte[] byteArray = MessageDigest.getInstance(encoding).digest(certificate.getEncoded()); - return byteArrayToHex(byteArray); - } - - public static void ensureNotOnMainThread(@NonNull Context context) throws IllegalStateException{ - Looper looper = Looper.myLooper(); - if (looper != null && looper == context.getMainLooper()) { - throw new IllegalStateException( - "calling this from your main thread can lead to deadlock"); - } - } - - public static boolean isDefaultBitmask() { - return BuildConfig.FLAVOR_branding.equals(DEFAULT_BITMASK); - } - - public static boolean preferAnonymousUsage() { - return BuildConfig.priotize_anonymous_usage; - } - - public static int getCurrentTimezone() { - return Calendar.getInstance().get(Calendar.ZONE_OFFSET) / 3600000; - } - - public static String getProviderFormattedString(Resources resources, @StringRes int resourceId) { - String appName = resources.getString(R.string.app_name); - return resources.getString(resourceId, appName); - } - - public static boolean stringEqual(@Nullable String string1, @Nullable String string2) { - return (string1 == null && string2 == null) || - (string1 != null && string1.equals(string2)); - } - - public static String getApkFileName() { - try { - return BuildConfig.update_apk_url.substring(BuildConfig.update_apk_url.lastIndexOf("/")); - } catch (Exception e) { - return null; - } - } - - public static String getVersionFileName() { - try { - return BuildConfig.version_file_url.substring(BuildConfig.version_file_url.lastIndexOf("/")); - } catch (Exception e) { - return null; - } - } - - public static String getSignatureFileName() { - try { - return BuildConfig.signature_url.substring(BuildConfig.signature_url.lastIndexOf("/")); - } catch (Exception e) { - return null; - } - } - -} diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java deleted file mode 100644 index 523c8c4c..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/utils/DateHelper.java +++ /dev/null @@ -1,29 +0,0 @@ -package se.leap.bitmaskclient.utils; - -import java.text.ParseException; -import java.text.SimpleDateFormat; -import java.util.Date; -import java.util.Locale; - -/** - * Contains helper methods related to date manipulation. - * - * @author Janak - */ -public class DateHelper { - private static final String DATE_PATTERN = "dd/MM/yyyy"; - private static final int ONE_DAY = 86400000; //1000*60*60*24 - - public static long getDateDiffToCurrentDateInDays(String startDate) throws ParseException { - SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US); - Date lastDate = sdf.parse(startDate); - Date currentDate = new Date(); - return (currentDate.getTime() - lastDate.getTime()) / ONE_DAY; - } - - public static String getCurrentDateString() { - SimpleDateFormat sdf = new SimpleDateFormat(DATE_PATTERN, Locale.US); - Date lastDate = new Date(); - return sdf.format(lastDate); - } -} diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/FileHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/FileHelper.java deleted file mode 100644 index ebcc32ba..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/utils/FileHelper.java +++ /dev/null @@ -1,46 +0,0 @@ -package se.leap.bitmaskclient.utils; - -import android.content.Context; - -import java.io.BufferedReader; -import java.io.File; -import java.io.FileWriter; -import java.io.IOException; -import java.io.InputStream; -import java.io.InputStreamReader; - -/** - * Created by cyberta on 18.03.18. - */ - -public class FileHelper { - public static File createFile(File dir, String fileName) { - return new File(dir, fileName); - } - - public static void persistFile(File file, String content) throws IOException { - FileWriter writer = new FileWriter(file); - writer.write(content); - writer.close(); - } - - public static String readPublicKey(Context context) { - { - InputStream inputStream; - try { - inputStream = context.getAssets().open("public.pgp"); - BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream)); - StringBuilder sb = new StringBuilder(); - String line; - while ((line = reader.readLine()) != null) { - sb.append(line).append("\n"); - } - reader.close(); - return sb.toString(); - } catch (IOException errabi) { - return null; - } - } - } - -} diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/IPAddress.java b/app/src/main/java/se/leap/bitmaskclient/utils/IPAddress.java deleted file mode 100644 index 2e3ef596..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/utils/IPAddress.java +++ /dev/null @@ -1,102 +0,0 @@ -package se.leap.bitmaskclient.utils; - -/* - * Copyright (C) 2006-2008 Alfresco Software Limited. - * - * 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 2 - * 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, write to the Free Software - * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. - - * As a special exception to the terms and conditions of version 2.0 of - * the GPL, you may redistribute this Program in connection with Free/Libre - * and Open Source Software ("FLOSS") applications as described in Alfresco's - * FLOSS exception. You should have recieved a copy of the text describing - * the FLOSS exception, and it is also available here: - * http://www.alfresco.com/legal/licensing" - */ - -import java.util.StringTokenizer; - -/** - * TCP/IP Address Utility Class - * - * @author gkspencer - */ -public class IPAddress { - - - /** - * Convert a TCP/IP address string into a byte array - * - * @param addr String - * @return byte[] - */ - public static byte[] asBytes(String addr) { - - // Convert the TCP/IP address string to an integer value - int ipInt = parseNumericAddress(addr); - if (ipInt == 0) - return null; - - // Convert to bytes - byte[] ipByts = new byte[4]; - - ipByts[3] = (byte) (ipInt & 0xFF); - ipByts[2] = (byte) ((ipInt >> 8) & 0xFF); - ipByts[1] = (byte) ((ipInt >> 16) & 0xFF); - ipByts[0] = (byte) ((ipInt >> 24) & 0xFF); - - // Return the TCP/IP bytes - return ipByts; - } - /** - * Check if the specified address is a valid numeric TCP/IP address and return as an integer value - * - * @param ipaddr String - * @return int - */ - private static int parseNumericAddress(String ipaddr) { - - // Check if the string is valid - if (ipaddr == null || ipaddr.length() < 7 || ipaddr.length() > 15) - return 0; - - // Check the address string, should be n.n.n.n format - StringTokenizer token = new StringTokenizer(ipaddr,"."); - if (token.countTokens() != 4) - return 0; - - int ipInt = 0; - while (token.hasMoreTokens()) { - - // Get the current token and convert to an integer value - String ipNum = token.nextToken(); - - try { - // Validate the current address part - int ipVal = Integer.valueOf(ipNum).intValue(); - if (ipVal < 0 || ipVal > 255) - return 0; - - // Add to the integer address - ipInt = (ipInt << 8) + ipVal; - } - catch (NumberFormatException ex) { - return 0; - } - } - - // Return the integer address - return ipInt; - } -} \ No newline at end of file diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/InputStreamHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/InputStreamHelper.java deleted file mode 100644 index 87996615..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/utils/InputStreamHelper.java +++ /dev/null @@ -1,21 +0,0 @@ -package se.leap.bitmaskclient.utils; - -import java.io.FileInputStream; -import java.io.FileNotFoundException; -import java.io.InputStream; - -/** - * Created by cyberta on 18.03.18. - */ - -public class InputStreamHelper { - //allows us to mock FileInputStream - public static InputStream getInputStreamFrom(String filePath) throws FileNotFoundException { - return new FileInputStream(filePath); - } - - public static String loadInputStreamAsString(InputStream is) { - java.util.Scanner s = new java.util.Scanner(is).useDelimiter("\\A"); - return s.hasNext() ? s.next() : ""; - } -} diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/KeyStoreHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/KeyStoreHelper.java deleted file mode 100644 index 48d4cbad..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/utils/KeyStoreHelper.java +++ /dev/null @@ -1,78 +0,0 @@ -package se.leap.bitmaskclient.utils; - -import java.io.IOException; -import java.io.InputStream; -import java.security.KeyStore; -import java.security.KeyStoreException; -import java.security.NoSuchAlgorithmException; -import java.security.cert.CertificateException; -import java.security.cert.CertificateFactory; -import java.security.cert.X509Certificate; - -/** - * Created by cyberta on 18.03.18. - */ - -public class KeyStoreHelper { - private static KeyStore trustedKeystore; - - /** - * Adds a new X509 certificate given its input stream and its provider name - * - * @param provider used to store the certificate in the keystore - * @param inputStream from which X509 certificate must be generated. - */ - public static void addTrustedCertificate(String provider, InputStream inputStream) { - CertificateFactory cf; - try { - cf = CertificateFactory.getInstance("X.509"); - X509Certificate cert = - (X509Certificate) cf.generateCertificate(inputStream); - trustedKeystore.setCertificateEntry(provider, cert); - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (KeyStoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /** - * Adds a new X509 certificate given in its string from and using its provider name - * - * @param provider used to store the certificate in the keystore - * @param certificate - */ - public static void addTrustedCertificate(String provider, String certificate) { - - try { - X509Certificate cert = ConfigHelper.parseX509CertificateFromString(certificate); - if (trustedKeystore == null) { - trustedKeystore = KeyStore.getInstance("BKS"); - trustedKeystore.load(null); - } - trustedKeystore.setCertificateEntry(provider, cert); - } catch (KeyStoreException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (NoSuchAlgorithmException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (CertificateException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } catch (IOException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - } - - /** - * @return class wide keystore - */ - public static KeyStore getKeystore() { - return trustedKeystore; - } - -} diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java deleted file mode 100644 index 5b62d0ff..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/utils/PreferenceHelper.java +++ /dev/null @@ -1,273 +0,0 @@ -package se.leap.bitmaskclient.utils; - -import android.content.Context; -import android.content.SharedPreferences; -import androidx.annotation.NonNull; - -import org.json.JSONException; -import org.json.JSONObject; - -import java.net.MalformedURLException; -import java.net.URL; -import java.util.HashSet; -import java.util.Set; - -import de.blinkt.openvpn.VpnProfile; -import se.leap.bitmaskclient.Provider; - -import static android.content.Context.MODE_PRIVATE; -import static se.leap.bitmaskclient.Constants.ALLOW_TETHERING_BLUETOOTH; -import static se.leap.bitmaskclient.Constants.ALLOW_TETHERING_USB; -import static se.leap.bitmaskclient.Constants.ALLOW_TETHERING_WIFI; -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.EXCLUDED_APPS; -import static se.leap.bitmaskclient.Constants.LAST_UPDATE_CHECK; -import static se.leap.bitmaskclient.Constants.LAST_USED_PROFILE; -import static se.leap.bitmaskclient.Constants.PROVIDER_CONFIGURED; -import static se.leap.bitmaskclient.Constants.PROVIDER_EIP_DEFINITION; -import static se.leap.bitmaskclient.Constants.PROVIDER_PRIVATE_KEY; -import static se.leap.bitmaskclient.Constants.PROVIDER_VPN_CERTIFICATE; -import static se.leap.bitmaskclient.Constants.RESTART_ON_UPDATE; -import static se.leap.bitmaskclient.Constants.SHARED_PREFERENCES; -import static se.leap.bitmaskclient.Constants.SHOW_EXPERIMENTAL; -import static se.leap.bitmaskclient.Constants.USE_IPv6_FIREWALL; -import static se.leap.bitmaskclient.Constants.USE_PLUGGABLE_TRANSPORTS; - -/** - * Created by cyberta on 18.03.18. - */ - -public class PreferenceHelper { - - public static Provider getSavedProviderFromSharedPreferences(@NonNull SharedPreferences preferences) { - Provider provider = new Provider(); - try { - provider.setMainUrl(new URL(preferences.getString(Provider.MAIN_URL, ""))); - provider.setProviderIp(preferences.getString(Provider.PROVIDER_IP, "")); - provider.setProviderApiIp(preferences.getString(Provider.PROVIDER_API_IP, "")); - provider.setGeoipUrl(preferences.getString(Provider.GEOIP_URL, "")); - provider.define(new JSONObject(preferences.getString(Provider.KEY, ""))); - provider.setCaCert(preferences.getString(Provider.CA_CERT, "")); - provider.setVpnCertificate(preferences.getString(PROVIDER_VPN_CERTIFICATE, "")); - provider.setPrivateKey(preferences.getString(PROVIDER_PRIVATE_KEY, "")); - provider.setEipServiceJson(new JSONObject(preferences.getString(PROVIDER_EIP_DEFINITION, ""))); - } catch (MalformedURLException | JSONException e) { - e.printStackTrace(); - } - - return provider; - } - - public static String getFromPersistedProvider(String toFetch, String providerDomain, SharedPreferences preferences) { - return preferences.getString(toFetch + "." + providerDomain, ""); - } - - // TODO: replace commit with apply after refactoring EIP - //FIXME: don't save private keys in shared preferences! use the keystore - public static void storeProviderInPreferences(SharedPreferences preferences, Provider provider) { - preferences.edit().putBoolean(PROVIDER_CONFIGURED, true). - putString(Provider.PROVIDER_IP, provider.getProviderIp()). - putString(Provider.GEOIP_URL, provider.getGeoipUrl().toString()). - putString(Provider.PROVIDER_API_IP, provider.getProviderApiIp()). - putString(Provider.MAIN_URL, provider.getMainUrlString()). - putString(Provider.KEY, provider.getDefinitionString()). - putString(Provider.CA_CERT, provider.getCaCert()). - putString(PROVIDER_EIP_DEFINITION, provider.getEipServiceJsonString()). - putString(PROVIDER_PRIVATE_KEY, provider.getPrivateKey()). - putString(PROVIDER_VPN_CERTIFICATE, provider.getVpnCertificate()). - commit(); - - String providerDomain = provider.getDomain(); - preferences.edit().putBoolean(PROVIDER_CONFIGURED, true). - putString(Provider.PROVIDER_IP + "." + providerDomain, provider.getProviderIp()). - putString(Provider.PROVIDER_API_IP + "." + providerDomain, provider.getProviderApiIp()). - putString(Provider.MAIN_URL + "." + providerDomain, provider.getMainUrlString()). - putString(Provider.GEOIP_URL + "." + providerDomain, provider.getGeoipUrl().toString()). - putString(Provider.KEY + "." + providerDomain, provider.getDefinitionString()). - putString(Provider.CA_CERT + "." + providerDomain, provider.getCaCert()). - putString(PROVIDER_EIP_DEFINITION + "." + providerDomain, provider.getEipServiceJsonString()). - 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 deleteProviderDetailsFromPreferences(@NonNull SharedPreferences preferences, String providerDomain) { - preferences.edit(). - remove(Provider.KEY + "." + providerDomain). - remove(Provider.CA_CERT + "." + providerDomain). - remove(Provider.PROVIDER_IP + "." + providerDomain). - remove(Provider.PROVIDER_API_IP + "." + providerDomain). - remove(Provider.MAIN_URL + "." + providerDomain). - remove(Provider.GEOIP_URL + "." + providerDomain). - remove(PROVIDER_EIP_DEFINITION + "." + providerDomain). - remove(PROVIDER_PRIVATE_KEY + "." + providerDomain). - remove(PROVIDER_VPN_CERTIFICATE + "." + providerDomain). - apply(); - } - - public static void setLastAppUpdateCheck(Context context) { - putLong(context, LAST_UPDATE_CHECK, System.currentTimeMillis()); - } - - public static long getLastAppUpdateCheck(Context context) { - return getLong(context, LAST_UPDATE_CHECK, 0); - } - - public static void restartOnUpdate(Context context, boolean isEnabled) { - putBoolean(context, RESTART_ON_UPDATE, isEnabled); - } - - public static boolean getRestartOnUpdate(Context context) { - return getBoolean(context, RESTART_ON_UPDATE, false); - } - - public static boolean getUsePluggableTransports(Context context) { - return getBoolean(context, USE_PLUGGABLE_TRANSPORTS, false); - } - - public static void usePluggableTransports(Context context, boolean isEnabled) { - putBoolean(context, USE_PLUGGABLE_TRANSPORTS, isEnabled); - } - - public static void saveBattery(Context context, boolean isEnabled) { - putBoolean(context, DEFAULT_SHARED_PREFS_BATTERY_SAVER, isEnabled); - } - - public static boolean getSaveBattery(Context context) { - return getBoolean(context, DEFAULT_SHARED_PREFS_BATTERY_SAVER, false); - } - - public static void allowUsbTethering(Context context, boolean isEnabled) { - putBoolean(context, ALLOW_TETHERING_USB, isEnabled); - } - - public static boolean isUsbTetheringAllowed(Context context) { - return getBoolean(context, ALLOW_TETHERING_USB, false); - } - - public static void allowWifiTethering(Context context, boolean isEnabled) { - putBoolean(context, ALLOW_TETHERING_WIFI, isEnabled); - } - - public static boolean isWifiTetheringAllowed(Context context) { - return getBoolean(context, ALLOW_TETHERING_WIFI, false); - } - - public static void allowBluetoothTethering(Context context, boolean isEnabled) { - putBoolean(context, ALLOW_TETHERING_BLUETOOTH, isEnabled); - } - - public static boolean isBluetoothTetheringAllowed(Context context) { - return getBoolean(context, ALLOW_TETHERING_BLUETOOTH, false); - } - - public static void setShowExperimentalFeatures(Context context, boolean show) { - putBoolean(context, SHOW_EXPERIMENTAL, show); - } - - public static boolean showExperimentalFeatures(Context context) { - return getBoolean(context, SHOW_EXPERIMENTAL, false); - } - - public static void setUseIPv6Firewall(Context context, boolean useFirewall) { - putBoolean(context, USE_IPv6_FIREWALL, useFirewall); - } - - public static boolean useIpv6Firewall(Context context) { - return getBoolean(context, USE_IPv6_FIREWALL, false); - } - - public static void saveShowAlwaysOnDialog(Context context, boolean showAlwaysOnDialog) { - putBoolean(context, ALWAYS_ON_SHOW_DIALOG, showAlwaysOnDialog); - } - - public static boolean getShowAlwaysOnDialog(Context context) { - return getBoolean(context, ALWAYS_ON_SHOW_DIALOG, true); - } - - public static JSONObject getEipDefinitionFromPreferences(SharedPreferences preferences) { - JSONObject result = new JSONObject(); - try { - String eipDefinitionString = preferences.getString(PROVIDER_EIP_DEFINITION, ""); - if (!eipDefinitionString.isEmpty()) { - result = new JSONObject(eipDefinitionString); - } - } catch (JSONException e) { - // TODO Auto-generated catch block - e.printStackTrace(); - } - return result; - } - - public static void setExcludedApps(Context context, Set apps) { - SharedPreferences prefs = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); - SharedPreferences.Editor prefsedit = prefs.edit(); - prefsedit.putStringSet(EXCLUDED_APPS, apps); - prefsedit.apply(); - } - - public static Set getExcludedApps(Context context) { - if (context == null) { - return null; - } - SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); - return preferences.getStringSet(EXCLUDED_APPS, new HashSet<>()); - } - - public static long getLong(Context context, String key, long defValue) { - SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); - return preferences.getLong(key, defValue); - } - - public static void putLong(Context context, String key, long value) { - SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); - preferences.edit().putLong(key, value).apply(); - } - - public static String getString(Context context, String key, String defValue) { - 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, MODE_PRIVATE); - preferences.edit().putString(key, value).apply(); - } - - public static Boolean getBoolean(Context context, String key, Boolean defValue) { - if (context == null) { - return false; - } - - SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); - return preferences.getBoolean(key, defValue); - } - - public static void putBoolean(Context context, String key, Boolean value) { - if (context == null) { - return; - } - - SharedPreferences preferences = context.getSharedPreferences(SHARED_PREFERENCES, MODE_PRIVATE); - preferences.edit().putBoolean(key, value).apply(); - } - -} diff --git a/app/src/main/java/se/leap/bitmaskclient/utils/ViewHelper.java b/app/src/main/java/se/leap/bitmaskclient/utils/ViewHelper.java deleted file mode 100644 index 5f4fc2a6..00000000 --- a/app/src/main/java/se/leap/bitmaskclient/utils/ViewHelper.java +++ /dev/null @@ -1,17 +0,0 @@ -package se.leap.bitmaskclient.utils; - -import android.content.Context; - -import androidx.annotation.DimenRes; - -/** - * Created by cyberta on 29.06.18. - */ - -public class ViewHelper { - - public static int convertDimensionToPx(Context context, @DimenRes int dimension) { - return context.getResources().getDimensionPixelSize(dimension); - } - -} -- cgit v1.2.3