Skip to content

Commit

Permalink
Added specific action key to Keyboard (done,next,go...) #35 #45 #1
Browse files Browse the repository at this point in the history
fixed ui bug in notes
  • Loading branch information
UriahShaulMandel committed Aug 20, 2019
1 parent 5651ed2 commit 6415290
Show file tree
Hide file tree
Showing 18 changed files with 246 additions and 126 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,12 @@
import com.bald.uriah.baldphone.views.ModularRecyclerView;

public class ContactRecyclerViewAdapter extends ModularRecyclerView.ModularAdapter<ContactRecyclerViewAdapter.ViewHolder> {
public final static String[] PROJECTION =
{ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data._ID,
ContactsContract.Contacts.PHOTO_URI,
ContactsContract.Data.LOOKUP_KEY,
ContactsContract.Data.STARRED};
public final static String[] PROJECTION = {
ContactsContract.Data.DISPLAY_NAME,
ContactsContract.Data._ID,
ContactsContract.Contacts.PHOTO_URI,
ContactsContract.Data.LOOKUP_KEY,
ContactsContract.Data.STARRED};
public static final int MODE_DEFAULT = 0;
public static final int MODE_SOS = 1;
public static final int MODE_SHARE = 2;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,12 @@ public void onStartInputView(EditorInfo info, boolean restarting) {
}
}

public static boolean defaultEditorActionExists(final int imeOptions) {
return ((imeOptions & EditorInfo.IME_FLAG_NO_ENTER_ACTION) == 0) &&
(imeOptions & EditorInfo.IME_MASK_ACTION) != EditorInfo.IME_ACTION_NONE;

}

public void changeLanguage(int newLanguageKeyboard) {
keyboardFrame.removeAllViews();
if (newLanguageKeyboard != NumberKeyboard.LANGUAGE_ID)
Expand All @@ -90,17 +96,36 @@ public void changeLanguage(int newLanguageKeyboard) {

final View view =
newLanguageKeyboard != KeyboardPicker.LANGUAGE_ID ?
keyboard = BaldKeyboard.newInstance(newLanguageKeyboard, this, this, this::backspace)
keyboard = BaldKeyboard.newInstance(newLanguageKeyboard, this, this, this::backspace, getCurrentInputEditorInfo().imeOptions)
:
new KeyboardPicker(this);
keyboardFrame.addView(view, new FrameLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, point.x > point.y ? (int) (point.y * 0.8) : ViewGroup.LayoutParams.MATCH_PARENT));
}

@Override
public void onStartInput(EditorInfo attribute, boolean restarting) {
super.onStartInput(attribute, restarting);
if (keyboard != null)
keyboard.imeOptionsChanged(getCurrentInputEditorInfo().imeOptions);
}

public void startVoiceListening() {
final InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
if (voiceExists(imeManager)) {
final IBinder token = getWindow().getWindow().getAttributes().token;
imeManager.setInputMethod(token, VOICE_RECOGNITION_IMS);
}
}

public void backspace() {
getCurrentInputConnection().deleteSurroundingText(1, 0);
}

