private static async Task RenderThumbnailAsync
        (
            this PortableAsyncRenderer <Bitmap> asyncRenderer,
            PortableCorrelatedEntity correlatedEntity,
            Action <Action> runOnUiThread,
            AndroidCachingImageView thumbnailView,
            int thumbnailWidth,
            int thumbnailHeight,
            Resources activityResources,
            Drawable originalBackground,
            AnimationDrawable loadingAnimation,
            Action <CorruptObjectException, ImageView> onCorrupt,
            Action <ImageView, Drawable, AnimationDrawable, bool> onFinished
        )
        {
            bool isLoaded = false;

            if (correlatedEntity.CorrelationTag != thumbnailView.GetCorrelationTag())
            {
                onFinished?.Invoke(thumbnailView,
                                   originalBackground,
                                   loadingAnimation,
                                   isLoaded);
                return;
            }

            Bitmap thumbnailBitmap = null;
            CorruptObjectException thumbnailException = null;

            BitmapDrawable thumbnailDrawable = AndroidCrapApplication.GetDrawableFromCache
                                               (
                correlatedEntity.CorrelationTag
                                               );

            if (thumbnailDrawable == null)
            {
                try
                {
                    /*thumbnailDrawable = AndroidCrapApplication.GetReusableBitmapDrawable(thumbnailWidth,
                     *                                                                   thumbnailHeight);*/
                    thumbnailBitmap = await asyncRenderer.GetThumbnailAsync
                                      (
                        correlatedEntity,
                        () => thumbnailView.GetCorrelationTag(),
                        thumbnailWidth,
                        thumbnailHeight//,
                        //thumbnailDrawable.Bitmap
                                      );
                }
                catch (Exception exception)
                {
                    AndroidCrapApplication.ApplicationLogger.LogError(exception);
                    thumbnailBitmap?.Dispose();

                    thumbnailException = exception as CorruptObjectException;

                    if (thumbnailException == null)
                    {
                        throw;
                    }
                    else
                    {
                        runOnUiThread
                        (
                            () => onCorrupt(thumbnailException,
                                            thumbnailView)
                        );
                    }

                    onFinished?.Invoke(thumbnailView,
                                       originalBackground,
                                       loadingAnimation,
                                       isLoaded);
                    return;
                }

                if (correlatedEntity.CorrelationTag != thumbnailView.GetCorrelationTag())
                {
                    thumbnailBitmap?.Dispose();

                    onFinished?.Invoke(thumbnailView,
                                       originalBackground,
                                       loadingAnimation,
                                       isLoaded);
                    return;
                }

                if (thumbnailBitmap == null)
                {
                    onFinished?.Invoke(thumbnailView,
                                       originalBackground,
                                       loadingAnimation,
                                       isLoaded);
                    return;
                }

                thumbnailDrawable = AndroidCrapApplication.AddBitmapToCache
                                    (
                    correlatedEntity.CorrelationTag,
                    thumbnailBitmap,
                    activityResources
                                    );
            }

            isLoaded = true;

            onFinished?.Invoke(thumbnailView,
                               originalBackground,
                               loadingAnimation,
                               isLoaded);
            runOnUiThread
            (
                () => { thumbnailView.SetImageDrawable(thumbnailDrawable); }
            );

            /*AndroidCrapApplication.ApplicationLogger.LogDebug("{0} : {1}",
             *                                                correlatedEntity.CorrelationTag,
             *                                                GC.GetTotalMemory(false));*/
        }
        protected override async void OnCreate
        (
            Bundle bundle
        )
        {
            //AndroidCrapApplication.ApplicationLogger.LogDebug("MEMORY 4 : {0}",
            //                                                  GC.GetTotalMemory(false));

            base.OnCreate(bundle);

            SetContentView(Resource.Layout.SplashScreenLayout);

            _progressCount = FindViewById <TextView>(Resource.Id.ProgressCount);

            ImageView splashProgress = FindViewById <ImageView>(Resource.Id.SplashProgress);

            splashProgress.SetBackgroundResource(Resource.Drawable.ProgressSphere);

            AnimationDrawable splashAnimation  = splashProgress.Background as AnimationDrawable;
            Exception         loadingException = null;

            //AndroidCrapApplication.ApplicationLogger.LogDebug("MEMORY 5 : {0}",
            //                                                  GC.GetTotalMemory(false));

            splashAnimation?.Start();
            _loadingContext = this;

            if (_loadingTask == null)
            {
                _loadingTask = CreateLoadingTask
                               (
                    GetString(Resource.String.SplashTextFormat),

                    updateText: (progressText) => UpdateText(progressText),

                    getContext: () => { return(GetContext()); }
                               );
            }

            //AndroidCrapApplication.ApplicationLogger.LogDebug("MEMORY 6 : {0}",
            //                                                  GC.GetTotalMemory(false));

            try
            {
                await _loadingTask;
            }
            catch (Exception exception)
            {
                loadingException = exception;
            }
            finally
            {
                _loadingTask = null;

                splashAnimation?.Stop();
                splashAnimation?.Dispose();
                splashAnimation = null;
                splashProgress  = null;
            }

            /*// http://stackoverflow.com/questions/477572/strange-out-of-memory-issue-while-loading-an-image-to-a-bitmap-object
             *
             * Java.Lang.JavaSystem.RunFinalization();
             * Java.Lang.JavaSystem.Gc();
             * Java.Lang.JavaSystem.RunFinalization();
             * System.Runtime.GCSettings.LargeObjectHeapCompactionMode
             *  = System.Runtime.GCLargeObjectHeapCompactionMode.CompactOnce;
             * GC.Collect();*/

            if (loadingException != null)
            {
                AndroidCrapApplication.ShowExceptionAndExit(this,
                                                            loadingException);
                return;
            }

            //AndroidCrapApplication.ApplicationLogger.LogDebug("MEMORY 7 : {0}",
            //                                                  GC.GetTotalMemory(false));
        }
