Move proximity sensor management to linphone manager
This commit is contained in:
parent
a5c09b7710
commit
2686150301
2 changed files with 71 additions and 78 deletions
|
@ -87,7 +87,7 @@ import java.util.TimerTask;
|
|||
/**
|
||||
* @author Sylvain Berfini
|
||||
*/
|
||||
public class CallActivity extends LinphoneGenericActivity implements OnClickListener, SensorEventListener, ActivityCompat.OnRequestPermissionsResultCallback {
|
||||
public class CallActivity extends LinphoneGenericActivity implements OnClickListener, ActivityCompat.OnRequestPermissionsResultCallback {
|
||||
private final static int SECONDS_BEFORE_HIDING_CONTROLS = 4000;
|
||||
private final static int SECONDS_BEFORE_DENYING_CALL_UPDATE = 30000;
|
||||
private static final int PERMISSIONS_REQUEST_CAMERA = 202;
|
||||
|
@ -117,19 +117,12 @@ public class CallActivity extends LinphoneGenericActivity implements OnClickList
|
|||
private Dialog dialog = null;
|
||||
private static long TimeRemind = 0;
|
||||
|
||||
private static PowerManager powerManager;
|
||||
private static PowerManager.WakeLock wakeLock;
|
||||
private static int field = 0x00000020;
|
||||
private SensorManager mSensorManager;
|
||||
private Sensor mProximity;
|
||||
|
||||
private LinearLayout callsList, conferenceList;
|
||||
private LayoutInflater inflater;
|
||||
private ViewGroup container;
|
||||
private boolean isConferenceRunning = false;
|
||||
private LinphoneCoreListenerBase mListener;
|
||||
private DrawerLayout sideMenu;
|
||||
private boolean mProximitySensingEnabled;
|
||||
|
||||
private Handler mHandler = new Handler();
|
||||
private Timer mTimer;
|
||||
|
@ -161,18 +154,6 @@ public class CallActivity extends LinphoneGenericActivity implements OnClickList
|
|||
|
||||
cameraNumber = AndroidCameraConfiguration.retrieveCameras().length;
|
||||
|
||||
try {
|
||||
// Yeah, this is hidden field.
|
||||
field = PowerManager.class.getClass().getField("PROXIMITY_SCREEN_OFF_WAKE_LOCK").getInt(null);
|
||||
} catch (Throwable ignored) {
|
||||
}
|
||||
|
||||
powerManager = (PowerManager) getSystemService(Context.POWER_SERVICE);
|
||||
wakeLock = powerManager.newWakeLock(field, getLocalClassName());
|
||||
|
||||
mSensorManager = (SensorManager) getSystemService(Context.SENSOR_SERVICE);
|
||||
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
|
||||
|
||||
mEncoderTexts = new HashMap<String, String>();
|
||||
mDecoderTexts = new HashMap<String, String>();
|
||||
|
||||
|
@ -814,26 +795,8 @@ public class CallActivity extends LinphoneGenericActivity implements OnClickList
|
|||
}
|
||||
}
|
||||
|
||||
private void enableProximitySensing(boolean enable){
|
||||
if (enable){
|
||||
if (!mProximitySensingEnabled){
|
||||
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
mProximitySensingEnabled = true;
|
||||
}
|
||||
} else {
|
||||
if (mProximitySensingEnabled){
|
||||
mSensorManager.unregisterListener(this);
|
||||
mProximitySensingEnabled = false;
|
||||
// Don't forgeting to release wakelock if held
|
||||
if(wakeLock.isHeld()) {
|
||||
wakeLock.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
private void showAudioView() {
|
||||
enableProximitySensing(true);
|
||||
LinphoneManager.getInstance().enableProximitySensing(true);
|
||||
replaceFragmentVideoByAudio();
|
||||
displayAudioCall();
|
||||
showStatusBar();
|
||||
|
@ -848,7 +811,7 @@ public class CallActivity extends LinphoneGenericActivity implements OnClickList
|
|||
}
|
||||
refreshInCallActions();
|
||||
|
||||
enableProximitySensing(false);
|
||||
LinphoneManager.getInstance().enableProximitySensing(false);
|
||||
|
||||
replaceFragmentAudioByVideo();
|
||||
hideStatusBar();
|
||||
|
@ -1206,7 +1169,7 @@ public class CallActivity extends LinphoneGenericActivity implements OnClickList
|
|||
handleViewIntent();
|
||||
|
||||
if (!isVideoEnabled(LinphoneManager.getLc().getCurrentCall())) {
|
||||
enableProximitySensing(true);
|
||||
LinphoneManager.getInstance().enableProximitySensing(true);
|
||||
removeCallbacks();
|
||||
}
|
||||
}
|
||||
|
@ -1268,8 +1231,6 @@ public class CallActivity extends LinphoneGenericActivity implements OnClickList
|
|||
mControls = null;
|
||||
mControlsHandler = null;
|
||||
|
||||
enableProximitySensing(false);
|
||||
|
||||
unbindDrawables(findViewById(R.id.topLayout));
|
||||
if (mTimer != null) {
|
||||
mTimer.cancel();
|
||||
|
@ -1552,7 +1513,6 @@ public class CallActivity extends LinphoneGenericActivity implements OnClickList
|
|||
private void displayConference(boolean isInConf){
|
||||
if(isInConf) {
|
||||
mControlsLayout.setVisibility(View.VISIBLE);
|
||||
enableProximitySensing(true);
|
||||
mActiveCallHeader.setVisibility(View.GONE);
|
||||
mNoCurrentCall.setVisibility(View.GONE);
|
||||
conferenceList.removeAllViews();
|
||||
|
@ -1574,39 +1534,6 @@ public class CallActivity extends LinphoneGenericActivity implements OnClickList
|
|||
}
|
||||
}
|
||||
|
||||
public static Boolean isProximitySensorNearby(final SensorEvent event) {
|
||||
float threshold = 4.001f; // <= 4 cm is near
|
||||
|
||||
final float distanceInCm = event.values[0];
|
||||
final float maxDistance = event.sensor.getMaximumRange();
|
||||
Log.d("Proximity sensor report ["+distanceInCm+"] , for max range ["+maxDistance+"]");
|
||||
|
||||
if (maxDistance <= threshold) {
|
||||
// Case binary 0/1 and short sensors
|
||||
threshold = maxDistance;
|
||||
}
|
||||
return distanceInCm < threshold;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
if (event.timestamp == 0) return;
|
||||
if(isProximitySensorNearby(event)){
|
||||
if(!wakeLock.isHeld()) {
|
||||
wakeLock.acquire();
|
||||
}
|
||||
} else {
|
||||
if(wakeLock.isHeld()) {
|
||||
wakeLock.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
|
||||
}
|
||||
|
||||
private void displayMissedChats() {
|
||||
int count = 0;
|
||||
LinphoneChatRoom[] chats = LinphoneManager.getLc().getChatRooms();
|
||||
|
|
|
@ -30,6 +30,10 @@ import android.content.Intent;
|
|||
import android.content.IntentFilter;
|
||||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.content.res.Resources;
|
||||
import android.hardware.Sensor;
|
||||
import android.hardware.SensorEvent;
|
||||
import android.hardware.SensorEventListener;
|
||||
import android.hardware.SensorManager;
|
||||
import android.media.AudioManager;
|
||||
import android.media.MediaPlayer;
|
||||
import android.net.ConnectivityManager;
|
||||
|
@ -124,7 +128,7 @@ import static android.media.AudioManager.STREAM_VOICE_CALL;
|
|||
* @author Guillaume Beraudo
|
||||
*
|
||||
*/
|
||||
public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessage.LinphoneChatMessageListener, LinphoneAccountCreator.LinphoneAccountCreatorListener {
|
||||
public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessage.LinphoneChatMessageListener, SensorEventListener, LinphoneAccountCreator.LinphoneAccountCreatorListener {
|
||||
|
||||
private static LinphoneManager instance;
|
||||
private Context mServiceContext;
|
||||
|
@ -151,10 +155,14 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
|
|||
private IntentFilter mHookIntentFilter;
|
||||
private Handler mHandler = new Handler();
|
||||
private WakeLock mIncallWakeLock;
|
||||
private WakeLock mProximityWakelock;
|
||||
private LinphoneAccountCreator accountCreator;
|
||||
private static List<LinphoneChatMessage> mPendingChatFileMessage;
|
||||
private static LinphoneChatMessage mUploadPendingFileMessage;
|
||||
private boolean mAreDisplayAlertMessage = false;
|
||||
private SensorManager mSensorManager;
|
||||
private Sensor mProximity;
|
||||
private boolean mProximitySensingEnabled;
|
||||
|
||||
public String wizardLoginViewDomain = null;
|
||||
|
||||
|
@ -191,6 +199,8 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
|
|||
mVibrator = (Vibrator) c.getSystemService(Context.VIBRATOR_SERVICE);
|
||||
mPowerManager = (PowerManager) c.getSystemService(Context.POWER_SERVICE);
|
||||
mConnectivityManager = (ConnectivityManager) c.getSystemService(Context.CONNECTIVITY_SERVICE);
|
||||
mSensorManager = (SensorManager) c.getSystemService(Context.SENSOR_SERVICE);
|
||||
mProximity = mSensorManager.getDefaultSensor(Sensor.TYPE_PROXIMITY);
|
||||
mR = c.getResources();
|
||||
mPendingChatFileMessage = new ArrayList<LinphoneChatMessage>();
|
||||
|
||||
|
@ -829,6 +839,7 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
|
|||
mHookReceiver = new HookReceiver();
|
||||
mServiceContext.registerReceiver(mHookReceiver, mHookIntentFilter);
|
||||
|
||||
mProximityWakelock = mPowerManager.newWakeLock(PowerManager.PROXIMITY_SCREEN_OFF_WAKE_LOCK, "manager_proximity_sensor");
|
||||
|
||||
updateNetworkReachability();
|
||||
|
||||
|
@ -981,6 +992,57 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
|
|||
}
|
||||
}
|
||||
|
||||
public void enableProximitySensing(boolean enable){
|
||||
if (enable){
|
||||
if (!mProximitySensingEnabled){
|
||||
mSensorManager.registerListener(this, mProximity, SensorManager.SENSOR_DELAY_NORMAL);
|
||||
mProximitySensingEnabled = true;
|
||||
}
|
||||
} else {
|
||||
if (mProximitySensingEnabled){
|
||||
mSensorManager.unregisterListener(this);
|
||||
mProximitySensingEnabled = false;
|
||||
// Don't forgeting to release wakelock if held
|
||||
if(mProximityWakelock.isHeld()) {
|
||||
mProximityWakelock.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public static Boolean isProximitySensorNearby(final SensorEvent event) {
|
||||
float threshold = 4.001f; // <= 4 cm is near
|
||||
|
||||
final float distanceInCm = event.values[0];
|
||||
final float maxDistance = event.sensor.getMaximumRange();
|
||||
Log.d("Proximity sensor report ["+distanceInCm+"] , for max range ["+maxDistance+"]");
|
||||
|
||||
if (maxDistance <= threshold) {
|
||||
// Case binary 0/1 and short sensors
|
||||
threshold = maxDistance;
|
||||
}
|
||||
return distanceInCm < threshold;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onSensorChanged(SensorEvent event) {
|
||||
if (event.timestamp == 0) return;
|
||||
if(isProximitySensorNearby(event)){
|
||||
if(!mProximityWakelock.isHeld()) {
|
||||
mProximityWakelock.acquire();
|
||||
}
|
||||
} else {
|
||||
if(mProximityWakelock.isHeld()) {
|
||||
mProximityWakelock.release();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onAccuracyChanged(Sensor sensor, int accuracy) {
|
||||
|
||||
}
|
||||
|
||||
public static void ContactsManagerDestroy() {
|
||||
if (ContactsManager.getInstance() != null)
|
||||
ContactsManager.getInstance().destroy();
|
||||
|
@ -1237,6 +1299,8 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
|
|||
|
||||
if (state == State.Connected) {
|
||||
if (mLc.getCallsNb() == 1) {
|
||||
//Enabling proximity sensor
|
||||
enableProximitySensing(true);
|
||||
//It is for incoming calls, because outgoing calls enter MODE_IN_COMMUNICATION immediately when they start.
|
||||
//However, incoming call first use the MODE_RINGING to play the local ring.
|
||||
if(call.getDirection() == CallDirection.Incoming) {
|
||||
|
@ -1254,6 +1318,8 @@ public class LinphoneManager implements LinphoneCoreListener, LinphoneChatMessag
|
|||
|
||||
if (state == State.CallEnd || state == State.Error) {
|
||||
if (mLc.getCallsNb() == 0) {
|
||||
//Disabling proximity sensor
|
||||
enableProximitySensing(false);
|
||||
Context activity = getContext();
|
||||
if (mAudioFocused) {
|
||||
int res = mAudioManager.abandonAudioFocus(null);
|
||||
|
|
Loading…
Reference in a new issue