Bug 1694481 - Simplify Android Listeners in GeckoAppShell. r=aklotz

GeckoAppShell has code to handle custom listeners, but we don't use them anymore.

Differential Revision: https://phabricator.services.mozilla.com/D106185
This commit is contained in:
Agi Sferro 2021-03-10 22:43:09 +00:00
parent 0d3819e98e
commit e4db9c53e4
3 changed files with 99 additions and 233 deletions

View File

@ -77,7 +77,6 @@ import android.text.TextUtils;
import android.util.Log;
import android.view.ContextThemeWrapper;
import android.view.Display;
import android.view.HapticFeedbackConstants;
import android.view.InputDevice;
import android.view.WindowManager;
import android.webkit.MimeTypeMap;
@ -157,10 +156,6 @@ public class GeckoAppShell {
return sCrashHandlerService;
}
public static synchronized boolean isCrashHandlingEnabled() {
return sCrashHandler != null;
}
@WrapForJNI(exceptionMode = "ignore")
/* package */ static synchronized String getAppNotes() {
return sAppNotes;
@ -310,7 +305,7 @@ public class GeckoAppShell {
}
if (!enable) {
lm.removeUpdates(getLocationListener());
lm.removeUpdates(sAndroidListeners);
return true;
}
@ -321,7 +316,7 @@ public class GeckoAppShell {
final Location lastKnownLocation = getLastKnownLocation(lm);
if (lastKnownLocation != null) {
getLocationListener().onLocationChanged(lastKnownLocation);
sAndroidListeners.onLocationChanged(lastKnownLocation);
}
final Criteria criteria = new Criteria();
@ -344,7 +339,7 @@ public class GeckoAppShell {
}
final Looper l = Looper.getMainLooper();
lm.requestLocationUpdates(provider, 100, 0.5f, getLocationListener(), l);
lm.requestLocationUpdates(provider, 100, 0.5f, sAndroidListeners, l);
return true;
}
@ -376,11 +371,8 @@ public class GeckoAppShell {
float altitudeAccuracy,
float heading, float speed, long time);
private static class DefaultListeners implements SensorEventListener,
LocationListener,
NotificationListener,
WakeLockDelegate,
HapticFeedbackDelegate {
private static class AndroidListeners implements SensorEventListener,
LocationListener {
@Override
public void onAccuracyChanged(final Sensor sensor, final int accuracy) {
}
@ -493,136 +485,85 @@ public class GeckoAppShell {
@Override
public void onStatusChanged(final String provider, final int status, final Bundle extras) {}
}
@Override // NotificationListener
public void showNotification(final String name, final String cookie, final String host,
final String title, final String text, final String imageUrl) {
// Default is to not show the notification, and immediate send close message.
GeckoAppShell.onNotificationClose(name, cookie);
private static final AndroidListeners sAndroidListeners = new AndroidListeners();
private static SimpleArrayMap<String, PowerManager.WakeLock> sWakeLocks;
/**
* Wake-lock for the CPU.
*/
final static String WAKE_LOCK_CPU = "cpu";
/**
* Wake-lock for the screen.
*/
final static String WAKE_LOCK_SCREEN = "screen";
/**
* Wake-lock for the audio-playing, eqaul to LOCK_CPU.
*/
final static String WAKE_LOCK_AUDIO_PLAYING = "audio-playing";
/**
* Wake-lock for the video-playing, eqaul to LOCK_SCREEN..
*/
final static String WAKE_LOCK_VIDEO_PLAYING = "video-playing";
final static int WAKE_LOCKS_COUNT = 2;
/**
* No one holds the wake-lock.
*/
final static int WAKE_LOCK_STATE_UNLOCKED = 0;
/**
* The wake-lock is held by a foreground window.
*/
final static int WAKE_LOCK_STATE_LOCKED_FOREGROUND = 1;
/**
* The wake-lock is held by a background window.
*/
final static int WAKE_LOCK_STATE_LOCKED_BACKGROUND = 2;
@SuppressLint("Wakelock") // We keep the wake lock independent from the function
// scope, so we need to suppress the linter warning.
private static void setWakeLockState(final String lock, final int state) {
if (sWakeLocks == null) {
sWakeLocks = new SimpleArrayMap<>(WAKE_LOCKS_COUNT);
}
@Override // NotificationListener
public void showPersistentNotification(final String name, final String cookie,
final String host, final String title,
final String text, final String imageUrl,
final String data) {
// Default is to not show the notification, and immediate send close message.
GeckoAppShell.onNotificationClose(name, cookie);
PowerManager.WakeLock wl = sWakeLocks.get(lock);
// we should still hold the lock for background audio.
if (WAKE_LOCK_AUDIO_PLAYING.equals(lock) &&
state == WAKE_LOCK_STATE_LOCKED_BACKGROUND) {
return;
}
@Override // NotificationListener
public void closeNotification(final String name) {
// Do nothing.
}
if (state == WAKE_LOCK_STATE_LOCKED_FOREGROUND && wl == null) {
final PowerManager pm = (PowerManager)
getApplicationContext().getSystemService(Context.POWER_SERVICE);
private SimpleArrayMap<String, PowerManager.WakeLock> mWakeLocks;
@Override // WakeLockDelegate
@SuppressLint("Wakelock") // We keep the wake lock independent from the function
// scope, so we need to suppress the linter warning.
public void setWakeLockState(final String lock, final int state) {
if (mWakeLocks == null) {
mWakeLocks = new SimpleArrayMap<>(WakeLockDelegate.LOCKS_COUNT);
}
PowerManager.WakeLock wl = mWakeLocks.get(lock);
// we should still hold the lock for background audio.
if (WakeLockDelegate.LOCK_AUDIO_PLAYING.equals(lock) &&
state == WakeLockDelegate.STATE_LOCKED_BACKGROUND) {
return;
}
if (state == WakeLockDelegate.STATE_LOCKED_FOREGROUND && wl == null) {
final PowerManager pm = (PowerManager)
getApplicationContext().getSystemService(Context.POWER_SERVICE);
if (WakeLockDelegate.LOCK_CPU.equals(lock) ||
WakeLockDelegate.LOCK_AUDIO_PLAYING.equals(lock)) {
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, lock);
} else if (WakeLockDelegate.LOCK_SCREEN.equals(lock) ||
WakeLockDelegate.LOCK_VIDEO_PLAYING.equals(lock)) {
// ON_AFTER_RELEASE is set, the user activity timer will be reset when the
// WakeLock is released, causing the illumination to remain on a bit longer.
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.ON_AFTER_RELEASE, lock);
} else {
Log.w(LOGTAG, "Unsupported wake-lock: " + lock);
return;
}
wl.acquire();
mWakeLocks.put(lock, wl);
} else if (state != WakeLockDelegate.STATE_LOCKED_FOREGROUND && wl != null) {
wl.release();
mWakeLocks.remove(lock);
}
}
@Override
public void performHapticFeedback(final int effect) {
final int[] pattern;
// Use default platform values.
if (effect == HapticFeedbackConstants.KEYBOARD_TAP) {
pattern = new int[] { 40 };
} else if (effect == HapticFeedbackConstants.LONG_PRESS) {
pattern = new int[] { 0, 1, 20, 21 };
} else if (effect == HapticFeedbackConstants.VIRTUAL_KEY) {
pattern = new int[] { 0, 10, 20, 30 };
if (WAKE_LOCK_CPU.equals(lock) ||
WAKE_LOCK_AUDIO_PLAYING.equals(lock)) {
wl = pm.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, lock);
} else if (WAKE_LOCK_SCREEN.equals(lock) ||
WAKE_LOCK_VIDEO_PLAYING.equals(lock)) {
// ON_AFTER_RELEASE is set, the user activity timer will be reset when the
// WakeLock is released, causing the illumination to remain on a bit longer.
wl = pm.newWakeLock(PowerManager.SCREEN_BRIGHT_WAKE_LOCK |
PowerManager.ON_AFTER_RELEASE, lock);
} else {
Log.w(LOGTAG, "Unsupported wake-lock: " + lock);
return;
}
vibrateOnHapticFeedbackEnabled(pattern);
wl.acquire();
sWakeLocks.put(lock, wl);
} else if (state != WAKE_LOCK_STATE_LOCKED_FOREGROUND && wl != null) {
wl.release();
sWakeLocks.remove(lock);
}
}
private static final DefaultListeners DEFAULT_LISTENERS = new DefaultListeners();
private static SensorEventListener sSensorListener = DEFAULT_LISTENERS;
private static LocationListener sLocationListener = DEFAULT_LISTENERS;
private static NotificationListener sNotificationListener = DEFAULT_LISTENERS;
private static WakeLockDelegate sWakeLockDelegate = DEFAULT_LISTENERS;
private static HapticFeedbackDelegate sHapticFeedbackDelegate = DEFAULT_LISTENERS;
public static SensorEventListener getSensorListener() {
return sSensorListener;
}
public static void setSensorListener(final SensorEventListener listener) {
sSensorListener = (listener != null) ? listener : DEFAULT_LISTENERS;
}
public static LocationListener getLocationListener() {
return sLocationListener;
}
public static void setLocationListener(final LocationListener listener) {
sLocationListener = (listener != null) ? listener : DEFAULT_LISTENERS;
}
public static NotificationListener getNotificationListener() {
return sNotificationListener;
}
public static void setNotificationListener(final NotificationListener listener) {
sNotificationListener = (listener != null) ? listener : DEFAULT_LISTENERS;
}
public static WakeLockDelegate getWakeLockDelegate() {
return sWakeLockDelegate;
}
public void setWakeLockDelegate(final WakeLockDelegate delegate) {
sWakeLockDelegate = (delegate != null) ? delegate : DEFAULT_LISTENERS;
}
public static HapticFeedbackDelegate getHapticFeedbackDelegate() {
return sHapticFeedbackDelegate;
}
public static void setHapticFeedbackDelegate(final HapticFeedbackDelegate delegate) {
sHapticFeedbackDelegate = (delegate != null) ? delegate : DEFAULT_LISTENERS;
}
@SuppressWarnings("fallthrough")
@WrapForJNI(calledFrom = "gecko")
private static void enableSensor(final int aSensortype) {
@ -636,7 +577,7 @@ public class GeckoAppShell {
Sensor.TYPE_GAME_ROTATION_VECTOR);
}
if (gGameRotationVectorSensor != null) {
sm.registerListener(getSensorListener(),
sm.registerListener(sAndroidListeners,
gGameRotationVectorSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
@ -651,7 +592,7 @@ public class GeckoAppShell {
Sensor.TYPE_ROTATION_VECTOR);
}
if (gRotationVectorSensor != null) {
sm.registerListener(getSensorListener(),
sm.registerListener(sAndroidListeners,
gRotationVectorSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
@ -666,7 +607,7 @@ public class GeckoAppShell {
Sensor.TYPE_ORIENTATION);
}
if (gOrientationSensor != null) {
sm.registerListener(getSensorListener(),
sm.registerListener(sAndroidListeners,
gOrientationSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
@ -678,7 +619,7 @@ public class GeckoAppShell {
Sensor.TYPE_ACCELEROMETER);
}
if (gAccelerometerSensor != null) {
sm.registerListener(getSensorListener(),
sm.registerListener(sAndroidListeners,
gAccelerometerSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
@ -689,7 +630,7 @@ public class GeckoAppShell {
gProximitySensor = sm.getDefaultSensor(Sensor.TYPE_PROXIMITY);
}
if (gProximitySensor != null) {
sm.registerListener(getSensorListener(),
sm.registerListener(sAndroidListeners,
gProximitySensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
@ -700,7 +641,7 @@ public class GeckoAppShell {
gLightSensor = sm.getDefaultSensor(Sensor.TYPE_LIGHT);
}
if (gLightSensor != null) {
sm.registerListener(getSensorListener(),
sm.registerListener(sAndroidListeners,
gLightSensor,
SensorManager.SENSOR_DELAY_NORMAL);
}
@ -712,7 +653,7 @@ public class GeckoAppShell {
Sensor.TYPE_LINEAR_ACCELERATION);
}
if (gLinearAccelerometerSensor != null) {
sm.registerListener(getSensorListener(),
sm.registerListener(sAndroidListeners,
gLinearAccelerometerSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
@ -723,7 +664,7 @@ public class GeckoAppShell {
gGyroscopeSensor = sm.getDefaultSensor(Sensor.TYPE_GYROSCOPE);
}
if (gGyroscopeSensor != null) {
sm.registerListener(getSensorListener(),
sm.registerListener(sAndroidListeners,
gGyroscopeSensor,
SensorManager.SENSOR_DELAY_FASTEST);
}
@ -744,51 +685,51 @@ public class GeckoAppShell {
switch (aSensortype) {
case GeckoHalDefines.SENSOR_GAME_ROTATION_VECTOR:
if (gGameRotationVectorSensor != null) {
sm.unregisterListener(getSensorListener(), gGameRotationVectorSensor);
sm.unregisterListener(sAndroidListeners, gGameRotationVectorSensor);
break;
}
// Fallthrough
case GeckoHalDefines.SENSOR_ROTATION_VECTOR:
if (gRotationVectorSensor != null) {
sm.unregisterListener(getSensorListener(), gRotationVectorSensor);
sm.unregisterListener(sAndroidListeners, gRotationVectorSensor);
break;
}
// Fallthrough
case GeckoHalDefines.SENSOR_ORIENTATION:
if (gOrientationSensor != null) {
sm.unregisterListener(getSensorListener(), gOrientationSensor);
sm.unregisterListener(sAndroidListeners, gOrientationSensor);
}
break;
case GeckoHalDefines.SENSOR_ACCELERATION:
if (gAccelerometerSensor != null) {
sm.unregisterListener(getSensorListener(), gAccelerometerSensor);
sm.unregisterListener(sAndroidListeners, gAccelerometerSensor);
}
break;
case GeckoHalDefines.SENSOR_PROXIMITY:
if (gProximitySensor != null) {
sm.unregisterListener(getSensorListener(), gProximitySensor);
sm.unregisterListener(sAndroidListeners, gProximitySensor);
}
break;
case GeckoHalDefines.SENSOR_LIGHT:
if (gLightSensor != null) {
sm.unregisterListener(getSensorListener(), gLightSensor);
sm.unregisterListener(sAndroidListeners, gLightSensor);
}
break;
case GeckoHalDefines.SENSOR_LINEAR_ACCELERATION:
if (gLinearAccelerometerSensor != null) {
sm.unregisterListener(getSensorListener(), gLinearAccelerometerSensor);
sm.unregisterListener(sAndroidListeners, gLinearAccelerometerSensor);
}
break;
case GeckoHalDefines.SENSOR_GYROSCOPE:
if (gGyroscopeSensor != null) {
sm.unregisterListener(getSensorListener(), gGyroscopeSensor);
sm.unregisterListener(sAndroidListeners, gGyroscopeSensor);
}
break;
default:
@ -851,16 +792,6 @@ public class GeckoAppShell {
@WrapForJNI(dispatchTo = "gecko")
private static native void notifyAlertListener(String name, String topic, String cookie);
/**
* Called by the NotificationListener to notify Gecko that a notification has been
* shown.
*/
public static void onNotificationShow(final String name, final String cookie) {
if (GeckoThread.isRunning()) {
notifyAlertListener(name, "alertshow", cookie);
}
}
/**
* Called by the NotificationListener to notify Gecko that a previously shown
* notification has been closed.
@ -953,9 +884,13 @@ public class GeckoAppShell {
// Don't perform haptic feedback if a vibration is currently playing,
// because the haptic feedback will nuke the vibration.
if (!sVibrationMaybePlaying || System.nanoTime() >= sVibrationEndTime) {
getHapticFeedbackDelegate().performHapticFeedback(
aIsLongPress ? HapticFeedbackConstants.LONG_PRESS
: HapticFeedbackConstants.VIRTUAL_KEY);
final int[] pattern;
if (aIsLongPress) {
pattern = new int[] { 0, 1, 20, 21 };
} else {
pattern = new int[] { 0, 10, 20, 30 };
}
vibrateOnHapticFeedbackEnabled(pattern);
sVibrationMaybePlaying = false;
sVibrationEndTime = 0;
}
@ -1513,15 +1448,15 @@ public class GeckoAppShell {
private static void notifyWakeLockChanged(final String topic, final String state) {
final int intState;
if ("unlocked".equals(state)) {
intState = WakeLockDelegate.STATE_UNLOCKED;
intState = WAKE_LOCK_STATE_UNLOCKED;
} else if ("locked-foreground".equals(state)) {
intState = WakeLockDelegate.STATE_LOCKED_FOREGROUND;
intState = WAKE_LOCK_STATE_LOCKED_FOREGROUND;
} else if ("locked-background".equals(state)) {
intState = WakeLockDelegate.STATE_LOCKED_BACKGROUND;
intState = WAKE_LOCK_STATE_LOCKED_BACKGROUND;
} else {
throw new IllegalArgumentException();
}
getWakeLockDelegate().setWakeLockState(topic, intState);
setWakeLockState(topic, intState);
}
@WrapForJNI(calledFrom = "gecko")

View File

@ -1,18 +0,0 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; 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;
/**
* A <code>HapticFeedbackDelegate</code> is responsible for performing haptic feedback.
*/
public interface HapticFeedbackDelegate {
/**
* Perform a haptic feedback effect. Called from the Gecko thread.
*
* @param effect Effect to perform from <code>android.view.HapticFeedbackConstants</code>.
*/
void performHapticFeedback(int effect);
}

View File

@ -1,51 +0,0 @@
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 20; 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;
/**
* A <code>WakeLockDelegate</code> is responsible for acquiring and release wake-locks.
*/
public interface WakeLockDelegate {
/**
* Wake-lock for the CPU.
*/
final String LOCK_CPU = "cpu";
/**
* Wake-lock for the screen.
*/
final String LOCK_SCREEN = "screen";
/**
* Wake-lock for the audio-playing, eqaul to LOCK_CPU.
*/
final String LOCK_AUDIO_PLAYING = "audio-playing";
/**
* Wake-lock for the video-playing, eqaul to LOCK_SCREEN..
*/
final String LOCK_VIDEO_PLAYING = "video-playing";
final int LOCKS_COUNT = 2;
/**
* No one holds the wake-lock.
*/
final int STATE_UNLOCKED = 0;
/**
* The wake-lock is held by a foreground window.
*/
final int STATE_LOCKED_FOREGROUND = 1;
/**
* The wake-lock is held by a background window.
*/
final int STATE_LOCKED_BACKGROUND = 2;
/**
* Set a wake-lock to a specified state. Called from the Gecko thread.
*
* @param lock Wake-lock to set from one of the LOCK_* constants.
* @param state New wake-lock state from one of the STATE_* constants.
*/
void setWakeLockState(String lock, int state);
}