Beispiel #1
0
        // private async void MigrateSchedulesAsync(Int64 currentVersion)
        // {
        //     if (_application.Preferences.LastMigrationVersion == currentVersion)
        //     {
        //         return ;
        //     }
        //
        //     if (!await _application.Manager.MigrateSchedulesAsync())
        //     {
        //         ShowSnackbar(Resource.String.schedulesMigrationErrorMessage);
        //         _ = _application.SaveLogAsync();
        //     }
        //     else
        //     {
        //         _application.Preferences.SetLastMigrationVersion(currentVersion);
        //     }
        // }

        private async void CheckForCriticalUpdatesAsync(Int64 currentVersion)
        {
            if (IsPermissionDenied(Manifest.Permission.Internet))
            {
                RequestPermissions(InternetPermissionRequestCode, Manifest.Permission.Internet);
                return;
            }

            ReleaseDescription latest = await ApplicationUtilities.GetLatestReleaseDescription();

            if (latest == null)
            {
                return;
            }

            if (!latest.IsCriticalUpdate) // && !_application.Preferences.CheckUpdatesOnStart
            {
                return;
            }

            if (latest.VersionCode == _application.Preferences.LastSeenUpdateVersion ||
                latest.VersionCode <= currentVersion)
            {
                return;
            }

            Java.Lang.ICharSequence dialogMessage = (latest.VersionNotes != null)
                ? latest.VersionNotes.FromHtml()
                : Resources.GetString(Resource.String.applicationUpdateAvailableMessage).FromMarkdown();

            Int32 dialogTitleId = latest.IsCriticalUpdate
                ? Resource.String.applicationCriticalUpdateAvailableDialogTitle
                : Resource.String.applicationUpdateAvailableDialogTitle;

            String packageId = latest.GooglePlayStorePackageId;

            if (packageId == null)
            {
                new CustomAlertDialog(this)
                .SetTitle(dialogTitleId)
                .SetMessage(dialogMessage)
                .SetPositiveButton(
                    Resource.String.openUpdateDownloadPageActionTitle,
                    () =>
                {
                    String url = ApplicationUtilities.LatestReleaseDownloadPageUrl;
                    StartActivity(IntentUtilities.CreateViewIntentFromUrl(url));
                }
                    )
                .SetNegativeButton(
                    Resource.String.gotItActionTitle,
                    () => _application.Preferences.SetLastSeenUpdateVersion(latest.VersionCode)
                    )
                .Show();

                return;
            }

            void OpenWithPlayStore()
            {
                try
                {
                    Intent intent = IntentUtilities.CreateGooglePlayStoreViewIntent(
                        this,
                        packageId
                        );

                    if (intent != null)
                    {
                        StartActivity(intent);
                    }
                }
                catch (ActivityNotFoundException)
                {
                    Intent intent = IntentUtilities.CreateGooglePlayStoreViewIntent(
                        this,
                        packageId,
                        true
                        );

                    if (intent != null)
                    {
                        StartActivity(intent);
                    }
                }
            }

            if (packageId == PackageName)
            {
                new CustomAlertDialog(this)
                .SetTitle(dialogTitleId)
                .SetMessage(dialogMessage)
                .SetPositiveButton(
                    Resource.String.openPlayMarketActionTitle,
                    () => OpenWithPlayStore()
                    )
                .SetNegativeButton(
                    Resource.String.gotItActionTitle,
                    () => _application.Preferences.SetLastSeenUpdateVersion(latest.VersionCode)
                    )
                .Show();

                return;
            }

            new CustomAlertDialog(this)
            .SetTitle(Resource.String.googlePlayStoreReleaseAvailableDialogTitle)
            .SetMessage(Resource.String.googlePlayStoreReleaseRelocatedMessage)
            .SetPositiveButton(Resource.String.openPlayMarketActionTitle, () => OpenWithPlayStore())
            .Show();
        }
