mirror of
https://github.com/VibedByKaKi/alexandroid.git
synced 2026-11-18 20:07:19 +01:00
135 lines
4.6 KiB
Java
135 lines
4.6 KiB
Java
package androidx.camera.view;
|
|
|
|
import android.content.Context;
|
|
import android.view.OrientationEventListener;
|
|
import androidx.annotation.CheckResult;
|
|
import androidx.annotation.GuardedBy;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import java.util.ArrayList;
|
|
import java.util.HashMap;
|
|
import java.util.Iterator;
|
|
import java.util.Map;
|
|
import java.util.concurrent.Executor;
|
|
import java.util.concurrent.atomic.AtomicBoolean;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
/* loaded from: classes.dex */
|
|
public final class RotationProvider {
|
|
|
|
@NonNull
|
|
@GuardedBy("mLock")
|
|
@VisibleForTesting
|
|
final OrientationEventListener mOrientationListener;
|
|
final Object mLock = new Object();
|
|
|
|
@NonNull
|
|
@GuardedBy("mLock")
|
|
final Map<Listener, ListenerWrapper> mListeners = new HashMap();
|
|
|
|
@VisibleForTesting
|
|
boolean mIgnoreCanDetectForTest = false;
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
public interface Listener {
|
|
void onRotationChanged(int i);
|
|
}
|
|
|
|
/* compiled from: r8-map-id-b9432e0f623b4a4bff4ba50a8a24c4aafad2c1e11092747b9fb5e6a22af5a940 */
|
|
public static class ListenerWrapper {
|
|
private final AtomicBoolean mEnabled = new AtomicBoolean(true);
|
|
private final Executor mExecutor;
|
|
private final Listener mListener;
|
|
|
|
public ListenerWrapper(Listener listener, Executor executor) {
|
|
this.mListener = listener;
|
|
this.mExecutor = executor;
|
|
}
|
|
|
|
/* JADX INFO: Access modifiers changed from: private */
|
|
public /* synthetic */ void lambda$onRotationChanged$0(int i) {
|
|
if (this.mEnabled.get()) {
|
|
this.mListener.onRotationChanged(i);
|
|
}
|
|
}
|
|
|
|
public void disable() {
|
|
this.mEnabled.set(false);
|
|
}
|
|
|
|
public void onRotationChanged(int i) {
|
|
this.mExecutor.execute(new CameraController$$ExternalSyntheticLambda6(this, i, 1));
|
|
}
|
|
}
|
|
|
|
public RotationProvider(@NonNull Context context) {
|
|
this.mOrientationListener = new OrientationEventListener(context) { // from class: androidx.camera.view.RotationProvider.1
|
|
private static final int INVALID_SURFACE_ROTATION = -1;
|
|
private int mRotation = -1;
|
|
|
|
@Override // android.view.OrientationEventListener
|
|
public void onOrientationChanged(int i) {
|
|
int orientationToSurfaceRotation;
|
|
ArrayList arrayList;
|
|
if (i == -1 || this.mRotation == (orientationToSurfaceRotation = RotationProvider.orientationToSurfaceRotation(i))) {
|
|
return;
|
|
}
|
|
this.mRotation = orientationToSurfaceRotation;
|
|
synchronized (RotationProvider.this.mLock) {
|
|
arrayList = new ArrayList(RotationProvider.this.mListeners.values());
|
|
}
|
|
if (arrayList.isEmpty()) {
|
|
return;
|
|
}
|
|
Iterator it2 = arrayList.iterator();
|
|
while (it2.hasNext()) {
|
|
((ListenerWrapper) it2.next()).onRotationChanged(orientationToSurfaceRotation);
|
|
}
|
|
}
|
|
};
|
|
}
|
|
|
|
@VisibleForTesting
|
|
public static int orientationToSurfaceRotation(int i) {
|
|
if (i >= 315 || i < 45) {
|
|
return 0;
|
|
}
|
|
if (i >= 225) {
|
|
return 1;
|
|
}
|
|
return i >= 135 ? 2 : 3;
|
|
}
|
|
|
|
@CheckResult
|
|
public boolean addListener(@NonNull Executor executor, @NonNull Listener listener) {
|
|
synchronized (this.mLock) {
|
|
try {
|
|
if (!this.mOrientationListener.canDetectOrientation() && !this.mIgnoreCanDetectForTest) {
|
|
return false;
|
|
}
|
|
this.mListeners.put(listener, new ListenerWrapper(listener, executor));
|
|
this.mOrientationListener.enable();
|
|
return true;
|
|
} catch (Throwable th) {
|
|
throw th;
|
|
}
|
|
}
|
|
}
|
|
|
|
public void removeListener(@NonNull Listener listener) {
|
|
synchronized (this.mLock) {
|
|
try {
|
|
ListenerWrapper listenerWrapper = this.mListeners.get(listener);
|
|
if (listenerWrapper != null) {
|
|
listenerWrapper.disable();
|
|
this.mListeners.remove(listener);
|
|
}
|
|
if (this.mListeners.isEmpty()) {
|
|
this.mOrientationListener.disable();
|
|
}
|
|
} catch (Throwable th) {
|
|
throw th;
|
|
}
|
|
}
|
|
}
|
|
}
|