44import android .content .SharedPreferences ;
55import android .graphics .Bitmap ;
66import android .graphics .BitmapFactory ;
7- import android .util .Log ;
87
98import 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}
0 commit comments