Skip to content
This repository has been archived by the owner on Mar 3, 2021. It is now read-only.

Commit

Permalink
Stability Fixes (#32)
Browse files Browse the repository at this point in the history
* implemented triple press

* updated strings.xml

* updated .gitignore
  • Loading branch information
nadchif authored May 21, 2020
1 parent c1adc28 commit eaa7517
Show file tree
Hide file tree
Showing 15 changed files with 168 additions and 138 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
*.iml
output.json
.gradle
/local.properties
/.idea/caches
Expand Down
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "com.chif.headsetcontrolplus"
minSdkVersion 23
targetSdkVersion 29
versionCode 12
versionName "0.2.7"
versionCode 15
versionName "0.2.9"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
1 change: 1 addition & 0 deletions app/release/output.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
[{"outputType":{"type":"APK"},"apkData":{"type":"MAIN","splits":[],"versionCode":15,"versionName":"0.2.9","enabled":true,"outputFile":"app-release.apk","fullName":"release","baseName":"release"},"path":"app-release.apk","properties":{}}]
Original file line number Diff line number Diff line change
Expand Up @@ -52,14 +52,14 @@ public class ForegroundService extends Service {
private static final Handler S_HANDLER = new Handler();
private static AudioManager sAudioManager;

private static int sKeyDownCount = 0;
private static int sKeyUpCount = 0;
private final Handler mHandler = new Handler();
private MediaSessionCompat mMediaSessionCompat;
private MediaPlayer mMediaPlayer;
private ScreenOnOffReceiver mScreenOnOffReceiver;
private String mGestureMode = "unknown";
private Runnable mGestureLongPressed;
private Runnable mGestureSinglePressed;
private Runnable mGestureDoublePressed;
private Context mContext;
private PlaybackStateCompat.Builder mStateBuilder;

Expand All @@ -83,9 +83,6 @@ public void onCreate() {
mMediaSessionCompat.setCallback(new MediaSessionCompat.Callback() {
@Override
public boolean onMediaButtonEvent(Intent mediaButtonEvent) {
if (mScreenOnOffReceiver.isScreenOn()) {
return super.onMediaButtonEvent(mediaButtonEvent);
}
return handleMediaButton(mediaButtonEvent);
}
});
Expand Down Expand Up @@ -175,57 +172,58 @@ private boolean handleMediaButton(final Intent mediaButtonEvent) {
return false;
}

if (keycode != KeyEvent.KEYCODE_HEADSETHOOK && keycode != KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE) {
// Not interested in any other key
// Not interested in any other key
if (ServiceBase.isSupportedKey(keycode)) {
Log.i(APP_TAG, "Ignored " + keycode);
return false;
}

SharedPreferences pref = PreferenceManager.getDefaultSharedPreferences(this);

// Long Press.
if (action == KeyEvent.ACTION_DOWN) {
mGestureLongPressed = new Runnable() {
public void run() {
mGestureMode = "long_press";
HeadsetControlPlusService.handleGesture("long");
Log.w(APP_TAG, "Executed long press Action");
}
};
// Start tracking long press. If no action up is detected after 950ms,
// consider ut as long press.
mHandler.postDelayed(mGestureLongPressed, 900);
}


// Single and Double Click.
// Single, Double, Triple Click.
if (action == KeyEvent.ACTION_UP) {
sKeyDownCount++;
mHandler.removeCallbacks(mGestureLongPressed);
mGestureSinglePressed = new Runnable() {
public void run() {
// Single press.
if (sKeyDownCount == 1) {
// Check if this keyup event is not following a long press event.
if (mGestureMode != "long_press") {
sKeyUpCount++;

// Single press.
if (sKeyUpCount == 1) {
mGestureSinglePressed = new Runnable() {
public void run() {
if (sKeyUpCount == 1) {
sKeyUpCount = 0;
HeadsetControlPlusService.handleGesture("single");
Log.w(APP_TAG, "Executed single press Action");
}
mGestureMode = "unknown";
}
// Double press.
if (sKeyDownCount == 2) {
if (mGestureMode != "long_press") {
};
S_HANDLER.postDelayed(mGestureSinglePressed, 550);
}

// Double press.
if (sKeyUpCount == 2) {
S_HANDLER.removeCallbacks(mGestureSinglePressed);
mGestureDoublePressed = new Runnable() {
public void run() {
if (sKeyUpCount == 2) {
sKeyUpCount = 0;
HeadsetControlPlusService.handleGesture("double");
Log.w(APP_TAG, "Executed double press Action");
mGestureMode = "unknown";
}
mGestureMode = "unknown";

}
sKeyDownCount = 0;
};
S_HANDLER.postDelayed(mGestureDoublePressed, 500);
}

// Triple press.
if (sKeyUpCount == 3) {
S_HANDLER.removeCallbacks(mGestureDoublePressed);
if (sKeyUpCount == 3) {
sKeyUpCount = 0;
HeadsetControlPlusService.handleGesture("triple");
Log.w(APP_TAG, "Executed triple press Action");
mGestureMode = "unknown";
}
};
if (sKeyDownCount == 1) {
mHandler.postDelayed(mGestureSinglePressed, 400);
}
}
}
Expand Down Expand Up @@ -280,9 +278,6 @@ public void onReceive(final Context context, final Intent intent) {
mMediaSessionCompat.setCallback(new MediaSessionCompat.Callback() {
@Override
public boolean onMediaButtonEvent(final Intent mediaButtonEvent) {
if (mScreenOnOffReceiver.isScreenOn()) {
return super.onMediaButtonEvent(mediaButtonEvent);
}
return handleMediaButton(mediaButtonEvent);
}
});
Expand Down
Original file line number Diff line number Diff line change
@@ -1,19 +1,18 @@
/**
* HeadsetControlPlusService.java
*
<p>Copyright 2020 github.com/nadchif
* <p>Copyright 2020 github.com/nadchif
*
<p>Licensed under the Apache License, Version 2.0 (the "License");
* <p>Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
* http://www.apache.org/licenses/LICENSE-2.0
*
<p>Unless required by applicable law or agreed to in writing, software
* <p>Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
* @todo replace long press functionality with triple press
*/


Expand All @@ -32,11 +31,12 @@
import androidx.preference.PreferenceManager;
import com.chif.headsetcontrolplus.providers.FlashlightProvider;
import com.chif.headsetcontrolplus.providers.StravaProvider;
import com.chif.headsetcontrolplus.shared.ServiceBase;

public class HeadsetControlPlusService extends AccessibilityService {
private static final String APP_TAG = HeadsetControlPlusService.class.getSimpleName();
private static final Handler S_HANDLER = new Handler();
private static int sKeyDownCount = 0;
private static int sKeyUpCount = 0;
private static String sActionsDefault;
private static String sActionsPlayPause;
private static String sActionsNext;
Expand All @@ -51,8 +51,8 @@ public class HeadsetControlPlusService extends AccessibilityService {
private static SharedPreferences pref;
private static String mGestureMode = "unknown";
private static boolean sIsSimulation = false;
private static Runnable sGestureLongPressed;
private static Runnable sGestureSinglePressed;
private static Runnable sGestureDoublePressed;
private static FlashlightProvider sFlashlightProvider;
private static StravaProvider sStravaProvider;
private static Context sContext;
Expand Down Expand Up @@ -95,22 +95,22 @@ private static void execAction(final String action) {

/**
* Handles gestures that were polled during screen off.
* @param gesture - Accepts "single", "double", and "long"
* @param gesture - Accepts "single", "double", and "triple"
*/
public static void handleGesture(final String gesture) {

final String singlePressAction = pref.getString("hcp_gestures_single_press",
sActionsPlayPause);
final String doublePressAction = pref.getString("hcp_gestures_double_press",
sActionsNext);
final String longPressAction = pref.getString("hcp_gestures_long_press",
final String triplePressAction = pref.getString("hcp_gestures_triple_press",
sActionsPrevious);
if (gesture == "single") {
execAction(singlePressAction);
} else if (gesture == "double") {
execAction(doublePressAction);
} else if (gesture == "long") {
execAction(doublePressAction);
} else if (gesture == "triple") {
execAction(triplePressAction);
}
}

Expand Down Expand Up @@ -170,28 +170,14 @@ private static void simulateDoublePress() {
}

/**
* Simulates a long press of the headset button. This is necessary for cases where after
* catching the initial long press event and it is assigned to do default, you have to
* Simulates triple press of the headset button. This is necessary for cases where after
* catching the initial triple press event and it is assigned to do default, you have to
* re-stage it with the sIsSimulation set to true, to allow the event to go through this service
* uninterrupted.
* @todo write Triple Press Simulation function
*/
private static void simulateLongPress() {
sIsSimulation = true; // Set to true each time, to allow it go through and be handled by system.

sAudioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_DOWN,
KeyEvent.KEYCODE_HEADSETHOOK));
sGestureLongPressed = new Runnable() {
public void run() {
sAudioManager.dispatchMediaKeyEvent(new KeyEvent(KeyEvent.ACTION_UP,
KeyEvent.KEYCODE_HEADSETHOOK));
}
};
// Schedule keyup event after longpress timeout.

//ideally use ViewConfiguration.get(this).getLongPressTimeout(), but for now will set 1000
S_HANDLER.postDelayed(sGestureLongPressed, 1000);
private static void simulateTriplePress() {

Log.i(APP_TAG, "hcp simulated long press");
}

/* Broadcast a togglepause intent */
Expand Down Expand Up @@ -242,7 +228,7 @@ private static void muteVolume() {
@Override
protected void onServiceConnected() {
sAudioManager = (AudioManager) getSystemService(AUDIO_SERVICE);
sActionsDefault = getString(R.string.pref_button_actions_default);
// sActionsDefault = getString(R.string.pref_button_actions_default);
sActionsPlayPause = getString(R.string.pref_button_actions_playpause);
sActionsNext = getString(R.string.pref_button_actions_next);
sActionsPrevious = getString(R.string.pref_button_actions_previous);
Expand Down Expand Up @@ -272,9 +258,7 @@ public boolean onKeyEvent(final KeyEvent event) {
return false;
}

if (keycode != KeyEvent.KEYCODE_HEADSETHOOK
&& keycode != KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE
&& keycode != KeyEvent.KEYCODE_MEDIA_PLAY) {
if (ServiceBase.isSupportedKey(keycode)) {
// Not interested in any other keys
Log.i(APP_TAG, "Ignored " + keycode);
return false;
Expand All @@ -290,72 +274,77 @@ public boolean onKeyEvent(final KeyEvent event) {

Log.d(APP_TAG, ("Broadcast Key " + keycode));
Intent intent = new Intent(getPackageName());
intent.putExtra("pressed", keycode);
sendBroadcast(intent);


final String singlePressAction = pref.getString("hcp_gestures_single_press",
sActionsPlayPause);
final String doublePressAction = pref.getString("hcp_gestures_double_press",
sActionsNext);
final String longPressAction = pref.getString("hcp_gestures_long_press",
final String triplePressAction = pref.getString("hcp_gestures_triple_press",
sActionsPrevious);

// Long Press.
if (action == KeyEvent.ACTION_DOWN) {
sGestureLongPressed = new Runnable() {
public void run() {
mGestureMode = "long_press";
Log.i(APP_TAG, "Exec Long Press Action");
if (longPressAction.equals(sActionsDefault)) {
simulateLongPress();
} else {
execAction(longPressAction);
}
}
};
// Start tracking long press. If no action up is detected after 1100ms,
// consider ut as long press.
S_HANDLER.postDelayed(sGestureLongPressed, 1100);
}

// Single and Double Click.
// Determin Single, Double or Click.
if (action == KeyEvent.ACTION_UP) {
sKeyDownCount++;
S_HANDLER.removeCallbacks(sGestureLongPressed);
sGestureSinglePressed = new Runnable() {
public void run() {
// Single press.
if (sKeyDownCount == 1) {
// Check if this keyup event is not following a long press event.
if (mGestureMode != "long_press") {
Log.i(APP_TAG, "Exec Single Press Action");
sKeyUpCount++;

// Single press.
if (sKeyUpCount == 1) {
sGestureSinglePressed = new Runnable() {
public void run() {
if (sKeyUpCount == 1) {
sKeyUpCount = 0;
Log.d(APP_TAG, "Exec Single Press Action");
if (singlePressAction.equals(sActionsDefault)) {
// Simulate the original event.
simulateSinglePress();
} else {
execAction(singlePressAction);
}
mGestureMode = "unknown";
}
mGestureMode = "unknown";
}
// Double press.
if (sKeyDownCount == 2) {
if (mGestureMode != "long_press") {
Log.i(APP_TAG, "Exec Double Press Action");
};
S_HANDLER.postDelayed(sGestureSinglePressed, 500);
}

// Double press.
if (sKeyUpCount == 2) {
S_HANDLER.removeCallbacks(sGestureSinglePressed);
sGestureDoublePressed = new Runnable() {
public void run() {
if (sKeyUpCount == 2) {
sKeyUpCount = 0;
Log.d(APP_TAG, "Exec Double Press Action");
if (doublePressAction.equals(sActionsDefault)) {
// Simulate the original event.
simulateDoublePress();
} else {
execAction(doublePressAction);
}
mGestureMode = "unknown";
}
mGestureMode = "unknown";

}
};
S_HANDLER.postDelayed(sGestureDoublePressed, 500);
}

// Triple press.
if (sKeyUpCount == 3) {
S_HANDLER.removeCallbacks(sGestureDoublePressed);
if (sKeyUpCount == 3) {
sKeyUpCount = 0;
Log.d(APP_TAG, "Exec Triple Press Action");
if (doublePressAction.equals(sActionsDefault)) {
// Simulate the original event.
simulateTriplePress();
} else {
execAction(triplePressAction);
}
sKeyDownCount = 0;
mGestureMode = "unknown";
}
};
if (sKeyDownCount == 1) {
S_HANDLER.postDelayed(sGestureSinglePressed, 400);
}
}
return true;
Expand Down
Loading

0 comments on commit eaa7517

Please sign in to comment.