public async Task UpdateRss(bool forceUpdate, CancellationToken ct)
        {
            try
            {
                if (ct.IsCancellationRequested)
                {
                    ct.ThrowIfCancellationRequested();
                }
                DateTime dateTime = DateTime.ParseExact(
                    prefs.GetString(General.MANUAL_LAST_SEARCH, "1984-10-12 14:00:00"),
                    "yyyy-MM-dd HH:mm:ss",
                    System.Globalization.CultureInfo.InvariantCulture);

                if (int.Parse(dateTime.Date.ToString("yyyyMMdd")) < int.Parse(DateTime.Now.ToString("yyyyMMdd")) ||
                    forceUpdate)
                {
                    rssEnCaso = LocalDatabase <RssEnCaso> .GetRssEnCasoDb().GetAll();

                    RssEnCaso[] rssEnCasoI = await RssEnCaso.GetRssEnCasoAsync(false, ct);

                    ISharedPreferencesEditor editor = prefs.Edit();
                    editor.PutString(General.MANUAL_LAST_SEARCH, DateTime.Now.ToString("yyyy-MM-dd HH:mm:ss"));
                    editor.Apply();

                    RssEnCaso[] insertRecords = rssEnCasoI.Except(rssEnCaso, new RssEnCaso()).ToArray();
                    foreach (RssEnCaso ins in insertRecords)
                    {
                        if (ct.IsCancellationRequested)
                        {
                            ct.ThrowIfCancellationRequested();
                        }
                        LocalDatabase <RssEnCaso> .GetRssEnCasoDb().Save(ins);
                    }

                    RssEnCaso[] deleteRecords = rssEnCaso.Except(rssEnCasoI, new RssEnCaso()).ToArray();
                    foreach (RssEnCaso del in deleteRecords)
                    {
                        if (ct.IsCancellationRequested)
                        {
                            ct.ThrowIfCancellationRequested();
                        }
                        LocalDatabase <RssEnCaso> .GetRssEnCasoDb().Delete(del);
                    }
                    manualRecyclerAdapter.UpdateData(rssEnCasoI);
                }
            }
            catch (WebException we)
            {
                Analytics.TrackEvent(we.Message);
                throw we;
            }
            catch (Exception ex)
            {
                Analytics.TrackEvent(ex.Message);
                throw ex;
            }
        }
