4
4
import android .content .Context ;
5
5
import android .content .Intent ;
6
6
import android .os .Bundle ;
7
+ import android .os .Vibrator ;
7
8
import android .support .v7 .widget .Toolbar ;
9
+ import android .text .TextUtils ;
10
+ import android .view .MotionEvent ;
8
11
import android .view .View ;
9
12
import android .widget .AdapterView ;
10
13
import android .widget .ArrayAdapter ;
11
14
import android .widget .Button ;
15
+ import android .widget .ImageButton ;
12
16
import android .widget .ListView ;
13
17
18
+ import java .util .ArrayList ;
19
+
14
20
/**
15
21
* This is the "Edit" activity for a Locale Plug-in.
16
22
* <p/>
@@ -38,6 +44,8 @@ public final class PluginActivity extends AbstractPluginActivity { //implements
38
44
private String patternValue ;
39
45
40
46
final Context context = this ;
47
+ private long lastTapStart = 0 ;
48
+ ArrayList <Long > taps = new ArrayList <>();
41
49
42
50
@ Override
43
51
protected void onCreate (final Bundle savedInstanceState ) {
@@ -58,6 +66,8 @@ protected void onCreate(final Bundle savedInstanceState) {
58
66
getSupportActionBar ().setHomeAsUpIndicator (R .drawable .ic_action_discard );
59
67
}
60
68
69
+ ResetTapPattern ();
70
+
61
71
patternsListView = (ListView ) findViewById (R .id .patternsListView );
62
72
buttonCustom = (Button ) findViewById (R .id .customButton );
63
73
@@ -83,15 +93,41 @@ public void onClick(View v) {
83
93
dialog .setContentView (R .layout .dialog_custom );
84
94
dialog .setTitle (R .string .vibrate_pattern );
85
95
96
+ ImageButton dialogButtonRevert = (ImageButton ) dialog .findViewById (R .id .dialogButtonRevert );
86
97
Button dialogButtonSave = (Button ) dialog .findViewById (R .id .dialogButtonSave );
87
98
Button dialogButtonCancel = (Button ) dialog .findViewById (R .id .dialogButtonCancel );
88
99
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
+ });
89
123
90
124
dialogButtonSave .setOnClickListener (new View .OnClickListener () {
91
125
@ Override
92
126
public void onClick (View v ) {
93
- //TODO - save the pattern
127
+ patternName = getString (R .string .custom );
128
+ patternValue = TextUtils .join ("," , taps );
94
129
dialog .dismiss ();
130
+ finish ();
95
131
}
96
132
});
97
133
@@ -105,7 +141,16 @@ public void onClick(View v) {
105
141
dialogButtonTry .setOnClickListener (new View .OnClickListener () {
106
142
@ Override
107
143
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 );
109
154
}
110
155
});
111
156
@@ -114,6 +159,15 @@ public void onClick(View v) {
114
159
});
115
160
}
116
161
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
+
117
171
@ Override
118
172
public void finish () {
119
173
if (!isCanceled ()) {
0 commit comments