Class DynamicEngine

All Implemented Interfaces:
ComponentCallbacks, ComponentCallbacks2, SensorEventListener, DynamicEventListener

public abstract class DynamicEngine extends DynamicStickyService implements SensorEventListener, DynamicEventListener
Service to monitor various system events to provide event specific functionality in the app.

Extend this service and implement the interface methods to monitor the different events.

Package must be granted Manifest.permission_group.PHONE permission to listen call events on API 23 and above.

Package must be granted Manifest.permission.PACKAGE_USAGE_STATS permission to detect the foreground app on API 21 and above.

  • Constructor Details

    • DynamicEngine

      public DynamicEngine()
  • Method Details

    • onCreate

      public void onCreate()
      Overrides:
      onCreate in class DynamicStickyService
    • initializeEvents

      public void initializeEvents()
      Initialize special events and check for some already occurred and ongoing events.
    • updateEventsPriority

      public void updateEventsPriority()
      Update the events priority.
    • updateEventsMap

      public void updateEventsMap(@NonNull String event, boolean active)
      Update the status of an event.
      Parameters:
      event - The event to update the status.
      active - true to if the event is currently active.
    • getSensorManager

      @NonNull public SensorManager getSensorManager()
      Get the sensor manager used by this service.
      Returns:
      The sensor manager used by this service.
    • getSpecialEventListener

      @NonNull public DynamicEventListener getSpecialEventListener()
      Get the listener to listen special events.
      Returns:
      The listener to listen special events.
    • getAppMonitor

      @NonNull public DynamicAppMonitor getAppMonitor()
      Get the task to monitor foreground app.
      Returns:
      The task to monitor foreground app.
    • setAppMonitorTask

      public void setAppMonitorTask(boolean running)
      Enable or disable the foreground app monitor task.
      Parameters:
      running - true to start monitoring the foreground app and receive listener callback.
      See Also:
    • setAppMonitorTaskPaused

      public void setAppMonitorTaskPaused(boolean paused)
      Pause or resume the foreground app monitor task.
      Parameters:
      paused - true to pause monitoring the foreground app and bo not receive listener callback.
      See Also:
    • onDestroy

      public void onDestroy()
      Overrides:
      onDestroy in class Service
    • getHinge

      public int getHinge()
      Returns the dynamic hinge state.
      Returns:
      The dynamic hinge state.
    • setHinge

      public void setHinge(int hinge)
      Set the dynamic hinge state.
      Parameters:
      hinge - The hinge state to be set.
    • isCall

      public boolean isCall()
      Get the status of call event.
      Returns:
      true if the device is on call.

      Either ringing or answered.

    • setCall

      public void setCall(boolean call)
      Set the status of call event.
      Parameters:
      call - true if the device is on call.

      Either ringing or answered.

    • isScreenOff

      public boolean isScreenOff()
      Get the status of screen of event.
      Returns:
      true if the device screen is off.
    • setScreenOff

      public void setScreenOff(boolean screenOff)
      Set the status of screen off event.
      Parameters:
      screenOff - true if the device screen is off.
    • isLocked

      public boolean isLocked()
      Get the status of lock event.
      Returns:
      true if the device is in the locked state or the lock screen is shown.
    • setLocked

      public void setLocked(boolean locked)
      Set the status of lock event.
      Parameters:
      locked - true if the device is in the locked state or the lock screen is shown.
    • isHeadset

      public boolean isHeadset()
      Get the status of the headset event.
      Returns:
      true if the device is connected to a headset or a audio output device.
    • setHeadset

      public void setHeadset(boolean headset)
      Set the status of headset event.
      Parameters:
      headset - true if the device is connected to a headset or a audio output device.
    • isCharging

      public boolean isCharging()
      Get the status of charging event.
      Returns:
      true if the device is charging or connected to a power source.
    • setCharging

      public void setCharging(boolean charging)
      Set the status of charging event.
      Parameters:
      charging - true if the device is charging or connected to a power source.
    • isDocked

      public boolean isDocked()
      Get the status of dock event.
      Returns:
      true if the device is docked.
    • setDocked

      public void setDocked(boolean docked)
      Set the status of dock event.
      Parameters:
      docked - true if the device is docked.
    • onSensorChanged

      public void onSensorChanged(SensorEvent sensorEvent)
      Specified by:
      onSensorChanged in interface SensorEventListener
    • onAccuracyChanged

      public void onAccuracyChanged(Sensor sensor, int i)
      Specified by:
      onAccuracyChanged in interface SensorEventListener
    • onInitialize

      @CallSuper public void onInitialize(boolean charging, boolean headset, boolean docked)
      Description copied from interface: DynamicEventListener
      This method will be called on initializing the service so that we can get the current charging, headset and dock state.
      Specified by:
      onInitialize in interface DynamicEventListener
      Parameters:
      charging - true if the device is charging or connected to a power source.
      headset - true if the device is connected to a headset or a audio output device.
      docked - true if the device is docked.
    • onHingeStateChange

      @CallSuper public void onHingeStateChange(int state)
      Description copied from interface: DynamicEventListener
      This method will be called when the hinge state is changed.
      Specified by:
      onHingeStateChange in interface DynamicEventListener
      Parameters:
      state - The current hinge state.
      See Also:
    • onCallStateChange

      @CallSuper public void onCallStateChange(boolean call)
      Description copied from interface: DynamicEventListener
      This method will be called when the call state is changed.

      Either on call or the device is idle.

      Specified by:
      onCallStateChange in interface DynamicEventListener
      Parameters:
      call - true if the device is on call.

      Either ringing or answered.

    • onScreenStateChange

      @CallSuper public void onScreenStateChange(boolean screenOff)
      Description copied from interface: DynamicEventListener
      This method will be called when the screen state is changed.

      Either the device screen is off or on.

      Specified by:
      onScreenStateChange in interface DynamicEventListener
      Parameters:
      screenOff - true if the device screen is off.
    • onLockStateChange

      @CallSuper public void onLockStateChange(boolean locked)
      Description copied from interface: DynamicEventListener
      This method will be called when the lock state is changed.

      Either the device is in the locked or unlocked state independent of the PIN, password or any other security lock.

      Specified by:
      onLockStateChange in interface DynamicEventListener
      Parameters:
      locked - true if the device is in the locked state or the lock screen is shown.
    • onHeadsetStateChange

      @CallSuper public void onHeadsetStateChange(boolean connected)
      Description copied from interface: DynamicEventListener
      This method will be called when the headset state is changed.

      Either the device is connected to a audio output device or volume is routed through the internal speaker.

      Specified by:
      onHeadsetStateChange in interface DynamicEventListener
      Parameters:
      connected - true if the device is connected to a headset or a audio output device.
    • onChargingStateChange

      @CallSuper public void onChargingStateChange(boolean charging)
      Description copied from interface: DynamicEventListener
      This method will be called when the charging state is changed.

      Either the device is connected to a power source using the battery.

      Specified by:
      onChargingStateChange in interface DynamicEventListener
      Parameters:
      charging - true if the device is charging or connected to a power source.
    • onDockStateChange

      @CallSuper public void onDockStateChange(boolean docked)
      Description copied from interface: DynamicEventListener
      This method will be called when the dock state is changed.

      Either the device is docked or not.

      Specified by:
      onDockStateChange in interface DynamicEventListener
      Parameters:
      docked - true if the device is docked.
    • onAppChange

      @CallSuper public void onAppChange(@Nullable DynamicAppInfo dynamicAppInfo)
      Description copied from interface: DynamicEventListener
      This method will be called when the foreground app is changed.

      Use it to provide the app specific functionality in the app.

      Specified by:
      onAppChange in interface DynamicEventListener
      Parameters:
      dynamicAppInfo - The dynamic app info of the foreground package.
    • onAccessibilityStateChanged

      protected void onAccessibilityStateChanged(boolean enabled)
      Description copied from class: DynamicStickyService
      Called back on change in the accessibility state.
      Overrides:
      onAccessibilityStateChanged in class DynamicStickyService
      Parameters:
      enabled - Whether accessibility is enabled.
    • onAccessibilityEvent

      public void onAccessibilityEvent(AccessibilityEvent event)
      Overrides:
      onAccessibilityEvent in class DynamicStickyService
    • getCurrentEvents

      @NonNull protected List<String> getCurrentEvents()
      Retrieve the current ongoing events.
      Returns:
      The list of current ongoing events.
    • getEventByPriority

      protected String getEventByPriority(@NonNull List<String> currentEvents, int priority)
      Get the event according to its priority.
      Parameters:
      currentEvents - The list of events.
      priority - The event priority to find event.
      Returns:
      The event according to its priority.
    • getHighestPriorityEvent

      protected String getHighestPriorityEvent()
      Get the event with highest priority.
      Returns:
      The highest priority event that has been occurred.