OSDN Git Service

ADWLauncher for IS01
[gb-231r1-is01/GB_2.3_IS01.git] / packages / apps / ADWLauncher / src / mobi / intuitit / android / widget / ListViewImageManager.java
diff --git a/packages/apps/ADWLauncher/src/mobi/intuitit/android/widget/ListViewImageManager.java b/packages/apps/ADWLauncher/src/mobi/intuitit/android/widget/ListViewImageManager.java
new file mode 100644 (file)
index 0000000..86a7b12
--- /dev/null
@@ -0,0 +1,196 @@
+package mobi.intuitit.android.widget;\r
+\r
+import java.io.IOException;\r
+import java.io.InputStream;\r
+import java.lang.ref.SoftReference;\r
+import java.util.ArrayList;\r
+import java.util.HashMap;\r
+import java.util.Map.Entry;\r
+\r
+import android.content.ContentResolver;\r
+import android.content.Context;\r
+import android.graphics.drawable.Drawable;\r
+import android.net.Uri;\r
+import android.util.Log;\r
+\r
+public class ListViewImageManager {\r
+\r
+       private static final String TAG = "ListViewImageManager";\r
+\r
+       private static final boolean LOGD = false;\r
+\r
+       private static ListViewImageManager instance;\r
+\r
+       public static ListViewImageManager getInstance() {\r
+               if (instance == null)\r
+                       instance = new ListViewImageManager();\r
+               return instance;\r
+       }\r
+\r
+       private final HashMap<String, SoftReference<Drawable>> mCacheForImageByUri = new HashMap<String, SoftReference<Drawable>>();\r
+       private final HashMap<Integer, ArrayList<String>> mWidgetCacheUsageByUri = new HashMap<Integer, ArrayList<String>>();\r
+\r
+       private final HashMap<Integer, SoftReference<Drawable>> mCacheForImageById = new HashMap<Integer, SoftReference<Drawable>>();\r
+       private final HashMap<Integer, ArrayList<Integer>> mWidgetCacheUsageById = new HashMap<Integer, ArrayList<Integer>>();\r
+\r
+       public Drawable getImageFromUri(Context mContext, int widgetId, String imgUri) {\r
+               Drawable d = null;\r
+               if (mCacheForImageByUri.containsKey(imgUri) && mCacheForImageByUri.get(imgUri) != null) {\r
+                       SoftReference<Drawable> ref = mCacheForImageByUri.get(imgUri);\r
+                       if (ref != null) {\r
+                               d = ref.get();\r
+                       }\r
+               }\r
+\r
+               if (LOGD)\r
+                       if (d != null)\r
+                               Log.d(TAG, "image URI restored (width = " + d.getMinimumWidth() + " / weight = " + d.getMinimumHeight()\r
+                                               + ")");\r
+\r
+               if (d == null) {\r
+                       Uri mUri = Uri.parse(imgUri);\r
+                       String scheme = mUri.getScheme();\r
+                       if (ContentResolver.SCHEME_ANDROID_RESOURCE.equals(scheme)) {\r
+                               // try {\r
+                               // // Load drawable through Resources, to get the source density\r
+                               // information\r
+                               // ContentResolver.OpenResourceIdResult r =\r
+                               // mContext.getContentResolver().getResourceId(mUri);\r
+                               // d = r.r.getDrawable(r.id);\r
+                               // } catch (Exception e) {\r
+                               // Log.w("ImageView", "Unable to open content: " + mUri, e);\r
+                               // }\r
+                               Log.w("ImageView", "Unable to open content: " + mUri);\r
+                       } else if (ContentResolver.SCHEME_CONTENT.equals(scheme) || ContentResolver.SCHEME_FILE.equals(scheme)) {\r
+                               try {\r
+                                       d = Drawable.createFromStream(mContext.getContentResolver().openInputStream(mUri), null);\r
+                               } catch (Exception e) {\r
+                                       Log.w("ImageView", "Unable to open content: " + mUri, e);\r
+                               }\r
+                       } else {\r
+                               d = Drawable.createFromPath(mUri.toString());\r
+                       }\r
+\r
+                       if (LOGD)\r
+                               Log.d(TAG, "image URI decoded (width = " + d.getMinimumWidth() + " / weight = " + d.getMinimumHeight()\r
+                                               + ")");\r
+                       mCacheForImageByUri.put(imgUri, new SoftReference<Drawable>(d));\r
+\r
+                       // store image key usage\r
+                       ArrayList<String> list = mWidgetCacheUsageByUri.get(widgetId);\r
+                       if (list == null)\r
+                               list = new ArrayList<String>();\r
+                       list.add(imgUri);\r
+                       mWidgetCacheUsageByUri.put(widgetId, list);\r
+\r
+               }\r
+               return d;\r
+       }\r
+\r
+       public Drawable getImageFromId(Context ctx, int widgetId, int imgId) {\r
+               Drawable drawable = null;\r
+               if (mCacheForImageById.containsKey(imgId) && mCacheForImageById.get(imgId) != null) {\r
+                       SoftReference<Drawable> ref = mCacheForImageById.get(imgId);\r
+                       if (ref != null) {\r
+                               drawable = ref.get();\r
+                       } else {\r
+                               if (LOGD)\r
+                                       Log.d(TAG, "image ID missing !!!!!!!!!!");\r
+                       }\r
+               }\r
+\r
+               if (LOGD)\r
+                       if (drawable != null)\r
+                               Log.d(TAG, "image ID restored");\r
+\r
+               if (drawable == null) {\r
+                       if (LOGD)\r
+                               Log.d(TAG, "image ID decoded");\r
+\r
+                       InputStream rawResource = ctx.getResources().openRawResource( imgId);\r
+                       try\r
+                       {\r
+                               drawable = Drawable.createFromStream(rawResource, ctx.getResources().getResourceName(imgId));\r
+                       }\r
+                       finally\r
+                       {\r
+                               try {\r
+                                       rawResource.close();\r
+                               } catch (IOException e) {\r
+                                       e.printStackTrace();\r
+                               }\r
+                       }\r
+\r
+                       mCacheForImageById.put(imgId, new SoftReference<Drawable>(drawable));\r
+\r
+                       // store image key usage\r
+                       ArrayList<Integer> list = mWidgetCacheUsageById.get(widgetId);\r
+                       if (list == null)\r
+                               list = new ArrayList<Integer>();\r
+                       list.add(imgId);\r
+                       mWidgetCacheUsageById.put(widgetId, list);\r
+\r
+               }\r
+               return drawable;\r
+       }\r
+\r
+       public void unbindDrawables() {\r
+\r
+               for (Entry<Integer, SoftReference<Drawable>> drawableEntry : mCacheForImageById.entrySet()) {\r
+                       if ((drawableEntry != null) && (drawableEntry.getValue() != null)\r
+                                       && (drawableEntry.getValue().get() != null))\r
+                               drawableEntry.getValue().get().setCallback(null);\r
+               }\r
+\r
+               for (Entry<String, SoftReference<Drawable>> drawableEntry : mCacheForImageByUri.entrySet()) {\r
+                       if ((drawableEntry != null) && (drawableEntry.getValue() != null)\r
+                                       && (drawableEntry.getValue().get() != null))\r
+                               drawableEntry.getValue().get().setCallback(null);\r
+               }\r
+       }\r
+\r
+       public void clearCache() {\r
+               mCacheForImageById.clear();\r
+               mCacheForImageByUri.clear();\r
+       }\r
+\r
+       public String clearCacheForWidget(Context ctx, int widgetId) {\r
+               Drawable drawable = null;\r
+               ArrayList<String> listByUri = mWidgetCacheUsageByUri.get(widgetId);\r
+               if (listByUri != null) {\r
+                       for (String imgUri : listByUri) {\r
+                               if (mCacheForImageByUri.containsKey(imgUri) && mCacheForImageByUri.get(imgUri) != null) {\r
+                                       SoftReference<Drawable> ref = mCacheForImageByUri.get(imgUri);\r
+                                       if (ref != null) {\r
+                                               drawable = ref.get();\r
+                                               if (drawable != null)\r
+                                                       drawable.setCallback(null);\r
+                                       }\r
+                                       mCacheForImageByUri.remove(imgUri);\r
+                                       Log.d(TAG, "image URI removed from cache : " + imgUri);\r
+                               }\r
+                       }\r
+               }\r
+               mWidgetCacheUsageByUri.remove(widgetId);\r
+\r
+               ArrayList<Integer> listById = mWidgetCacheUsageById.get(widgetId);\r
+               if (listById != null) {\r
+                       for (Integer imgId : listById) {\r
+                               if (mCacheForImageById.containsKey(imgId) && mCacheForImageById.get(imgId) != null) {\r
+                                       SoftReference<Drawable> ref = mCacheForImageById.get(imgId);\r
+                                       if (ref != null) {\r
+                                               drawable = ref.get();\r
+                                               if (drawable != null)\r
+                                                       drawable.setCallback(null);\r
+                                       }\r
+                                       mCacheForImageById.remove(imgId);\r
+                                       Log.d(TAG, "image ID removed from cache : " + imgId);\r
+                               }\r
+                       }\r
+               }\r
+               mWidgetCacheUsageById.remove(widgetId);\r
+\r
+               return null;\r
+       }\r
+\r
+}
\ No newline at end of file