diff options
author | RHenigan <heniganr1@gmail.com> | 2020-03-24 14:39:22 -0400 |
---|---|---|
committer | RHenigan <heniganr1@gmail.com> | 2020-03-24 14:39:22 -0400 |
commit | f2a5835bad1df51353b9614d9ad3fdd93f7788c1 (patch) | |
tree | 58979396c3b597befa35d0bfcef8df1eef29b7c5 /android | |
parent | 99934709fe36c32a171d434d70404e49b1a83d9f (diff) | |
download | sdl_android-f2a5835bad1df51353b9614d9ad3fdd93f7788c1.tar.gz |
Hash iconUrl and fix minor issues
Diffstat (limited to 'android')
3 files changed, 67 insertions, 23 deletions
diff --git a/android/hello_sdl_android/src/main/AndroidManifest.xml b/android/hello_sdl_android/src/main/AndroidManifest.xml index 72a191542..c16d7cb29 100755 --- a/android/hello_sdl_android/src/main/AndroidManifest.xml +++ b/android/hello_sdl_android/src/main/AndroidManifest.xml @@ -17,6 +17,7 @@ android:icon="@mipmap/ic_launcher" android:label="@string/app_name" android:theme="@style/AppTheme" + android:usesCleartextTraffic="true" tools:ignore="DeepLinks"> <activity android:name=".MainActivity" 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 e00a50db2..5c97b470c 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 @@ -4,56 +4,67 @@ import android.content.Context; import android.content.SharedPreferences; import android.graphics.Bitmap; import android.graphics.BitmapFactory; - -import com.smartdevicelink.proxy.rpc.DateTime; +import android.util.Log; import org.json.JSONException; import org.json.JSONObject; import java.io.ByteArrayOutputStream; import java.io.File; -import java.io.FileNotFoundException; import java.io.FileOutputStream; -import java.io.IOException; -import java.util.Date; +import java.math.BigInteger; +import java.security.MessageDigest; +import java.security.NoSuchAlgorithmException; -public class LockScreenDeviceIconManager { +class LockScreenDeviceIconManager { private Context context; + protected static final String SDL_DEVICE_STATUS_SHARED_PREFS = "sdl"; + private static final String LAST_UPDATED_TIME = "lastUpdatedTime"; + private static final String STORED_URL = "storedUrl"; + private static final String TAG = "LockScreenManager"; + LockScreenDeviceIconManager(Context context) { this.context = context; } - boolean updateCachedImage(String iconIUrl) { - SharedPreferences sharedPref = this.context.getSharedPreferences("sdl", Context.MODE_PRIVATE); - String iconParameters = sharedPref.getString(iconIUrl, null); + boolean updateCachedImage(String iconUrl) { + String iconHash = getMD5HashFromIconUrl(iconUrl); + SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE); + String iconParameters = sharedPref.getString(iconHash, null); if(iconParameters == null) { + Log.d(TAG, "No Image Details Found In Preferences"); return true; } else { + Log.d(TAG, "Image Details Found"); JSONObject jsonObject = null; try { jsonObject = new JSONObject(iconParameters); long lastUpdatedTime = 0; - lastUpdatedTime = (long) jsonObject.get("lastUpdatedTime"); + lastUpdatedTime = (long) jsonObject.get(LAST_UPDATED_TIME); long currentTime = System.currentTimeMillis(); long timeDifference = currentTime - lastUpdatedTime; 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"); return true; } } } void saveFileToCache(Bitmap icon, String iconUrl) { - File f = new File(this.context.getCacheDir(), iconUrl); + + String iconHash = getMD5HashFromIconUrl(iconUrl); + + File f = new File(this.context.getCacheDir(), iconHash); try { - f.createNewFile(); + 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(); @@ -65,43 +76,71 @@ public class LockScreenDeviceIconManager { fos.close(); JSONObject iconParams; + Log.d(TAG, "Attempting to save to system preferences"); iconParams = buildDeviceIconParameters(f.getAbsolutePath()); - writeDeviceIconParametersToSystemPreferences(iconUrl, iconParams); + writeDeviceIconParametersToSystemPreferences(iconHash, iconParams); } catch (Exception e) { + Log.d(TAG, "Failed to save to cache or system preferences"); + e.printStackTrace(); + } + } + + 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; } Bitmap getFileFromCache(String iconUrl) { - SharedPreferences sharedPref = this.context.getSharedPreferences("sdl", Context.MODE_PRIVATE); - String iconParameters = sharedPref.getString(iconUrl, null); + String iconHash = getMD5HashFromIconUrl(iconUrl); + SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE); + String iconParameters = sharedPref.getString(iconHash, null); if (iconParameters != null) { + Log.d(TAG, "System Preferences Found"); JSONObject jsonObject = null; try { - jsonObject = new JSONObject("storedUrl"); - String storedUrl = jsonObject.getString("storedUrl"); + Log.d(TAG, "Attempting to get file from cache"); + jsonObject = new JSONObject(iconParameters); + String storedUrl = jsonObject.getString(STORED_URL); return BitmapFactory.decodeFile(storedUrl); } catch (JSONException e) { + Log.d(TAG, "Failed to get file from cache"); e.printStackTrace(); return null; } } else { + Log.d(TAG, "Failed to get system preferences"); return null; } } - private void writeDeviceIconParametersToSystemPreferences(String iconUrl, JSONObject jsonObject) throws JSONException { - SharedPreferences sharedPref = this.context.getSharedPreferences("sdl", Context.MODE_PRIVATE); + private void writeDeviceIconParametersToSystemPreferences(String iconHash, JSONObject jsonObject) throws JSONException { + Log.d(TAG, "Attempting to write to system preferences"); + SharedPreferences sharedPref = this.context.getSharedPreferences(SDL_DEVICE_STATUS_SHARED_PREFS, Context.MODE_PRIVATE); SharedPreferences.Editor editor = sharedPref.edit(); - editor.putString(jsonObject.getString(iconUrl), jsonObject.toString()); + editor.putString(iconHash, jsonObject.toString()); editor.commit(); } private JSONObject buildDeviceIconParameters(String storedUrl) throws JSONException { + Log.d(TAG, "Attempting to write JSON"); JSONObject parametersJson = new JSONObject(); - parametersJson.put("storedUrl", storedUrl); - parametersJson.put("lastUpdatedTime", System.currentTimeMillis()); + parametersJson.put(STORED_URL, storedUrl); + parametersJson.put(LAST_UPDATED_TIME, System.currentTimeMillis()); return parametersJson; } } 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 f5b7c5520..1ab61e40a 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,11 +380,14 @@ public class LockScreenManager extends BaseSubManager { public void run(){ try{ if(mLockScreenDeviceIconManager.updateCachedImage(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); } + 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); @@ -393,6 +396,7 @@ public class LockScreenManager extends BaseSubManager { } }catch(IOException e){ Log.e(TAG, "device Icon Error Downloading"); + Log.e(TAG, e.toString()); } } }).start(); |