Skip to content

Commit 926b99c

Browse files
committed
Refactor: Move code to LayoutModifier
Layout modifying functions are removed from Config to LayoutModifier as static classes. The two classes are (weakly) mutually dependent, the refactoring is purely for the purpose of making shorter classes. The only change is that 'modify_numpad' is changed to remove duplicated code. This has the side effect of making the "double tap for caps lock" option affect the shift key in the numpad.
1 parent 52af262 commit 926b99c

File tree

3 files changed

+221
-220
lines changed

3 files changed

+221
-220
lines changed

srcs/juloo.keyboard2/Config.java

Lines changed: 1 addition & 217 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,9 @@
55
import android.content.res.Resources;
66
import android.util.DisplayMetrics;
77
import android.util.TypedValue;
8-
import android.view.KeyEvent;
98
import java.util.ArrayList;
10-
import java.util.Arrays;
11-
import java.util.HashMap;
12-
import java.util.HashSet;
139
import java.util.List;
1410
import java.util.Map;
15-
import java.util.Set;
1611
import juloo.keyboard2.prefs.CustomExtraKeysPreference;
1712
import juloo.keyboard2.prefs.ExtraKeysPreference;
1813
import juloo.keyboard2.prefs.LayoutsPreference;
@@ -28,10 +23,6 @@ public final class Config
2823
public final float labelTextSize;
2924
public final float sublabelTextSize;
3025

31-
public final KeyboardData.Row bottom_row;
32-
public final KeyboardData.Row number_row;
33-
public final KeyboardData num_pad;
34-
3526
// From preferences
3627
/** [null] represent the [system] layout. */
3728
public List<KeyboardData> layouts;
@@ -94,16 +85,6 @@ private Config(SharedPreferences prefs, Resources res, IKeyEventHandler h)
9485
keyPadding = res.getDimension(R.dimen.key_padding);
9586
labelTextSize = 0.33f;
9687
sublabelTextSize = 0.22f;
97-
try
98-
{
99-
number_row = KeyboardData.load_number_row(res);
100-
bottom_row = KeyboardData.load_bottom_row(res);
101-
num_pad = KeyboardData.load_num_pad(res);
102-
}
103-
catch (Exception e)
104-
{
105-
throw new RuntimeException(e.getMessage()); // Not recoverable
106-
}
10788
// from prefs
10889
refresh(res);
10990
// initialized later
@@ -216,190 +197,6 @@ public void set_clipboard_history_enabled(boolean e)
216197
_prefs.edit().putBoolean("clipboard_history_enabled", e).commit();
217198
}
218199

