public static void SetUrlDrawable(this ImageView imageView, string url, Drawable defaultDrawable, long cacheDurationMs, IUrlImageViewCallback callback)
 {
     UrlImageViewHelper.SetUrlDrawable(imageView, url, defaultDrawable, cacheDurationMs, callback);
 }
예제 #2
0
 public static void SetUrlDrawable(ImageView imageView, string url, Drawable defaultDrawable, long cacheDurationMs, IUrlImageViewCallback callback)
 {
     SetUrlDrawable(imageView.Context, imageView, url, defaultDrawable, cacheDurationMs, callback);
 }
 public static void SetUrlDrawable(this ImageView imageView, string url, IUrlImageViewCallback callback)
 {
     UrlImageViewHelper.SetUrlDrawable(imageView, url, callback);
 }
예제 #4
0
 public static void SetUrlDrawable(ImageView imageView, string url, Drawable defaultDrawable, IUrlImageViewCallback callback)
 {
     SetUrlDrawable(imageView.Context, imageView, url, defaultDrawable, CACHE_DURATION_THREE_DAYS, callback);
 }
예제 #5
0
 public static void LoadUrlDrawable(Context context, string url, long cacheDurationMs, IUrlImageViewCallback callback)
 {
     SetUrlDrawable(context, null, url, null, cacheDurationMs, callback);
 }
예제 #6
0
        private static void SetUrlDrawable(Context context, ImageView imageView, string url, Drawable defaultDrawable, long cacheDurationMs, IUrlImageViewCallback callback)
        {
            Cleanup(context);

            if (imageView != null)
            {
                pendingViews.Remove(imageView);
            }

            if (string.IsNullOrEmpty(url))
            {
                if (imageView != null)
                {
                    imageView.SetImageDrawable(defaultDrawable);
                }

                return;
            }

            var cache    = UrlImageCache.Instance;
            var drawable = cache.Get(url);

            if (drawable != null)
            {
                if (imageView != null)
                {
                    imageView.SetImageDrawable(drawable);
                }
                if (callback != null)
                {
                    callback.OnLoaded(imageView, drawable, url, true);
                }
                return;
            }

            var baseDir  = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
            var filename = System.IO.Path.Combine(baseDir, GetFilenameForUrl(url));

            var file = new System.IO.FileInfo(filename);

            if (file.Exists)
            {
                try
                {
                    if (cacheDurationMs == CACHE_DURATION_INFINITE || DateTime.UtcNow < file.LastWriteTimeUtc.AddMilliseconds(cacheDurationMs))
                    {
                        drawable = LoadDrawableFromFile(context, filename);
                        //var fis = context.OpenFileInput(filename);
                        //drawable = LoadDrawableFromStream(context, fis);
                        //fis.Close();

                        if (imageView != null)
                        {
                            imageView.SetImageDrawable(drawable);
                        }

                        cache.Put(url, drawable);

                        if (callback != null)
                        {
                            callback.OnLoaded(imageView, drawable, url, true);
                        }

                        return;
                    }
                    else
                    {
                        //TODO: File cache expired, refreshing
                        Android.Util.Log.Debug(LOGTAG, "File Cache Expired: " + file.Name);
                    }
                }
                catch (Exception ex)
                {
                    Android.Util.Log.Debug(LOGTAG, "File Cache Exception " + ex.ToString());
                }
            }

            if (imageView != null)
            {
                imageView.SetImageDrawable(defaultDrawable);
            }

            if (imageView != null)
            {
                pendingViews.Put(imageView, url);
            }

            //Check to see if another view is already waiting for this url so we don't download it again
            var currentDownload = pendingDownloads.Get(url);

            if (currentDownload != null)
            {
                if (imageView != null)
                {
                    currentDownload.Add(imageView);
                }

                return;
            }

            var downloads = new List <ImageView>();

            if (imageView != null)
            {
                downloads.Add(imageView);
            }

            pendingDownloads.Put(url, downloads);

            var downloaderTask = new AnonymousAsyncTask <string, string, BitmapDrawable>((p) =>
            {
                try
                {
                    var client = new System.Net.WebClient();
                    var data   = client.DownloadData(url);

                    System.IO.File.WriteAllBytes(filename, data);

                    return(LoadDrawableFromFile(context, filename));
                }
                catch (Exception ex)
                {
                    Android.Util.Log.Debug(LOGTAG, "Download Error: " + ex.ToString());
                    return(null);
                }
            }, (bd) =>
            {
                try
                {
                    var usableResult = bd;
                    if (usableResult == null)
                    {
                        usableResult = (BitmapDrawable)defaultDrawable;
                    }

                    pendingDownloads.Remove(url);

                    cache.Put(url, usableResult);

                    foreach (var iv in downloads)
                    {
                        var pendingUrl = pendingViews.Get(iv);
                        if (!url.Equals(pendingUrl))
                        {
                            continue;
                        }
                        pendingViews.Remove(iv);

                        if (usableResult != null)
                        {
                            var fnewImage  = usableResult;
                            var fimageView = iv;

                            fimageView.SetImageDrawable(fnewImage);

                            if (callback != null)
                            {
                                callback.OnLoaded(fimageView, bd, url, false);
                            }
                        }
                    }
                }
                catch (Exception ex)
                {
                    Android.Util.Log.Debug(LOGTAG, "PostExecute Error: " + ex.ToString());
                }
            });

            downloaderTask.Execute(new Java.Lang.Object[] { });
        }
