public void Run()
        {
            if (mHandler == null)
            {
                mHandler = new Handler(Looper.MainLooper);
            }
            ActivityManager.RunningAppProcessInfo state = new ActivityManager.RunningAppProcessInfo();
            ActivityManager.GetMyMemoryState(state);
            var  importance   = state.Importance;
            bool serviceInUse = state.ImportanceReasonCode == ImportanceReason.ServiceInUse;

            if (importance != mPreviousImportance || serviceInUse != mPreviousServiceInUse)
            {
                mPreviousImportance   = importance;
                mPreviousServiceInUse = serviceInUse;
                string message = "New importance = " + importance;
                if (serviceInUse)
                {
                    message += " (Reason: Service in use)";
                }
                Log.Warn(TAG, message);
            }
            mHandler?.PostDelayed(this, 1000 * 2);
            //D.WriteLine("Running");
        }
        public static bool IsInBackground()
        {
            var AppProcessInfo = new ActivityManager.RunningAppProcessInfo();

            ActivityManager.GetMyMemoryState(AppProcessInfo);
            return(AppProcessInfo.Importance == Importance.Background || AppProcessInfo.Importance == Importance.Visible);
        }
Example #3
0
            public override void OnReceive(Context context, Intent intent)
            {
                try
                {
                    if (intent.Action == DownloadManager.ActionDownloadComplete)
                    {
                        DownloadManager       downloadManagerExcuter = (DownloadManager)Application.Context.GetSystemService(Context.DownloadService);
                        long                  downloadId             = intent.GetLongExtra(DownloadManager.ExtraDownloadId, -1);
                        DownloadManager.Query query = new DownloadManager.Query();
                        query.SetFilterById(downloadId);
                        ICursor c = downloadManagerExcuter.InvokeQuery(query);

                        if (c.MoveToFirst())
                        {
                            SqLiteDatabase dbDatabase = new SqLiteDatabase();

                            int columnIndex = c.GetColumnIndex(DownloadManager.ColumnStatus);
                            if (c.GetInt(columnIndex) == (int)DownloadStatus.Successful)
                            {
                                string downloadedPath = c.GetString(c.GetColumnIndex(DownloadManager.ColumnLocalUri));

                                ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo();
                                ActivityManager.GetMyMemoryState(appProcessInfo);
                                if (appProcessInfo.Importance == Importance.Foreground || appProcessInfo.Importance == Importance.Background)
                                {
                                    dbDatabase.Update_WatchOfflineVideos(Video.id, downloadedPath);
                                    var videoViewer = ContextActivty as Video_Viewer_Activity;
                                    var fullScreen  = ContextActivty as FullScreenVideoActivity;

                                    if (videoViewer != null)
                                    {
                                        if (videoViewer.VideoActionsController.Videodata.id == Video.id)
                                        {
                                            videoViewer.VideoActionsController.Download_icon.SetImageDrawable(Application.Context.GetDrawable(Resource.Drawable.ic_checked_red));
                                            videoViewer.VideoActionsController.Download_icon.Tag = "Downloaded";
                                        }
                                    }
                                    else if (fullScreen != null)
                                    {
                                        if (fullScreen.VideoActionsController.Videodata.id == Video.id)
                                        {
                                            fullScreen.VideoActionsController.Download_icon.SetImageDrawable(Application.Context.GetDrawable(Resource.Drawable.ic_checked_red));
                                            fullScreen.VideoActionsController.Download_icon.Tag = "Downloaded";
                                        }
                                    }
                                }
                                else
                                {
                                    dbDatabase.Update_WatchOfflineVideos(Video.id, downloadedPath);
                                }
                            }
                            dbDatabase.Dispose();
                        }
                    }
                }
                catch (Exception exception)
                {
                    Crashes.TrackError(exception);
                }
            }
