summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/GeckoSystemStateListener.java
blob: 6a71eff1fe8cf33bbf511ecbdea23be666a9efc0 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Configuration;
import android.database.ContentObserver;
import android.hardware.input.InputManager;
import android.net.Uri;
import android.os.Build;
import android.os.Handler;
import android.os.Looper;
import android.provider.Settings;
import android.util.Log;
import android.view.InputDevice;
import androidx.annotation.RequiresApi;
import org.mozilla.gecko.annotation.WrapForJNI;
import org.mozilla.gecko.util.InputDeviceUtils;
import org.mozilla.gecko.util.ThreadUtils;

public class GeckoSystemStateListener implements InputManager.InputDeviceListener {
  private static final String LOGTAG = "SystemStateListener";

  private static final GeckoSystemStateListener listenerInstance = new GeckoSystemStateListener();

  private boolean mInitialized;
  private ContentObserver mContentObserver;
  private static Context sApplicationContext;
  private InputManager mInputManager;
  private boolean mIsNightMode;

  public static GeckoSystemStateListener getInstance() {
    return listenerInstance;
  }

  private GeckoSystemStateListener() {}

  public synchronized void initialize(final Context context) {
    if (mInitialized) {
      Log.w(LOGTAG, "Already initialized!");
      return;
    }
    mInputManager = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
    mInputManager.registerInputDeviceListener(listenerInstance, ThreadUtils.getUiHandler());

    sApplicationContext = context;
    final ContentResolver contentResolver = sApplicationContext.getContentResolver();
    final Uri animationSetting = Settings.System.getUriFor(Settings.Global.ANIMATOR_DURATION_SCALE);
    mContentObserver =
        new ContentObserver(new Handler(Looper.getMainLooper())) {
          @Override
          public void onChange(final boolean selfChange) {
            onDeviceChanged();
          }
        };
    contentResolver.registerContentObserver(animationSetting, false, mContentObserver);

    final Uri invertSetting =
        Settings.Secure.getUriFor(Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED);
    contentResolver.registerContentObserver(invertSetting, false, mContentObserver);

    mIsNightMode =
        (sApplicationContext.getResources().getConfiguration().uiMode
                & Configuration.UI_MODE_NIGHT_MASK)
            == Configuration.UI_MODE_NIGHT_YES;

    mInitialized = true;
  }

  public synchronized void shutdown() {
    if (!mInitialized) {
      Log.w(LOGTAG, "Already shut down!");
      return;
    }

    if (mInputManager != null) {
      Log.e(LOGTAG, "mInputManager should be valid!");
      return;
    }

    mInputManager.unregisterInputDeviceListener(listenerInstance);

    final ContentResolver contentResolver = sApplicationContext.getContentResolver();
    contentResolver.unregisterContentObserver(mContentObserver);

    mInitialized = false;
    mInputManager = null;
    mContentObserver = null;
  }

  @RequiresApi(api = Build.VERSION_CODES.JELLY_BEAN_MR1)
  @WrapForJNI(calledFrom = "gecko")
  /**
   * For prefers-reduced-motion media queries feature.
   *
   * <p>Uses `Settings.Global` which was introduced in API version 17.
   */
  private static boolean prefersReducedMotion() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.JELLY_BEAN_MR1) {
      return false;
    }

    final ContentResolver contentResolver = sApplicationContext.getContentResolver();

    return Settings.Global.getFloat(contentResolver, Settings.Global.ANIMATOR_DURATION_SCALE, 1)
        == 0.0f;
  }

  @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
  @WrapForJNI(calledFrom = "gecko")
  /**
   * For inverted-colors queries feature.
   *
   * <p>Uses `Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED` which was introduced in API
   * version 21.
   */
  private static boolean isInvertedColors() {
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
      return false;
    }

    final ContentResolver contentResolver = sApplicationContext.getContentResolver();

    return Settings.Secure.getInt(
            contentResolver, Settings.Secure.ACCESSIBILITY_DISPLAY_INVERSION_ENABLED, 0)
        == 1;
  }

  /** For prefers-color-scheme media queries feature. */
  public boolean isNightMode() {
    return mIsNightMode;
  }

  public void updateNightMode(final int newUIMode) {
    final boolean isNightMode =
        (newUIMode & Configuration.UI_MODE_NIGHT_MASK) == Configuration.UI_MODE_NIGHT_YES;
    if (isNightMode == mIsNightMode) {
      return;
    }
    mIsNightMode = isNightMode;
    onDeviceChanged();
  }

  @WrapForJNI(stubName = "OnDeviceChanged", calledFrom = "any", dispatchTo = "gecko")
  private static native void nativeOnDeviceChanged();

  public static void onDeviceChanged() {
    if (GeckoThread.isStateAtLeast(GeckoThread.State.PROFILE_READY)) {
      nativeOnDeviceChanged();
    } else {
      GeckoThread.queueNativeCallUntil(
          GeckoThread.State.PROFILE_READY, GeckoSystemStateListener.class, "nativeOnDeviceChanged");
    }
  }

  private void notifyDeviceChanged(final int deviceId) {
    final InputDevice device = InputDevice.getDevice(deviceId);
    if (device == null || !InputDeviceUtils.isPointerTypeDevice(device)) {
      return;
    }
    onDeviceChanged();
  }

  @Override
  public void onInputDeviceAdded(final int deviceId) {
    notifyDeviceChanged(deviceId);
  }

  @Override
  public void onInputDeviceRemoved(final int deviceId) {
    // Call onDeviceChanged directly without checking device source types
    // since we can no longer get a valid `InputDevice` in the case of
    // device removal.
    onDeviceChanged();
  }

  @Override
  public void onInputDeviceChanged(final int deviceId) {
    notifyDeviceChanged(deviceId);
  }
}