Skip to content

Commit 8f0062e

Browse files
committed
Updates based on PR feedback
1 parent 4105cbd commit 8f0062e

3 files changed

Lines changed: 29 additions & 67 deletions

File tree

android/sdl_android/src/androidTest/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManagerTests.java

Lines changed: 13 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,6 @@ public class LockScreenDeviceIconManagerTests extends AndroidTestCase2 {
3131
private static final String ICON_URL = "http://i.imgur.com/TgkvOIZ.png";
3232
private static final String LAST_UPDATED_TIME = "lastUpdatedTime";
3333
private static final String STORED_PATH = "storedPath";
34-
private static final String INVALID_JSON_STRING = "Invalid JSON";
3534

3635
public void setup() throws Exception {
3736
super.setUp();
@@ -48,8 +47,8 @@ public void testUpdateCacheImageShouldReturnTrueWhenSharedPreferencesDoesNotExis
4847
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(null);
4948

5049
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
51-
boolean shouldUpdate = lockScreenDeviceIconManager.shouldUpdateCachedImage(ICON_URL);
52-
assertTrue(shouldUpdate);
50+
boolean imageUpToDate = lockScreenDeviceIconManager.isIconCachedAndValid(ICON_URL);
51+
assertFalse(imageUpToDate);
5352
}
5453

5554
public void testUpdateCacheImageShouldReturnTrueWhenUnableToReadSharedPreference() {
@@ -65,8 +64,8 @@ public void testUpdateCacheImageShouldReturnTrueWhenUnableToReadSharedPreference
6564

6665

6766
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
68-
boolean shouldUpdate = lockScreenDeviceIconManager.shouldUpdateCachedImage(ICON_URL);
69-
assertTrue(shouldUpdate);
67+
boolean imageUpToDate = lockScreenDeviceIconManager.isIconCachedAndValid(ICON_URL);
68+
assertFalse(imageUpToDate);
7069
}
7170

7271
public void testUpdateCacheImageShouldReturnTrueSharedPreferenceReturnsAnOutdatedIcon() {
@@ -76,8 +75,8 @@ public void testUpdateCacheImageShouldReturnTrueSharedPreferenceReturnsAnOutdate
7675
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(35));
7776

7877
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
79-
boolean shouldUpdate = lockScreenDeviceIconManager.shouldUpdateCachedImage(ICON_URL);
80-
assertTrue(shouldUpdate);
78+
boolean imageUpToDate = lockScreenDeviceIconManager.isIconCachedAndValid(ICON_URL);
79+
assertFalse(imageUpToDate);
8180
}
8281

8382
public void testUpdateCacheImageShouldReturnFalseWhenSharedPreferenceReturnsAnUpdatedIcon() {
@@ -87,8 +86,8 @@ public void testUpdateCacheImageShouldReturnFalseWhenSharedPreferenceReturnsAnUp
8786
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(15));
8887

8988
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
90-
boolean shouldUpdate = lockScreenDeviceIconManager.shouldUpdateCachedImage(ICON_URL);
91-
assertFalse(shouldUpdate);
89+
boolean imageUpToDate = lockScreenDeviceIconManager.isIconCachedAndValid(ICON_URL);
90+
assertTrue(imageUpToDate);
9291
}
9392

9493
public void testSaveFileToCacheShouldReturnBeforeWritingSharedPrefsIfSavingToCacheFails() {
@@ -170,7 +169,7 @@ public void testGetFileFromCacheShouldReturnNullIfFailedToFindIcon() {
170169
Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor);
171170
Mockito.when(sharedPrefsEditor.remove(anyString())).thenReturn(sharedPrefsEditor);
172171
Mockito.when(sharedPrefsEditor.commit()).thenReturn(true);
173-
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(buildJSONAsString(15, ""));
172+
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(15));
174173

