2015-07-24 13:22:28 -07:00
|
|
|
package org.thoughtcrime.securesms.components;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.support.annotation.NonNull;
|
|
|
|
import android.support.annotation.Nullable;
|
|
|
|
import android.util.AttributeSet;
|
|
|
|
import android.widget.EditText;
|
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.components.KeyboardAwareLinearLayout.OnKeyboardShownListener;
|
|
|
|
import org.thoughtcrime.securesms.util.ServiceUtil;
|
|
|
|
|
|
|
|
public class InputAwareLayout extends KeyboardAwareLinearLayout implements OnKeyboardShownListener {
|
|
|
|
private InputView current;
|
|
|
|
|
|
|
|
public InputAwareLayout(Context context) {
|
|
|
|
this(context, null);
|
|
|
|
}
|
|
|
|
|
|
|
|
public InputAwareLayout(Context context, AttributeSet attrs) {
|
|
|
|
this(context, attrs, 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public InputAwareLayout(Context context, AttributeSet attrs, int defStyle) {
|
|
|
|
super(context, attrs, defStyle);
|
|
|
|
addOnKeyboardShownListener(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override public void onKeyboardShown() {
|
2015-07-28 12:40:13 -07:00
|
|
|
hideAttachedInput(true);
|
2015-07-24 13:22:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
public void show(@NonNull final EditText imeTarget, @NonNull final InputView input) {
|
|
|
|
if (isKeyboardOpen()) {
|
|
|
|
hideSoftkey(imeTarget, new Runnable() {
|
|
|
|
@Override public void run() {
|
2015-07-30 18:24:42 -07:00
|
|
|
hideAttachedInput(true);
|
2015-07-24 13:22:28 -07:00
|
|
|
input.show(getKeyboardHeight(), true);
|
2015-07-30 18:24:42 -07:00
|
|
|
current = input;
|
2015-07-24 13:22:28 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
} else {
|
2015-07-30 18:24:42 -07:00
|
|
|
if (current != null) current.hide(true);
|
|
|
|
input.show(getKeyboardHeight(), current != null);
|
|
|
|
current = input;
|
2015-07-24 13:22:28 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public InputView getCurrentInput() {
|
|
|
|
return current;
|
|
|
|
}
|
|
|
|
|
|
|
|
public void hideCurrentInput(EditText imeTarget) {
|
|
|
|
if (isKeyboardOpen()) hideSoftkey(imeTarget, null);
|
2015-07-28 12:40:13 -07:00
|
|
|
else hideAttachedInput(false);
|
2015-07-24 13:22:28 -07:00
|
|
|
}
|
|
|
|
|
2015-07-28 12:40:13 -07:00
|
|
|
public void hideAttachedInput(boolean instant) {
|
|
|
|
if (current != null) current.hide(instant);
|
2015-07-24 13:22:28 -07:00
|
|
|
current = null;
|
|
|
|
}
|
|
|
|
|
|
|
|
public boolean isInputOpen() {
|
|
|
|
return (isKeyboardOpen() || (current != null && current.isShowing()));
|
|
|
|
}
|
|
|
|
|
|
|
|
public void showSoftkey(final EditText inputTarget) {
|
|
|
|
postOnKeyboardOpen(new Runnable() {
|
|
|
|
@Override public void run() {
|
2015-07-28 12:40:13 -07:00
|
|
|
hideAttachedInput(true);
|
2015-07-24 13:22:28 -07:00
|
|
|
}
|
|
|
|
});
|
|
|
|
inputTarget.post(new Runnable() {
|
|
|
|
@Override public void run() {
|
|
|
|
inputTarget.requestFocus();
|
|
|
|
ServiceUtil.getInputMethodManager(inputTarget.getContext()).showSoftInput(inputTarget, 0);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-11-20 09:59:23 -08:00
|
|
|
public void hideSoftkey(final EditText inputTarget, @Nullable Runnable runAfterClose) {
|
2015-07-24 13:22:28 -07:00
|
|
|
if (runAfterClose != null) postOnKeyboardClose(runAfterClose);
|
|
|
|
|
|
|
|
ServiceUtil.getInputMethodManager(inputTarget.getContext())
|
|
|
|
.hideSoftInputFromWindow(inputTarget.getWindowToken(), 0);
|
|
|
|
}
|
|
|
|
|
|
|
|
public interface InputView {
|
|
|
|
void show(int height, boolean immediate);
|
|
|
|
void hide(boolean immediate);
|
|
|
|
boolean isShowing();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|