1
0
Fork 0

removed some unused code and modernized the switch() expressions

This commit is contained in:
sspanak 2024-10-18 20:56:34 +03:00 committed by Dimo Karaivanov
parent 80d50fb546
commit 60aba7084d
19 changed files with 144 additions and 246 deletions

View file

@ -22,45 +22,35 @@ public class AddWordResult {
}
public String toHumanFriendlyString(Context context) {
switch (statusCode) {
case CODE_SUCCESS:
return context.getString(R.string.add_word_success, word);
return switch (statusCode) {
case CODE_SUCCESS
-> context.getString(R.string.add_word_success, word);
case CODE_WORD_EXISTS:
return context.getResources().getString(R.string.add_word_exist, word);
case CODE_WORD_EXISTS
-> context.getResources().getString(R.string.add_word_exist, word);
case CODE_BLANK_WORD:
return context.getString(R.string.add_word_blank);
case CODE_BLANK_WORD
-> context.getString(R.string.add_word_blank);
case CODE_INVALID_LANGUAGE:
return context.getResources().getString(R.string.add_word_invalid_language);
case CODE_INVALID_LANGUAGE
-> context.getResources().getString(R.string.add_word_invalid_language);
default:
return context.getString(R.string.error_unexpected);
}
default
-> context.getString(R.string.error_unexpected);
};
}
@NonNull
@Override
public String toString() {
switch (statusCode) {
case CODE_SUCCESS:
return "Success";
case CODE_BLANK_WORD:
return "Blank word";
case CODE_INVALID_LANGUAGE:
return "Invalid language";
case CODE_WORD_EXISTS:
return "Word '" + word + "' exists";
case CODE_GENERAL_ERROR:
return "General error";
default:
return "Unknown error";
}
return switch (statusCode) {
case CODE_SUCCESS -> "Success";
case CODE_BLANK_WORD -> "Blank word";
case CODE_INVALID_LANGUAGE -> "Invalid language";
case CODE_WORD_EXISTS -> "Word '" + word + "' exists";
case CODE_GENERAL_ERROR -> "General error";
default -> "Unknown error";
};
}
}

View file