Example #4
0
            public virtual void Run()
            {
                ActivityManager.RunningAppProcessInfo state = new ActivityManager.RunningAppProcessInfo();
                ActivityManager.GetMyMemoryState(state);
                int  importance   = (int)state.Importance;
                bool serviceInUse =
                    state.ImportanceReasonCode
                    ==
                    //ActivityManager.RunningAppProcessInfo.ReasonProviderInUse
                    Android.App.ImportanceReason.ProviderInUse
                ;

                if (importance != mPreviousImportance || serviceInUse != mPreviousServiceInUse)
                {
                    mPreviousImportance   = importance;
                    mPreviousServiceInUse = serviceInUse;
                    string message = "New importance = " + importance;
                    if (serviceInUse)
                    {
                        message += " (Reason: Service in use)";
                    }
                    Log.Warn(TAG, message);
                }
                mHandler.PostDelayed(this, 1000);
            }
            public override void OnReceive(Context context, Intent intent)
            {
                try
                {
                    if (intent.Action == DownloadManager.ActionDownloadComplete)
                    {
                        if (ActivityContext == null)
                        {
                            return;
                        }

                        DownloadManager       downloadManagerExcuter = (DownloadManager)Application.Context.GetSystemService(Context.DownloadService);
                        long                  downloadId             = intent.GetLongExtra(DownloadManager.ExtraDownloadId, -1);
                        DownloadManager.Query query = new DownloadManager.Query();
                        query.SetFilterById(downloadId);
                        ICursor c         = downloadManagerExcuter.InvokeQuery(query);
                        var     sqlEntity = new SqLiteDatabase();

                        if (c.MoveToFirst())
                        {
                            int columnIndex = c.GetColumnIndex(DownloadManager.ColumnStatus);
                            if (c.GetInt(columnIndex) == (int)DownloadStatus.Successful)
                            {
                                string downloadedPath = c.GetString(c.GetColumnIndex(DownloadManager.ColumnLocalUri));

                                ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo();
                                ActivityManager.GetMyMemoryState(appProcessInfo);
                                if (appProcessInfo.Importance == Importance.Foreground || appProcessInfo.Importance == Importance.Background)
                                {
                                    sqlEntity.InsertOrUpdate_LatestDownloadsSound(Sound.Id, downloadedPath);
                                    if (TypeActivity == "Main")
                                    {
                                        if (ActivityContext is HomeActivity tabbedMain)
                                        {
                                            tabbedMain.SoundController.BtnIconDownload.Tag = "Downloaded";
                                            tabbedMain.SoundController.BtnIconDownload.SetImageResource(Resource.Drawable.ic_check_circle);
                                            tabbedMain.SoundController.BtnIconDownload.SetColorFilter(Color.Red);

                                            tabbedMain.SoundController.ProgressBarDownload.Visibility = ViewStates.Invisible;
                                            tabbedMain.SoundController.BtnIconDownload.Visibility     = ViewStates.Visible;

                                            tabbedMain.LibrarySynchronizer.AddToLatestDownloads(Sound);
                                        }
                                    }
                                }
                                else
                                {
                                    sqlEntity.InsertOrUpdate_LatestDownloadsSound(Sound.Id, downloadedPath);
                                }
                            }
                        }

                        sqlEntity.Dispose();
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                }
            }
        internal void ActivateKp2aKeyboard()
        {
            string currentIme = Android.Provider.Settings.Secure.GetString(
                ContentResolver,
                Android.Provider.Settings.Secure.DefaultInputMethod);

            string kp2aIme = Kp2aInputMethodName;



            if (currentIme == kp2aIme)
            {
                //keyboard already activated. bring it up.
                InputMethodManager imeManager = (InputMethodManager)ApplicationContext.GetSystemService(InputMethodService);
                if (imeManager == null)
                {
                    Toast.MakeText(this, Resource.String.not_possible_im_picker, ToastLength.Long).Show();
                    return;
                }
                imeManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.None);
            }
            else
            {
                if (!IsKp2aInputMethodEnabled)
                {
                    //must be enabled in settings first
                    Toast.MakeText(this, Resource.String.please_activate_keyboard, ToastLength.Long).Show();
                    Intent settingsIntent = new Intent(Android.Provider.Settings.ActionInputMethodSettings);
                    settingsIntent.SetFlags(ActivityFlags.NewTask);
                    StartActivity(settingsIntent);
                }
                else
                {
                    //let's bring up the keyboard switching dialog.
                    //Unfortunately this no longer works starting with Android 9 if our app is not in foreground.
                    bool mustUseHelperActivity = false;
                    if ((int)Build.VERSION.SdkInt >= 28)
                    {
                        ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo();
                        ActivityManager.GetMyMemoryState(appProcessInfo);
                        mustUseHelperActivity = (appProcessInfo.Importance != Importance.Foreground);
                    }
                    if (mustUseHelperActivity)
                    {
                        StartActivity(typeof(SwitchImeActivity));
                    }
                    else
                    {
#if !EXCLUDE_KEYBOARD
                        Keepass2android.Kbbridge.ImeSwitcher.SwitchToKeyboard(this, kp2aIme, false);
#endif
                    }
                }
            }
        }
