application_status_listener.h 3.2 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798
  1. // Copyright 2014 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #ifndef BASE_ANDROID_APPLICATION_STATUS_LISTENER_H_
  5. #define BASE_ANDROID_APPLICATION_STATUS_LISTENER_H_
  6. #include <jni.h>
  7. #include <memory>
  8. #include "base/android/jni_android.h"
  9. #include "base/base_export.h"
  10. #include "base/callback_forward.h"
  11. #include "base/memory/ref_counted.h"
  12. namespace base {
  13. namespace android {
  14. // Define application state values like APPLICATION_STATE_VISIBLE in a
  15. // way that ensures they're always the same than their Java counterpart.
  16. //
  17. // Note that these states represent the most visible Activity state.
  18. // If there are activities with states paused and stopped, only
  19. // HAS_PAUSED_ACTIVITIES should be returned.
  20. //
  21. // A Java counterpart will be generated for this enum.
  22. // GENERATED_JAVA_ENUM_PACKAGE: org.chromium.base
  23. enum ApplicationState {
  24. APPLICATION_STATE_UNKNOWN = 0,
  25. APPLICATION_STATE_HAS_RUNNING_ACTIVITIES = 1,
  26. APPLICATION_STATE_HAS_PAUSED_ACTIVITIES = 2,
  27. APPLICATION_STATE_HAS_STOPPED_ACTIVITIES = 3,
  28. APPLICATION_STATE_HAS_DESTROYED_ACTIVITIES = 4
  29. };
  30. // A native helper class to listen to state changes of the Android
  31. // Application. This mirrors org.chromium.base.ApplicationStatus.
  32. // any thread.
  33. //
  34. // To start listening, create a new instance, passing a callback to a
  35. // function that takes an ApplicationState parameter. To stop listening,
  36. // simply delete the listener object. The implementation guarantees
  37. // that the callback will always be called on the thread that created
  38. // the listener.
  39. //
  40. // Example:
  41. //
  42. // void OnApplicationStateChange(ApplicationState state) {
  43. // ...
  44. // }
  45. //
  46. // // Start listening.
  47. // auto my_listener = ApplicationStatusListener::New(
  48. // base::BindRepeating(&OnApplicationStateChange));
  49. //
  50. // ...
  51. //
  52. // // Stop listening.
  53. // my_listener.reset();
  54. //
  55. class BASE_EXPORT ApplicationStatusListener {
  56. public:
  57. using ApplicationStateChangeCallback =
  58. base::RepeatingCallback<void(ApplicationState)>;
  59. ApplicationStatusListener(const ApplicationStatusListener&) = delete;
  60. ApplicationStatusListener& operator=(const ApplicationStatusListener&) =
  61. delete;
  62. virtual ~ApplicationStatusListener();
  63. // Sets the callback to call when application state changes.
  64. virtual void SetCallback(const ApplicationStateChangeCallback& callback) = 0;
  65. // Notify observers that application state has changed.
  66. virtual void Notify(ApplicationState state) = 0;
  67. // Create a new listener. This object should only be used on a single thread.
  68. static std::unique_ptr<ApplicationStatusListener> New(
  69. const ApplicationStateChangeCallback& callback);
  70. // Internal use only: must be public to be called from JNI and unit tests.
  71. static void NotifyApplicationStateChange(ApplicationState state);
  72. // Expose jni call for ApplicationStatus.getStateForApplication.
  73. static ApplicationState GetState();
  74. // Returns true if the app is currently foregrounded.
  75. static bool HasVisibleActivities();
  76. protected:
  77. ApplicationStatusListener();
  78. };
  79. } // namespace android
  80. } // namespace base
  81. #endif // BASE_ANDROID_APPLICATION_STATUS_LISTENER_H_