Skip to content
This repository has been archived by the owner on Jul 22, 2024. It is now read-only.

Commit

Permalink
Add support for keyboard domains (#2308)
Browse files Browse the repository at this point in the history
* Adds for keyboard domains


Set column width as the max of each row


Fix keyboard panels column width

* Fix delegate name
  • Loading branch information
keianhzo authored and bluemarvin committed Nov 18, 2019
1 parent 64da0a2 commit 61e7f55
Show file tree
Hide file tree
Showing 45 changed files with 195 additions and 31 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,13 +20,14 @@ public class CustomKeyboard extends Keyboard {
private Key mEnterKey;
private Key mSpaceKey;
private Key mModeChangeKey;
private int mMaxColums;
private int mMaxColumns;
private int[] mDisabledKeysIndexes;

public static final int KEYCODE_SYMBOLS_CHANGE = -10;
public static final int KEYCODE_VOICE_INPUT = -11;
public static final int KEYCODE_LANGUAGE_CHANGE = -12;
public static final int KEYCODE_EMOJI = -13;
public static final int KEYCODE_DOMAIN = -14;

public CustomKeyboard(Context context, int xmlLayoutResId) {
super(context, xmlLayoutResId, 0);
Expand All @@ -36,7 +37,7 @@ public CustomKeyboard(Context context, int xmlLayoutResId) {
public CustomKeyboard (Context context, int layoutTemplateResId, CharSequence characters, int columns, int horizontalPadding, int verticalGap) {
this(context, layoutTemplateResId);

mMaxColums = columns;
mMaxColumns = columns;

int x = 0;
int y = 0;
Expand Down Expand Up @@ -236,7 +237,7 @@ public int[] getShiftKeyIndices() {
}

public int getMaxColumns() {
return mMaxColums;
return mMaxColumns;
}

public void disableKeys(int[] disabledKeyIndexes) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@

import java.util.Locale;
import java.util.regex.Pattern;
import java.util.stream.Stream;

public abstract class BaseKeyboard implements KeyboardInterface {
protected Context mContext;
Expand Down Expand Up @@ -51,4 +52,10 @@ public String getModeChangeKeyText() {
public float getAlphabeticKeyboardWidth() {
return WidgetPlacement.dpDimension(mContext, R.dimen.keyboard_alphabetic_width);
}

@Override
public String[] getDomains(String... domains) {
return Stream.of(new String[]{".com", ".net", ".org", ".co"}, domains).flatMap(Stream::of)
.toArray(String[]::new);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_danish, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".dk");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_dutch, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".nl");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
import androidx.annotation.Nullable;

import java.util.Locale;
import java.util.stream.Stream;

public class EnglishKeyboard extends BaseKeyboard {
private CustomKeyboard mKeyboard;
Expand Down Expand Up @@ -47,4 +48,9 @@ public String getSpaceKeyText(String aComposingText) {

return StringUtils.getStringByLocale(mContext, R.string.settings_language_english, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".uk", ".us");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_finnish, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".fi");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_french, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".fr");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,4 +63,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_german, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".de");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -47,4 +47,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_italian, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".it");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -208,4 +208,9 @@ public void clear() {
mConverter.init();
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".jp");
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ public enum Action {
default boolean usesTextOverride() { return false; }
String getComposingText(String aComposing, String aCode);
String getKeyboardTitle();
default String[] getDomains(String... domains) { return null; }
Locale getLocale();
String getSpaceKeyText(String aComposingText);
String getEnterKeyText(int aIMEOptions, String aComposingText);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -405,4 +405,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_korean, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".kr");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_norwegian, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".no");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_polish, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".po");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_russian, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".ru");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -48,4 +48,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_spanish, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".es");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,9 @@ public Locale getLocale() {
public String getSpaceKeyText(String aComposingText) {
return StringUtils.getStringByLocale(mContext, R.string.settings_language_swedish, getLocale());
}

@Override
public String[] getDomains(String... domains) {
return super.getDomains(".se");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import android.util.TypedValue;
import android.widget.FrameLayout;
import android.widget.GridLayout;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

Expand All @@ -15,7 +16,7 @@
import java.util.List;


public class LanguageSelectorView extends FrameLayout {
public class KeyboardSelectorView extends FrameLayout {
public static class Item {
public final String title;
public final Object tag;
Expand All @@ -27,37 +28,39 @@ public Item(String aTitle, Object aTag) {
}

public interface Delegate {
void onLanguageClick(Item aItem);
void onItemClick(Item aItem);
}

private GridLayout mLangRowContainer;
private Delegate mDelegate;
private List<Item> mItems;
private List<UITextButton> mButtons = new ArrayList<>();
private int mFirstColItemWidth;
private int mSecondColItemWidth;
private int mDomainColItemWidth;
private int mNarrowColItemWidth;
private int mWideColItemWidth;
private static final int kMaxItemsPerColumn = 4;

public LanguageSelectorView(@NonNull Context context) {
public KeyboardSelectorView(@NonNull Context context) {
super(context);
initialize();
}

public LanguageSelectorView(@NonNull Context context, @Nullable AttributeSet attrs) {
public KeyboardSelectorView(@NonNull Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initialize();
}

public LanguageSelectorView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
public KeyboardSelectorView(@NonNull Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initialize();
}

private void initialize() {
inflate(getContext(), R.layout.language_selection, this);
mLangRowContainer = findViewById(R.id.langRowContainer);
mFirstColItemWidth = WidgetPlacement.pixelDimension(getContext(), R.dimen.lang_selector_first_col_item_width);
mSecondColItemWidth = WidgetPlacement.pixelDimension(getContext(), R.dimen.lang_selector_second_col_item_width);
mDomainColItemWidth = WidgetPlacement.pixelDimension(getContext(), R.dimen.lang_selector_domain_col_item_width);
mNarrowColItemWidth = WidgetPlacement.pixelDimension(getContext(), R.dimen.lang_selector_narrow_col_item_width);
mWideColItemWidth = WidgetPlacement.pixelDimension(getContext(), R.dimen.lang_selector_wide_col_item_width);
}

public void setDelegate(Delegate aDelegate) {
Expand All @@ -73,14 +76,34 @@ public void setItems(List<Item> aItems) {
mLangRowContainer.setColumnCount(columns);
mLangRowContainer.setRowCount(rows);

int index = 0;
for (Item item: aItems) {
int[] columnWidth = new int[columns];
for (int i=0; i<aItems.size(); i++) {
GridLayout.LayoutParams params = new GridLayout.LayoutParams();
params.width = index < kMaxItemsPerColumn ? mFirstColItemWidth : mSecondColItemWidth;
UITextButton button = createLangButton(item);
params.width = GridLayout.LayoutParams.WRAP_CONTENT;
UITextButton button = createItemButton(aItems.get(i));
int padding = button.getPaddingStart() + button.getPaddingEnd();
float textWidth = button.getPaint().measureText(aItems.get(i).title) + padding;
int column = i/kMaxItemsPerColumn;
int width;
if (textWidth < mDomainColItemWidth) {
width = mDomainColItemWidth;
} else if (textWidth < mNarrowColItemWidth) {
width = mNarrowColItemWidth;
} else {
width = mWideColItemWidth;
}
if (columnWidth[column] < width) {
columnWidth[column] = width;
}
mLangRowContainer.addView(button, params);
mButtons.add(button);
++index;
}

// Assign widest row width to the whole column rows
for (int i=0; i<aItems.size(); i++) {
int column = i/kMaxItemsPerColumn;
UITextButton button = (UITextButton)mLangRowContainer.getChildAt(i);
button.getLayoutParams().width = columnWidth[column];
}
}

Expand All @@ -98,11 +121,11 @@ public List<Item> getItems() {
private OnClickListener clickHandler = v -> {
UITextButton button = (UITextButton) v;
if (mDelegate != null) {
mDelegate.onLanguageClick((Item)button.getTag());
mDelegate.onItemClick((Item)button.getTag());
}
};

private UITextButton createLangButton(Item aItem) {
private UITextButton createItemButton(Item aItem) {
UITextButton button = new UITextButton(getContext());
button.setTintColorList(R.drawable.lang_selector_button_color);
button.setBackground(getContext().getDrawable(R.drawable.lang_selector_button_background));
Expand Down
Loading

0 comments on commit 61e7f55

Please sign in to comment.