示例#1
0
 public override void Run()
 {
     Process.SetThreadPriority(ThreadPriority.Background);
     base.Run();
 }
            void Run()
            {
                Process.SetThreadPriority(ThreadPriority.Background);
                if (_context._adapter == null)
                {
                    _context.SendMessage(ConstantsCustomGallery.FETCH_STARTED);
                }

                File file;
                var  selectedImages = new List <long>();

                if (_context._images != null)
                {
                    for (int i = 0, l = _context._images.Count; i < l; i++)
                    {
                        var image = _context._images[i];
                        file = new File(image.Path);
                        if (file.Exists() && image.IsSelected)
                        {
                            selectedImages.Add(image.Id);
                        }
                    }
                }

                var cursor = _context.Application.ApplicationContext.ContentResolver.Query(
                    MediaStore.Images.Media.ExternalContentUri, _context._projection,
                    MediaStore.Images.Media.InterfaceConsts.BucketDisplayName + " =?", new string[] { _context._album },
                    MediaStore.Images.Media.InterfaceConsts.DateAdded);

                if (cursor == null)
                {
                    _context.SendMessage(ConstantsCustomGallery.ERROR);
                    return;
                }

                /*
                 * In case this runnable is executed to onChange calling loadImages,
                 * using countSelected variable can result in a race condition. To avoid that,
                 * tempCountSelected keeps track of number of selected images. On handling
                 * FETCH_COMPLETED message, countSelected is assigned value of tempCountSelected.
                 */
                int tempCountSelected = 0;
                var temp = new List <Image>(cursor.Count);

                if (cursor.MoveToLast())
                {
                    do
                    {
                        if (Thread.Interrupted())
                        {
                            return;
                        }

                        var id         = cursor.GetLong(cursor.GetColumnIndex(_context._projection[0]));
                        var name       = cursor.GetString(cursor.GetColumnIndex(_context._projection[1]));
                        var path       = cursor.GetString(cursor.GetColumnIndex(_context._projection[2]));
                        var isSelected = selectedImages.Contains(id);
                        if (isSelected)
                        {
                            tempCountSelected++;
                        }

                        file = null;
                        try
                        {
                            file = new File(path);
                        }
                        catch (Exception e)
                        {
                            Log.Debug("Exception : ", e.ToString());
                        }

                        if (file.Exists())
                        {
                            temp.Add(new Image(id, name, path, isSelected));
                        }
                    } while (cursor.MoveToPrevious());
                }

                cursor.Close();

                if (_context._images == null)
                {
                    _context._images = new List <Image>();
                }

                _context._images.Clear();
                _context._images.AddRange(temp);

                _context.SendMessage(ConstantsCustomGallery.FETCH_COMPLETED, tempCountSelected);
            }
示例#3
0
        /// <summary>
        /// Executes the download in a separate thread
        /// </summary>
        internal void Run()
        {
            Process.SetThreadPriority(ThreadPriority.Background);

            var state = new State(this.downloadInfo, this.downloaderService);

            PowerManager.WakeLock wakeLock = null;
            var finalStatus = ExpansionDownloadStatus.UnknownError;

            try
            {
                var pm = this.context.GetSystemService(Context.PowerService).JavaCast <PowerManager>();
                wakeLock = pm.NewWakeLock(WakeLockFlags.Partial, this.GetType().Name);
                wakeLock.Acquire();

                bool finished = false;
                do
                {
                    Log.Debug(Tag, "DownloadThread : initiating download for " + this.downloadInfo.FileName + " at " + this.downloadInfo.Uri);
                    var requestUri = new Uri(state.RequestUri);
                    var minute     = (int)TimeSpan.FromMinutes(1).TotalMilliseconds;
                    var request    = new HttpWebRequest(requestUri)
                    {
                        Proxy             = WebRequest.DefaultWebProxy,
                        UserAgent         = this.UserAgent,
                        Timeout           = minute,
                        ReadWriteTimeout  = minute,
                        AllowAutoRedirect = false
                    };

                    try
                    {
                        this.ExecuteDownload(state, request);
                        finished = true;
                    }
                    catch (RetryDownloadException)
                    {
                        // fall through
                    }
                    catch (Exception ex)
                    {
                        Debug.WriteLine("An exception in the download thread...");
                        Debug.WriteLine(ex.Message);
                        Debug.WriteLine(ex.StackTrace);
                        throw;
                    }
                    finally
                    {
                        request.Abort();
                    }
                }while (!finished);

                Log.Debug(Tag, "DownloadThread : download completed for " + this.downloadInfo.FileName);
                Log.Debug(Tag, "DownloadThread :   at " + this.downloadInfo.Uri);

                this.FinalizeDestinationFile(state);
                finalStatus = ExpansionDownloadStatus.Success;
            }
            catch (StopRequestException error)
            {
                // remove the cause before printing, in case it contains PII
                Debug.WriteLine(
                    "LVLDL Aborting request for download {0}: {1}", this.downloadInfo.FileName, error.Message);
                Debug.WriteLine(error.StackTrace);
                finalStatus = error.FinalStatus;
            }
            catch (Exception ex)
            {
                // sometimes the socket code throws unchecked exceptions
                Debug.WriteLine("LVLDL Exception for {0}: {1}", this.downloadInfo.FileName, ex.Message);
                finalStatus = ExpansionDownloadStatus.UnknownError;
            }
            finally
            {
                if (wakeLock != null)
                {
                    wakeLock.Release();
                }

                CleanupDestination(state, finalStatus);
                this.NotifyDownloadCompleted(
                    finalStatus, state.CountRetry, state.RetryAfter, state.RedirectCount, state.GotData);
            }
        }