From 54b124cde9d498d35d596a030bc3acf8a167ba10 Mon Sep 17 00:00:00 2001 From: Jonas Lochmann Date: Mon, 12 Aug 2019 00:00:00 +0000 Subject: [PATCH] Add alternative keyboard at the authentication screen --- .../android/ui/login/NewLoginFragment.kt | 39 ++++++++- .../timelimit/android/ui/view/KeyboardView.kt | 82 +++++++++++++++++++ .../res/drawable/ic_keyboard_black_24dp.xml | 9 ++ app/src/main/res/layout/keyboard_btn.xml | 19 +++++ .../layout/new_login_fragment_password.xml | 35 ++++++-- 5 files changed, 176 insertions(+), 8 deletions(-) create mode 100644 app/src/main/java/io/timelimit/android/ui/view/KeyboardView.kt create mode 100644 app/src/main/res/drawable/ic_keyboard_black_24dp.xml create mode 100644 app/src/main/res/layout/keyboard_btn.xml diff --git a/app/src/main/java/io/timelimit/android/ui/login/NewLoginFragment.kt b/app/src/main/java/io/timelimit/android/ui/login/NewLoginFragment.kt index d96e11c..af26a10 100644 --- a/app/src/main/java/io/timelimit/android/ui/login/NewLoginFragment.kt +++ b/app/src/main/java/io/timelimit/android/ui/login/NewLoginFragment.kt @@ -16,6 +16,7 @@ package io.timelimit.android.ui.login import android.content.Context +import android.os.Build import android.os.Bundle import android.view.LayoutInflater import android.view.View @@ -32,6 +33,7 @@ import io.timelimit.android.data.model.User import io.timelimit.android.databinding.NewLoginFragmentBinding import io.timelimit.android.extensions.setOnEnterListenr import io.timelimit.android.ui.main.getActivityViewModel +import io.timelimit.android.ui.view.KeyboardViewListener class NewLoginFragment: DialogFragment() { companion object { @@ -94,7 +96,21 @@ class NewLoginFragment: DialogFragment() { binding.userList.recycler.layoutManager = LinearLayoutManager(context) binding.enterPassword.apply { - password.setOnEnterListenr { + showKeyboardButton.setOnClickListener { + showCustomKeyboard = !showCustomKeyboard + + if (showCustomKeyboard) { + inputMethodManager.hideSoftInputFromWindow(password.windowToken, 0) + } else { + inputMethodManager.showSoftInput(password, 0) + } + + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) { + password.showSoftInputOnFocus = !showCustomKeyboard + } + } + + fun go() { model.tryParentLogin( password = password.text.toString(), keepSignedIn = checkDontAskAgain.isChecked, @@ -102,6 +118,21 @@ class NewLoginFragment: DialogFragment() { setAsDeviceUser = checkAssignMyself.isChecked ) } + + keyboard.listener = object: KeyboardViewListener { + override fun onItemClicked(content: String) { + val start = Math.max(password.selectionStart, 0) + val end = Math.max(password.selectionEnd, 0) + + password.text.replace(Math.min(start, end), Math.max(start, end), content, 0, content.length) + } + + override fun onGoClicked() { + go() + } + } + + password.setOnEnterListenr { go() } } binding.childPassword.apply { @@ -134,8 +165,10 @@ class NewLoginFragment: DialogFragment() { binding.switcher.displayedChild = PARENT_AUTH } - binding.enterPassword.password.requestFocus() - inputMethodManager.showSoftInput(binding.enterPassword.password, 0) + if (!binding.enterPassword.showCustomKeyboard) { + binding.enterPassword.password.requestFocus() + inputMethodManager.showSoftInput(binding.enterPassword.password, 0) + } binding.enterPassword.showKeepLoggedInOption = status.isConnectedMode diff --git a/app/src/main/java/io/timelimit/android/ui/view/KeyboardView.kt b/app/src/main/java/io/timelimit/android/ui/view/KeyboardView.kt new file mode 100644 index 0000000..14ca97d --- /dev/null +++ b/app/src/main/java/io/timelimit/android/ui/view/KeyboardView.kt @@ -0,0 +1,82 @@ +/* + * TimeLimit Copyright 2019 Jonas Lochmann + * + * This program is free software: you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation version 3 of the License. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program. If not, see . + */ +package io.timelimit.android.ui.view + +import android.content.Context +import android.util.AttributeSet +import android.view.LayoutInflater +import android.widget.Button +import android.widget.HorizontalScrollView +import com.google.android.flexbox.FlexDirection +import com.google.android.flexbox.FlexWrap +import com.google.android.flexbox.FlexboxLayout +import io.timelimit.android.R + +class KeyboardView(context: Context, attributeSet: AttributeSet): HorizontalScrollView(context, attributeSet) { + companion object { + private const val chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ 0123456789.,-" + } + + var listener: KeyboardViewListener? = null + + private val flexbox = FlexboxLayout(context) + private val inflater = LayoutInflater.from(context) + + init { + flexbox.flexDirection = FlexDirection.COLUMN + flexbox.flexWrap = FlexWrap.WRAP + + addView(flexbox) + + chars.forEach { char -> + inflater.inflate(R.layout.keyboard_btn, flexbox, false).let { button -> + button as Button + + button.apply { + transformationMethod = null + text = char.toString() + minWidth = 0 + minHeight = 0 + + setOnClickListener { + listener?.onItemClicked(char.toString()) + } + } + + flexbox.addView(button) + } + } + + inflater.inflate(R.layout.keyboard_btn, flexbox, false).let { button -> + button as Button + + button.apply { + setText(R.string.generic_go) + + setOnClickListener { + listener?.onGoClicked() + } + } + + flexbox.addView(button) + } + } +} + +interface KeyboardViewListener { + fun onItemClicked(content: String) + fun onGoClicked() +} \ No newline at end of file diff --git a/app/src/main/res/drawable/ic_keyboard_black_24dp.xml b/app/src/main/res/drawable/ic_keyboard_black_24dp.xml new file mode 100644 index 0000000..f0b93eb --- /dev/null +++ b/app/src/main/res/drawable/ic_keyboard_black_24dp.xml @@ -0,0 +1,9 @@ + + + diff --git a/app/src/main/res/layout/keyboard_btn.xml b/app/src/main/res/layout/keyboard_btn.xml new file mode 100644 index 0000000..4f226b6 --- /dev/null +++ b/app/src/main/res/layout/keyboard_btn.xml @@ -0,0 +1,19 @@ + + +