Skip to content

Commit fcc44e9

Browse files
committed
Fixes from PR Review
1 parent 641a48c commit fcc44e9

3 files changed

Lines changed: 56 additions & 76 deletions

File tree

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

Lines changed: 22 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
import android.content.Context;
44
import android.content.SharedPreferences;
55
import android.graphics.Bitmap;
6-
import android.graphics.BitmapFactory;
7-
import android.util.Log;
86

97
import com.smartdevicelink.AndroidTestCase2;
108
import com.smartdevicelink.util.AndroidTools;
@@ -32,7 +30,7 @@ public class LockScreenDeviceIconManagerTests extends AndroidTestCase2 {
3230
private LockScreenDeviceIconManager lockScreenDeviceIconManager;
3331
private static final String ICON_URL = "http://i.imgur.com/TgkvOIZ.png";
3432
private static final String LAST_UPDATED_TIME = "lastUpdatedTime";
35-
private static final String STORED_URL = "storedUrl";
33+
private static final String STORED_PATH = "storedPath";
3634
private static final String INVALID_JSON_STRING = "Invalid JSON";
3735

3836
public void setup() throws Exception {
@@ -50,41 +48,46 @@ public void testUpdateCacheImageShouldReturnTrueWhenSharedPreferencesDoesNotExis
5048
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(null);
5149

5250
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
53-
boolean shouldUpdate = lockScreenDeviceIconManager.updateCachedImage(ICON_URL);
51+
boolean shouldUpdate = lockScreenDeviceIconManager.shouldUpdateCachedImage(ICON_URL);
5452
assertTrue(shouldUpdate);
5553
}
5654

5755
public void testUpdateCacheImageShouldReturnTrueWhenUnableToReadSharedPreference() {
5856
final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class);
57+
final SharedPreferences.Editor sharedPrefsEditor = Mockito.mock(SharedPreferences.Editor.class);
5958
final Context context = Mockito.mock(Context.class);
6059
Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs);
61-
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(INVALID_JSON_STRING);
60+
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn("");
61+
Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs);
62+
Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor);
63+
Mockito.when(sharedPrefsEditor.remove(anyString())).thenReturn(sharedPrefsEditor);
64+
Mockito.when(sharedPrefsEditor.commit()).thenReturn(true);
6265

6366

6467
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
65-
boolean shouldUpdate = lockScreenDeviceIconManager.updateCachedImage(ICON_URL);
68+
boolean shouldUpdate = lockScreenDeviceIconManager.shouldUpdateCachedImage(ICON_URL);
6669
assertTrue(shouldUpdate);
6770
}
6871

6972
public void testUpdateCacheImageShouldReturnTrueSharedPreferenceReturnsAnOutdatedIcon() {
7073
final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class);
7174
final Context context = Mockito.mock(Context.class);
7275
Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs);
73-
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(buildJSONAsString(35, ""));
76+
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(35));
7477

7578
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
76-
boolean shouldUpdate = lockScreenDeviceIconManager.updateCachedImage(ICON_URL);
79+
boolean shouldUpdate = lockScreenDeviceIconManager.shouldUpdateCachedImage(ICON_URL);
7780
assertTrue(shouldUpdate);
7881
}
7982

8083
public void testUpdateCacheImageShouldReturnFalseWhenSharedPreferenceReturnsAnUpdatedIcon() {
8184
final SharedPreferences sharedPrefs = Mockito.mock(SharedPreferences.class);
8285
final Context context = Mockito.mock(Context.class);
8386
Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs);
84-
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(buildJSONAsString(15, ""));
87+
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(15));
8588

8689
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
87-
boolean shouldUpdate = lockScreenDeviceIconManager.updateCachedImage(ICON_URL);
90+
boolean shouldUpdate = lockScreenDeviceIconManager.shouldUpdateCachedImage(ICON_URL);
8891
assertFalse(shouldUpdate);
8992
}
9093

@@ -151,7 +154,7 @@ public void testGetFileFromCacheShouldReturnNullIfInvalidDataFromSharedPref() {
151154
Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor);
152155
Mockito.when(sharedPrefsEditor.remove(anyString())).thenReturn(sharedPrefsEditor);
153156
Mockito.when(sharedPrefsEditor.commit()).thenReturn(true);
154-
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(INVALID_JSON_STRING);
157+
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn("");
155158

156159

157160
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
@@ -208,10 +211,10 @@ public void testGetFileFromCacheShouldReturnBitmapIfIconFoundInCache() {
208211
assertNotNull(cachedIcon);
209212
}
210213