Example #7
0
        public void GenerateLocalNotificationOnlyIfInBackground(NotificationViewModel viewModel)
        {
            ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
            ActivityManager.GetMyMemoryState(myProcess);
            bool isInBackground = myProcess.Importance != Importance.Foreground;

            if (isInBackground)
            {
                new LocalNotificationsManager().GenerateLocalNotification(viewModel, 0);
                LocalPreferencesHelper.TermsNotificationWasShown = true;
            }
        }
Example #8
0
        private void GenerateLocalNotificationOnlyIfInBackgroundBroadcasted(Context context,
                                                                            NotificationViewModel viewModel)
        {
            ActivityManager.RunningAppProcessInfo myProcess = new ActivityManager.RunningAppProcessInfo();
            ActivityManager.GetMyMemoryState(myProcess);
            bool isInBackground = myProcess.Importance != Importance.Foreground;

            if (isInBackground)
            {
                new LocalNotificationsManager(context).GenerateLocalNotification(viewModel, 0);
                LocalPreferencesHelper.LastDateTimeTermsNotificationWasShown = SystemTime.Now();
            }
        }
Example #9
0
 private bool IsInForeground()
 {
     ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo();
     ActivityManager.GetMyMemoryState(appProcessInfo);
     return(appProcessInfo.Importance == Importance.Visible || appProcessInfo.Importance == Importance.Foreground);
 }
        internal void ActivateKp2aKeyboard()
        {
            string currentIme = Android.Provider.Settings.Secure.GetString(
                ContentResolver,
                Android.Provider.Settings.Secure.DefaultInputMethod);

            string kp2aIme = Kp2aInputMethodName;



            if (currentIme == kp2aIme)
            {
                //keyboard already activated. bring it up.
                InputMethodManager imeManager = (InputMethodManager)ApplicationContext.GetSystemService(InputMethodService);
                if (imeManager == null)
                {
                    Toast.MakeText(this, Resource.String.not_possible_im_picker, ToastLength.Long).Show();
                    return;
                }
                try
                {
                    imeManager.ToggleSoftInput(ShowFlags.Forced, HideSoftInputFlags.None);
                }
                catch (Exception e)
                {
                    Kp2aLog.LogUnexpectedError(e);

                    try
                    {
                        imeManager.ToggleSoftInput(ShowFlags.Implicit, HideSoftInputFlags.ImplicitOnly);
                        return;
                    }
                    catch (Exception)
                    {
                        Toast.MakeText(this, Resource.String.not_possible_im_picker, ToastLength.Long).Show();
                    }
                    return;
                }
            }
            else
            {
                if (!IsKp2aInputMethodEnabled)
                {
                    //must be enabled in settings first
                    Toast.MakeText(this, Resource.String.please_activate_keyboard, ToastLength.Long).Show();
                    Intent settingsIntent = new Intent(Android.Provider.Settings.ActionInputMethodSettings);
                    try
                    {
                        settingsIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ExcludeFromRecents);
                        StartActivity(settingsIntent);
                    }
                    catch (Exception e)
                    {
                        //seems like on Huawei devices this call can fail.
                        Kp2aLog.LogUnexpectedError(e);
                        Toast.MakeText(this, "Failed to switch keyboard.", ToastLength.Long).Show();
                    }
                }
                else
                {
                    //let's bring up the keyboard switching dialog.
                    //Unfortunately this no longer works starting with Android 9 if our app is not in foreground.
                    bool mustUseHelperActivity = false;
                    if ((int)Build.VERSION.SdkInt >= 28)
                    {
                        ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo();
                        ActivityManager.GetMyMemoryState(appProcessInfo);
                        //at least on Samsung devices, we always need the helper activity
                        mustUseHelperActivity = (appProcessInfo.Importance != Importance.Foreground) || (Build.Manufacturer != "Google");
                    }
                    if (mustUseHelperActivity)
                    {
                        try
                        {
                            Intent switchImeIntent = new Intent(this, typeof(SwitchImeActivity));
                            switchImeIntent.SetFlags(ActivityFlags.NewTask | ActivityFlags.ExcludeFromRecents);
                            StartActivity(switchImeIntent);
                        }
                        catch (Exception e)
                        {
                            //seems like on Huawei devices this call can fail.
                            Kp2aLog.LogUnexpectedError(e);
                            Toast.MakeText(this, "Failed to switch keyboard.", ToastLength.Long).Show();
                        }
                    }
                    else
                    {
#if !EXCLUDE_KEYBOARD
                        Keepass2android.Kbbridge.ImeSwitcher.SwitchToKeyboard(this, kp2aIme, false);
#endif
                    }
                }
            }
        }
            public override void OnReceive(Context context, Intent intent)
            {
                try
                {
                    if (intent.Action == DownloadManager.ActionDownloadComplete)
                    {
                        if (ActivityContext == null)
                        {
                            return;
                        }

                        DownloadManager       downloadManagerExcuter = (DownloadManager)ActivityContext.GetSystemService(Context.DownloadService);
                        long                  downloadId             = intent.GetLongExtra(DownloadManager.ExtraDownloadId, -1);
                        DownloadManager.Query query = new DownloadManager.Query();
                        query.SetFilterById(downloadId);
                        ICursor c         = downloadManagerExcuter.InvokeQuery(query);
                        var     sqlEntity = new SqLiteDatabase();

                        if (c.MoveToFirst())
                        {
                            int columnIndex = c.GetColumnIndex(DownloadManager.ColumnStatus);
                            if (c.GetInt(columnIndex) == (int)DownloadStatus.Successful)
                            {
                                string downloadedPath = c.GetString(c.GetColumnIndex(DownloadManager.ColumnLocalUri));

                                ActivityManager.RunningAppProcessInfo appProcessInfo = new ActivityManager.RunningAppProcessInfo();
                                ActivityManager.GetMyMemoryState(appProcessInfo);
                                if (appProcessInfo.Importance == Importance.Foreground || appProcessInfo.Importance == Importance.Background)
                                {
                                    sqlEntity.Update_WatchOfflineVideos(Video.VideoId, downloadedPath);
                                    if (TypeActivity == "Main")
                                    {
                                        TabbedMainActivity.GetInstance()?.VideoActionsController.DownloadIcon.SetImageResource(Resource.Drawable.ic_checked_red);
                                        TabbedMainActivity.GetInstance().VideoActionsController.DownloadIcon.Tag = "Downloaded";
                                        TabbedMainActivity.GetInstance()?.LibrarySynchronizer.AddToWatchOffline(Video);
                                    }
                                    else if (TypeActivity == "GlobalPlayer")
                                    {
                                        GlobalPlayerActivity.GetInstance()?.VideoActionsController.DownloadIcon.SetImageResource(Resource.Drawable.ic_checked_red);
                                        GlobalPlayerActivity.GetInstance().VideoActionsController.DownloadIcon.Tag = "Downloaded";
                                        GlobalPlayerActivity.GetInstance()?.LibrarySynchronizer.AddToWatchOffline(Video);
                                    }
                                    else
                                    {
                                        if (ActivityContext is FullScreenVideoActivity fullScreen)
                                        {
                                            fullScreen.VideoActionsController.DownloadIcon.SetImageResource(Resource.Drawable.ic_checked_red);
                                            fullScreen.VideoActionsController.DownloadIcon.Tag = "Downloaded";
                                            //fullScreen tabbedMain.LibrarySynchronizer.OfflineVideoList.Add(Video);
                                        }
                                    }
                                }
                                else
                                {
                                    sqlEntity.Connect();
                                    sqlEntity.Update_WatchOfflineVideos(Video.VideoId, downloadedPath);
                                    sqlEntity.Close();
                                }
                            }
                        }
                    }
                }
                catch (Exception exception)
                {
                    Console.WriteLine(exception);
                }
            }