예제 #7
0
 public static void LoadUrlDrawable(Context context, string url, IUrlImageViewCallback callback)
 {
     SetUrlDrawable(context, null, url, null, CACHE_DURATION_THREE_DAYS, callback);
 }
예제 #8
0
	    public static void SetUrlDrawable(ImageView imageView, string url, Drawable defaultDrawable, long cacheDurationMs, IUrlImageViewCallback callback) 
		{
	        SetUrlDrawable(imageView.Context, imageView, url, defaultDrawable, cacheDurationMs, callback);
	    }
예제 #9
0
        private static void SetUrlDrawable(Context context, ImageView imageView, string url, int defaultResource, long cacheDurationMs, IUrlImageViewCallback callback)
        {
            Drawable d = null;

            if (defaultResource != 0)
            {
                d = imageView.Resources.GetDrawable(defaultResource);
            }
            SetUrlDrawable(context, imageView, url, d, cacheDurationMs, callback);
        }
예제 #10
0
	    public static void SetUrlDrawable(ImageView imageView, string url, Drawable defaultDrawable, IUrlImageViewCallback callback) 
		{
	        SetUrlDrawable(imageView.Context, imageView, url, defaultDrawable, CACHE_DURATION_THREE_DAYS, callback);
	    }
예제 #11
0
	    public static void LoadUrlDrawable(Context context, string url, long cacheDurationMs, IUrlImageViewCallback callback) 
		{
	        SetUrlDrawable(context, null, url, null, cacheDurationMs, callback);
	    }
예제 #12
0
	    public static void LoadUrlDrawable(Context context, string url, IUrlImageViewCallback callback) 
		{
	        SetUrlDrawable(context, null, url, null, CACHE_DURATION_THREE_DAYS, callback);
	    }
