From 76df95c9fb8306f5076ccb8e703054f287e78cd8 Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Fri, 3 May 2019 17:34:43 +0200 Subject: [PATCH] Fixed preview orientation issue upon device rotation --- .../java/org/linphone/LinphoneService.java | 5 ++ .../utils/DeviceOrientationEventListener.java | 59 +++++++++++++++++++ 2 files changed, 64 insertions(+) create mode 100644 app/src/main/java/org/linphone/utils/DeviceOrientationEventListener.java diff --git a/app/src/main/java/org/linphone/LinphoneService.java b/app/src/main/java/org/linphone/LinphoneService.java index 0bc1e9464..ff36f76f3 100644 --- a/app/src/main/java/org/linphone/LinphoneService.java +++ b/app/src/main/java/org/linphone/LinphoneService.java @@ -48,6 +48,7 @@ import org.linphone.notifications.NotificationsManager; import org.linphone.receivers.BluetoothManager; import org.linphone.settings.LinphonePreferences; import org.linphone.utils.ActivityMonitor; +import org.linphone.utils.DeviceOrientationEventListener; import org.linphone.utils.LinphoneUtils; import org.linphone.views.LinphoneGL2JNIViewOverlay; import org.linphone.views.LinphoneOverlay; @@ -103,6 +104,7 @@ public final class LinphoneService extends Service { private LinphoneManager mLinphoneManager; private ContactsManager mContactsManager; private BluetoothManager mBluetoothManager; + private DeviceOrientationEventListener mOrientationHelper; private Class mIncomingReceivedActivity = CallIncomingActivity.class; @@ -138,6 +140,7 @@ public final class LinphoneService extends Service { } mWindowManager = (WindowManager) getSystemService(WINDOW_SERVICE); + mOrientationHelper = new DeviceOrientationEventListener(this); mListener = new CoreListenerStub() { @@ -213,6 +216,7 @@ public final class LinphoneService extends Service { mBluetoothManager = new BluetoothManager(); Compatibility.createChatShortcuts(this); + mOrientationHelper.enable(); return START_STICKY; } @@ -246,6 +250,7 @@ public final class LinphoneService extends Service { mActivityCallbacks = null; } destroyOverlay(); + mOrientationHelper.disable(); Core core = LinphoneManager.getCore(); if (core != null) { diff --git a/app/src/main/java/org/linphone/utils/DeviceOrientationEventListener.java b/app/src/main/java/org/linphone/utils/DeviceOrientationEventListener.java new file mode 100644 index 000000000..a8f492f08 --- /dev/null +++ b/app/src/main/java/org/linphone/utils/DeviceOrientationEventListener.java @@ -0,0 +1,59 @@ +package org.linphone.utils; + +/* +DeviceOrientationEventListener.java +Copyright (C) 2019 Belledonne Communications, Grenoble, France + +This program is free software; you can redistribute it and/or +modify it under the terms of the GNU General Public License +as published by the Free Software Foundation; either version 2 +of the License, or (at your option) any later version. + +This program is distributed in the hope that it will be useful, +but WITHOUT ANY WARRANTY; without even the implied warranty of +MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +GNU General Public License for more details. + +You should have received a copy of the GNU General Public License +along with this program; if not, write to the Free Software +Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. +*/ + +import android.content.Context; +import android.view.OrientationEventListener; +import org.linphone.LinphoneManager; +import org.linphone.core.Core; +import org.linphone.core.tools.Log; + +public class DeviceOrientationEventListener extends OrientationEventListener { + private int mAlwaysChangingPhoneAngle; + + public DeviceOrientationEventListener(Context context) { + super(context); + mAlwaysChangingPhoneAngle = -1; + } + + @Override + public void onOrientationChanged(final int orientation) { + if (orientation == OrientationEventListener.ORIENTATION_UNKNOWN) { + return; + } + + int degrees = 270; + if (orientation < 45 || orientation > 315) degrees = 0; + else if (orientation < 135) degrees = 90; + else if (orientation < 225) degrees = 180; + + if (mAlwaysChangingPhoneAngle == degrees) { + return; + } + mAlwaysChangingPhoneAngle = degrees; + Log.i("[Orientation Helper] Device orientation changed to " + degrees); + + int rotation = (360 - degrees) % 360; + Core core = LinphoneManager.getCore(); + if (core != null) { + core.setDeviceRotation(rotation); + } + } +}