Skip to content

Commit 0259aba

Browse files
committed
Adding documentation and PR changes
1 parent f9b3dba commit 0259aba

3 files changed

Lines changed: 53 additions & 9 deletions

File tree

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ public void testRetrieveIconShouldCallOnImageRetrievedWithIconWhenCachedIconIsUp
9191
Mockito.when(context.getSharedPreferences(anyString(), anyInt())).thenReturn(sharedPrefs);
9292
Mockito.when(sharedPrefs.getString(anyString(), (String) isNull())).thenReturn(daysToMillisecondsAsString(15));
9393
Mockito.when(sharedPrefs.edit()).thenReturn(sharedPrefsEditor);
94-
Mockito.when(sharedPrefsEditor.remove(anyString())).thenReturn(sharedPrefsEditor);
94+
Mockito.when(sharedPrefsEditor.clear()).thenReturn(sharedPrefsEditor);
9595

9696
lockScreenDeviceIconManager = new LockScreenDeviceIconManager(context);
9797
lockScreenDeviceIconManager.retrieveIcon(ICON_URL, listener);

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

Lines changed: 51 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,12 @@
1616
import java.security.MessageDigest;
1717
import java.security.NoSuchAlgorithmException;
1818

19+
/**
20+
* <strong>LockScreenDeviceIconManager</strong> <br>
21+
*
22+
* The LockScreenDeviceIconManager handles the logic of caching and retrieving cached lock screen icons <br>
23+
*
24+
*/
1925
class LockScreenDeviceIconManager {
2026

2127
private Context context;
@@ -33,6 +39,12 @@ interface OnIconRetrievedListener {
3339
lockScreenDirectory.mkdirs();
3440
}
3541

42+
/**
43+
* Will try to return a lock screen icon either from cache or downloaded
44+
* if it fails iconRetrievedListener.OnError will be called with corresponding error message
45+
* @param iconURL url that the lock screen icon is downloaded from
46+
* @param iconRetrievedListener an interface that will implement onIconReceived and OnError methods
47+
*/
3648
void retrieveIcon(String iconURL, OnIconRetrievedListener iconRetrievedListener) {
3749
Bitmap icon = null;
3850
try {
@@ -46,10 +58,15 @@ void retrieveIcon(String iconURL, OnIconRetrievedListener iconRetrievedListener)
4658
}
4759
iconRetrievedListener.onImageRetrieved(icon);
4860
} else {
61+
// The icon is unknown or expired. Download the image, save it to the cache, and update the archive file
4962
DebugTool.logInfo("Lock Screen Icon Update Needed");
5063
icon = AndroidTools.downloadImage(iconURL);
51-
saveFileToCache(icon, iconURL);
52-
iconRetrievedListener.onImageRetrieved(icon);
64+
if (icon != null) {
65+
saveFileToCache(icon, iconURL);
66+
iconRetrievedListener.onImageRetrieved(icon);
67+
} else {
68+
iconRetrievedListener.onError("Icon downloaded was null");
69+
}
5370
}
5471
} catch (IOException e) {
5572
iconRetrievedListener.onError("device Icon Error Downloading, Will attempt to grab cached Icon even if expired: \n" + e.toString());
@@ -58,6 +75,11 @@ void retrieveIcon(String iconURL, OnIconRetrievedListener iconRetrievedListener)
5875
}
5976
}
6077

78+
/**
79+
* Will decide if a cached icon is available and up to date
80+
* @param iconUrl url will be hashed and used to look up last updated timestamp in shared preferences
81+
* @return True when icon details are in shared preferences and less than 30 days old, False if icon details are too old or not found
82+
*/
6183
private boolean isIconCachedAndValid(String iconUrl) {
6284
String iconHash = getMD5HashFromIconUrl(iconUrl);
6385
SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE);
@@ -73,7 +95,7 @@ private boolean isIconCachedAndValid(String iconUrl) {
7395
} catch (NumberFormatException e) {
7496
DebugTool.logInfo("Invalid time stamp stored to shared preferences, clearing cache and share preferences");
7597
clearIconDirectory();
76-
sharedPref.edit().remove(iconHash).commit();
98+
sharedPref.edit().clear().commit();
7799
}
78100
long currentTime = System.currentTimeMillis();
79101