@ -145,20 +145,11 @@ public class ReadOps {
public String getSimilarWordPositions(@NonNull SQLiteDatabase db, @NonNull Language language, @NonNull String sequence, String wordFilter, int minPositions) {
int generations;
switch (sequence.length()) {
case 2:
generations = wordFilter.isEmpty() ? 1 : 10;
break;
case 3:
case 4:
generations = wordFilter.isEmpty() ? 2 : 10;
break;
default:
generations = 10;
break;
}
int generations = switch (sequence.length()) {
case 2 -> wordFilter.isEmpty() ? 1 : 10;
case 3, 4 -> wordFilter.isEmpty() ? 2 : 10;
default -> 10;
};
return getWordPositions(db, language, sequence, generations, minPositions, wordFilter);
}

View file

@ -108,7 +108,7 @@ public class DictionaryLoader {
public static void load(Context context, Language language) {
DictionaryLoadingBar progressBar = DictionaryLoadingBar.getInstance(context);
getInstance(context).setOnStatusChange(status -> progressBar.show(context, status));
self.load(context, new ArrayList<Language>() {{ add(language); }});
self.load(context, new ArrayList<>() {{ add(language); }});
}

View file

@ -1,7 +1,6 @@
package io.github.sspanak.tt9.hacks;
import android.view.KeyEvent;
import android.view.inputmethod.InputConnection;
import androidx.annotation.NonNull;
@ -10,20 +9,15 @@ import io.github.sspanak.tt9.ime.helpers.SuggestionOps;
import io.github.sspanak.tt9.ime.helpers.TextField;
import io.github.sspanak.tt9.ime.helpers.TextSelection;
import io.github.sspanak.tt9.ime.modes.InputMode;
import io.github.sspanak.tt9.preferences.settings.SettingsStore;
public class AppHacks {
private final InputConnection inputConnection;
private final InputType inputType;
private final SettingsStore settings;
private final TextField textField;
private final TextSelection textSelection;
public AppHacks(SettingsStore settings, InputConnection inputConnection, InputType inputType, TextField textField, TextSelection textSelection) {
this.inputConnection = inputConnection;
public AppHacks(InputType inputType, TextField textField, TextSelection textSelection) {
this.inputType = inputType;
this.settings = settings;
this.textField = textField;
this.textSelection = textSelection;
}

View file

@ -26,7 +26,7 @@ import io.github.sspanak.tt9.util.Text;
public abstract class TypingHandler extends KeyPadHandler {
// internal settings/data
@NonNull protected AppHacks appHacks = new AppHacks(null,null, null, null, null);
@NonNull protected AppHacks appHacks = new AppHacks(null, null, null);
@NonNull protected InputType inputType = new InputType(null, null);
@NonNull protected TextField textField = new TextField(null, null);
@NonNull protected TextSelection textSelection = new TextSelection(this,null);
@ -87,7 +87,7 @@ public abstract class TypingHandler extends KeyPadHandler {
textSelection = new TextSelection(this, connection);
// changing the TextField and notifying all interested classes is an atomic operation
appHacks = new AppHacks(settings, connection, inputType, textField, textSelection);
appHacks = new AppHacks(inputType, textField, textSelection);
suggestionOps.setTextField(textField);
}

View file

@ -54,18 +54,17 @@ public class InputField {
// See the example below:
// https://github.com/openboard-team/openboard/blob/c3772cd56e770975ea5570db903f93b199de8b32/app/src/main/java/org/dslul/openboard/inputmethod/latin/inputlogic/InputLogic.java#L756
int standardAction = field.imeOptions & (EditorInfo.IME_MASK_ACTION | EditorInfo.IME_FLAG_NO_ENTER_ACTION);
switch (standardAction) {
case EditorInfo.IME_ACTION_DONE:
case EditorInfo.IME_ACTION_GO:
case EditorInfo.IME_ACTION_NEXT:
case EditorInfo.IME_ACTION_PREVIOUS:
case EditorInfo.IME_ACTION_SEARCH:
case EditorInfo.IME_ACTION_SEND:
case EditorInfo.IME_ACTION_UNSPECIFIED:
return standardAction;
default:
return IME_ACTION_ENTER;
}
return switch (standardAction) {
case EditorInfo.IME_ACTION_DONE,
EditorInfo.IME_ACTION_GO,
EditorInfo.IME_ACTION_NEXT,
EditorInfo.IME_ACTION_PREVIOUS,
EditorInfo.IME_ACTION_SEARCH,
EditorInfo.IME_ACTION_SEND,
EditorInfo.IME_ACTION_UNSPECIFIED
-> standardAction;
default -> IME_ACTION_ENTER;
};
}

View file

@ -85,40 +85,19 @@ public class Key {
public static int codeToNumber(SettingsStore settings, int keyCode) {
switch (keyCode) {
case KeyEvent.KEYCODE_0:
case KeyEvent.KEYCODE_NUMPAD_0:
return 0;
case KeyEvent.KEYCODE_1:
case KeyEvent.KEYCODE_NUMPAD_1:
return settings.getUpsideDownKeys() ? 7 : 1;
case KeyEvent.KEYCODE_2:
case KeyEvent.KEYCODE_NUMPAD_2:
return settings.getUpsideDownKeys() ? 8 : 2;
case KeyEvent.KEYCODE_3:
case KeyEvent.KEYCODE_NUMPAD_3:
return settings.getUpsideDownKeys() ? 9 : 3;
case KeyEvent.KEYCODE_4:
case KeyEvent.KEYCODE_NUMPAD_4:
return 4;
case KeyEvent.KEYCODE_5:
case KeyEvent.KEYCODE_NUMPAD_5:
return 5;
case KeyEvent.KEYCODE_6:
case KeyEvent.KEYCODE_NUMPAD_6:
return 6;
case KeyEvent.KEYCODE_7:
case KeyEvent.KEYCODE_NUMPAD_7:
return settings.getUpsideDownKeys() ? 1 : 7;
case KeyEvent.KEYCODE_8:
case KeyEvent.KEYCODE_NUMPAD_8:
return settings.getUpsideDownKeys() ? 2 : 8;
case KeyEvent.KEYCODE_9:
case KeyEvent.KEYCODE_NUMPAD_9:
return settings.getUpsideDownKeys() ? 3 : 9;
default:
return -1;
}
return switch (keyCode) {
case KeyEvent.KEYCODE_0, KeyEvent.KEYCODE_NUMPAD_0 -> 0;
case KeyEvent.KEYCODE_1, KeyEvent.KEYCODE_NUMPAD_1 -> settings.getUpsideDownKeys() ? 7 : 1;
case KeyEvent.KEYCODE_2, KeyEvent.KEYCODE_NUMPAD_2 -> settings.getUpsideDownKeys() ? 8 : 2;
case KeyEvent.KEYCODE_3, KeyEvent.KEYCODE_NUMPAD_3 -> settings.getUpsideDownKeys() ? 9 : 3;
case KeyEvent.KEYCODE_4, KeyEvent.KEYCODE_NUMPAD_4 -> 4;
case KeyEvent.KEYCODE_5, KeyEvent.KEYCODE_NUMPAD_5 -> 5;
case KeyEvent.KEYCODE_6, KeyEvent.KEYCODE_NUMPAD_6 -> 6;
case KeyEvent.KEYCODE_7, KeyEvent.KEYCODE_NUMPAD_7 -> settings.getUpsideDownKeys() ? 1 : 7;
case KeyEvent.KEYCODE_8, KeyEvent.KEYCODE_NUMPAD_8 -> settings.getUpsideDownKeys() ? 2 : 8;
case KeyEvent.KEYCODE_9, KeyEvent.KEYCODE_NUMPAD_9 -> settings.getUpsideDownKeys() ? 3 : 9;
default -> -1;
};
}
public static int numberToCode(int number) {

View file

@ -199,13 +199,11 @@ abstract public class StandardInputType {
return InputMode.CASE_CAPITALIZE;
}
switch (field.inputType & InputType.TYPE_MASK_FLAGS) {
case InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS:
return InputMode.CASE_UPPER;
case InputType.TYPE_TEXT_FLAG_CAP_WORDS:
return InputMode.CASE_CAPITALIZE;
}
return switch (field.inputType & InputType.TYPE_MASK_FLAGS) {
case InputType.TYPE_TEXT_FLAG_CAP_CHARACTERS -> InputMode.CASE_UPPER;
case InputType.TYPE_TEXT_FLAG_CAP_WORDS -> InputMode.CASE_CAPITALIZE;
default -> InputMode.CASE_UNDEFINED;
};
return InputMode.CASE_UNDEFINED;
}
}

View file

@ -36,11 +36,6 @@ public class TextField extends InputField {
}
public boolean isEmpty() {
return getStringBeforeCursor(1).isEmpty() && getStringAfterCursor(1).isEmpty();
}
public String getStringAfterCursor(int numberOfChars) {
CharSequence character = connection != null ? connection.getTextAfterCursor(numberOfChars, 0) : null;
return character != null ? character.toString() : "";

View file

@ -22,16 +22,13 @@ public class AutoTextCase {
* or Dutch words such as: "'s-Hertogenbosch".
*/
public String adjustSuggestionTextCase(Text word, int newTextCase) {
switch (newTextCase) {
case InputMode.CASE_UPPER:
return word.toUpperCase();
case InputMode.CASE_LOWER:
return word.toLowerCase();
case InputMode.CASE_CAPITALIZE:
return word.isMixedCase() || word.isUpperCase() ? word.toString() : word.capitalize();
default:
return word.toString();
}
return switch (newTextCase) {
case InputMode.CASE_UPPER -> word.toUpperCase();
case InputMode.CASE_LOWER -> word.toLowerCase();
case InputMode.CASE_CAPITALIZE ->
word.isMixedCase() || word.isUpperCase() ? word.toString() : word.capitalize();
default -> word.toString();
};
}

View file

@ -49,23 +49,22 @@ public class VoiceInputError {
@NonNull
private static String codeToString(Context context, int code) {
switch (code) {
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
return context.getString(R.string.voice_input_error_no_permissions);
case SpeechRecognizer.ERROR_LANGUAGE_NOT_SUPPORTED:
return context.getString(R.string.voice_input_error_language_not_supported);
case SpeechRecognizer.ERROR_NETWORK:
return context.getString(R.string.voice_input_error_no_network);
case ERROR_NOT_AVAILABLE:
return context.getString(R.string.voice_input_error_not_available);
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
case SpeechRecognizer.ERROR_SERVER:
case SpeechRecognizer.ERROR_SERVER_DISCONNECTED:
case SpeechRecognizer.ERROR_TOO_MANY_REQUESTS:
return context.getString(R.string.voice_input_error_network_failed);
default:
return context.getString(R.string.voice_input_error_generic);
}
return switch (code) {
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS
-> context.getString(R.string.voice_input_error_no_permissions);
case SpeechRecognizer.ERROR_LANGUAGE_NOT_SUPPORTED
-> context.getString(R.string.voice_input_error_language_not_supported);
case SpeechRecognizer.ERROR_NETWORK
-> context.getString(R.string.voice_input_error_no_network);
case ERROR_NOT_AVAILABLE
-> context.getString(R.string.voice_input_error_not_available);
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT,
SpeechRecognizer.ERROR_SERVER,
SpeechRecognizer.ERROR_SERVER_DISCONNECTED,
SpeechRecognizer.ERROR_TOO_MANY_REQUESTS
-> context.getString(R.string.voice_input_error_network_failed);
default -> context.getString(R.string.voice_input_error_generic);
};
}
@ -105,31 +104,19 @@ public class VoiceInputError {
private static String codeToDebugStringCommon(int code) {
switch (code) {
case SpeechRecognizer.ERROR_AUDIO:
return "Audio capture error.";
case SpeechRecognizer.ERROR_CLIENT:
return "Speech recognition client error.";
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS:
return "No microphone permissions.";
case SpeechRecognizer.ERROR_NETWORK:
return "No network connection.";
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT:
return "Network timeout.";
case SpeechRecognizer.ERROR_NO_MATCH:
return "No match.";
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY:
return "Voice input service is busy.";
case SpeechRecognizer.ERROR_SERVER:
return "Speech recognition server error.";
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT:
return "No speech detected.";
case ERROR_NOT_AVAILABLE:
return "Voice input is not available.";
case ERROR_INVALID_LANGUAGE:
return "Invalid language for voice input.";
default:
return null;
}
return switch (code) {
case SpeechRecognizer.ERROR_AUDIO -> "Audio capture error.";
case SpeechRecognizer.ERROR_CLIENT -> "Speech recognition client error.";
case SpeechRecognizer.ERROR_INSUFFICIENT_PERMISSIONS -> "No microphone permissions.";
case SpeechRecognizer.ERROR_NETWORK -> "No network connection.";
case SpeechRecognizer.ERROR_NETWORK_TIMEOUT -> "Network timeout.";
case SpeechRecognizer.ERROR_NO_MATCH -> "No match.";
case SpeechRecognizer.ERROR_RECOGNIZER_BUSY -> "Voice input service is busy.";
case SpeechRecognizer.ERROR_SERVER -> "Speech recognition server error.";
case SpeechRecognizer.ERROR_SPEECH_TIMEOUT -> "No speech detected.";
case ERROR_NOT_AVAILABLE -> "Voice input is not available.";
case ERROR_INVALID_LANGUAGE -> "Invalid language for voice input.";
default -> null;
};
}
}

View file

@ -149,34 +149,21 @@ public class PreferencesActivity extends ActivityWithNavigation implements Prefe
return new MainSettingsScreen(this);
}
switch (name) {
case AppearanceScreen.NAME:
return new AppearanceScreen(this);
case DebugScreen.NAME:
return new DebugScreen(this);
case DeleteWordsScreen.NAME:
return new DeleteWordsScreen(this);
case HotkeysScreen.NAME:
return new HotkeysScreen(this);
case KeyPadScreen.NAME:
return new KeyPadScreen(this);
case LanguagesScreen.NAME:
return new LanguagesScreen(this);
case LanguageSelectionScreen.NAME:
return new LanguageSelectionScreen(this);
case ModePredictiveScreen.NAME:
return new ModePredictiveScreen(this);
case ModeAbcScreen.NAME:
return new ModeAbcScreen(this);
case PunctuationScreen.NAME:
return new PunctuationScreen(this);
case SetupScreen.NAME:
return new SetupScreen(this);
case UsageStatsScreen.NAME:
return new UsageStatsScreen(this);
default:
return new MainSettingsScreen(this);
}
return switch (name) {
case AppearanceScreen.NAME -> new AppearanceScreen(this);
case DebugScreen.NAME -> new DebugScreen(this);
case DeleteWordsScreen.NAME -> new DeleteWordsScreen(this);
case HotkeysScreen.NAME -> new HotkeysScreen(this);
case KeyPadScreen.NAME -> new KeyPadScreen(this);
case LanguagesScreen.NAME -> new LanguagesScreen(this);
case LanguageSelectionScreen.NAME -> new LanguageSelectionScreen(this);
case ModePredictiveScreen.NAME -> new ModePredictiveScreen(this);
case ModeAbcScreen.NAME -> new ModeAbcScreen(this);
case PunctuationScreen.NAME -> new PunctuationScreen(this);
case SetupScreen.NAME -> new SetupScreen(this);
case UsageStatsScreen.NAME -> new UsageStatsScreen(this);
default -> new MainSettingsScreen(this);
};
}

View file

@ -131,14 +131,11 @@ abstract class AbstractPreferenceCharList extends ItemTextInput {
private String getCharName(char c) {
switch (c) {
case '\n':
return getContext().getString(R.string.char_newline);
case ' ':
return getContext().getString(R.string.char_space);
default:
return String.valueOf(c);
}
return switch (c) {
case '\n' -> getContext().getString(R.string.char_newline);
case ' ' -> getContext().getString(R.string.char_space);
default -> String.valueOf(c);
};
}

View file

@ -31,15 +31,14 @@ public class PopupDialogActivity extends AppCompatActivity {
String popupType = i != null ? i.getStringExtra(PopupDialog.PARAMETER_DIALOG_TYPE) : "";
popupType = popupType != null ? popupType : "";
switch (popupType) {
case AddWordDialog.TYPE:
return new AddWordDialog(this, i, this::onDialogClose);
case AutoUpdateMonologue.TYPE:
return new AutoUpdateMonologue(this, i, this::onDialogClose);
default:
return switch (popupType) {
case AddWordDialog.TYPE -> new AddWordDialog(this, i, this::onDialogClose);
case AutoUpdateMonologue.TYPE -> new AutoUpdateMonologue(this, i, this::onDialogClose);
default -> {
Logger.w(LOG_TAG, "Unknown popup type: '" + popupType + "'. Not displaying anything.");
return null;
yield null;
}
};
}
private void onDialogClose(String message) {

View file

@ -29,14 +29,15 @@ abstract class BaseMainLayout {
/** setDarkTheme
* Changes the main view colors according to the theme.
* <p>Changes the main view colors according to the theme.</p>
*
* We need to do this manually, instead of relying on the Context to resolve the appropriate colors,
* <p>We need to do this manually, instead of relying on the Context to resolve the appropriate colors,
* because this View is part of the main service View. And service Views are always locked to the
* system context and theme.
* system context and theme.</p>
*
* More info:
* <p>More info:
* <a href="https://stackoverflow.com/questions/72382886/system-applies-night-mode-to-views-added-in-service-type-application-overlay">...</a>
* </p>
*/
void setDarkTheme(boolean dark) {}
@ -105,11 +106,6 @@ abstract class BaseMainLayout {
}
int getHeight() {
return getHeight(false);
}
abstract void showCommandPalette();
abstract void hideCommandPalette();
abstract boolean isCommandPaletteShown();

View file

@ -46,15 +46,13 @@ class MainLayoutTray extends BaseMainLayout {
view.findViewById(R.id.main_soft_keys).setVisibility(LinearLayout.GONE);
view.findViewById(R.id.main_command_keys).setVisibility(LinearLayout.VISIBLE);
height = 0;
getHeight();
getHeight(true);
}
void hideCommandPalette() {
view.findViewById(R.id.main_command_keys).setVisibility(LinearLayout.GONE);
height = 0;
getHeight();
getHeight(true);
}
boolean isCommandPaletteShown() {
@ -67,16 +65,14 @@ class MainLayoutTray extends BaseMainLayout {
view.findViewById(R.id.main_soft_keys).setVisibility(LinearLayout.GONE);
view.findViewById(R.id.text_editing_container).setVisibility(LinearLayout.VISIBLE);
height = 0;
getHeight();
getHeight(true);
}
@Override
void hideTextEditingPalette() {
view.findViewById(R.id.text_editing_container).setVisibility(LinearLayout.GONE);
height = 0;
getHeight();
getHeight(true);
}
@Override

View file

@ -22,7 +22,7 @@ import io.github.sspanak.tt9.util.Logger;
import io.github.sspanak.tt9.util.TextTools;
public class SoftKeyNumber extends SoftKey {
private final static SparseArray<Integer> NUMBERS = new SparseArray<Integer>() {{
private final static SparseArray<Integer> NUMBERS = new SparseArray<>() {{
put(R.id.soft_key_0, 0);
put(R.id.soft_key_1, 1);
put(R.id.soft_key_2, 2);
@ -35,7 +35,7 @@ public class SoftKeyNumber extends SoftKey {
put(R.id.soft_key_9, 9);
}};
private final static SparseArray<Integer> UPSIDE_DOWN_NUMBERS = new SparseArray<Integer>() {{
private final static SparseArray<Integer> UPSIDE_DOWN_NUMBERS = new SparseArray<>() {{
put(1, 7);
put(2, 8);
put(3, 9);
@ -146,14 +146,11 @@ public class SoftKeyNumber extends SoftKey {
int number = getNumber(getId());
switch (number) {
case 0:
return getSpecialCharList(tt9);
case 1:
return tt9.isNumericModeStrict() ? null : PUNCTUATION_LABEL;
default:
return getKeyCharList(tt9, number);
}
return switch (number) {
case 0 -> getSpecialCharList(tt9);
case 1 -> tt9.isNumericModeStrict() ? null : PUNCTUATION_LABEL;
default -> getKeyCharList(tt9, number);
};
}

View file

@ -28,14 +28,11 @@ public class SoftKeyPunctuation extends SoftKey {
@Override
protected String getTitle() {
String keyChar = getKeyChar();
switch (keyChar) {
case "":
return "PUNC";
case "*":
return "";
default:
return keyChar;
}
return switch (keyChar) {
case "" -> "PUNC";
case "*" -> "";
default -> keyChar;
};
}
private String getKeyChar() {

View file

@ -2,7 +2,6 @@ package io.github.sspanak.tt9.util;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.TimeZone;
import java.util.regex.Pattern;