219-
KeyValue action_key()
220-
{
221-
// Update the name to avoid caching in KeyModifier
222-
return (actionLabel == null) ? null : KeyValue.makeActionKey(actionLabel);
223-
}
224-
225-
/** Update the layout according to the configuration.
226-
* - Remove the switching key if it isn't needed
227-
* - Remove "localized" keys from other locales (not in 'extra_keys')
228-
* - Replace the action key to show the right label
229-
* - Swap the enter and action keys
230-
* - Add the optional numpad and number row
231-
* - Add the extra keys
232-
*/
233-
public KeyboardData modify_layout(KeyboardData kw)
234-
{
235-
final KeyValue action_key = action_key();
236-
// Extra keys are removed from the set as they are encountered during the
237-
// first iteration then automatically added.
238-
final Map<KeyValue, KeyboardData.PreferredPos> extra_keys = new HashMap<KeyValue, KeyboardData.PreferredPos>();
239-
final Set<KeyValue> remove_keys = new HashSet<KeyValue>();
240-
// Make sure the config key is accessible to avoid being locked in a custom
241-
// layout.
242-
extra_keys.put(KeyValue.getKeyByName("config"), KeyboardData.PreferredPos.ANYWHERE);
243-
extra_keys.putAll(extra_keys_param);
244-
extra_keys.putAll(extra_keys_custom);
245-
if (extra_keys_subtype != null && kw.locale_extra_keys)
246-
{
247-
Set<KeyValue> present = new HashSet<KeyValue>();
248-
present.addAll(kw.getKeys().keySet());
249-
present.addAll(extra_keys_param.keySet());
250-
present.addAll(extra_keys_custom.keySet());
251-
extra_keys_subtype.compute(extra_keys,
252-
new ExtraKeys.Query(kw.script, present));
253-
}
254-
KeyboardData.Row added_number_row = null;
255-
if (add_number_row && !show_numpad)
256-
added_number_row = modify_number_row(number_row, kw);
257-
if (added_number_row != null)
258-
remove_keys.addAll(added_number_row.getKeys(0).keySet());
259-
if (kw.bottom_row)
260-
kw = kw.insert_row(bottom_row, kw.rows.size());
261-
kw = kw.mapKeys(new KeyboardData.MapKeyValues() {
262-
public KeyValue apply(KeyValue key, boolean localized)
263-
{
264-
boolean is_extra_key = extra_keys.containsKey(key);
265-
if (is_extra_key)
266-
extra_keys.remove(key);
267-
if (localized && !is_extra_key)
268-
return null;
269-
if (remove_keys.contains(key))
270-
return null;
271-
switch (key.getKind())
272-
{
273-
case Event:
274-
switch (key.getEvent())
275-
{
276-
case CHANGE_METHOD_PICKER:
277-
if (switch_input_immediate)
278-
return KeyValue.getKeyByName("change_method_prev");
279-
return key;
280-
case ACTION:
281-
return (swapEnterActionKey && action_key != null) ?
282-
KeyValue.getKeyByName("enter") : action_key;
283-
case SWITCH_FORWARD:
284-
return (layouts.size() > 1) ? key : null;
285-
case SWITCH_BACKWARD:
286-
return (layouts.size() > 2) ? key : null;
287-
case SWITCH_VOICE_TYPING:
288-
case SWITCH_VOICE_TYPING_CHOOSER:
289-
return shouldOfferVoiceTyping ? key : null;
290-
}
291-
break;
292-
case Keyevent:
293-
switch (key.getKeyevent())
294-
{
295-
case KeyEvent.KEYCODE_ENTER:
296-
return (swapEnterActionKey && action_key != null) ? action_key : key;
297-
}
298-
break;
299-
case Modifier:
300-
switch (key.getModifier())
301-
{
302-
case SHIFT:
303-
if (double_tap_lock_shift)
304-
return key.withFlags(key.getFlags() | KeyValue.FLAG_LOCK);
305-
}
306-
break;
307-
}
308-
return key;
309-
}
310-
});
311-
if (show_numpad)
312-
kw = kw.addNumPad(modify_numpad(num_pad, kw));
313-
if (extra_keys.size() > 0)
314-
kw = kw.addExtraKeys(extra_keys.entrySet().iterator());
315-
if (added_number_row != null)
316-
kw = kw.insert_row(added_number_row, 0);
317-
return kw;
318-
}
319-
320-
/** Handle the numpad layout. The [main_kw] is used to adapt the numpad to
321-
the main layout's script. */
322-
public KeyboardData modify_numpad(KeyboardData kw, KeyboardData main_kw)
323-
{
324-
final KeyValue action_key = action_key();
325-
final int map_digit = KeyModifier.modify_numpad_script(main_kw.numpad_script);
326-
return kw.mapKeys(new KeyboardData.MapKeyValues() {
327-
public KeyValue apply(KeyValue key, boolean localized)
328-
{
329-
switch (key.getKind())
330-
{
331-
case Event:
332-
switch (key.getEvent())
333-
{
334-
case ACTION:
335-
return (swapEnterActionKey && action_key != null) ?
336-
KeyValue.getKeyByName("enter") : action_key;
337-
}
338-
break;
339-
case Keyevent:
340-
switch (key.getKeyevent())
341-
{
342-
case KeyEvent.KEYCODE_ENTER:
343-
return (swapEnterActionKey && action_key != null) ? action_key : key;
344-
}
345-
break;
346-
case Char:
347-
char prev_c = key.getChar();
348-
char c = prev_c;
349-
if (inverse_numpad)
350-
c = inverse_numpad_char(c);
351-
if (map_digit != -1)
352-
{
353-
KeyValue modified = ComposeKey.apply(map_digit, c);
354-
if (modified != null) // Was modified by script
355-
return modified;
356-
}
357-
if (prev_c != c) // Was inverted
358-
return key.withChar(c);
359-
break;
360-
}
361-
return key;
362-
}
363-
});
364-
}
365-
366-
static KeyboardData.MapKeyValues numpad_script_map(String numpad_script)
367-
{
368-
final int map_digit = KeyModifier.modify_numpad_script(numpad_script);
369-
if (map_digit == -1)
370-
return null;
371-
return new KeyboardData.MapKeyValues() {
372-
public KeyValue apply(KeyValue key, boolean localized)
373-
{
374-
switch (key.getKind())
375-
{
376-
case Char:
377-
KeyValue modified = ComposeKey.apply(map_digit, key.getChar());
378-
if (modified != null)
379-
return modified;
380-
break;
381-
}
382-
return key;
383-
}
384-
};
385-
}
386-
387-
/** Modify the pin entry layout. [main_kw] is used to map the digits into the
388-
same script. */
389-
public KeyboardData modify_pinentry(KeyboardData kw, KeyboardData main_kw)
390-
{
391-
KeyboardData.MapKeyValues m = numpad_script_map(main_kw.numpad_script);
392-
return m == null ? kw : kw.mapKeys(m);
393-
}
394-
395-
/** Modify the number row according to [main_kw]'s script. */
396-
public KeyboardData.Row modify_number_row(KeyboardData.Row row,
397-
KeyboardData main_kw)
398-
{
399-
KeyboardData.MapKeyValues m = numpad_script_map(main_kw.numpad_script);
400-
return m == null ? row : row.mapKeys(m);
401-
}
402-
403200
private float get_dip_pref(DisplayMetrics dm, String pref_name, float def)
404201
{
405202
float value;
@@ -446,27 +243,14 @@ private int getThemeId(Resources res, String theme_name)
446243
}
447244
}
448245

