Skip to content

Commit

Permalink
Fixed issue with unexpected key learning after timeout
Browse files Browse the repository at this point in the history
Extracted switch-direction dependent classes to separated packages
Extracted framework classes to separated package
Introduced KeyCodesStorage to connect array adapter and preferences
  • Loading branch information
bezloginu committed Jul 29, 2016
1 parent 1c105b6 commit aad19c5
Show file tree
Hide file tree
Showing 19 changed files with 199 additions and 116 deletions.
4 changes: 2 additions & 2 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -27,8 +27,8 @@
</intent-filter>
</receiver>

<activity android:name=".KeyCodesForNextLearningActivity" />
<activity android:name=".KeyCodesForPreviousLearningActivity"></activity>
<activity android:name=".next.KeyCodesForNextLearningActivity" />
<activity android:name=".previous.KeyCodesForPreviousLearningActivity"></activity>
</application>

</manifest>
26 changes: 14 additions & 12 deletions app/src/main/java/spotifykeys/mtcn/com/spotifykeys/KeysService.java
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,8 @@
import android.os.Messenger;
import android.os.RemoteException;

import spotifykeys.mtcn.com.spotifykeys.preferences.KeyCodesForNext;
import spotifykeys.mtcn.com.spotifykeys.preferences.KeyCodesForPrevious;
import spotifykeys.mtcn.com.spotifykeys.preferences.SwitchTrackWhenPaused;
import spotifykeys.mtcn.com.spotifykeys.next.KeyCodesForNextPreference;
import spotifykeys.mtcn.com.spotifykeys.previous.KeyCodesForPreviousPreference;