@@ -83,6 +105,11 @@ private boolean isIconCachedAndValid(String iconUrl) {
83105
}
84106
}
85107

108+
/**
109+
* Will try to save icon to cache
110+
* @param icon the icon bitmap that should be saved to cache
111+
* @param iconUrl the url where the icon was retrieved will be hashed and used for file and file details lookup
112+
*/
86113
private void saveFileToCache(Bitmap icon, String iconUrl) {
87114
String iconHash = getMD5HashFromIconUrl(iconUrl);
88115
File f = new File(this.context.getCacheDir() + "/" + STORED_ICON_DIRECTORY_PATH, iconHash);
@@ -96,15 +123,18 @@ private void saveFileToCache(Bitmap icon, String iconUrl) {
96123
fos.write(bitmapData);
97124
fos.flush();
98125
fos.close();
126+
writeDeviceIconParametersToSharedPreferences(iconHash);
99127
} catch (Exception e) {
100128
DebugTool.logError("Failed to save icon to cache");
101129
e.printStackTrace();
102-
return;
103130
}
104-
105-
writeDeviceIconParametersToSharedPreferences(iconHash);
106131
}
107132

133+
/**
134+
* Will try to retrieve icon bitmap from cached directory
135+
* @param iconUrl the url where the icon was retrieved will be hashed and used to look up file location
136+
* @return bitmap of device icon or null if it fails to find the icon or read from shared preferences
137+
*/
108138
private Bitmap getFileFromCache(String iconUrl) {
109139
String iconHash = getMD5HashFromIconUrl(iconUrl);
110140
SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE);
@@ -115,24 +145,34 @@ private Bitmap getFileFromCache(String iconUrl) {
115145
if(cachedIcon == null) {
116146
DebugTool.logError("Failed to get Bitmap from decoding file cache");
117147
clearIconDirectory();
118-
sharedPref.edit().remove(iconHash).commit();
148+
sharedPref.edit().clear().commit();
119149
return null;
120150
} else {
121151
return cachedIcon;
122152
}
123153
} else {
124-
DebugTool.logError("Failed to get system preferences");
154+
DebugTool.logError("Failed to get shared preferences");
125155
return null;
126156
}
127157
}
128158

159+
/**
160+
* Will write information about the icon to shared preferences
161+
* icon information will have a look up key of the hashed icon url and the current timestamp to indicated when the icon was last updated.
162+
* @param iconHash the url where the icon was retrieved will be hashed and used lookup key
163+
*/
129164
private void writeDeviceIconParametersToSharedPreferences(String iconHash) {
130165
SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE);
131166
SharedPreferences.Editor editor = sharedPref.edit();
132167
editor.putString(iconHash, String.valueOf(System.currentTimeMillis()));
133168
editor.commit();
134169
}
135170

171+
/**
172+
* Create an MD5 hash of the icon url for file storage and lookup/shared preferences look up
173+
* @param iconUrl the url where the icon was retrieved
174+
* @return MD5 hash of the icon URL
175+
*/
136176
private String getMD5HashFromIconUrl(String iconUrl) {
137177
String iconHash = null;
138178
try {
@@ -151,6 +191,9 @@ private String getMD5HashFromIconUrl(String iconUrl) {
151191
return iconHash;
152192
}
153193

194+
/**
195+
* Clears all files in the directory where lock screen icons are cached
196+
*/
154197
private void clearIconDirectory() {
155198
File iconDir = new File(context.getCacheDir() + "/" + STORED_ICON_DIRECTORY_PATH);
156199
if (iconDir.listFiles() != null) {

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

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,7 @@ public void onNotified(RPCNotification notification) {
236236
msg.getUrl() != null) {
237237
// send intent to activity to download icon from core
238238
deviceIconUrl = msg.getUrl();
239+
deviceIconUrl = deviceIconUrl.replace("http://", "https://");
239240
downloadDeviceIcon(deviceIconUrl);
240241
}
241242
}

0 commit comments

Comments
 (0)