Skip to content

Commit 0f854f0

Browse files
committed
Make parameters/variables final where possible
1 parent 1f1914d commit 0f854f0

5 files changed

Lines changed: 95 additions & 85 deletions

File tree

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,7 @@
1010
- Reduced digit row key height
1111
- Added fillColour attribute for the keyboard as a whole
1212
- Lightened key fill and text colours slightly
13+
- Made parameters/variables final where possible
1314

1415

1516
## [v0.3.0] Basic keyboard behaviour (2021-07-08)

app/src/main/java/io/github/yawnoc/strokeinput/InputContainer.java

Lines changed: 49 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ public class InputContainer
6969
private final Paint keyBorderPaint;
7070
private final Paint keyTextPaint;
7171

72-
public InputContainer(Context context, AttributeSet attributes) {
72+
public InputContainer(final Context context, final AttributeSet attributes) {
7373

7474
super(context, attributes);
7575

@@ -126,27 +126,30 @@ public interface OnInputListener {
126126
void onLongPress(String valueText);
127127
}
128128

129-
public void setOnInputListener(OnInputListener listener) {
129+
public void setOnInputListener(final OnInputListener listener) {
130130
inputListener = listener;
131131
}
132132

133-
public void setKeyboard(Keyboard keyboard) {
133+
public void setKeyboard(final Keyboard keyboard) {
134134
this.keyboard = keyboard;
135135
keyArray = keyboard.getKeyList().toArray(new Keyboard.Key[0]);
136136
requestLayout();
137137
}
138138

139-
public void onClick(View view) {
139+
public void onClick(final View view) {
140140
}
141141

142142
@Override
143-
public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
144-
145-
int paddingHorizontal = getPaddingLeft() + getPaddingRight();
146-
int paddingVertical = getPaddingTop() + getPaddingBottom();
143+
public void onMeasure(
144+
final int widthMeasureSpec,
145+
final int heightMeasureSpec
146+
)
147+
{
148+
final int paddingHorizontal = getPaddingLeft() + getPaddingRight();
149+
final int paddingVertical = getPaddingTop() + getPaddingBottom();
147150

148-
int keyboardWidth;
149-
int keyboardHeight;
151+
final int keyboardWidth;
152+
final int keyboardHeight;
150153
if (keyboard == null) {
151154
keyboardWidth = 0;
152155
keyboardHeight = 0;
@@ -163,7 +166,7 @@ public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
163166
}
164167

165168
@Override
166-
public void onDraw(Canvas canvas) {
169+
public void onDraw(final Canvas canvas) {
167170

168171
if (keyboard == null) {
169172
return;
@@ -187,11 +190,11 @@ public void onDraw(Canvas canvas) {
187190
keyTextPaint.setColor(key.keyTextColour);
188191
keyTextPaint.setTextSize(key.keyTextSize);
189192

190-
float key_text_x = (
193+
final float key_text_x = (
191194
key.width / 2f
192195
+ key.keyTextOffsetX
193196
);
194-
float key_text_y = (
197+
final float key_text_y = (
195198
(key.height - keyTextPaint.ascent() - keyTextPaint.descent()) / 2f
196199
+ key.keyTextOffsetY
197200
);
@@ -216,9 +219,9 @@ public void onDraw(Canvas canvas) {
216219
Lighten a dark colour and darken a light colour.
217220
Used for key press colour changes.
218221
*/
219-
private static int getContrastingColour(int colour) {
222+
private static int getContrastingColour(final int colour) {
220223

221-
float[] colourHSL = new float[3];
224+
final float[] colourHSL = new float[3];
222225
ColorUtils.colorToHSL(colour, colourHSL);
223226

224227
float colourLightness = colourHSL[2];
@@ -240,21 +243,21 @@ Handle logic for multiple pointers (e.g. two-thumb typing).
240243
*/
241244
@SuppressLint("ClickableViewAccessibility")
242245
@Override
243-
public boolean onTouchEvent(MotionEvent motionEvent) {
246+
public boolean onTouchEvent(final MotionEvent motionEvent) {
244247

245248
if (motionEvent.getPointerCount() > 2) {
246249
abortAllKeyBehaviour();
247250
return true;
248251
}
249252

250-
long eventTime = motionEvent.getEventTime();
251-
int eventAction = motionEvent.getActionMasked();
252-
int eventActionIndex = motionEvent.getActionIndex();
253-
int eventPointerId = motionEvent.getPointerId(eventActionIndex);
254-
int eventPointerIndex = motionEvent.findPointerIndex(eventPointerId);
255-
int eventPointerX = (int) motionEvent.getX(eventPointerIndex);
256-
int eventPointerY = (int) motionEvent.getY(eventPointerIndex);
257-
int eventMetaState = motionEvent.getMetaState();
253+
final long eventTime = motionEvent.getEventTime();
254+
final int eventAction = motionEvent.getActionMasked();
255+
final int eventActionIndex = motionEvent.getActionIndex();
256+
final int eventPointerId = motionEvent.getPointerId(eventActionIndex);
257+
final int eventPointerIndex = motionEvent.findPointerIndex(eventPointerId);
258+
final int eventPointerX = (int) motionEvent.getX(eventPointerIndex);
259+
final int eventPointerY = (int) motionEvent.getY(eventPointerIndex);
260+
final int eventMetaState = motionEvent.getMetaState();
258261

259262
boolean eventHandled = true;
260263

@@ -337,32 +340,32 @@ public boolean onTouchEvent(MotionEvent motionEvent) {
337340
}
338341

339342
private boolean sendSinglePointerMotionEvent(
340-
long time,
341-
int action,
342-
int x,
343-
int y,
344-
int metaState
343+
final long time,
344+
final int action,
345+
final int x,
346+
final int y,
347+
final int metaState
345348
)
346349
{
347-
MotionEvent sentEvent =
350+
final MotionEvent sentEvent =
348351
MotionEvent.obtain(time, time, action, x, y, metaState);
349352

350353
return onSinglePointerTouchEvent(sentEvent);
351354
}
352355

353-
private boolean onSinglePointerTouchEvent(MotionEvent motionEvent) {
356+
private boolean onSinglePointerTouchEvent(final MotionEvent motionEvent) {
354357

355-
int eventX = (int) motionEvent.getX() - getPaddingLeft();
356-
int eventY = (int) motionEvent.getY() - getPaddingTop();
358+
final int eventX = (int) motionEvent.getX() - getPaddingLeft();
359+
final int eventY = (int) motionEvent.getY() - getPaddingTop();
357360

358-
Keyboard.Key key = getKeyAtPoint(eventX, eventY);
361+
final Keyboard.Key key = getKeyAtPoint(eventX, eventY);
359362
if (key == null) {
360363
abortAllKeyBehaviour();
361364
return true;
362365
}
363-
String valueText = key.valueText;
366+
final String valueText = key.valueText;
364367

365-
int eventAction = motionEvent.getAction();
368+
final int eventAction = motionEvent.getAction();
366369

367370
switch (eventAction) {
368371

@@ -387,9 +390,9 @@ private boolean onSinglePointerTouchEvent(MotionEvent motionEvent) {
387390
return true;
388391
}
389392

390-
private Keyboard.Key getKeyAtPoint(int x, int y) {
393+
private Keyboard.Key getKeyAtPoint(final int x, final int y) {
391394

392-
for (Keyboard.Key key : keyArray) {
395+
for (final Keyboard.Key key : keyArray) {
393396
if (key.containsPoint(x, y)) {
394397
return key;
395398
}
@@ -398,12 +401,15 @@ private Keyboard.Key getKeyAtPoint(int x, int y) {
398401
return null;
399402
}
400403

401-
private void setCurrentlyPressedKey(Keyboard.Key key) {
404+
private void setCurrentlyPressedKey(final Keyboard.Key key) {
402405
currentlyPressedKey = key;
403406
invalidate();
404407
}
405408

406-
private void sendAppropriateExtendedPressHandlerMessage(Keyboard.Key key) {
409+
private void sendAppropriateExtendedPressHandlerMessage(
410+
final Keyboard.Key key
411+
)
412+
{
407413
if (key.isRepeatable) {
408414
sendExtendedPressHandlerMessage(
409415
MESSAGE_KEY_REPEAT,
@@ -419,8 +425,8 @@ else if (key.isLongPressable) {
419425
}
420426

421427
private void sendExtendedPressHandlerMessage(
422-
int messageWhat,
423-
long delayMilliseconds
428+
final int messageWhat,
429+
final long delayMilliseconds
424430
)
425431
{
426432
extendedPressHandler.sendMessageDelayed(
@@ -434,7 +440,7 @@ private void removeAllExtendedPressHandlerMessages() {
434440
removeExtendedPressHandlerMessages(MESSAGE_LONG_PRESS);
435441
}
436442

437-
private void removeExtendedPressHandlerMessages(int messageWhat) {
443+
private void removeExtendedPressHandlerMessages(final int messageWhat) {
438444
extendedPressHandler.removeMessages(messageWhat);
439445
}
440446

app/src/main/java/io/github/yawnoc/strokeinput/Keyboard.java

Lines changed: 27 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -66,9 +66,10 @@ public class Keyboard {
6666
private final int screenWidth;
6767
private final int screenHeight;
6868

69-
public Keyboard(Context context, int layoutResourceId) {
69+
public Keyboard(final Context context, final int layoutResourceId) {
7070

71-
DisplayMetrics displayMetrics = context.getResources().getDisplayMetrics();
71+
final DisplayMetrics displayMetrics =
72+
context.getResources().getDisplayMetrics();
7273

7374
screenWidth = displayMetrics.widthPixels;
7475
screenHeight = displayMetrics.heightPixels;
@@ -109,14 +110,14 @@ public static class Row {
109110
private final Keyboard parentKeyboard;
110111

111112
public Row(
112-
Keyboard parentKeyboard,
113-
Resources resources,
114-
XmlResourceParser xmlResourceParser
113+
final Keyboard parentKeyboard,
114+
final Resources resources,
115+
final XmlResourceParser xmlResourceParser
115116
)
116117
{
117118
this.parentKeyboard = parentKeyboard;
118119

119-
TypedArray attributesArray =
120+
final TypedArray attributesArray =
120121
resources.obtainAttributes(
121122
Xml.asAttributeSet(xmlResourceParser),
122123
R.styleable.Keyboard
@@ -211,26 +212,26 @@ public static class Key {
211212
// Key meta-properties
212213
private final Keyboard grandparentKeyboard;
213214

214-
public Key(Row parentRow) {
215+
public Key(final Row parentRow) {
215216
grandparentKeyboard = parentRow.parentKeyboard;
216217
width = parentRow.keyWidth;
217218
height = parentRow.keyHeight;
218219
}
219220

220221
public Key(
221-
Row parentRow,
222-
int x,
223-
int y,
224-
Resources resources,
225-
XmlResourceParser xmlResourceParser
222+
final Row parentRow,
223+
final int x,
224+
final int y,
225+
final Resources resources,
226+
final XmlResourceParser xmlResourceParser
226227
)
227228
{
228229
this(parentRow);
229230

230231
this.x = x;
231232
this.y = y;
232233

233-
TypedArray attributesArray =
234+
final TypedArray attributesArray =
234235
resources.obtainAttributes(
235236
Xml.asAttributeSet(xmlResourceParser),
236237
R.styleable.Keyboard
@@ -304,7 +305,7 @@ public Key(
304305
attributesArray.recycle();
305306
}
306307

307-
public boolean containsPoint(int x, int y) {
308+
public boolean containsPoint(final int x, final int y) {
308309
return (
309310
this.x <= x && x <= this.x + this.width
310311
&&
@@ -322,8 +323,8 @@ public int getHeight() {
322323
}
323324

324325
private void loadKeyboard(
325-
Context context,
326-
XmlResourceParser xmlResourceParser
326+
final Context context,
327+
final XmlResourceParser xmlResourceParser
327328
)
328329
{
329330
try {
@@ -339,7 +340,7 @@ private void loadKeyboard(
339340
int maximumX = x;
340341
int maximumY = y;
341342

342-
Resources resources = context.getResources();
343+
final Resources resources = context.getResources();
343344

344345
int event;
345346

@@ -349,7 +350,7 @@ private void loadKeyboard(
349350
{
350351
switch (event) {
351352
case XmlResourceParser.START_TAG:
352-
String xmlTag = xmlResourceParser.getName();
353+
final String xmlTag = xmlResourceParser.getName();
353354
switch (xmlTag) {
354355
case "Keyboard":
355356
parseKeyboardAttributes(resources, xmlResourceParser);
@@ -392,11 +393,11 @@ else if (inRow) {
392393
}
393394

394395
private void parseKeyboardAttributes(
395-
Resources resources,
396-
XmlResourceParser xmlResourceParser
396+
final Resources resources,
397+
final XmlResourceParser xmlResourceParser
397398
)
398399
{
399-
TypedArray attributesArray =
400+
final TypedArray attributesArray =
400401
resources.obtainAttributes(
401402
Xml.asAttributeSet(xmlResourceParser),
402403
R.styleable.Keyboard
@@ -465,13 +466,13 @@ private void parseKeyboardAttributes(
465466
}
466467

467468
private static int getDimensionOrFraction(
468-
TypedArray array,
469-
int attributeIndex,
470-
int baseValue,
471-
int defaultValue
469+
final TypedArray array,
470+
final int attributeIndex,
471+
final int baseValue,
472+
final int defaultValue
472473
)
473474
{
474-
TypedValue value = array.peekValue(attributeIndex);
475+
final TypedValue value = array.peekValue(attributeIndex);
475476
if (value == null) {
476477
return defaultValue;
477478
}

app/src/main/java/io/github/yawnoc/strokeinput/MainActivity.java

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -32,30 +32,32 @@ public class MainActivity
3232
implements View.OnClickListener
3333
{
3434
@Override
35-
protected void onCreate(Bundle savedInstanceState) {
35+
protected void onCreate(final Bundle savedInstanceState) {
3636

3737
super.onCreate(savedInstanceState);
3838
setContentView(R.layout.activity_main);
3939

40-
Button inputSettingsButton = findViewById(R.id.input_settings_button);
40+
final Button inputSettingsButton =
41+
findViewById(R.id.input_settings_button);
4142
inputSettingsButton.setOnClickListener(this);
4243

43-
Button switchKeyboardButton = findViewById(R.id.switch_keyboard_button);
44+
final Button switchKeyboardButton =
45+
findViewById(R.id.switch_keyboard_button);
4446
switchKeyboardButton.setOnClickListener(this);
4547

4648
}
4749

4850
@Override
49-
public void onClick(View view) {
51+
public void onClick(final View view) {
5052

51-
int viewId = view.getId();
53+
final int viewId = view.getId();
5254

5355
if (viewId == R.id.input_settings_button) {
5456
Intent intent = new Intent(Settings.ACTION_INPUT_METHOD_SETTINGS);
5557
startActivity(intent);
5658
}
5759
else if (viewId == R.id.switch_keyboard_button) {
58-
InputMethodManager inputMethodManager =
60+
final InputMethodManager inputMethodManager =
5961
(InputMethodManager) getSystemService(INPUT_METHOD_SERVICE);
6062
inputMethodManager.showInputMethodPicker();
6163
}

0 commit comments

Comments
 (0)