Beispiel #2
0
        protected override void OnCreate(Bundle savedInstanceState)
        {
            _application = ApplicationContext as SmtuScheduleApplication;

            // Если пользователь нажал на уведомление, содержащее полезную нагрузку и пришедшее, когда
            // приложение не было запущено, либо находилось в фоновом режиме.
            if (Intent.Extras != null)
            {
                ICollection <String> keys = Intent.Extras.KeySet();
                if (IntentUtilities.IsDataKeysCollectionValidToCreateViewIntent(keys))
                {
                    Dictionary <String, String> data = new Dictionary <String, String>();
                    foreach (String key in keys)
                    {
                        data[key] = Intent.Extras.GetString(key);
                    }

                    Intent intent = IntentUtilities.CreateIntentFromData(this, data);
                    if (intent != null)
                    {
                        StartActivity(intent);
                    }

                    Finish();
                }
                else if (IntentUtilities.IsDataKeysCollectionValidToCreateUpcomingLessonIntent(keys))
                {
                    const String dateKey = IntentUtilities.DataUpcomingLessonDateKey;
                    if (Int64.TryParse(Intent.Extras.GetString(dateKey), out Int64 ticks))
                    {
                        _application.Preferences.CurrentScheduleDate = new DateTime(ticks);
                    }

                    const String scheduleIdKey = IntentUtilities.DataUpcomingLessonScheduleIdKey;
                    if (Int32.TryParse(Intent.Extras.GetString(scheduleIdKey), out Int32 scheduleId))
                    {
                        _application.Preferences.SetCurrentScheduleId(scheduleId);
                    }
                }
            }

            _currentlyUsedDarkTheme = _application.Preferences.UseDarkTheme;
            _isThemeChanged         = false;
            _application.Preferences.ThemeChanged += () =>
            {
                _isThemeChanged = (_currentlyUsedDarkTheme != _application.Preferences.UseDarkTheme);
            };

            _application.Preferences.UpdateSchedulesOnStartChanged += () =>
            {
                if (_application.Preferences.UpdateSchedulesOnStart)
                {
                    PeriodicWorkUtilities.CreateAndEnqueueWork <UpdateSchedulesWorker>(
                        this,
                        UpdateSchedulesWorkerTag,
                        TimeSpan.FromHours(12),
                        true
                        );
                }
                else
                {
                    PeriodicWorkUtilities.CancelWork(this, UpdateSchedulesWorkerTag);
                }
            };

            _application.Preferences.LessonRemindTimesChanged += () =>
            {
                if (_application.Preferences.LessonRemindTimes != LessonRemindTime.Never)
                {
                    PeriodicWorkUtilities.CreateAndEnqueueWork <LessonsRemindWorker>(
                        this,
                        UpcomingLessonRemindWorkerTag,
                        TimeSpan.FromDays(1),
                        false
                        );
                }
                else
                {
                    PeriodicWorkUtilities.CancelWork(this, UpcomingLessonRemindWorkerTag);
                }
            };

            SetTheme(_application.Preferences.UseDarkTheme ? Resource.Style.Theme_SmtuSchedule_Dark
                : Resource.Style.Theme_SmtuSchedule_Light);

            base.OnCreate(savedInstanceState);

            _stateManager = new MainActivityStateManager(MainActivityState.NotInitialized);

            SetContentView(Resource.Layout.mainActivity);

            _contentLayout = FindViewById <RelativeLayout>(Resource.Id.mainContentRelativeLayout);

            _toolbar = FindViewById <Toolbar>(Resource.Id.mainActivityToolbar);
            _toolbar.InflateMenu(Resource.Menu.mainMenu);
            _toolbar.Title          = null;
            _toolbar.MenuItemClick += (s, e) =>
            {
                switch (e.Item.ItemId)
                {
                case Resource.Id.selectViewingDateMenuItem:
                    ShowCustomDatePickerDialog();
                    break;

                case Resource.Id.downloadSchedulesMenuItem:
                    StartDownloadActivityAsync();
                    break;

                case Resource.Id.startPreferencesMenuItem:
                    StartPreferencesActivity();
                    break;

                case Resource.Id.aboutApplicationMenuItem:
                    new CustomAlertDialog(this)
                    .SetTitle(Resource.String.aboutApplicationDialogTitle)
                    .SetMessage(Resource.String.aboutApplicationMessage)
                    .SetPositiveButton(Resource.String.thanksActionTitle)
                    .Show();
                    break;
                }
            };

            if (IsPermissionDenied(Manifest.Permission.WriteExternalStorage))
            {
                RequestPermissions(ExternalStoragePermissionsRequestCode, ExternalStoragePermissions);
            }
            else
            {
                ContinueActivityInitializationAsync();
            }
        }