449-
char inverse_numpad_char(char c)
450-
{
451-
switch (c)
452-
{
453-
case '7': return '1';
454-
case '8': return '2';
455-
case '9': return '3';
456-
case '1': return '7';
457-
case '2': return '8';
458-
case '3': return '9';
459-
default: return c;
460-
}
461-
}
462-
463246
private static Config _globalConfig = null;
464247

465248
public static void initGlobalConfig(SharedPreferences prefs, Resources res,
466249
IKeyEventHandler handler)
467250
{
468251
migrate(prefs);
469252
_globalConfig = new Config(prefs, res, handler);
253+
LayoutModifier.init(_globalConfig, res);
470254
}
471255

472256
public static Config globalConfig()

srcs/juloo.keyboard2/Keyboard2.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ KeyboardData current_layout()
6262
{
6363
if (_currentSpecialLayout != null)
6464
return _currentSpecialLayout;
65-
return _config.modify_layout(current_layout_unmodified());
65+
return LayoutModifier.modify_layout(current_layout_unmodified());
6666
}
6767

6868
void setTextLayout(int l)
@@ -92,13 +92,13 @@ KeyboardData loadLayout(int layout_id)
9292
/** Load a layout that contains a numpad. */
9393
KeyboardData loadNumpad(int layout_id)
9494
{
95-
return _config.modify_numpad(KeyboardData.load(getResources(), layout_id),
95+
return LayoutModifier.modify_numpad(KeyboardData.load(getResources(), layout_id),
9696
current_layout_unmodified());
9797
}
9898

9999
KeyboardData loadPinentry(int layout_id)
100100
{
101-
return _config.modify_pinentry(KeyboardData.load(getResources(), layout_id),
101+
return LayoutModifier.modify_pinentry(KeyboardData.load(getResources(), layout_id),
102102
current_layout_unmodified());
103103
}
104104

0 commit comments

Comments
 (0)