예제 #13
0
		private static void SetUrlDrawable(Context context, ImageView imageView, string url, Drawable defaultDrawable, long cacheDurationMs, IUrlImageViewCallback callback)
		{
			Cleanup(context);

			if (imageView != null)
				pendingViews.Remove(imageView);

			if (string.IsNullOrEmpty(url))
			{
				if (imageView != null)
					imageView.SetImageDrawable(defaultDrawable);

				return;
			}

			var cache = UrlImageCache.Instance;
			var drawable = cache.Get(url);

			if (drawable != null)
			{
				if (imageView != null)
					imageView.SetImageDrawable(drawable);
				if (callback != null)
					callback.OnLoaded(imageView, drawable, url, true);
				return;
			}

			var baseDir = System.Environment.GetFolderPath(System.Environment.SpecialFolder.Personal);
			var filename = System.IO.Path.Combine(baseDir, GetFilenameForUrl(url));
					
			var file = new System.IO.FileInfo(filename);
			if (file.Exists)
			{
				try
				{
					if (cacheDurationMs == CACHE_DURATION_INFINITE || DateTime.UtcNow < file.LastWriteTimeUtc.AddMilliseconds(cacheDurationMs))
					{
						drawable = LoadDrawableFromFile(context, filename);
						//var fis = context.OpenFileInput(filename);
						//drawable = LoadDrawableFromStream(context, fis);
						//fis.Close();

						if (imageView != null)
							imageView.SetImageDrawable(drawable);

						cache.Put(url, drawable);

						if (callback != null)
							callback.OnLoaded(imageView, drawable, url, true);

						return;
					}
					else
					{
						//TODO: File cache expired, refreshing
						Android.Util.Log.Debug(LOGTAG, "File Cache Expired: " + file.Name);
					}
				}
				catch (Exception ex)
				{
					Android.Util.Log.Debug(LOGTAG, "File Cache Exception " + ex.ToString());
				}
			}

			if (imageView != null)
				imageView.SetImageDrawable(defaultDrawable);

			if (imageView != null)
				pendingViews.Put(imageView, url);

			//Check to see if another view is already waiting for this url so we don't download it again
			var currentDownload = pendingDownloads.Get(url);
			if (currentDownload != null)
			{
				if (imageView != null)
					currentDownload.Add(imageView);

				return;
			}

			var downloads = new List<ImageView>();
			if (imageView != null)
				downloads.Add(imageView);

			pendingDownloads.Put(url, downloads);

			var downloaderTask = new AnonymousAsyncTask<string, string, BitmapDrawable>((p) => 
			{
				try
				{
					var client = new System.Net.WebClient();
					var data = client.DownloadData(url);

					System.IO.File.WriteAllBytes(filename, data);

					return LoadDrawableFromFile(context, filename);
				}
				catch (Exception ex) 
				{
					Android.Util.Log.Debug(LOGTAG, "Download Error: " + ex.ToString());
					return null; 
				}


			}, (bd) => 
			{
				try
				{
					var usableResult = bd;
					if (usableResult == null)
						usableResult = (BitmapDrawable)defaultDrawable;

					pendingDownloads.Remove(url);

					cache.Put(url, usableResult);

					foreach (var iv in downloads)
					{
						var pendingUrl = pendingViews.Get (iv);
						if (!url.Equals(pendingUrl))
							continue;
						pendingViews.Remove(iv);

						if (usableResult != null)
						{
							var fnewImage = usableResult;
							var fimageView = iv;

							fimageView.SetImageDrawable(fnewImage);

							if (callback != null)
								callback.OnLoaded(fimageView, bd, url, false);
						}
					}
				}
				catch (Exception ex)
				{
					Android.Util.Log.Debug(LOGTAG, "PostExecute Error: " + ex.ToString()); 
				}

			});

			downloaderTask.Execute(new Java.Lang.Object[]{});
		}
예제 #14
0
	    private static void SetUrlDrawable(Context context, ImageView imageView, string url, int defaultResource, long cacheDurationMs, IUrlImageViewCallback callback) 
		{
	        Drawable d = null;
	        if (defaultResource != 0)
				d = imageView.Resources.GetDrawable(defaultResource);
	        SetUrlDrawable(context, imageView, url, d, cacheDurationMs, callback);
	    }
 public static void SetUrlDrawable(this ImageView imageView, string url, Drawable defaultDrawable, long cacheDurationMs, IUrlImageViewCallback callback)
 {
     UrlImageViewHelper.SetUrlDrawable(imageView, url, defaultDrawable, cacheDurationMs, callback);
 }
 public static void SetUrlDrawable(this ImageView imageView, string url, IUrlImageViewCallback callback)
 {
     UrlImageViewHelper.SetUrlDrawable(imageView, url, callback);
 }