Skip to content
This repository was archived by the owner on Jan 1, 2023. It is now read-only.

Commit 90f18a4

Browse files
Record tap pattern and test
1 parent 43272e6 commit 90f18a4

File tree

2 files changed

+57
-3
lines changed

2 files changed

+57
-3
lines changed

mobile/src/main/java/org/codechimp/ttw/PluginActivity.java

Lines changed: 56 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,19 @@
44
import android.content.Context;
55
import android.content.Intent;
66
import android.os.Bundle;
7+
import android.os.Vibrator;
78
import android.support.v7.widget.Toolbar;
9+
import android.text.TextUtils;
10+
import android.view.MotionEvent;
811
import android.view.View;
912
import android.widget.AdapterView;
1013
import android.widget.ArrayAdapter;
1114
import android.widget.Button;
15+
import android.widget.ImageButton;
1216
import android.widget.ListView;
1317

18+
import java.util.ArrayList;
19+
1420
/**
1521
* This is the "Edit" activity for a Locale Plug-in.
1622
* <p/>
@@ -38,6 +44,8 @@ public final class PluginActivity extends AbstractPluginActivity { //implements
3844
private String patternValue;
3945

4046
final Context context = this;
47+
private long lastTapStart = 0;
48+
ArrayList<Long> taps = new ArrayList<>();
4149

4250
@Override
4351
protected void onCreate(final Bundle savedInstanceState) {
@@ -58,6 +66,8 @@ protected void onCreate(final Bundle savedInstanceState) {
5866
getSupportActionBar().setHomeAsUpIndicator(R.drawable.ic_action_discard);
5967
}
6068

69+
ResetTapPattern();
70+
6171
patternsListView = (ListView) findViewById(R.id.patternsListView);
6272
buttonCustom = (Button) findViewById(R.id.customButton);
6373

@@ -83,15 +93,41 @@ public void onClick(View v) {
8393
dialog.setContentView(R.layout.dialog_custom);
8494
dialog.setTitle(R.string.vibrate_pattern);
8595

96+
ImageButton dialogButtonRevert = (ImageButton) dialog.findViewById(R.id.dialogButtonRevert);
8697
Button dialogButtonSave = (Button) dialog.findViewById(R.id.dialogButtonSave);
8798
Button dialogButtonCancel = (Button) dialog.findViewById(R.id.dialogButtonCancel);
8899
Button dialogButtonTry = (Button) dialog.findViewById(R.id.dialogButtonTry);
100+
Button dialogButtonTap = (Button) dialog.findViewById(R.id.dialogButtonTap);
101+
102+
dialogButtonTap.setOnTouchListener(new ImageButton.OnTouchListener() {
103+
@Override
104+
public boolean onTouch(View v, MotionEvent event) {
105+
if (event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_DOWN) {
106+
Long now = System.currentTimeMillis();
107+
if (lastTapStart > 0)
108+
taps.add(now - lastTapStart);
109+
110+
lastTapStart = now;
111+
}
112+
113+
return false;
114+
}
115+
});
116+
117+
dialogButtonRevert.setOnClickListener(new View.OnClickListener() {
118+
@Override
119+
public void onClick(View v) {
120+
ResetTapPattern();
121+
}
122+
});
89123

90124
dialogButtonSave.setOnClickListener(new View.OnClickListener() {
91125
@Override
92126
public void onClick(View v) {
93-
//TODO - save the pattern
127+
patternName = getString(R.string.custom);
128+
patternValue = TextUtils.join(",", taps);
94129
dialog.dismiss();
130+
finish();
95131
}
96132
});
97133

@@ -105,7 +141,16 @@ public void onClick(View v) {
105141
dialogButtonTry.setOnClickListener(new View.OnClickListener() {
106142
@Override
107143
public void onClick(View v) {
108-
// Do nothing
144+
if (taps.size() < 2) return;
145+
Vibrator vibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);
146+
147+
//Break into an array of longs
148+
long[] patternLongs = new long[taps.size()];
149+
for (int i = 0; i < taps.size(); i++) {
150+
patternLongs[i] = taps.get(i);
151+
}
152+
153+
vibrator.vibrate(patternLongs, -1);
109154
}
110155
});
111156

@@ -114,6 +159,15 @@ public void onClick(View v) {
114159
});
115160
}
116161

162+
163+
/**
164+
* Clears and adds a starting 0 tap required for vibration patterns
165+
*/
166+
private void ResetTapPattern() {
167+
taps.clear();
168+
taps.add(0l);
169+
}
170+
117171
@Override
118172
public void finish() {
119173
if (!isCanceled()) {

mobile/src/main/res/layout/dialog_custom.xml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,8 +62,8 @@
6262
android:layout_height="fill_parent"
6363
android:background="?android:attr/selectableItemBackground"
6464
android:text="@string/tap_pattern"
65-
android:textColor="@color/SecondaryText"
6665
android:textAllCaps="false"
66+
android:textColor="@color/SecondaryText"
6767
android:textSize="32sp" />
6868
</LinearLayout>
6969

0 commit comments

Comments
 (0)