175174
try {
176175
tempFolder.create();
@@ -195,12 +194,14 @@ public void testGetFileFromCacheShouldReturnBitmapIfIconFoundInCache() {
195194
Mockito.when(sharedPrefsEditor.commit()).thenReturn(true);
196195
Bitmap deviceLogo = null;
197196

197+
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
198+
198199
try {
199200
tempFolder.create();
200201
File newFolder = tempFolder.newFolder();
201202
Mockito.when(context.getCacheDir()).thenReturn(newFolder);
202203
deviceLogo = AndroidTools.downloadImage(ICON_URL);
203-
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(buildJSONAsString(15, newFolder.getPath() + "/sdl/lock_screen_icon/" + getMD5HashFromIconUrl(ICON_URL)));
204+
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(15));
204205
} catch (IOException e) {
205206
e.printStackTrace();
206207
}
@@ -211,41 +212,9 @@ public void testGetFileFromCacheShouldReturnBitmapIfIconFoundInCache() {
211212
assertNotNull(cachedIcon);
212213
}
213214

214-
private String buildJSONAsString(long DaysOld, String cahceIconPath) {
215-
JSONObject jsonObject = new JSONObject();
216-
try {
217-
jsonObject.put(STORED_PATH, cahceIconPath);
218-
long timeDifferenceInMilliSeconds = DaysOld * 1000 * 60 * 60 * 24;
219-
jsonObject.put(LAST_UPDATED_TIME, System.currentTimeMillis() - timeDifferenceInMilliSeconds);
220-
return jsonObject.toString();
221-
} catch (JSONException e) {
222-
e.printStackTrace();
223-
return null;
224-
}
225-
}
226-
227-
private String getMD5HashFromIconUrl(String iconUrl) {
228-
String iconHash = null;
229-
try {
230-
MessageDigest md = MessageDigest.getInstance("MD5");
231-
byte[] messageDigest = md.digest(iconUrl.getBytes());
232-
BigInteger no = new BigInteger(1, messageDigest);
233-
String hashtext = no.toString(16);
234-
while (hashtext.length() < 32) {
235-
hashtext = "0" + hashtext;
236-
}
237-
iconHash = hashtext;
238-
} catch (NoSuchAlgorithmException e) {
239-
e.printStackTrace();
240-
}
241-
return iconHash;
242-
}
243-
244215
private String daysToMillisecondsAsString(int days) {
245216
long milliSeconds = (long) days * 24 * 60 * 60 * 1000;
246217
long previousDay = System.currentTimeMillis() - milliSeconds;
247-
return previousDay + "";
218+
return String.valueOf(previousDay);
248219
}
249-
250-
251220
}

android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManager.java

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,6 @@
77

88
import com.smartdevicelink.util.DebugTool;
99

