OmniGears: add optional threshold filter for wired headset events
some devices and headsets created unneded events during plug and unplug
this makes it possible to define a threshold in seconds where
events that are too fast after each other are ignore
Change-Id: I33b16e68ee0846917ef9fdbdebea9cb85fc8ece3
diff --git a/res/values/custom_strings.xml b/res/values/custom_strings.xml
index 9dae5c0..69233e5 100644
--- a/res/values/custom_strings.xml
+++ b/res/values/custom_strings.xml
@@ -716,6 +716,8 @@
<string name="app_chooser_right">Right</string>
<string name="app_chooser_timeout_title">App chooser dialog timeout</string>
<string name="app_chooser_position_title">App chooser dialog position</string>
+ <string name="wired_events_threshold_title">Wired headset event threshold</string>
+ <string name="wired_events_threshold_summary">Ignore headset events if they are within threshold time</string>
<!-- Quick Pulldown-->
<string name="quick_pulldown_title">Quick pulldown</string>
diff --git a/res/xml/event_service_settings.xml b/res/xml/event_service_settings.xml
index 8983e64..48fe122 100644
--- a/res/xml/event_service_settings.xml
+++ b/res/xml/event_service_settings.xml
@@ -104,5 +104,15 @@
android:persistent="false"
android:title="@string/app_chooser_position_title" />
+ <org.omnirom.omnigears.preference.SeekBarPreference
+ android:dependency="event_service_enabled"
+ android:key="wired_events_threshold"
+ android:max="6"
+ android:persistent="false"
+ android:title="@string/wired_events_threshold_title"
+ android:summary="@string/wired_events_threshold_summary"
+ settings:min="0"
+ settings:unitsLeft=""
+ settings:unitsRight="@string/unit_sec" />
</PreferenceCategory>
</PreferenceScreen>
diff --git a/src/org/omnirom/omnigears/service/EventService.java b/src/org/omnirom/omnigears/service/EventService.java
index 19f2d04..59aa91b 100644
--- a/src/org/omnirom/omnigears/service/EventService.java
+++ b/src/org/omnirom/omnigears/service/EventService.java
@@ -71,6 +71,8 @@
private static final int LEFT = 0;
private static final int RIGHT = 1;
private static final Interpolator FAST_OUT_SLOW_IN = new PathInterpolator(0.4f, 0f, 0.2f, 1f);
+ private static boolean mOverlayShown;
+ private static long mLastUnplugEventTimestamp;
private WindowManager mWindowManager;
private View mFloatingWidget = null;
@@ -121,7 +123,16 @@
break;
case AudioManager.ACTION_HEADSET_PLUG:
boolean useHeadset = intent.getIntExtra("state", 0) == 1;
+ final int threshold = getPrefs(context).getInt(EventServiceSettings.WIRED_EVENTS_THRESHOLD, 0);
+
if (useHeadset && !mWiredHeadsetConnected) {
+ if (mLastUnplugEventTimestamp != 0) {
+ final long eventDelta = System.currentTimeMillis() - mLastUnplugEventTimestamp;
+ if (eventDelta < threshold * 1000) {
+ if (DEBUG) Log.d(TAG, "Ignore AudioManager.ACTION_HEADSET_PLUG = " + useHeadset + " delta = " + eventDelta);
+ return;
+ }
+ }
mWiredHeadsetConnected = true;
if (DEBUG) Log.d(TAG, "AudioManager.ACTION_HEADSET_PLUG = true");
@@ -138,6 +149,7 @@
} else {
mWiredHeadsetConnected = false;
if (DEBUG) Log.d(TAG, "AudioManager.ACTION_HEADSET_PLUG = false");
+ mLastUnplugEventTimestamp = System.currentTimeMillis();
}
break;
}
@@ -149,11 +161,11 @@
};
public void openAppChooserDialog(final Context context) {
- final LayoutInflater inflater = LayoutInflater.from(new ContextThemeWrapper(
+ if (!mOverlayShown) {
+ final LayoutInflater inflater = LayoutInflater.from(new ContextThemeWrapper(
context, android.R.style.Theme_DeviceDefault_Light_Dialog));
- mFloatingWidget = inflater.inflate(R.layout.layout_floating_widget, null);
+ mFloatingWidget = inflater.inflate(R.layout.layout_floating_widget, null);
- if (mFloatingWidget.getWindowToken() == null) {
final WindowManager.LayoutParams params = new WindowManager.LayoutParams(
WindowManager.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.WRAP_CONTENT,
@@ -218,15 +230,13 @@
mCloseHandler.postDelayed(new Runnable() {
@Override
public void run() {
- boolean stillThere = (mFloatingWidget != null
- && mFloatingWidget.getWindowToken() != null);
-
- if (stillThere) {
+ if (mOverlayShown) {
slideAnimation(context, false);
}
}
}, timeout * 1000);
}
+ mOverlayShown = true;
}
}
@@ -386,7 +396,10 @@
.translationX(endValue)
.setDuration(ANIM_DURATION)
.setInterpolator(FAST_OUT_SLOW_IN)
- .withEndAction(() -> mWindowManager.removeViewImmediate(mFloatingWidget))
+ .withEndAction(() -> {
+ mOverlayShown = false;
+ mWindowManager.removeViewImmediate(mFloatingWidget);
+ })
.start();
}
}
diff --git a/src/org/omnirom/omnigears/service/EventServiceSettings.java b/src/org/omnirom/omnigears/service/EventServiceSettings.java
index 5d89ebc..6c5ff86 100644
--- a/src/org/omnirom/omnigears/service/EventServiceSettings.java
+++ b/src/org/omnirom/omnigears/service/EventServiceSettings.java
@@ -58,6 +58,7 @@
public static final String HEADSET_APP_LIST = "headset_app_list";
public static final String APP_CHOOSER_TIMEOUT = "app_chooser_timeout";
public static final String APP_CHOOSER_POSITION = "app_chooser_position";
+ public static final String WIRED_EVENTS_THRESHOLD = "wired_events_threshold";
// -- For backward compatibility
public static final String OLD_EVENT_A2DP_CONNECT = "bt_a2dp_connect_app";
@@ -77,6 +78,7 @@
private Handler mHandler = new Handler();
private String mServiceRunning;
private String mServiceStopped;
+ private SeekBarPreference mWiredThresholdTimeout;
@Override
public int getMetricsCategory() {
@@ -132,6 +134,10 @@
mChooserTimeout.setValue(getPrefs().getInt(EventServiceSettings.APP_CHOOSER_TIMEOUT, 15));
mChooserTimeout.setOnPreferenceChangeListener(this);
+ mWiredThresholdTimeout = (SeekBarPreference) findPreference(WIRED_EVENTS_THRESHOLD);
+ mWiredThresholdTimeout.setValue(getPrefs().getInt(EventServiceSettings.WIRED_EVENTS_THRESHOLD, 0));
+ mWiredThresholdTimeout.setOnPreferenceChangeListener(this);
+
mA2DPappSelect = (AppMultiSelectListPreference) findPreference(EVENT_A2DP_CONNECT);
Set<String> value = getPrefs().getStringSet(EVENT_A2DP_CONNECT, null);
if (value != null) mA2DPappSelect.setValues(value);
@@ -231,6 +237,10 @@
getPrefs().edit().putInt(APP_CHOOSER_POSITION, value).commit();
updateChooserPositionSummary(value);
return true;
+ } else if (preference == mWiredThresholdTimeout) {
+ int value = ((int) newValue);
+ getPrefs().edit().putInt(WIRED_EVENTS_THRESHOLD, value).commit();
+ return true;
}
return false;
}