211-
private String buildJSONAsString(long DaysOld, String cahceIconUrl) {
214+
private String buildJSONAsString(long DaysOld, String cahceIconPath) {
212215
JSONObject jsonObject = new JSONObject();
213216
try {
214-
jsonObject.put(STORED_URL, cahceIconUrl);
217+
jsonObject.put(STORED_PATH, cahceIconPath);
215218
long timeDifferenceInMilliSeconds = DaysOld * 1000 * 60 * 60 * 24;
216219
jsonObject.put(LAST_UPDATED_TIME, System.currentTimeMillis() - timeDifferenceInMilliSeconds);
217220
return jsonObject.toString();
@@ -238,5 +241,11 @@ private String getMD5HashFromIconUrl(String iconUrl) {
238241
return iconHash;
239242
}
240243

244+
private String daysToMillisecondsAsString(int days) {
245+
long milliSeconds = (long) days * 24 * 60 * 60 * 1000;
246+
long previousDay = System.currentTimeMillis() - milliSeconds;
247+
return previousDay + "";
248+
}
249+
241250

242251
}

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

Lines changed: 29 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import android.content.SharedPreferences;
55
import android.graphics.Bitmap;
66
import android.graphics.BitmapFactory;
7-
import android.util.Log;
87

98
import com.smartdevicelink.util.DebugTool;
109

@@ -24,7 +23,7 @@ class LockScreenDeviceIconManager {
2423
private static final String SDL_DEVICE_STATUS_SHARED_PREFS = "sdl.lockScreenIcon";
2524
private static final String STORED_ICON_PATH = "sdl/lock_screen_icon/";
2625
private static final String LAST_UPDATED_TIME = "lastUpdatedTime";
27-
private static final String STORED_URL = "storedUrl";
26+
private static final String STORED_PATH = "storedPath";
2827
private static final String TAG = "LockScreenManager";
2928

3029

@@ -34,7 +33,7 @@ class LockScreenDeviceIconManager {
3433
lockScreenDirectory.mkdirs();
3534
}
3635

37-
boolean updateCachedImage(String iconUrl) {
36+
boolean shouldUpdateCachedImage(String iconUrl) {
3837
String iconHash = getMD5HashFromIconUrl(iconUrl);
3938
SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE);
4039
String iconParameters = sharedPref.getString(iconHash, null);
@@ -43,37 +42,33 @@ boolean updateCachedImage(String iconUrl) {
4342
return true;
4443
} else {
4544
DebugTool.logInfo("Icon Details Found");
46-
JSONObject jsonObject = null;
45+
long lastUpdatedTime = 0;
4746
try {
48-
jsonObject = new JSONObject(iconParameters);
49-
long lastUpdatedTime = 0;
50-
lastUpdatedTime = (long) jsonObject.get(LAST_UPDATED_TIME);
51-
long currentTime = System.currentTimeMillis();
52-
53-
long timeDifference = currentTime - lastUpdatedTime;
54-
long daysBetweenLastUpdate = timeDifference / (1000 * 60 * 60 * 24);
55-
return daysBetweenLastUpdate >= 30;
56-
} catch (JSONException e) {
57-
e.printStackTrace();
58-
DebugTool.logError("Exception Trying to read shared preferences");
59-
return true;
47+
lastUpdatedTime = Long.parseLong(iconParameters);
48+
} catch (NumberFormatException e) {
49+
DebugTool.logInfo("Invalid time stamp stored to shared preferences, clearing cache and share preferences");
50+
clearIconDirectory();
51+
sharedPref.edit().remove(iconHash).commit();
6052
}
53+
long currentTime = System.currentTimeMillis();
54+
55+
long timeDifference = currentTime - lastUpdatedTime;
56+
long daysBetweenLastUpdate = timeDifference / (1000 * 60 * 60 * 24);
57+
return daysBetweenLastUpdate >= 30;
6158
}
6259
}
6360

6461
void saveFileToCache(Bitmap icon, String iconUrl) {
65-
6662
String iconHash = getMD5HashFromIconUrl(iconUrl);
67-
6863
File f = new File(this.context.getCacheDir() + "/" + STORED_ICON_PATH, iconHash);
6964
ByteArrayOutputStream bos = new ByteArrayOutputStream();
7065
icon.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos);
71-
byte[] bitmapdata = bos.toByteArray();
66+
byte[] bitmapData = bos.toByteArray();
7267

7368
FileOutputStream fos = null;
7469
try {
7570
fos = new FileOutputStream(f);
76-
fos.write(bitmapdata);
71+
fos.write(bitmapData);
7772
fos.flush();
7873
fos.close();
7974
} catch (Exception e) {
@@ -82,15 +77,7 @@ void saveFileToCache(Bitmap icon, String iconUrl) {
8277
return;
8378
}
8479

85-
JSONObject iconParams;
86-
try {
87-
iconParams = buildDeviceIconParameters(f.getAbsolutePath());
88-
writeDeviceIconParametersToSystemPreferences(iconHash, iconParams);
89-
} catch (JSONException e) {
90-
DebugTool.logError("Failed to save to shared preferences, clearing cache icon directory");
91-
clearIconDirectory();
92-
e.printStackTrace();
93-
}
80+
writeDeviceIconParametersToSystemPreferences(iconHash);
9481
}
9582

9683
Bitmap getFileFromCache(String iconUrl) {
@@ -99,45 +86,29 @@ Bitmap getFileFromCache(String iconUrl) {
9986
String iconParameters = sharedPref.getString(iconHash, null);
10087

10188
if (iconParameters != null) {
102-
JSONObject jsonObject = null;
103-
try {
104-
jsonObject = new JSONObject(iconParameters);
105-
String storedUrl = jsonObject.getString(STORED_URL);
106-
Bitmap cachedIcon = BitmapFactory.decodeFile(storedUrl);
107-
if(cachedIcon == null) {
108-
DebugTool.logError("Failed to get Bitmap from decoding file cache");
109-
clearIconDirectory();
110-
return null;
111-
} else {
112-
return cachedIcon;
113-
}
114-
} catch (JSONException e) {
115-
DebugTool.logError("Failed to get file from cache, removing shared pref");
89+
Bitmap cachedIcon = BitmapFactory.decodeFile(this.context.getCacheDir() + "/" + STORED_ICON_PATH + "/" + iconHash);
90+
if(cachedIcon == null) {
91+
DebugTool.logError("Failed to get Bitmap from decoding file cache");
92+
clearIconDirectory();
11693
sharedPref.edit().remove(iconHash).commit();
117-
e.printStackTrace();
11894
return null;
95+
} else {
96+
return cachedIcon;
11997
}
12098
} else {
12199
DebugTool.logError("Failed to get system preferences");
122100
return null;
123101
}
124102
}
125103

126-
private void writeDeviceIconParametersToSystemPreferences(String iconHash, JSONObject jsonObject) {
104+
private void writeDeviceIconParametersToSystemPreferences(String iconHash) {
127105
SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE);
128106
SharedPreferences.Editor editor = sharedPref.edit();
129-
editor.putString(iconHash, jsonObject.toString());
107+
editor.putString(iconHash, System.currentTimeMillis() + "");
130108
editor.commit();
131109
}
132110

133-
private JSONObject buildDeviceIconParameters(String storedUrl) throws JSONException {
134-
JSONObject parametersJson = new JSONObject();
135-
parametersJson.put(STORED_URL, storedUrl);
136-
parametersJson.put(LAST_UPDATED_TIME, System.currentTimeMillis());
137-
return parametersJson;
138-
}
139-
140-
private String getMD5HashFromIconUrl(String iconUrl) {
111+
String getMD5HashFromIconUrl(String iconUrl) {
141112
String iconHash = null;
142113
try {
143114
MessageDigest md = MessageDigest.getInstance("MD5");
@@ -157,8 +128,10 @@ private String getMD5HashFromIconUrl(String iconUrl) {
157128

158129
private void clearIconDirectory() {
159130
File iconDir = new File(context.getCacheDir() + "/" + STORED_ICON_PATH);
160-
for (File child : iconDir.listFiles()) {
161-
child.delete();
131+
if (iconDir.listFiles() != null) {
132+
for (File child : iconDir.listFiles()) {
133+
child.delete();
134+
}
162135
}
163136
}
164137
}

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

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@
5555
import com.smartdevicelink.proxy.rpc.enums.RequestType;
5656
import com.smartdevicelink.proxy.rpc.listeners.OnRPCNotificationListener;
5757
import com.smartdevicelink.util.AndroidTools;
58+
import com.smartdevicelink.util.DebugTool;
5859

5960
import java.io.IOException;
6061
import java.lang.ref.WeakReference;
@@ -379,23 +380,20 @@ private void downloadDeviceIcon(final String url){
379380
@Override
380381
public void run(){
381382
try{
382-
if(mLockScreenDeviceIconManager.updateCachedImage(url)) {
383-
Log.d(TAG, "URL: " + url);
384-
Log.d(TAG, "Image Update Needed");
383+
if(mLockScreenDeviceIconManager.shouldUpdateCachedImage(url)) {
384+
DebugTool.logInfo("Lock Screen Icon Update Needed");
385385
deviceLogo = AndroidTools.downloadImage(url);
386386
mLockScreenDeviceIconManager.saveFileToCache(deviceLogo, url);
387387
} else {
388-
Log.d(TAG, "Image Is Up To Date");
388+
DebugTool.logInfo("Image Is Up To Date");
389389
deviceLogo = mLockScreenDeviceIconManager.getFileFromCache(url);
390390
if (deviceLogo == null) {
391391
deviceLogo = AndroidTools.downloadImage(url);
392392
mLockScreenDeviceIconManager.saveFileToCache(deviceLogo, url);
393393
}
394394
}
395395
} catch(IOException e){
396-
Log.e(TAG, "device Icon Error Downloading");
397-
Log.e(TAG, e.toString());
398-
Log.e(TAG, "Attempt to grab Cached image even if expired");
396+
Log.e(TAG, "device Icon Error Downloading, Will attempt to grab cached Icon even if expired: \n" + e.toString());
399397
deviceLogo = mLockScreenDeviceIconManager.getFileFromCache(url);
400398
}
401399
if(deviceLogo != null) {

0 commit comments

Comments
 (0)