10-
import org.json.JSONException;
11-
import org.json.JSONObject;
12-
1310
import java.io.ByteArrayOutputStream;
1411
import java.io.File;
1512
import java.io.FileOutputStream;
@@ -22,29 +19,25 @@ class LockScreenDeviceIconManager {
2219
private Context context;
2320
private static final String SDL_DEVICE_STATUS_SHARED_PREFS = "sdl.lockScreenIcon";
2421
private static final String STORED_ICON_PATH = "sdl/lock_screen_icon/";
25-
private static final String LAST_UPDATED_TIME = "lastUpdatedTime";
26-
private static final String STORED_PATH = "storedPath";
27-
private static final String TAG = "LockScreenManager";
28-
2922

3023
LockScreenDeviceIconManager(Context context) {
3124
this.context = context;
3225
File lockScreenDirectory = new File(context.getCacheDir(), STORED_ICON_PATH);
3326
lockScreenDirectory.mkdirs();
3427
}
3528

36-
boolean shouldUpdateCachedImage(String iconUrl) {
29+
boolean isIconCachedAndValid(String iconUrl) {
3730
String iconHash = getMD5HashFromIconUrl(iconUrl);
3831
SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE);
39-
String iconParameters = sharedPref.getString(iconHash, null);
40-
if(iconParameters == null) {
32+
String iconLastUpdatedTime = sharedPref.getString(iconHash, null);
33+
if(iconLastUpdatedTime == null) {
4134
DebugTool.logInfo("No Icon Details Found In Shared Preferences");
42-
return true;
35+
return false;
4336
} else {
4437
DebugTool.logInfo("Icon Details Found");
4538
long lastUpdatedTime = 0;
4639
try {
47-
lastUpdatedTime = Long.parseLong(iconParameters);
40+
lastUpdatedTime = Long.parseLong(iconLastUpdatedTime);
4841
} catch (NumberFormatException e) {
4942
DebugTool.logInfo("Invalid time stamp stored to shared preferences, clearing cache and share preferences");
5043
clearIconDirectory();
@@ -54,7 +47,7 @@ boolean shouldUpdateCachedImage(String iconUrl) {
5447

5548
long timeDifference = currentTime - lastUpdatedTime;
5649
long daysBetweenLastUpdate = timeDifference / (1000 * 60 * 60 * 24);
57-
return daysBetweenLastUpdate >= 30;
50+
return daysBetweenLastUpdate < 30;
5851
}
5952
}
6053

@@ -77,15 +70,15 @@ void saveFileToCache(Bitmap icon, String iconUrl) {
7770
return;
7871
}
7972

80-
writeDeviceIconParametersToSystemPreferences(iconHash);
73+
writeDeviceIconParametersToSharedPreferences(iconHash);
8174
}
8275

8376
Bitmap getFileFromCache(String iconUrl) {
8477
String iconHash = getMD5HashFromIconUrl(iconUrl);
8578
SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE);
86-
String iconParameters = sharedPref.getString(iconHash, null);
79+
String iconLastUpdatedTime = sharedPref.getString(iconHash, null);
8780

88-
if (iconParameters != null) {
81+
if (iconLastUpdatedTime != null) {
8982
Bitmap cachedIcon = BitmapFactory.decodeFile(this.context.getCacheDir() + "/" + STORED_ICON_PATH + "/" + iconHash);
9083
if(cachedIcon == null) {
9184
DebugTool.logError("Failed to get Bitmap from decoding file cache");
@@ -101,10 +94,10 @@ Bitmap getFileFromCache(String iconUrl) {
10194
}
10295
}
10396

104-
private void writeDeviceIconParametersToSystemPreferences(String iconHash) {
97+
private void writeDeviceIconParametersToSharedPreferences(String iconHash) {
10598
SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE);
10699
SharedPreferences.Editor editor = sharedPref.edit();
107-
editor.putString(iconHash, System.currentTimeMillis() + "");
100+
editor.putString(iconHash, String.valueOf(System.currentTimeMillis()));
108101
editor.commit();
109102
}
110103

android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenManager.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -380,17 +380,17 @@ private void downloadDeviceIcon(final String url){
380380
@Override
381381
public void run(){
382382
try{
383-
if(mLockScreenDeviceIconManager.shouldUpdateCachedImage(url)) {
384-
DebugTool.logInfo("Lock Screen Icon Update Needed");
385-
deviceLogo = AndroidTools.downloadImage(url);
386-
mLockScreenDeviceIconManager.saveFileToCache(deviceLogo, url);
387-
} else {
383+
if(mLockScreenDeviceIconManager.isIconCachedAndValid(url)) {
388384
DebugTool.logInfo("Image Is Up To Date");
389385
deviceLogo = mLockScreenDeviceIconManager.getFileFromCache(url);
390386
if (deviceLogo == null) {
391387
deviceLogo = AndroidTools.downloadImage(url);
392388
mLockScreenDeviceIconManager.saveFileToCache(deviceLogo, url);
393389
}
390+
} else {
391+
DebugTool.logInfo("Lock Screen Icon Update Needed");
392+
deviceLogo = AndroidTools.downloadImage(url);
393+
mLockScreenDeviceIconManager.saveFileToCache(deviceLogo, url);
394394
}
395395
} catch(IOException e){
396396
Log.e(TAG, "device Icon Error Downloading, Will attempt to grab cached Icon even if expired: \n" + e.toString());

0 commit comments

Comments
 (0)