/**
* Created by COMPUTER on 2016-07-26.
Expand All @@ -20,10 +19,10 @@ public class KeysService extends android.app.Service {
@Override
public void onCreate() {
SharedPreferences sharedPreferences = getSharedPreferences(MainActivity.APP_NAME, Context.MODE_PRIVATE);
mSwitchTrackWhenPaused = new SwitchTrackWhenPaused(sharedPreferences);
mKeyCodesForNext = new KeyCodesForNext(sharedPreferences);
mKeyCodesForPrevious = new KeyCodesForPrevious(sharedPreferences);
mSpotifyProxy = new SpotifyProxy(this, mSwitchTrackWhenPaused);
mSwitchTrackWhenPausedPreference = new SwitchTrackWhenPausedPreference(sharedPreferences);
mKeyCodesForNextPreference = new KeyCodesForNextPreference(sharedPreferences);
mKeyCodesForPreviousPreference = new KeyCodesForPreviousPreference(sharedPreferences);
mSpotifyProxy = new SpotifyProxy(this, mSwitchTrackWhenPausedPreference);
mKeyEventsHandler = new KeyEventsHandler(this, mKeyEventsHandlerListener);

mKeyEventsHandler.subscribe();
Expand Down Expand Up @@ -56,6 +55,9 @@ public void handleMessage(Message msg) {
case MessageIds.OBTAIN_KEY_CODE:
mKeyCodeObtainerMessenger = msg.replyTo;
break;
case MessageIds.OBTAIN_KEY_CODE_ABORTED:
mKeyCodeObtainerMessenger = null;
break;
}
}
}
Expand All @@ -68,9 +70,9 @@ public void onKeyPressed(int keyCode) {
if(mKeyCodeObtainerMessenger != null) {
sendKeyCode(keyCode);
mKeyCodeObtainerMessenger = null;
} else if (mKeyCodesForNext.get().contains(keyCodeString)) {
} else if (mKeyCodesForNextPreference.get().contains(keyCodeString)) {
mSpotifyProxy.nextTrack();
} else if (mKeyCodesForPrevious.get().contains(keyCodeString)) {
} else if (mKeyCodesForPreviousPreference.get().contains(keyCodeString)) {
mSpotifyProxy.previousTrack();
}
}
Expand All @@ -88,9 +90,9 @@ private void sendKeyCode(int keyCode) {
}
};

private SwitchTrackWhenPaused mSwitchTrackWhenPaused = null;
private KeyCodesForNext mKeyCodesForNext = null;
private KeyCodesForPrevious mKeyCodesForPrevious = null;
private SwitchTrackWhenPausedPreference mSwitchTrackWhenPausedPreference = null;
private KeyCodesForNextPreference mKeyCodesForNextPreference = null;
private KeyCodesForPreviousPreference mKeyCodesForPreviousPreference = null;
private SpotifyProxy mSpotifyProxy = null;
private KeyEventsHandler mKeyEventsHandler = null;
private final Messenger mMessenger = new Messenger(new MessageHandler());
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,18 +9,19 @@
import android.widget.Switch;
import android.widget.Toast;

import spotifykeys.mtcn.com.spotifykeys.preferences.CommitFailedException;
import spotifykeys.mtcn.com.spotifykeys.preferences.SwitchTrackWhenPaused;
import spotifykeys.mtcn.com.spotifykeys.framework.preferences.CommitFailedException;
import spotifykeys.mtcn.com.spotifykeys.next.KeyCodesForNextLearningActivity;
import spotifykeys.mtcn.com.spotifykeys.previous.KeyCodesForPreviousLearningActivity;

public class MainActivity extends AppCompatActivity {
public static final String APP_NAME = "SpotifyKeysPreferences";
public static final String APP_NAME = "SpotifyKeys";

@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

mSwitchTrackWhenPaused = new SwitchTrackWhenPaused(getSharedPreferences(APP_NAME, Context.MODE_PRIVATE));
mSwitchTrackWhenPausedPreference = new SwitchTrackWhenPausedPreference(getSharedPreferences(APP_NAME, Context.MODE_PRIVATE));

mLearnKeyCodeForPreviousButton = (Button)this.findViewById(R.id.buttonLearnPrevious);
mLearnKeyCodeForPreviousButton.setOnClickListener(new LearnKeyCodeForPreviousButtonClickListener());
Expand All @@ -30,7 +31,7 @@ protected void onCreate(Bundle savedInstanceState) {

mSwitchTrackWhenPausedSwitch = (Switch)this.findViewById(R.id.switchSwitchTrackWhenPaused);
mSwitchTrackWhenPausedSwitch.setOnClickListener(new SwitchTrackWhenPausedSwitchClickListener());
mSwitchTrackWhenPausedSwitch.setChecked(mSwitchTrackWhenPaused.get());
mSwitchTrackWhenPausedSwitch.setChecked(mSwitchTrackWhenPausedPreference.get());

this.startService(new Intent(this, KeysService.class));
}
Expand All @@ -53,8 +54,8 @@ class SwitchTrackWhenPausedSwitchClickListener implements Switch.OnClickListener
@Override
public void onClick(View view) {
try {
boolean state = mSwitchTrackWhenPaused.get();
mSwitchTrackWhenPaused.set(!state);
boolean state = mSwitchTrackWhenPausedPreference.get();
mSwitchTrackWhenPausedPreference.set(!state);
} catch (CommitFailedException e) {
e.printStackTrace();

Expand All @@ -64,7 +65,7 @@ public void onClick(View view) {
}
}

private SwitchTrackWhenPaused mSwitchTrackWhenPaused = null;
private SwitchTrackWhenPausedPreference mSwitchTrackWhenPausedPreference = null;
private Button mLearnKeyCodeForPreviousButton = null;
private Button mLearnKeyCodeForNextButton = null;
private Switch mSwitchTrackWhenPausedSwitch = null;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,5 @@
public class MessageIds {
public static final int OBTAIN_KEY_CODE = 0x100001;
public static final int KEY_CODE = 0x100002;
public static final int OBTAIN_KEY_CODE_ABORTED = 0x100003;
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,13 @@
import android.content.Intent;
import android.content.IntentFilter;

import spotifykeys.mtcn.com.spotifykeys.preferences.SwitchTrackWhenPaused;

/**
* Created by COMPUTER on 2016-07-27.
*/
public class SpotifyProxy extends BroadcastReceiver {
SpotifyProxy(Context context, SwitchTrackWhenPaused switchTrackWhenPaused) {
SpotifyProxy(Context context, SwitchTrackWhenPausedPreference switchTrackWhenPausedPreference) {
mContext = context;
mSwitchTrackWhenPaused = switchTrackWhenPaused;
mSwitchTrackWhenPausedPreference = switchTrackWhenPausedPreference;
mIsPlaybackActive = false;
}

Expand All @@ -33,13 +31,13 @@ public void unsubscribe() {
}

public void nextTrack() {
if(mIsPlaybackActive || mSwitchTrackWhenPaused.get()) {
if(mIsPlaybackActive || mSwitchTrackWhenPausedPreference.get()) {
this.sendCommand(NEXT_TRACK_COMMAND);
}
}

public void previousTrack() {
if(mIsPlaybackActive || mSwitchTrackWhenPaused.get()) {
if(mIsPlaybackActive || mSwitchTrackWhenPausedPreference.get()) {
this.sendCommand(PREVIOUS_TRACK_COMMAND);
}
}
Expand All @@ -51,7 +49,7 @@ private void sendCommand(String action) {
}

private final Context mContext;
private final SwitchTrackWhenPaused mSwitchTrackWhenPaused;
private final SwitchTrackWhenPausedPreference mSwitchTrackWhenPausedPreference;
private boolean mIsPlaybackActive;

private static final String PLAYBACK_STATE_PARAM_NAME = "playing";
Expand Down
Original file line number Diff line number Diff line change
@@ -1,12 +1,14 @@
package spotifykeys.mtcn.com.spotifykeys.preferences;
package spotifykeys.mtcn.com.spotifykeys;

import android.content.SharedPreferences;

import spotifykeys.mtcn.com.spotifykeys.framework.preferences.Preference;

/**
* Created by COMPUTER on 2016-07-28.
*/
public class SwitchTrackWhenPaused extends Preference<Boolean> {
public SwitchTrackWhenPaused(SharedPreferences sharedPreferences) {
public class SwitchTrackWhenPausedPreference extends Preference<Boolean> {
public SwitchTrackWhenPausedPreference(SharedPreferences sharedPreferences) {
super(sharedPreferences);
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package spotifykeys.mtcn.com.spotifykeys;
package spotifykeys.mtcn.com.spotifykeys.framework;

import android.app.ProgressDialog;
import android.content.ComponentName;
Expand All @@ -15,25 +15,22 @@
import android.support.v7.app.AppCompatActivity;
import android.view.View;
import android.widget.AdapterView;
import android.widget.ArrayAdapter;
import android.widget.Button;
import android.widget.ListView;
import android.widget.Toast;

import java.util.ArrayList;

import spotifykeys.mtcn.com.spotifykeys.preferences.CommitFailedException;
import spotifykeys.mtcn.com.spotifykeys.preferences.KeyCodes;
import spotifykeys.mtcn.com.spotifykeys.KeysService;
import spotifykeys.mtcn.com.spotifykeys.MessageIds;

/**
* Created by COMPUTER on 2016-07-28.
*/
abstract class KeyCodesLearningActivity extends AppCompatActivity {
public abstract class KeyCodesLearningActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

mKeyCodes = createKeyCodesStorage();
mKeyCodesStorage = createKeyCodesStorage();

mLearningProgressDialog = new ProgressDialog(this);
mLearningProgressDialog.setTitle("Waiting for key press");
Expand All @@ -60,46 +57,16 @@ protected void initLearnKeyCodeButton(int id) {
protected void initKeyCodesListView(int id) {
mKeyCodesListView = (ListView)this.findViewById(id);
mKeyCodesListView.setOnItemLongClickListener(new LearntKeyCodeClickListener());
mKeyCodesAdapter = new ArrayAdapter<>(this, android.R.layout.simple_list_item_1, new ArrayList<>(mKeyCodes.get()));
mKeyCodesListView.setAdapter(mKeyCodesAdapter);
}

abstract KeyCodes createKeyCodesStorage();

private void startKeyCodeLearning() {
mLearnKeyCodeButton.setClickable(false);

try {
Message msg = new Message();
msg.what = MessageIds.OBTAIN_KEY_CODE;
msg.replyTo = mMessenger;
mServiceMessenger.send(msg);

mLearningTimer.start();
mLearningProgressDialog.show();
} catch (RemoteException e) {
e.printStackTrace();
finishKeyCodeLearning("Failed to start learning procedure");
}
mKeyCodesListView.setAdapter(mKeyCodesStorage.getAdapter());
}

private void learnKeyCode(int keyCode) {
String keyCodeString = Integer.toString(keyCode);

if(!mKeyCodes.get().contains(keyCodeString)) {
mKeyCodesAdapter.add(keyCodeString);
protected abstract KeyCodesStorage createKeyCodesStorage();

try {
mKeyCodes.insert(keyCode);
finishKeyCodeLearning("Key has been learnt");
} catch (CommitFailedException e) {
e.printStackTrace();
mKeyCodesAdapter.remove(keyCodeString);
finishKeyCodeLearning("Unable to save the key");
}
} else {
finishKeyCodeLearning("Key has been already stored");
}
private void sendMessage(int messageId) throws RemoteException {
Message msg = new Message();
msg.what = messageId;
msg.replyTo = mMessenger;
mServiceMessenger.send(msg);
}

private void finishKeyCodeLearning(String finishText) {
Expand Down Expand Up @@ -127,7 +94,8 @@ private class MessageHandler extends Handler {
public void handleMessage(Message msg) {
switch (msg.what) {
case MessageIds.KEY_CODE:
KeyCodesLearningActivity.this.learnKeyCode(msg.arg1);
String finishText = mKeyCodesStorage.store(msg.arg1) ? "Key code has been learnt" : "Key code learning failed";
finishKeyCodeLearning(finishText);
break;
}
}
Expand All @@ -136,22 +104,24 @@ public void handleMessage(Message msg) {
private class LearnKeyCodeButtonClickListener implements Button.OnClickListener {
@Override
public void onClick(View view) {
startKeyCodeLearning();
mLearnKeyCodeButton.setClickable(false);

try {
sendMessage(MessageIds.OBTAIN_KEY_CODE);
mLearningTimer.start();
mLearningProgressDialog.show();
} catch (RemoteException e) {
e.printStackTrace();
finishKeyCodeLearning("Failed to start learning procedure");
}
}
}

private class LearntKeyCodeClickListener implements ListView.OnItemLongClickListener {
@Override
public boolean onItemLongClick(AdapterView<?> adapterView, View view, int position, long id) {
String keyCode = mKeyCodesAdapter.getItem(position);

try {
mKeyCodes.remove(Integer.parseInt(keyCode));
mKeyCodesAdapter.remove(keyCode);
} catch (CommitFailedException e) {
e.printStackTrace();

Toast toast = Toast.makeText(KeyCodesLearningActivity.this, "Unable to remove learnt key", Toast.LENGTH_LONG);
if(!mKeyCodesStorage.remove(position)) {
Toast toast = Toast.makeText(KeyCodesLearningActivity.this, "Unable to remove the key", Toast.LENGTH_LONG);
toast.show();
}

Expand All @@ -167,21 +137,26 @@ public void onTick(long l) {

@Override
public void onFinish() {
try {
sendMessage(MessageIds.OBTAIN_KEY_CODE_ABORTED);
} catch (RemoteException e) {
e.printStackTrace();
}

KeyCodesLearningActivity.this.finishKeyCodeLearning("Key learning timed out");
}
};

private KeyCodes mKeyCodes = null;
private KeyCodesStorage mKeyCodesStorage = null;

private ProgressDialog mLearningProgressDialog = null;
private Button mLearnKeyCodeButton = null;
private ListView mKeyCodesListView = null;
private ArrayAdapter<String> mKeyCodesAdapter = null;

private KeysListenerServiceConnection mServiceConnection = null;
private Messenger mServiceMessenger = null;
private final Messenger mMessenger = new Messenger(new MessageHandler());

private static final long LEARN_TIMER_DURATION_MS = 5000;
private static final long LEARN_TIMER_STEP_MS = 1000;
};
}
Loading

0 comments on commit aad19c5

Please sign in to comment.