1919class LockScreenDeviceIconManager {
2020
2121 private Context context ;
22- protected static final String SDL_DEVICE_STATUS_SHARED_PREFS = "sdl" ;
22+ private static final String SDL_DEVICE_STATUS_SHARED_PREFS = "sdl.lockScreenIcon" ;
23+ private static final String STORED_ICON_PATH = "sdl/lock_screen_icon/" ;
2324 private static final String LAST_UPDATED_TIME = "lastUpdatedTime" ;
2425 private static final String STORED_URL = "storedUrl" ;
2526 private static final String TAG = "LockScreenManager" ;
2627
2728
2829 LockScreenDeviceIconManager (Context context ) {
2930 this .context = context ;
31+ File lockScreenDirectory = new File (context .getCacheDir (), STORED_ICON_PATH );
32+ lockScreenDirectory .mkdirs ();
3033 }
3134
3235 boolean updateCachedImage (String iconUrl ) {
@@ -49,7 +52,6 @@ boolean updateCachedImage(String iconUrl) {
4952 long daysBetweenLastUpdate = timeDifference / (1000 * 60 * 60 * 24 );
5053 Log .d (TAG , "Time since last update: " + daysBetweenLastUpdate );
5154 return daysBetweenLastUpdate >= 30 ;
52-
5355 } catch (JSONException e ) {
5456 e .printStackTrace ();
5557 Log .d (TAG , "Exception Trying to read system preferences" );
@@ -62,46 +64,33 @@ void saveFileToCache(Bitmap icon, String iconUrl) {
6264
6365 String iconHash = getMD5HashFromIconUrl (iconUrl );
6466
65- File f = new File (this .context .getCacheDir (), iconHash );
66- try {
67- Log .d (TAG , "Attempting to save to cache" );
68- ByteArrayOutputStream bos = new ByteArrayOutputStream ();
69- icon .compress (Bitmap .CompressFormat .PNG , 0 /*ignored for PNG*/ , bos );
70- byte [] bitmapdata = bos .toByteArray ();
67+ File f = new File (this .context .getCacheDir () + "/" + STORED_ICON_PATH , iconHash );
68+ Log .d (TAG , "Attempting to save to cache" );
69+ ByteArrayOutputStream bos = new ByteArrayOutputStream ();
70+ icon .compress (Bitmap .CompressFormat .PNG , 0 /*ignored for PNG*/ , bos );
71+ byte [] bitmapdata = bos .toByteArray ();
7172
72- FileOutputStream fos = null ;
73+ FileOutputStream fos = null ;
74+ try {
7375 fos = new FileOutputStream (f );
7476 fos .write (bitmapdata );
7577 fos .flush ();
7678 fos .close ();
77- JSONObject iconParams ;
78-
79- Log .d (TAG , "Attempting to save to system preferences" );
80- iconParams = buildDeviceIconParameters (f .getAbsolutePath ());
81- writeDeviceIconParametersToSystemPreferences (iconHash , iconParams );
8279 } catch (Exception e ) {
83- Log .d (TAG , "Failed to save to cache or system preferences " );
80+ Log .d (TAG , "Failed to save to Icon to Cache " );
8481 e .printStackTrace ();
8582 }
86- }
8783
88- private String getMD5HashFromIconUrl ( String iconUrl ) {
89- String iconHash = null ;
84+ JSONObject iconParams ;
85+ Log . d ( TAG , "Attempting to save to system preferences" ) ;
9086 try {
91- MessageDigest md = MessageDigest .getInstance ("MD5" );
92- byte [] messageDigest = md .digest (iconUrl .getBytes ());
93- BigInteger no = new BigInteger (1 , messageDigest );
94- String hashtext = no .toString (16 );
95- while (hashtext .length () < 32 ) {
96- hashtext = "0" + hashtext ;
97- }
98- iconHash = hashtext ;
99- } catch (NoSuchAlgorithmException e ) {
100- Log .d (TAG , "Unable to Hash URL" );
87+ iconParams = buildDeviceIconParameters (f .getAbsolutePath ());
88+ writeDeviceIconParametersToSystemPreferences (iconHash , iconParams );
89+ } catch (JSONException e ) {
90+ Log .d (TAG , "Failed to save to system preferences, clearing cache icon directory" );
91+ clearIconDirectory ();
10192 e .printStackTrace ();
10293 }
103- Log .d (TAG , "icon hash: " + iconHash );
104- return iconHash ;
10594 }
10695
10796 Bitmap getFileFromCache (String iconUrl ) {
@@ -116,9 +105,17 @@ Bitmap getFileFromCache(String iconUrl) {
116105 Log .d (TAG , "Attempting to get file from cache" );
117106 jsonObject = new JSONObject (iconParameters );
118107 String storedUrl = jsonObject .getString (STORED_URL );
119- return BitmapFactory .decodeFile (storedUrl );
108+ Bitmap cachedIcon = BitmapFactory .decodeFile (storedUrl );
109+ if (cachedIcon == null ) {
110+ Log .d (TAG , "Failed to get Bitmap from decoding file cache" );
111+ clearIconDirectory ();
112+ return null ;
113+ } else {
114+ return cachedIcon ;
115+ }
120116 } catch (JSONException e ) {
121- Log .d (TAG , "Failed to get file from cache" );
117+ Log .d (TAG , "Failed to get file from cache, removing from shared pref" );
118+ sharedPref .edit ().remove (iconHash ).commit ();
122119 e .printStackTrace ();
123120 return null ;
124121 }
@@ -143,4 +140,30 @@ private JSONObject buildDeviceIconParameters(String storedUrl) throws JSONExcept
143140 parametersJson .put (LAST_UPDATED_TIME , System .currentTimeMillis ());
144141 return parametersJson ;
145142 }
143+
144+ private String getMD5HashFromIconUrl (String iconUrl ) {
145+ String iconHash = null ;
146+ try {
147+ MessageDigest md = MessageDigest .getInstance ("MD5" );
148+ byte [] messageDigest = md .digest (iconUrl .getBytes ());
149+ BigInteger no = new BigInteger (1 , messageDigest );
150+ String hashtext = no .toString (16 );
151+ while (hashtext .length () < 32 ) {
152+ hashtext = "0" + hashtext ;
153+ }
154+ iconHash = hashtext ;
155+ } catch (NoSuchAlgorithmException e ) {
156+ Log .d (TAG , "Unable to Hash URL" );
157+ e .printStackTrace ();
158+ }
159+ Log .d (TAG , "icon hash: " + iconHash );
160+ return iconHash ;
161+ }
162+
163+ private void clearIconDirectory () {
164+ File iconDir = new File (context .getCacheDir () + "/" + STORED_ICON_PATH );
165+ for (File child : iconDir .listFiles ()) {
166+ child .delete ();
167+ }
168+ }
146169}
0 commit comments