@Override
public void onClick(View v) {
final char code = (char) v.getTag();
final char actualCode = (char) v.getTag();
final InputConnection ic = getCurrentInputConnection();
final int actualCode = keyboard.codes()[code];
final EditorInfo editorInfo = getCurrentInputEditorInfo();
switch (actualCode) {
case BaldKeyboard.BACKSPACE:
backspace();
Expand All @@ -114,17 +139,11 @@ public void onClick(View v) {
}
break;
case BaldKeyboard.ENTER:
final boolean search = getCurrentInputEditorInfo().imeOptions == EditorInfo.IME_ACTION_SEARCH;
ic.sendKeyEvent(
new KeyEvent(KeyEvent.ACTION_DOWN,
search ? KeyEvent.KEYCODE_SEARCH : KeyEvent.KEYCODE_ENTER
)
);
ic.sendKeyEvent(
new KeyEvent(KeyEvent.ACTION_UP,
search ? KeyEvent.KEYCODE_SEARCH : KeyEvent.KEYCODE_ENTER
)
);
if (defaultEditorActionExists(editorInfo.imeOptions)) {
ic.performEditorAction(editorInfo.imeOptions & EditorInfo.IME_MASK_ACTION);
} else {
sendDownUpKeyEvents(KeyEvent.KEYCODE_ENTER);
}
break;
case BaldKeyboard.HIDE:
hideWindow();
Expand All @@ -140,20 +159,8 @@ public void onClick(View v) {
startVoiceListening();
break;
default:
final String str = String.valueOf(keyboard.codes()[code]);
final String str = String.valueOf(actualCode);
ic.commitText(str, 1);
}
}

public void startVoiceListening() {
final InputMethodManager imeManager = (InputMethodManager) getApplicationContext().getSystemService(INPUT_METHOD_SERVICE);
if (voiceExists(imeManager)) {
final IBinder token = getWindow().getWindow().getAttributes().token;
imeManager.setInputMethod(token, VOICE_RECOGNITION_IMS);
}
}

public void backspace() {
getCurrentInputConnection().deleteSurroundingText(1, 0);
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,26 @@
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.inputmethod.EditorInfo;
import android.widget.FrameLayout;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.Keep;
import androidx.annotation.LayoutRes;
import androidx.constraintlayout.widget.ConstraintLayout;

import com.bald.uriah.baldphone.R;
import com.bald.uriah.baldphone.utils.S;

import static android.view.inputmethod.EditorInfo.IME_ACTION_DONE;
import static android.view.inputmethod.EditorInfo.IME_ACTION_GO;
import static android.view.inputmethod.EditorInfo.IME_ACTION_NEXT;
import static android.view.inputmethod.EditorInfo.IME_ACTION_NONE;
import static android.view.inputmethod.EditorInfo.IME_ACTION_SEARCH;
import static android.view.inputmethod.EditorInfo.IME_ACTION_SEND;
import static android.view.inputmethod.EditorInfo.IME_ACTION_UNSPECIFIED;

public abstract class BaldKeyboard extends FrameLayout {
public static final char SHIFT = (char) 1;
public static final char LANGUAGE = (char) 2;
Expand All @@ -46,49 +58,98 @@ public abstract class BaldKeyboard extends FrameLayout {
protected final ConstraintLayout keyboard;
protected final View[] children;
protected final View backspace;

private final View enter;
private final TextView tv_enter;
private final ImageView iv_enter;
private final Vibrator vibrator;
private final Runnable backspaceRunnable;

private Thread backspaceThread;

@Keep
public BaldKeyboard(Context context, View.OnClickListener onClickListener, Runnable backspaceRunnable) {
public BaldKeyboard(final Context context, final View.OnClickListener onClickListener, final Runnable backspaceRunnable, final int imeOptions) {
super(context);
vibrator = (Vibrator) context.getSystemService(Context.VIBRATOR_SERVICE);
this.backspaceRunnable = backspaceRunnable;
final ContextThemeWrapper contextThemeWrapper = new ContextThemeWrapper(context, S.getTheme(context));
keyboard = (ConstraintLayout) LayoutInflater.from(contextThemeWrapper).inflate(layout(), this, false);
children = new View[keyboard.getChildCount()];
final char[] codes = codes();
for (int i = 0; i < children.length - 1/*cause of space view...*/; i++) {
final View view = keyboard.getChildAt(i + 1/*cause of space view...*/);
view.setOnClickListener(onClickListener);
view.setTag((char) i);
view.setTag(codes[i]);
children[i] = view;
}
backspace = keyboard.getChildAt(backspaceIndex());
backspace = keyboard.findViewById(R.id.backspace);
backspace.setOnTouchListener(getBackSpaceListener());
enter = keyboard.findViewById(R.id.enter);
tv_enter = keyboard.findViewById(R.id.tv_enter);
iv_enter = keyboard.findViewById(R.id.iv_enter);
imeOptionsChanged(imeOptions);
addView(keyboard);
}

public static BaldKeyboard newInstance(int language, Context context, View.OnClickListener onClickListener, Runnable backspaceRunnable) {
public static BaldKeyboard newInstance(int language, Context context, View.OnClickListener onClickListener, Runnable backspaceRunnable, int imeOptions) {
switch (language) {
case HebrewKeyboard.LANGUAGE_ID:
return new HebrewKeyboard(context, onClickListener, backspaceRunnable);
return new HebrewKeyboard(context, onClickListener, backspaceRunnable, imeOptions);
case EnglishKeyboard.LANGUAGE_ID:
return new EnglishKeyboard(context, onClickListener, backspaceRunnable);
return new EnglishKeyboard(context, onClickListener, backspaceRunnable, imeOptions);
case NumberKeyboard.LANGUAGE_ID:
return new NumberKeyboard(context, onClickListener, backspaceRunnable);
return new NumberKeyboard(context, onClickListener, backspaceRunnable, imeOptions);
default:
throw new IllegalArgumentException("language must be 0/1/2, it is currently:" + language);
}

}

@LayoutRes
protected abstract int layout();

abstract char[] codes();

protected abstract int backspaceIndex();
protected void imeOptionsChanged(int imeOptions) {
final int relevantImeOptions = imeOptions & EditorInfo.IME_MASK_ACTION;
if (BaldInputMethodService.defaultEditorActionExists(imeOptions)) {
switch (relevantImeOptions) {
case IME_ACTION_SEARCH:
tv_enter.setText(R.string.search);
iv_enter.setImageResource(R.drawable.search_on_button);
break;
case IME_ACTION_DONE:
tv_enter.setText(R.string.done);
iv_enter.setImageResource(R.drawable.check_on_button);
break;
case IME_ACTION_GO:
tv_enter.setText(R.string.go);
iv_enter.setImageResource(R.drawable.check_on_button);
break;
case IME_ACTION_SEND:
tv_enter.setText(R.string.send);
iv_enter.setImageResource(R.drawable.send_on_button);
break;
case IME_ACTION_NEXT:
tv_enter.setText(R.string.next);
iv_enter.setImageResource(R.drawable.arrow_end_on_background);
break;
//should never be here
case IME_ACTION_NONE:
case IME_ACTION_UNSPECIFIED:
default:
tv_enter.setText(R.string.enter);
iv_enter.setImageResource(R.drawable.enter_on_keyboard);
break;
}
} else {
tv_enter.setText(R.string.enter);
iv_enter.setImageResource(R.drawable.enter_on_keyboard);
}

}

// protected abstract View
abstract int nextLanguage();

private View.OnTouchListener getBackSpaceListener() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -44,8 +44,8 @@ public class EnglishKeyboard extends BaldKeyboard implements BaldKeyboard.Capita
};
private boolean caps;

public EnglishKeyboard(Context context, OnClickListener onClickListener, Runnable backspace) {
super(context, onClickListener, backspace);
public EnglishKeyboard(Context context, OnClickListener onClickListener, Runnable backspace, int imeOptions) {
super(context, onClickListener, backspace, imeOptions);
}

public void setCaps() {
Expand Down Expand Up @@ -75,8 +75,4 @@ protected char[] codes() {
return caps ? usKeyboardCodesCAPS : usKeyboardCodes;
}

@Override
protected int backspaceIndex() {
return 29;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,8 @@ public class HebrewKeyboard extends BaldKeyboard {

};

public HebrewKeyboard(Context context, OnClickListener onClickListener, Runnable backspace) {
super(context, onClickListener, backspace);
public HebrewKeyboard(Context context, OnClickListener onClickListener, Runnable backspace, int imeOptions) {
super(context, onClickListener, backspace, imeOptions);
}

@Override
Expand All @@ -55,8 +55,4 @@ protected char[] codes() {
return hebrewKeyboardCodes;
}

@Override
protected int backspaceIndex() {
return 9;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,8 @@ public class NumberKeyboard extends BaldKeyboard {
NUMBERS, SPEECH_TO_TEXT, ' ', HIDE, '.', ENTER,
};

public NumberKeyboard(Context context, OnClickListener onClickListener, Runnable backspace) {
super(context, onClickListener, backspace);
public NumberKeyboard(Context context, OnClickListener onClickListener, Runnable backspace, int imeOptions) {
super(context, onClickListener, backspace, imeOptions);
}

@Override
Expand All @@ -53,8 +53,4 @@ protected char[] codes() {
return numbersKeyboardsCodes;
}

@Override
protected int backspaceIndex() {
return 29;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -52,11 +52,7 @@ public NotesView(@NonNull HomeScreenActivity activity) {
@Override
public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container) {
homeScreen.recognizerManager.setNotesFragment(this);

sharedPreferences =
getContext()
.getSharedPreferences(BPrefs.KEY, Context.MODE_PRIVATE);
if (sharedPreferences == null) throw new AssertionError();
sharedPreferences = BPrefs.get(getContext());
final View view = inflater.inflate(R.layout.notes_fragment, container, false);
editText = view.findViewById(R.id.edit_text);
editText.setText(sharedPreferences.getString(BPrefs.NOTE_KEY, ""));
Expand Down Expand Up @@ -135,5 +131,4 @@ public void setNotesFragment(NotesView notesFragment) {
this.notesFragment = new WeakReference<>(notesFragment);
}
}

}
}
File renamed without changes.
28 changes: 28 additions & 0 deletions app/src/main/res/drawable/search_on_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?bald_decoration_on_button"
android:pathData="M15.5,14h-0.79l-0.28,-0.27C15.41,12.59 16,11.11 16,9.5 16,5.91 13.09,3 9.5,3S3,5.91 3,9.5 5.91,16 9.5,16c1.61,0 3.09,-0.59 4.23,-1.57l0.27,0.28v0.79l5,4.99L20.49,19l-4.99,-5zM9.5,14C7.01,14 5,11.99 5,9.5S7.01,5 9.5,5 14,7.01 14,9.5 11.99,14 9.5,14z" />
</vector>
28 changes: 28 additions & 0 deletions app/src/main/res/drawable/send_on_button.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<!--
~ Licensed to the Apache Software Foundation (ASF) under one
~ or more contributor license agreements. See the NOTICE file
~ distributed with this work for additional information
~ regarding copyright ownership. The ASF licenses this file
~ to you under the Apache License, Version 2.0 (the
~ "License"); you may not use this file except in compliance
~ with the License. You may obtain a copy of the License at
~
~ http://www.apache.org/licenses/LICENSE-2.0
~
~ Unless required by applicable law or agreed to in writing,
~ software distributed under the License is distributed on an
~ "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
~ KIND, either express or implied. See the License for the
~ specific language governing permissions and limitations
~ under the License.
-->

<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="48dp"
android:height="48dp"
android:viewportWidth="24.0"
android:viewportHeight="24.0">
<path
android:fillColor="?bald_decoration_on_button"
android:pathData="M2.01,21L23,12 2.01,3 2,10l15,2 -15,2z" />
</vector>
2 changes: 1 addition & 1 deletion app/src/main/res/layout/he_keyboard_layout.xml
Original file line number Diff line number Diff line change
Expand Up @@ -748,7 +748,7 @@
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginBottom="16dp"
android:src="@drawable/enter_test"
android:src="@drawable/enter_on_keyboard"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
Expand Down
Loading

0 comments on commit 6415290

Please sign in to comment.