示例#3
0
        protected override void OnResume()
        {
            bool initializeAdapter = ((BrowseViewState == null) ||
                                      (PortableObjectRepository <Activity> .HasUnreadSourceChanges));

            if (initializeAdapter)
            {
                List <ObjectItem> objectRenderers;

                try
                {
                    objectRenderers
                        = PortableObjectRepository <Activity> .GetEnabledObjects <ObjectItem>()
                          .ToList();
                }
                catch (Exception exception)
                {
                    IsHandlingException = true;

                    base.OnResume();

                    AndroidCrapApplication.ShowExceptionAndExit(this,
                                                                exception);
                    return;
                }

                _objectViewAdapter = new AndroidBrowseObjectsAdapter
                                     (
                    RunOnUiThread,

                    (viewGroup) =>
                {
                    try
                    {
                        return(LayoutInflater.Inflate(ItemLayoutResource,
                                                      viewGroup,
                                                      attachToRoot: false));
                    }
                    catch (Exception exception)
                    {
                        throw exception;
                    }
                },

                    () => { return(BrowseFooterFragment.GetScaleType()); },
                    (scaleType) => BrowseFooterFragment.SetScaleType(scaleType),

                    () => { return(Resources); },

                    LoadingResource,

                    this.EditObject,

                    objectRenderers,

                    getString: (resource) => GetString(resource),

                    onCorrupt: (imageException,
                                imageView) =>
                {
                    this.HandleCorruptObject
                    (
                        imageException,
                        imageView,
                        getCount: () => { return(CorruptMesssageCount); },
                        setCount: (currentMassageCount) => { CorruptMesssageCount = currentMassageCount; }
                    );
                }
                                     );
            }

            base.OnResume();

            AbstractBrowseView.ItemClick -= ObjectItemClicked;
            AbstractBrowseView.ItemClick += ObjectItemClicked;

            bool isDataChanged = false;

            PortableBaseObject pendingObject = PortableObjectRepository <Activity> .PopPendingObject();

            while (pendingObject != null)
            {
                _objectViewAdapter.AddPendingObject(pendingObject);

                isDataChanged = true;

                pendingObject = PortableObjectRepository <Activity> .PopPendingObject();
            }

            if (isDataChanged)
            {
                _objectViewAdapter.NotifyDataSetChanged();
            }
        }