Showing
5 changed files
with
164 additions
and
10 deletions
... | @@ -2,7 +2,7 @@ apply plugin: 'com.android.library' | ... | @@ -2,7 +2,7 @@ apply plugin: 'com.android.library' |
2 | 2 | ||
3 | ext { | 3 | ext { |
4 | PUBLISH_GROUP_ID = 'ly.warp' | 4 | PUBLISH_GROUP_ID = 'ly.warp' |
5 | - PUBLISH_VERSION = '4.5.3.1' | 5 | + PUBLISH_VERSION = '4.5.4' |
6 | PUBLISH_ARTIFACT_ID = 'warply-android-sdk' | 6 | PUBLISH_ARTIFACT_ID = 'warply-android-sdk' |
7 | } | 7 | } |
8 | 8 | ||
... | @@ -37,7 +37,7 @@ dependencies { | ... | @@ -37,7 +37,7 @@ dependencies { |
37 | api 'androidx.appcompat:appcompat:1.4.1' | 37 | api 'androidx.appcompat:appcompat:1.4.1' |
38 | api 'androidx.recyclerview:recyclerview:1.2.1' | 38 | api 'androidx.recyclerview:recyclerview:1.2.1' |
39 | api 'androidx.cardview:cardview:1.0.0' | 39 | api 'androidx.cardview:cardview:1.0.0' |
40 | - | 40 | + api "androidx.security:security-crypto:1.1.0-alpha03" // For minSDK 23 use 1.0.0, for minSDK 21 use 1.1.0 that is currently in alpha |
41 | api 'org.altbeacon:android-beacon-library:2.19.3' | 41 | api 'org.altbeacon:android-beacon-library:2.19.3' |
42 | api 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2' | 42 | api 'org.jbundle.util.osgi.wrapped:org.jbundle.util.osgi.wrapped.org.apache.http.client:4.1.2' |
43 | api "commons-logging:commons-logging:1.2" | 43 | api "commons-logging:commons-logging:1.2" | ... | ... |
1 | +package ly.warp.sdk.utils; | ||
2 | + | ||
3 | +import android.app.Application; | ||
4 | +import android.content.Context; | ||
5 | +import android.content.SharedPreferences; | ||
6 | +import android.preference.PreferenceManager; | ||
7 | + | ||
8 | +import androidx.security.crypto.EncryptedSharedPreferences; | ||
9 | +import androidx.security.crypto.MasterKey; | ||
10 | + | ||
11 | +import java.io.IOException; | ||
12 | +import java.lang.ref.WeakReference; | ||
13 | +import java.security.GeneralSecurityException; | ||
14 | + | ||
15 | +/** | ||
16 | + * Created by Panagiotis Triantafyllou on 21-Nov-18. | ||
17 | + */ | ||
18 | + | ||
19 | +public class PrefsUtils { | ||
20 | + | ||
21 | + // =========================================================== | ||
22 | + // Constants | ||
23 | + // =========================================================== | ||
24 | + private static final String FILE_SECURE_SHARED_PREFS = "com.gr.gapp.obfuscatepreferences"; | ||
25 | + private static final String KEY_ACC_TOKEN = "key_acc_token"; | ||
26 | + private static final String KEY_REF_TOKEN = "key_ref_token"; | ||
27 | + private static final String KEY_CL_SECRET = "key_cl_secret"; | ||
28 | + private static final String KEY_CID = "key_cid"; | ||
29 | + | ||
30 | + // =========================================================== | ||
31 | + // Fields | ||
32 | + // =========================================================== | ||
33 | + | ||
34 | + private static WeakReference<Application> sContext; | ||
35 | + private static SharedPreferences mSecurePref; | ||
36 | + | ||
37 | + // =========================================================== | ||
38 | + // Methods | ||
39 | + // =========================================================== | ||
40 | + | ||
41 | + public static void initialize(Application application) { | ||
42 | + sContext = new WeakReference<>(application); | ||
43 | + } | ||
44 | + | ||
45 | + private static SharedPreferences getPrefs() { | ||
46 | + return PreferenceManager.getDefaultSharedPreferences(sContext.get()); | ||
47 | + } | ||
48 | + | ||
49 | + private static SharedPreferences getPrefs(Context context) { | ||
50 | + try { | ||
51 | + MasterKey masterKey = new | ||
52 | + MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS). | ||
53 | + setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build(); | ||
54 | + | ||
55 | + SharedPreferences encryptedSharedPreferences = EncryptedSharedPreferences.create( | ||
56 | + context, | ||
57 | + context.getPackageName() + "_preferences", | ||
58 | + masterKey, | ||
59 | + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
60 | + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
61 | + ); | ||
62 | + | ||
63 | + return encryptedSharedPreferences; | ||
64 | + } catch (GeneralSecurityException | IOException e) { | ||
65 | + WarpUtils.log("PrefUtils Get Encrypted Shared Preferences Error", e); | ||
66 | + return PreferenceManager.getDefaultSharedPreferences(context); | ||
67 | + } | ||
68 | + } | ||
69 | + | ||
70 | + // =========================================================== | ||
71 | + // Getter & Setter | ||
72 | + // =========================================================== | ||
73 | + | ||
74 | + /** | ||
75 | + * Removes all user preferences. | ||
76 | + */ | ||
77 | + public static void clearPrefs() { | ||
78 | + getPrefs().edit().clear().apply(); | ||
79 | + } | ||
80 | + | ||
81 | + public static String getAccToken(Context context) { | ||
82 | + return getPrefs(context).getString(KEY_ACC_TOKEN, ""); | ||
83 | + } | ||
84 | + | ||
85 | + public static void setAccToken(Context context, String at) { | ||
86 | + getPrefs(context).edit().putString(KEY_ACC_TOKEN, at).apply(); | ||
87 | + } | ||
88 | + | ||
89 | + public static String getRefToken(Context context) { | ||
90 | + return getPrefs(context).getString(KEY_REF_TOKEN, ""); | ||
91 | + } | ||
92 | + | ||
93 | + public static void setRefToken(Context context, String rt) { | ||
94 | + getPrefs(context).edit().putString(KEY_REF_TOKEN, rt).apply(); | ||
95 | + } | ||
96 | + | ||
97 | + public static String getClSecret(Context context) { | ||
98 | + return getPrefs(context).getString(KEY_CL_SECRET, ""); | ||
99 | + } | ||
100 | + | ||
101 | + public static void setClSecret(Context context, String cs) { | ||
102 | + getPrefs(context).edit().putString(KEY_CL_SECRET, cs).apply(); | ||
103 | + } | ||
104 | + | ||
105 | + public static String getCid(Context context) { | ||
106 | + return getPrefs(context).getString(KEY_CID, ""); | ||
107 | + } | ||
108 | + | ||
109 | + public static void setCid(Context context, String cid) { | ||
110 | + getPrefs(context).edit().putString(KEY_CID, cid).apply(); | ||
111 | + } | ||
112 | +} |
... | @@ -49,6 +49,9 @@ import android.view.View; | ... | @@ -49,6 +49,9 @@ import android.view.View; |
49 | import android.view.animation.Animation; | 49 | import android.view.animation.Animation; |
50 | import android.view.animation.ScaleAnimation; | 50 | import android.view.animation.ScaleAnimation; |
51 | 51 | ||
52 | +import androidx.security.crypto.EncryptedSharedPreferences; | ||
53 | +import androidx.security.crypto.MasterKey; | ||
54 | + | ||
52 | import org.apache.http.client.HttpClient; | 55 | import org.apache.http.client.HttpClient; |
53 | import org.apache.http.client.methods.HttpRequestBase; | 56 | import org.apache.http.client.methods.HttpRequestBase; |
54 | import org.apache.http.params.HttpProtocolParams; | 57 | import org.apache.http.params.HttpProtocolParams; |
... | @@ -59,6 +62,7 @@ import java.io.IOException; | ... | @@ -59,6 +62,7 @@ import java.io.IOException; |
59 | import java.io.InputStream; | 62 | import java.io.InputStream; |
60 | import java.io.InputStreamReader; | 63 | import java.io.InputStreamReader; |
61 | import java.nio.charset.Charset; | 64 | import java.nio.charset.Charset; |
65 | +import java.security.GeneralSecurityException; | ||
62 | import java.security.MessageDigest; | 66 | import java.security.MessageDigest; |
63 | import java.security.NoSuchAlgorithmException; | 67 | import java.security.NoSuchAlgorithmException; |
64 | import java.util.zip.GZIPInputStream; | 68 | import java.util.zip.GZIPInputStream; |
... | @@ -190,11 +194,26 @@ public class WarpUtils { | ... | @@ -190,11 +194,26 @@ public class WarpUtils { |
190 | } | 194 | } |
191 | 195 | ||
192 | private static SharedPreferences getPreferences(Context context) { | 196 | private static SharedPreferences getPreferences(Context context) { |
193 | - | ||
194 | if (_prefs == null) { | 197 | if (_prefs == null) { |
195 | - | 198 | + try { |
196 | - _prefs = context.getSharedPreferences(PREFERENCES_NAME, | 199 | + MasterKey masterKey = new |
197 | - Context.MODE_PRIVATE); | 200 | + MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS). |
201 | + setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build(); | ||
202 | + | ||
203 | + SharedPreferences encryptedSharedPreferences = EncryptedSharedPreferences.create( | ||
204 | + context, | ||
205 | + PREFERENCES_NAME, | ||
206 | + masterKey, | ||
207 | + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
208 | + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
209 | + ); | ||
210 | + | ||
211 | + _prefs = encryptedSharedPreferences; | ||
212 | + } catch (GeneralSecurityException | IOException e) { | ||
213 | + WarpUtils.log("WarpUtils Get Encrypted Shared Preferences Error", e); | ||
214 | + _prefs = context.getSharedPreferences(PREFERENCES_NAME, | ||
215 | + Context.MODE_PRIVATE); | ||
216 | + } | ||
198 | } | 217 | } |
199 | 218 | ||
200 | return _prefs; | 219 | return _prefs; | ... | ... |
... | @@ -5,10 +5,15 @@ import android.content.Context; | ... | @@ -5,10 +5,15 @@ import android.content.Context; |
5 | import android.content.SharedPreferences; | 5 | import android.content.SharedPreferences; |
6 | import android.text.TextUtils; | 6 | import android.text.TextUtils; |
7 | 7 | ||
8 | +import androidx.security.crypto.EncryptedSharedPreferences; | ||
9 | +import androidx.security.crypto.MasterKey; | ||
10 | + | ||
8 | import org.json.JSONArray; | 11 | import org.json.JSONArray; |
9 | import org.json.JSONException; | 12 | import org.json.JSONException; |
10 | import org.json.JSONObject; | 13 | import org.json.JSONObject; |
11 | 14 | ||
15 | +import java.io.IOException; | ||
16 | +import java.security.GeneralSecurityException; | ||
12 | import java.util.Map; | 17 | import java.util.Map; |
13 | import java.util.Set; | 18 | import java.util.Set; |
14 | 19 | ||
... | @@ -20,8 +25,25 @@ public class WarplyPreferences { | ... | @@ -20,8 +25,25 @@ public class WarplyPreferences { |
20 | private SharedPreferences warplySharedPrefs; | 25 | private SharedPreferences warplySharedPrefs; |
21 | 26 | ||
22 | public WarplyPreferences(Context context) { | 27 | public WarplyPreferences(Context context) { |
23 | - this.warplySharedPrefs = context. | 28 | + try { |
24 | - getSharedPreferences(WARPLY_SHARED_PREFS, Activity.MODE_PRIVATE); | 29 | + MasterKey masterKey = new |
30 | + MasterKey.Builder(context, MasterKey.DEFAULT_MASTER_KEY_ALIAS). | ||
31 | + setKeyScheme(MasterKey.KeyScheme.AES256_GCM).build(); | ||
32 | + | ||
33 | + SharedPreferences encryptedSharedPreferences = EncryptedSharedPreferences.create( | ||
34 | + context, | ||
35 | + WARPLY_SHARED_PREFS, | ||
36 | + masterKey, | ||
37 | + EncryptedSharedPreferences.PrefKeyEncryptionScheme.AES256_SIV, | ||
38 | + EncryptedSharedPreferences.PrefValueEncryptionScheme.AES256_GCM | ||
39 | + ); | ||
40 | + | ||
41 | + this.warplySharedPrefs = encryptedSharedPreferences; | ||
42 | + } catch (GeneralSecurityException | IOException e) { | ||
43 | + WarpUtils.log("WarplyPreferences Encrypted Shared Preferences Error", e); | ||
44 | + this.warplySharedPrefs = context. | ||
45 | + getSharedPreferences(WARPLY_SHARED_PREFS, Activity.MODE_PRIVATE); | ||
46 | + } | ||
25 | } | 47 | } |
26 | 48 | ||
27 | public void saveLocationChangedReceiverStatus(String status) { | 49 | public void saveLocationChangedReceiverStatus(String status) { |
... | @@ -48,7 +70,8 @@ public class WarplyPreferences { | ... | @@ -48,7 +70,8 @@ public class WarplyPreferences { |
48 | public String getAppStatus() { | 70 | public String getAppStatus() { |
49 | return warplySharedPrefs.getString("appStatus", "background"); | 71 | return warplySharedPrefs.getString("appStatus", "background"); |
50 | } | 72 | } |
51 | - public boolean isForeground(){ | 73 | + |
74 | + public boolean isForeground() { | ||
52 | return !getAppStatus().equals("background"); | 75 | return !getAppStatus().equals("background"); |
53 | } | 76 | } |
54 | 77 | ... | ... |
... | @@ -30,7 +30,7 @@ public class WarpConstants { | ... | @@ -30,7 +30,7 @@ public class WarpConstants { |
30 | /** | 30 | /** |
31 | * The version of the SDK installed in the device | 31 | * The version of the SDK installed in the device |
32 | */ | 32 | */ |
33 | - public static final String SDK_VERSION = "4.5.3"; | 33 | + public static final String SDK_VERSION = "4.5.4"; |
34 | 34 | ||
35 | /** | 35 | /** |
36 | * The URL of the server where it should ping | 36 | * The URL of the server where it should ping | ... | ... |
-
Please register or login to post a comment