예제 #2
0
        private async void DialogPositiveButtonClick(object sender, DialogClickEventArgs dialogClickEventArgs)
        {
            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this);
            bool isWiFi = prefs.GetBoolean(General.ALARM_ONLY_WIFI, true);

            try
            {
                CancellationTokenSource token      = new CancellationTokenSource();
                RssEnCaso[]             rssEnCasos = await RssEnCaso.GetRssEnCasoAsync(true, token.Token);

                if (rssEnCasos.Length > 0)
                {
                    DownloadReturns downloadReturns     = General.ExecuteDownload(this, rssEnCasos[0], isWiFi, true);
                    string          notificationMessage = "";
                    switch (downloadReturns)
                    {
                    case DownloadReturns.NoInternetConection:
                        notificationMessage = this.GetString(Resource.String.download_error_no_internet_conection);
                        break;

                    case DownloadReturns.NoWiFiFound:
                        notificationMessage = this.GetString(Resource.String.download_error_no_wifi_found);
                        break;

                    case DownloadReturns.NoWritePermission:
                        notificationMessage = this.GetString(Resource.String.download_error_no_write_permission);
                        break;
                    }
                    if (!string.IsNullOrEmpty(notificationMessage))
                    {
                        Toast.MakeText(this, notificationMessage, ToastLength.Long).Show();
                    }
                }
            }
            catch (WebException we)
            {
                Analytics.TrackEvent(we.Message);
            }
        }
        public async override void OnReceive(Context context, Intent intent)
        {
            General.ProgramNextAlarm(context);


            ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(context);
            bool isWiFi = prefs.GetBoolean(General.ALARM_ONLY_WIFI, true);

            RssEnCaso rssEnCaso = null;

            try
            {
                CancellationTokenSource cts        = new CancellationTokenSource();
                RssEnCaso[]             rssEnCasos = await RssEnCaso.GetRssEnCasoAsync(true, cts.Token);

                rssEnCaso = rssEnCasos[0];
            }
            catch (WebException we)
            {
                Intent callIntent = new Intent(context, typeof(MainActivity));
                callIntent.PutExtra(General.ALARM_NOTIFICATION_EXTRA, (int)DownloadReturns.NoInternetConection);
                PendingIntent pendingIntent       = PendingIntent.GetActivity(context, 0, callIntent, PendingIntentFlags.UpdateCurrent);
                string        notificationMessage = context.GetString(Resource.String.download_error_no_internet_conection);
                General.ShowNotification(context, notificationMessage, Resource.Drawable.main_nav_list, pendingIntent);

                Analytics.TrackEvent(we.Message);
            }

            if (rssEnCaso != null)
            {
                DownloadReturns downloadReturns     = General.ExecuteDownload(context, rssEnCaso, isWiFi, false);
                string          notificationMessage = "";
                Intent          callIntent          = new Intent(context, typeof(MainActivity));
                switch (downloadReturns)
                {
                case DownloadReturns.NoInternetConection:
                    callIntent.PutExtra(General.ALARM_NOTIFICATION_EXTRA, (int)DownloadReturns.NoInternetConection);
                    notificationMessage = context.GetString(Resource.String.download_error_no_internet_conection);
                    break;

                case DownloadReturns.NoWiFiFound:
                    callIntent.PutExtra(General.ALARM_NOTIFICATION_EXTRA, (int)DownloadReturns.NoWiFiFound);
                    notificationMessage = context.GetString(Resource.String.download_error_no_wifi_found);
                    break;

                case DownloadReturns.NoWritePermission:
                    callIntent.PutExtra(General.ALARM_NOTIFICATION_EXTRA, (int)DownloadReturns.NoWritePermission);
                    notificationMessage = context.GetString(Resource.String.download_error_no_write_permission);
                    break;

                case DownloadReturns.ServiceStarted:
                    callIntent = null;
                    break;
                }
                if (callIntent != null)
                {
                    PendingIntent pendingIntent = PendingIntent.GetActivity(context, 0, callIntent, PendingIntentFlags.UpdateCurrent);
                    General.ShowNotification(context, notificationMessage, Resource.Drawable.main_nav_list, pendingIntent);
                }
            }

            //var jobScheduler = (JobScheduler)context.GetSystemService(Context.JobSchedulerService);

            //var javaClass = Java.Lang.Class.FromType(typeof(DownloadJobService));
            //var componentName = new ComponentName(context, javaClass);
            //var jobInfo = new JobInfo
            //    .Builder(1, componentName)
            //    .SetMinimumLatency(1000)
            //    .SetOverrideDeadline(2000)
            //    .SetRequiredNetworkType(NetworkType.Any)
            //    .SetRequiresDeviceIdle(false)
            //    .SetRequiresCharging(false)
            //    .Build();

            //var scheduleResult = jobScheduler.Schedule(jobInfo);
            //if (JobScheduler.ResultSuccess == scheduleResult)
            //{

            //}
            //else
            //{

            //}
        }
        public override bool OnStartJob(JobParameters @params)
        {
            Task.Run(async() =>
            {
                ISharedPreferences prefs = PreferenceManager.GetDefaultSharedPreferences(this.ApplicationContext);
                bool isWiFi = prefs.GetBoolean(General.ALARM_ONLY_WIFI, true);

                RssEnCaso rssEnCaso = null;
                try
                {
                    CancellationTokenSource cts = new CancellationTokenSource();
                    RssEnCaso[] rssEnCasos      = await RssEnCaso.GetRssEnCasoAsync(true, cts.Token);
                    rssEnCaso = rssEnCasos[0];
                }
                catch (WebException we)
                {
                    Intent callIntent = new Intent(this.ApplicationContext, typeof(MainActivity));
                    callIntent.PutExtra(General.ALARM_NOTIFICATION_EXTRA, (int)DownloadReturns.NoInternetConection);
                    PendingIntent pendingIntent = PendingIntent.GetActivity(this.ApplicationContext, 0, callIntent, PendingIntentFlags.UpdateCurrent);
                    string notificationMessage  = this.ApplicationContext.GetString(Resource.String.download_error_no_internet_conection);
                    General.ShowNotification(this.ApplicationContext, notificationMessage, Resource.Drawable.main_nav_list, pendingIntent);

                    Analytics.TrackEvent(we.Message);
                }

                if (rssEnCaso != null)
                {
                    DownloadReturns downloadReturns = General.ExecuteDownload(this.ApplicationContext, rssEnCaso, isWiFi, false);
                    string notificationMessage      = "";
                    Intent callIntent = new Intent(this.ApplicationContext, typeof(MainActivity));
                    switch (downloadReturns)
                    {
                    case DownloadReturns.NoInternetConection:
                        callIntent.PutExtra(General.ALARM_NOTIFICATION_EXTRA, (int)DownloadReturns.NoInternetConection);
                        notificationMessage = this.ApplicationContext.GetString(Resource.String.download_error_no_internet_conection);
                        break;

                    case DownloadReturns.NoWiFiFound:
                        callIntent.PutExtra(General.ALARM_NOTIFICATION_EXTRA, (int)DownloadReturns.NoWiFiFound);
                        notificationMessage = this.ApplicationContext.GetString(Resource.String.download_error_no_wifi_found);
                        break;

                    case DownloadReturns.NoWritePermission:
                        callIntent.PutExtra(General.ALARM_NOTIFICATION_EXTRA, (int)DownloadReturns.NoWritePermission);
                        notificationMessage = this.ApplicationContext.GetString(Resource.String.download_error_no_write_permission);
                        break;

                    case DownloadReturns.ServiceStarted:
                        callIntent = null;
                        break;
                    }
                    if (callIntent != null)
                    {
                        PendingIntent pendingIntent = PendingIntent.GetActivity(this.ApplicationContext, 0, callIntent, PendingIntentFlags.UpdateCurrent);
                        General.ShowNotification(this.ApplicationContext, notificationMessage, Resource.Drawable.main_nav_list, pendingIntent);
                    }
                }

                // Have to tell the JobScheduler the work is done.
                JobFinished(@params, false);
            });
            return(true);
        }