diff options
author | RHenigan <heniganr1@gmail.com> | 2020-03-25 14:44:42 -0400 |
---|---|---|
committer | RHenigan <heniganr1@gmail.com> | 2020-03-25 14:44:42 -0400 |
commit | 3958a341d058493a74ef48df75a22b81757bf008 (patch) | |
tree | f24e2178b4e3120ef965cce09da3ec3ac3e7abcc /android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen | |
parent | f2a5835bad1df51353b9614d9ad3fdd93f7788c1 (diff) | |
download | sdl_android-3958a341d058493a74ef48df75a22b81757bf008.tar.gz |
Handle Edge Cases and initial tests
Diffstat (limited to 'android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen')
2 files changed, 68 insertions, 36 deletions
diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManager.java b/android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManager.java index 5c97b470c..be39f5850 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManager.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenDeviceIconManager.java @@ -19,7 +19,8 @@ import java.security.NoSuchAlgorithmException; class LockScreenDeviceIconManager { private Context context; - protected static final String SDL_DEVICE_STATUS_SHARED_PREFS = "sdl"; + private static final String SDL_DEVICE_STATUS_SHARED_PREFS = "sdl.lockScreenIcon"; + private static final String STORED_ICON_PATH = "sdl/lock_screen_icon/"; private static final String LAST_UPDATED_TIME = "lastUpdatedTime"; private static final String STORED_URL = "storedUrl"; private static final String TAG = "LockScreenManager"; @@ -27,6 +28,8 @@ class LockScreenDeviceIconManager { LockScreenDeviceIconManager(Context context) { this.context = context; + File lockScreenDirectory = new File(context.getCacheDir(), STORED_ICON_PATH); + lockScreenDirectory.mkdirs(); } boolean updateCachedImage(String iconUrl) { @@ -49,7 +52,6 @@ class LockScreenDeviceIconManager { long daysBetweenLastUpdate = timeDifference / (1000 * 60 * 60 * 24); Log.d(TAG, "Time since last update: " + daysBetweenLastUpdate); return daysBetweenLastUpdate >= 30; - } catch (JSONException e) { e.printStackTrace(); Log.d(TAG, "Exception Trying to read system preferences"); @@ -62,46 +64,33 @@ class LockScreenDeviceIconManager { String iconHash = getMD5HashFromIconUrl(iconUrl); - File f = new File(this.context.getCacheDir(), iconHash); - try { - Log.d(TAG, "Attempting to save to cache"); - ByteArrayOutputStream bos = new ByteArrayOutputStream(); - icon.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos); - byte[] bitmapdata = bos.toByteArray(); + File f = new File(this.context.getCacheDir() + "/" + STORED_ICON_PATH, iconHash); + Log.d(TAG, "Attempting to save to cache"); + ByteArrayOutputStream bos = new ByteArrayOutputStream(); + icon.compress(Bitmap.CompressFormat.PNG, 0 /*ignored for PNG*/, bos); + byte[] bitmapdata = bos.toByteArray(); - FileOutputStream fos = null; + FileOutputStream fos = null; + try { fos = new FileOutputStream(f); fos.write(bitmapdata); fos.flush(); fos.close(); - JSONObject iconParams; - - Log.d(TAG, "Attempting to save to system preferences"); - iconParams = buildDeviceIconParameters(f.getAbsolutePath()); - writeDeviceIconParametersToSystemPreferences(iconHash, iconParams); } catch (Exception e) { - Log.d(TAG, "Failed to save to cache or system preferences"); + Log.d(TAG, "Failed to save to Icon to Cache"); e.printStackTrace(); } - } - private String getMD5HashFromIconUrl(String iconUrl) { - String iconHash = null; + JSONObject iconParams; + Log.d(TAG, "Attempting to save to system preferences"); try { - MessageDigest md = MessageDigest.getInstance("MD5"); - byte[] messageDigest = md.digest(iconUrl.getBytes()); - BigInteger no = new BigInteger(1, messageDigest); - String hashtext = no.toString(16); - while (hashtext.length() < 32) { - hashtext = "0" + hashtext; - } - iconHash = hashtext; - } catch (NoSuchAlgorithmException e) { - Log.d(TAG, "Unable to Hash URL"); + iconParams = buildDeviceIconParameters(f.getAbsolutePath()); + writeDeviceIconParametersToSystemPreferences(iconHash, iconParams); + } catch (JSONException e) { + Log.d(TAG, "Failed to save to system preferences, clearing cache icon directory"); + clearIconDirectory(); e.printStackTrace(); } - Log.d(TAG, "icon hash: " + iconHash); - return iconHash; } Bitmap getFileFromCache(String iconUrl) { @@ -116,9 +105,17 @@ class LockScreenDeviceIconManager { Log.d(TAG, "Attempting to get file from cache"); jsonObject = new JSONObject(iconParameters); String storedUrl = jsonObject.getString(STORED_URL); - return BitmapFactory.decodeFile(storedUrl); + Bitmap cachedIcon = BitmapFactory.decodeFile(storedUrl); + if(cachedIcon == null) { + Log.d(TAG, "Failed to get Bitmap from decoding file cache"); + clearIconDirectory(); + return null; + } else { + return cachedIcon; + } } catch (JSONException e) { - Log.d(TAG, "Failed to get file from cache"); + Log.d(TAG, "Failed to get file from cache, removing from shared pref"); + sharedPref.edit().remove(iconHash).commit(); e.printStackTrace(); return null; } @@ -143,4 +140,30 @@ class LockScreenDeviceIconManager { parametersJson.put(LAST_UPDATED_TIME, System.currentTimeMillis()); return parametersJson; } + + private String getMD5HashFromIconUrl(String iconUrl) { + String iconHash = null; + try { + MessageDigest md = MessageDigest.getInstance("MD5"); + byte[] messageDigest = md.digest(iconUrl.getBytes()); + BigInteger no = new BigInteger(1, messageDigest); + String hashtext = no.toString(16); + while (hashtext.length() < 32) { + hashtext = "0" + hashtext; + } + iconHash = hashtext; + } catch (NoSuchAlgorithmException e) { + Log.d(TAG, "Unable to Hash URL"); + e.printStackTrace(); + } + Log.d(TAG, "icon hash: " + iconHash); + return iconHash; + } + + private void clearIconDirectory() { + File iconDir = new File(context.getCacheDir() + "/" + STORED_ICON_PATH); + for (File child : iconDir.listFiles()) { + child.delete(); + } + } } diff --git a/android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenManager.java b/android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenManager.java index 1ab61e40a..00f8768eb 100644 --- a/android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenManager.java +++ b/android/sdl_android/src/main/java/com/smartdevicelink/managers/lockscreen/LockScreenManager.java @@ -380,24 +380,33 @@ public class LockScreenManager extends BaseSubManager { public void run(){ try{ if(mLockScreenDeviceIconManager.updateCachedImage(url)) { + Log.d(TAG, "URL: " + url); Log.d(TAG, "Image Update Needed"); deviceLogo = AndroidTools.downloadImage(url); mLockScreenDeviceIconManager.saveFileToCache(deviceLogo, url); } else { Log.d(TAG, "Image Is Up To Date"); deviceLogo = mLockScreenDeviceIconManager.getFileFromCache(url); + if (deviceLogo == null) { + deviceLogo = AndroidTools.downloadImage(url); + mLockScreenDeviceIconManager.saveFileToCache(deviceLogo, url); + } } - + } catch(IOException e){ + Log.e(TAG, "device Icon Error Downloading"); + Log.e(TAG, e.toString()); + Log.e(TAG, "Attempt to grab Cached image even if expired"); + deviceLogo = mLockScreenDeviceIconManager.getFileFromCache(url); + } + if(deviceLogo != null) { Intent intent = new Intent(SDLLockScreenActivity.LOCKSCREEN_DEVICE_LOGO_DOWNLOADED); intent.putExtra(SDLLockScreenActivity.LOCKSCREEN_DEVICE_LOGO_EXTRA, deviceLogoEnabled); intent.putExtra(SDLLockScreenActivity.LOCKSCREEN_DEVICE_LOGO_BITMAP, deviceLogo); if (context.get() != null) { context.get().sendBroadcast(intent); } - }catch(IOException e){ - Log.e(TAG, "device Icon Error Downloading"); - Log.e(TAG, e.toString()); } + } }).start(); } |