public void Fetch(string artUrl, FetchListener listener) {
			// WARNING: for the sake of simplicity, simultaneous multi-thread fetch requests
			// are not handled properly: they may cause redundant costly operations, like HTTP
			// requests and bitmap rescales. For production-level apps, we recommend you use
			// a proper image loading library, like Glide.
			var bMap = cache [artUrl];
			if (bMap != null) {
				LogHelper.Debug(Tag, "getOrFetch: album art is in cache, using it", artUrl);
				listener.OnFetched(artUrl, bMap[BigBitmapIndex], bMap[IconBitmapIndex]);
				return;
			}
			LogHelper.Debug(Tag, "getOrFetch: starting asynctask to fetch ", artUrl);

			Task.Run (async () => {
				Bitmap[] bitmaps;
				try {
					var bitmap = await BitmapHelper.FetchAndRescaleBitmap(artUrl, MaxArtWidth, MaxArtHeight);
					var icon = BitmapHelper.Scale(bitmap, MaxArtWidthIcon, MaxArtHeightIcon);
					bitmaps = new [] {bitmap, icon};
					cache.Put(artUrl, bitmaps);
				} catch(IOException) {
					return null;
				}
				LogHelper.Debug(Tag, "doInBackground: putting bitmap in cache. cache size=" + cache.Size());
				return bitmaps;
			}).ContinueWith ((antecedent) => {
				var bitmaps = antecedent.Result;
				if (bitmaps == null)
					listener.OnError(artUrl, new ArgumentException("got null bitmaps"));
				else
					listener.OnFetched(artUrl, bitmaps[BigBitmapIndex], bitmaps[IconBitmapIndex]);
			}, TaskContinuationOptions.OnlyOnRanToCompletion);
		}
예제 #2
0
        public void Fetch(string artUrl, FetchListener listener)
        {
            // WARNING: for the sake of simplicity, simultaneous multi-thread fetch requests
            // are not handled properly: they may cause redundant costly operations, like HTTP
            // requests and bitmap rescales. For production-level apps, we recommend you use
            // a proper image loading library, like Glide.
            Bitmap[] bMap = cache[artUrl];
            if (bMap != null)
            {
                LogHelper.Debug(Tag, "getOrFetch: album art is in cache, using it", artUrl);
                listener.OnFetched(artUrl, bMap[BigBitmapIndex], bMap[IconBitmapIndex]);
                return;
            }
            LogHelper.Debug(Tag, "getOrFetch: starting asynctask to fetch ", artUrl);

            Task.Run(async() => {
                Bitmap[] bitmaps;
                try
                {
                    Bitmap bitmap = await BitmapHelper.FetchAndRescaleBitmap(artUrl, MaxArtWidth, MaxArtHeight);
                    Bitmap icon   = BitmapHelper.Scale(bitmap, MaxArtWidthIcon, MaxArtHeightIcon);
                    bitmaps       = new[] { bitmap, icon };
                    cache.Put(artUrl, bitmaps);
                }
                catch (IOException)
                {
                    return(null);
                }
                LogHelper.Debug(Tag, "doInBackground: putting bitmap in cache. cache size=" + cache.Size());
                return(bitmaps);
            }).ContinueWith((antecedent) => {
                var bitmaps = antecedent.Result;
                if (bitmaps == null)
                {
                    listener.OnError(artUrl, new ArgumentException("got null bitmaps"));
                }
                else
                {
                    listener.OnFetched(artUrl, bitmaps[BigBitmapIndex], bitmaps[IconBitmapIndex]);
                }
            }, TaskContinuationOptions.OnlyOnRanToCompletion);
        }