diff options
| author | Danny Baumann <dannybaumann@web.de> | 2015-04-21 20:55:13 +0200 |
|---|---|---|
| committer | Gerrit Code Review <gerrit@cyanogenmod.org> | 2015-06-02 20:31:43 +0000 |
| commit | faf5a341fd019adbb756502bc4373980506eba93 (patch) | |
| tree | 4733ee2a69101a2c63f10939178a2ab84fe91efd | |
| parent | 9cd8afb9221421086cec90473b6802f105afb436 (diff) | |
| download | android_packages_apps_InCallUI-faf5a341fd019adbb756502bc4373980506eba93.tar.gz android_packages_apps_InCallUI-faf5a341fd019adbb756502bc4373980506eba93.tar.bz2 android_packages_apps_InCallUI-faf5a341fd019adbb756502bc4373980506eba93.zip | |
[1/2] InCallUI: SmartMute
Mute the incoming call by flipping the phone
Change-Id: I3ff27450eecbf501fafb2d75317a0d7dd844e7b2
Signed-off-by: linuxxxxx <joey@cyanogenmoditalia.it>
| -rw-r--r-- | src/com/android/incallui/AccelerometerListener.java | 73 | ||||
| -rw-r--r-- | src/com/android/incallui/InCallPresenter.java | 41 | ||||
| -rw-r--r-- | src/com/android/incallui/ProximitySensor.java | 9 |
3 files changed, 115 insertions, 8 deletions
diff --git a/src/com/android/incallui/AccelerometerListener.java b/src/com/android/incallui/AccelerometerListener.java index 1a707786..813f7891 100644 --- a/src/com/android/incallui/AccelerometerListener.java +++ b/src/com/android/incallui/AccelerometerListener.java @@ -46,7 +46,7 @@ public final class AccelerometerListener { // mOrientation. private int mPendingOrientation; - private OrientationListener mListener; + private ChangeListener mListener; // Device orientation public static final int ORIENTATION_UNKNOWN = 0; @@ -54,16 +54,28 @@ public final class AccelerometerListener { public static final int ORIENTATION_HORIZONTAL = 2; private static final int ORIENTATION_CHANGED = 1234; + private static final int FACE_UP_CHANGED = 1235; private static final int VERTICAL_DEBOUNCE = 100; private static final int HORIZONTAL_DEBOUNCE = 500; private static final double VERTICAL_ANGLE = 50.0; - public interface OrientationListener { - public void orientationChanged(int orientation); + // Flip detection + private static final int FACE_UP_GRAVITY_THRESHOLD = 7; + private static final int FACE_DOWN_GRAVITY_THRESHOLD = -7; + private static final int SENSOR_SAMPLES = 3; + private static final int MIN_ACCEPT_COUNT = SENSOR_SAMPLES - 1; + + private boolean mWasFaceUp; + private boolean[] mSamples = new boolean[SENSOR_SAMPLES]; + private int mSampleIndex; + + public interface ChangeListener { + void onOrientationChanged(int orientation); + void onDeviceFlipped(boolean faceDown); } - public AccelerometerListener(Context context, OrientationListener listener) { + public AccelerometerListener(Context context, ChangeListener listener) { mListener = listener; mSensorManager = (SensorManager)context.getSystemService(Context.SENSOR_SERVICE); mSensor = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER); @@ -75,6 +87,8 @@ public final class AccelerometerListener { if (enable) { mOrientation = ORIENTATION_UNKNOWN; mPendingOrientation = ORIENTATION_UNKNOWN; + mWasFaceUp = false; + resetFlipSamples(); mSensorManager.registerListener(mSensorListener, mSensor, SensorManager.SENSOR_DELAY_NORMAL); } else { @@ -84,6 +98,22 @@ public final class AccelerometerListener { } } + private void resetFlipSamples() { + for (int i = 0; i < SENSOR_SAMPLES; i++) { + mSamples[i] = false; + } + } + + private boolean filterFlipSamples() { + int trues = 0; + for (int i = 0; i < mSamples.length; i++) { + if (mSamples[i]) { + ++trues; + } + } + return trues >= MIN_ACCEPT_COUNT; + } + private void setOrientation(int orientation) { synchronized (this) { if (mPendingOrientation == orientation) { @@ -112,6 +142,17 @@ public final class AccelerometerListener { } } + private void setIsFaceUp(boolean faceUp) { + synchronized (this) { + if (mWasFaceUp != faceUp) { + mHandler.removeMessages(FACE_UP_CHANGED); + mHandler.obtainMessage(FACE_UP_CHANGED, faceUp ? 1 : 0, 0).sendToTarget(); + mWasFaceUp = faceUp; + resetFlipSamples(); + } + } + } + private void onSensorEvent(double x, double y, double z) { if (VDEBUG) Log.d(TAG, "onSensorEvent(" + x + ", " + y + ", " + z + ")"); @@ -128,6 +169,25 @@ public final class AccelerometerListener { final int orientation = (angle > VERTICAL_ANGLE ? ORIENTATION_VERTICAL : ORIENTATION_HORIZONTAL); if (VDEBUG) Log.d(TAG, "angle: " + angle + " orientation: " + orientation); setOrientation(orientation); + + boolean nowFaceUp, wasFaceUp; + synchronized (this) { + nowFaceUp = wasFaceUp = mWasFaceUp; + } + + if (!wasFaceUp) { + // Check if its face up enough. + mSamples[mSampleIndex] = z > FACE_UP_GRAVITY_THRESHOLD; + } else { + // Check if its face down enough. + mSamples[mSampleIndex] = z < FACE_DOWN_GRAVITY_THRESHOLD; + } + if (filterFlipSamples()) { + nowFaceUp = !wasFaceUp; + } + + mSampleIndex = ((mSampleIndex + 1) % SENSOR_SAMPLES); + setIsFaceUp(nowFaceUp); } SensorEventListener mSensorListener = new SensorEventListener() { @@ -152,9 +212,12 @@ public final class AccelerometerListener { : (mOrientation == ORIENTATION_VERTICAL ? "vertical" : "unknown"))); } - mListener.orientationChanged(mOrientation); + mListener.onOrientationChanged(mOrientation); } break; + case FACE_UP_CHANGED: + mListener.onDeviceFlipped(msg.arg1 == 0); + break; } } }; diff --git a/src/com/android/incallui/InCallPresenter.java b/src/com/android/incallui/InCallPresenter.java index 72c1b173..2ffba4fa 100644 --- a/src/com/android/incallui/InCallPresenter.java +++ b/src/com/android/incallui/InCallPresenter.java @@ -20,11 +20,13 @@ import android.app.Activity; import android.content.Context; import android.content.Intent; import android.content.ActivityNotFoundException; +import android.content.SharedPreferences; import android.content.pm.ActivityInfo; import android.graphics.Point; import android.net.Uri; import android.os.Bundle; import android.os.Handler; +import android.preference.PreferenceManager; import android.telecom.DisconnectCause; import android.telecom.PhoneAccount; import android.telecom.Phone; @@ -61,7 +63,8 @@ import java.util.concurrent.CopyOnWriteArrayList; * that want to listen in on the in-call state changes. * TODO: This class has become more of a state machine at this point. Consider renaming. */ -public class InCallPresenter implements CallList.Listener, InCallPhoneListener { +public class InCallPresenter implements CallList.Listener, + InCallPhoneListener, AccelerometerListener.ChangeListener { private static final String EXTRA_FIRST_TIME_SHOWN = "com.android.incallui.intent.extra.FIRST_TIME_SHOWN"; @@ -98,6 +101,7 @@ public class InCallPresenter implements CallList.Listener, InCallPhoneListener { private InCallActivity mInCallActivity; private InCallState mInCallState = InCallState.NO_CALLS; private ProximitySensor mProximitySensor; + private AccelerometerListener mAccelerometerListener; private boolean mServiceConnected = false; private boolean mAccountSelectionCancelled = false; private InCallCameraManager mInCallCameraManager = null; @@ -250,6 +254,8 @@ public class InCallPresenter implements CallList.Listener, InCallPhoneListener { mProximitySensor = new ProximitySensor(context, mAudioModeProvider); addListener(mProximitySensor); + mAccelerometerListener = new AccelerometerListener(context, this); + mCallList = callList; // This only gets called by the service so this is okay. @@ -435,6 +441,10 @@ public class InCallPresenter implements CallList.Listener, InCallPhoneListener { newState = startOrFinishUi(newState); Log.d(this, "onCallListChange newState changed to " + newState); + if (!newState.isIncoming() && mAccelerometerListener != null) { + mAccelerometerListener.enable(false); + } + // Set the new state before announcing it to the world Log.i(this, "Phone switching state: " + oldState + " -> " + newState); mInCallState = newState; @@ -468,6 +478,10 @@ public class InCallPresenter implements CallList.Listener, InCallPhoneListener { Log.i(this, "Phone switching state: " + oldState + " -> " + newState); mInCallState = newState; + if (newState.isIncoming() && mAccelerometerListener != null) { + mAccelerometerListener.enable(true); + } + for (IncomingCallListener listener : mIncomingCallListeners) { listener.onIncomingCall(oldState, mInCallState, call); } @@ -500,6 +514,23 @@ public class InCallPresenter implements CallList.Listener, InCallPhoneListener { } } + @Override + public void onOrientationChanged(int orientation) { + // ignored + } + + @Override + public void onDeviceFlipped(boolean faceDown) { + if (!faceDown) { + return; + } + + SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(mContext); + if (prefs.getBoolean("button_smart_mute", false)) { + getTelecomManager().silenceRinger(); + } + } + /** * Given the call list, return the state in which the in-call screen should be. */ @@ -858,6 +889,9 @@ public class InCallPresenter implements CallList.Listener, InCallPhoneListener { if (incomingCall != null) { TelecomAdapter.getInstance().answerCall( incomingCall.getId(), VideoProfile.VideoState.AUDIO_ONLY); + if (mAccelerometerListener != null) { + mAccelerometerListener.enable(false); + } return true; } @@ -1200,6 +1234,11 @@ public class InCallPresenter implements CallList.Listener, InCallPhoneListener { } mProximitySensor = null; + if (mAccelerometerListener != null) { + mAccelerometerListener.enable(false); + mAccelerometerListener = null; + } + mAudioModeProvider = null; if (mStatusBarNotifier != null) { diff --git a/src/com/android/incallui/ProximitySensor.java b/src/com/android/incallui/ProximitySensor.java index 6acdf914..65dae8e1 100644 --- a/src/com/android/incallui/ProximitySensor.java +++ b/src/com/android/incallui/ProximitySensor.java @@ -36,7 +36,7 @@ import com.google.common.base.Objects; * sensor should be enabled and disabled. Most of that state is fed into this class through * public methods. */ -public class ProximitySensor implements AccelerometerListener.OrientationListener, +public class ProximitySensor implements AccelerometerListener.ChangeListener, InCallStateListener, AudioModeListener { private static final String TAG = ProximitySensor.class.getSimpleName(); @@ -76,11 +76,16 @@ public class ProximitySensor implements AccelerometerListener.OrientationListene * Called to identify when the device is laid down flat. */ @Override - public void orientationChanged(int orientation) { + public void onOrientationChanged(int orientation) { mOrientation = orientation; updateProximitySensorMode(); } + @Override + public void onDeviceFlipped(boolean faceDown) { + // ignored + } + /** * Called to keep track of the overall UI state. */ |
