From 35aa663567539655815ae5fd3ff5867239cfd61b Mon Sep 17 00:00:00 2001 From: Sylvain Berfini Date: Wed, 1 Aug 2012 17:47:04 +0200 Subject: [PATCH] Preview rotate in video call --- res/layout-land/video.xml | 20 +++++++ src/org/linphone/LinphoneActivity.java | 55 +++++++++++++++++++ src/org/linphone/VideoCallFragment.java | 7 ++- .../linphone/compatibility/ApiEightPlus.java | 33 +++++++++++ .../linphone/compatibility/ApiFivePlus.java | 6 ++ .../linphone/compatibility/Compatibility.java | 9 +++ 6 files changed, 129 insertions(+), 1 deletion(-) create mode 100644 res/layout-land/video.xml create mode 100644 src/org/linphone/compatibility/ApiEightPlus.java diff --git a/res/layout-land/video.xml b/res/layout-land/video.xml new file mode 100644 index 000000000..46a71800f --- /dev/null +++ b/res/layout-land/video.xml @@ -0,0 +1,20 @@ + + + + + + + + \ No newline at end of file diff --git a/src/org/linphone/LinphoneActivity.java b/src/org/linphone/LinphoneActivity.java index 904dc3264..79066eb78 100644 --- a/src/org/linphone/LinphoneActivity.java +++ b/src/org/linphone/LinphoneActivity.java @@ -35,6 +35,7 @@ import org.linphone.core.LinphoneCall; import org.linphone.core.LinphoneCall.State; import org.linphone.core.LinphoneCallLog; import org.linphone.core.LinphoneCallLog.CallStatus; +import org.linphone.core.LinphoneCore; import org.linphone.core.LinphoneCore.RegistrationState; import org.linphone.core.LinphoneCoreFactory; import org.linphone.core.Log; @@ -43,6 +44,7 @@ import org.linphone.setup.SetupActivity; import org.linphone.ui.AddressText; import android.app.Activity; +import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.database.Cursor; @@ -56,6 +58,7 @@ import android.support.v4.app.FragmentActivity; import android.support.v4.app.FragmentManager; import android.support.v4.app.FragmentTransaction; import android.view.KeyEvent; +import android.view.OrientationEventListener; import android.view.View; import android.view.View.OnClickListener; import android.view.WindowManager; @@ -89,6 +92,7 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene private Handler mHandler = new Handler(); private List contactList, sipContactList; private Cursor contactCursor, sipContactCursor; + private OrientationEventListener mOrientationHelper; static final boolean isInstanciated() { return instance != null; @@ -135,6 +139,16 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene int missedCalls = LinphoneManager.getLc().getMissedCallsCount(); displayMissedCalls(missedCalls); + int rotation = Compatibility.getRotation(getWindowManager().getDefaultDisplay()); + // Inverse landscape rotation to initiate linphoneCore correctly + if (rotation == 270) + rotation = 90; + else if (rotation == 90) + rotation = 270; + + LinphoneManager.getLc().setDeviceRotation(rotation); + mAlwaysChangingPhoneAngle = rotation; + instance = this; } @@ -604,6 +618,7 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene public void startVideoActivity(LinphoneCall currentCall) { Intent intent = new Intent(this, InCallActivity.class); intent.putExtra("VideoEnabled", true); + startOrientationSensor(); startActivityForResult(intent, callActivity); } @@ -612,6 +627,46 @@ public class LinphoneActivity extends FragmentActivity implements OnClickListene intent.putExtra("VideoEnabled", false); startActivityForResult(intent, callActivity); } + + /** + * Register a sensor to track phoneOrientation changes + */ + private synchronized void startOrientationSensor() { + if (mOrientationHelper == null) { + mOrientationHelper = new LocalOrientationEventListener(this); + } + mOrientationHelper.enable(); + } + + private int mAlwaysChangingPhoneAngle = -1; + private class LocalOrientationEventListener extends OrientationEventListener { + public LocalOrientationEventListener(Context context) { + super(context); + } + @Override + public void onOrientationChanged(final int o) { + if (o == OrientationEventListener.ORIENTATION_UNKNOWN) return; + + int degrees=270; + if (o < 45 || o >315) degrees=0; + else if (o<135) degrees=90; + else if (o<225) degrees=180; + + if (mAlwaysChangingPhoneAngle == degrees) return; + mAlwaysChangingPhoneAngle = degrees; + + Log.d("Phone orientation changed to ", degrees); + int rotation = (360 - degrees) % 360; + LinphoneCore lc=LinphoneManager.getLcIfManagerNotDestroyedOrNull(); + if (lc!=null){ + lc.setDeviceRotation(rotation); + LinphoneCall currentCall = lc.getCurrentCall(); + if (currentCall != null && currentCall.cameraEnabled() && currentCall.getCurrentParamsCopy().getVideoEnabled()) { + lc.updateCall(currentCall, null); + } + } + } + } public void showPreferenceErrorDialog(String message) { diff --git a/src/org/linphone/VideoCallFragment.java b/src/org/linphone/VideoCallFragment.java index a80e6d87a..06ebbfbdc 100644 --- a/src/org/linphone/VideoCallFragment.java +++ b/src/org/linphone/VideoCallFragment.java @@ -138,6 +138,9 @@ public class VideoCallFragment extends Fragment implements OnGestureListener, On } } + private void rotatePreviewIfNeeded() { + } + @Override public void onResume() { super.onResume(); @@ -151,9 +154,11 @@ public class VideoCallFragment extends Fragment implements OnGestureListener, On } } + rotatePreviewIfNeeded(); + mWakeLock.acquire(); } - + @Override public void onPause() { synchronized (androidVideoWindowImpl) { diff --git a/src/org/linphone/compatibility/ApiEightPlus.java b/src/org/linphone/compatibility/ApiEightPlus.java new file mode 100644 index 000000000..b5c5df28b --- /dev/null +++ b/src/org/linphone/compatibility/ApiEightPlus.java @@ -0,0 +1,33 @@ +package org.linphone.compatibility; + +import android.annotation.TargetApi; +import android.view.Display; + +/* +ApiEightPlus.java +Copyright (C) 2012 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., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. +*/ +/** + * @author Sylvain Berfini + */ +@TargetApi(8) +public class ApiEightPlus { + + public static int getRotation(Display display) { + return display.getRotation(); + } +} diff --git a/src/org/linphone/compatibility/ApiFivePlus.java b/src/org/linphone/compatibility/ApiFivePlus.java index c906bf31e..9691633b0 100644 --- a/src/org/linphone/compatibility/ApiFivePlus.java +++ b/src/org/linphone/compatibility/ApiFivePlus.java @@ -27,6 +27,7 @@ import android.provider.ContactsContract.CommonDataKinds.SipAddress; import android.provider.ContactsContract.Contacts; import android.provider.ContactsContract.Data; import android.provider.ContactsContract.Intents.Insert; +import android.view.Display; /* ApiFivePlus.java @@ -275,4 +276,9 @@ public class ApiFivePlus { } return null; } + + @SuppressWarnings("deprecation") + public static int getRotation(Display display) { + return display.getOrientation(); + } } diff --git a/src/org/linphone/compatibility/Compatibility.java b/src/org/linphone/compatibility/Compatibility.java index 758c314d4..5704e1402 100644 --- a/src/org/linphone/compatibility/Compatibility.java +++ b/src/org/linphone/compatibility/Compatibility.java @@ -34,6 +34,7 @@ import android.content.Intent; import android.database.Cursor; import android.graphics.Bitmap; import android.net.Uri; +import android.view.Display; /** * @author Sylvain Berfini */ @@ -176,4 +177,12 @@ public class Compatibility { } return null; } + + public static int getRotation(Display display) { + if (Version.sdkStrictlyBelow(8)) { + return ApiFivePlus.getRotation(display); + } else { + return ApiEightPlus.getRotation(display); + } + } }