Skip to content

Commit

Permalink
Refactor keybord view creation and reset
Browse files Browse the repository at this point in the history
  • Loading branch information
menny committed Jun 21, 2018
1 parent 04e5276 commit 6fa9315
Show file tree
Hide file tree
Showing 21 changed files with 242 additions and 113 deletions.
86 changes: 38 additions & 48 deletions app/src/main/java/com/anysoftkeyboard/AnySoftKeyboard.java
Original file line number Diff line number Diff line change
Expand Up @@ -163,6 +163,7 @@ public abstract class AnySoftKeyboard extends AnySoftKeyboardWithGestureTyping {
private View mFullScreenExtractView;
private EditText mFullScreenExtractTextView;
private boolean mFrenchSpacePunctuationBehavior;
private ImageView mCandidatesCloseIcon;

public AnySoftKeyboard() {
super();
Expand Down Expand Up @@ -775,30 +776,14 @@ public void setCandidatesView(@NonNull View view) {
super.setCandidatesView(view);
mCandidatesParent = view.getParent() instanceof View ? (View) view.getParent() : null;

mCandidateCloseText = view.findViewById(R.id.close_suggestions_strip_text);
mCandidatesCloseIcon = view.findViewById(R.id.close_suggestions_strip_icon);

mCandidateView = view.findViewById(R.id.candidates);
mCandidateView.setService(this);
mCandidateView.setKeyboardTheme(getCurrentKeyboardTheme());
setCandidatesViewShown(false);

final KeyboardTheme theme = getCurrentKeyboardTheme();
final TypedArray a = theme.getPackageContext().obtainStyledAttributes(null, R.styleable.AnyKeyboardViewTheme, 0, theme.getThemeResId());
int closeTextColor = ContextCompat.getColor(this, R.color.candidate_other);
float fontSizePixel = getResources().getDimensionPixelSize(R.dimen.candidate_font_height);
Drawable suggestionCloseDrawable = null;
try {
closeTextColor = a.getColor(R.styleable.AnyKeyboardViewTheme_suggestionOthersTextColor, closeTextColor);
fontSizePixel = a.getDimension(R.styleable.AnyKeyboardViewTheme_suggestionTextSize, fontSizePixel);
suggestionCloseDrawable = a.getDrawable(R.styleable.AnyKeyboardViewTheme_suggestionCloseImage);
} catch (Exception e) {
Logger.w(TAG, e, "Failed to retrieve styleable values.");
}
a.recycle();

mCandidateCloseText = view.findViewById(R.id.close_suggestions_strip_text);
ImageView closeIcon = view.findViewById(R.id.close_suggestions_strip_icon);
if (suggestionCloseDrawable != null) closeIcon.setImageDrawable(suggestionCloseDrawable);

closeIcon.setOnClickListener(new OnClickListener() {
mCandidatesCloseIcon.setOnClickListener(new OnClickListener() {
// two seconds is enough.
private static final long DOUBLE_TAP_TIMEOUT = 2 * 1000 - 50;

Expand All @@ -810,14 +795,40 @@ public void onClick(View v) {
mKeyboardHandler.sendMessageDelayed(mKeyboardHandler.obtainMessage(KeyboardUIStateHandler.MSG_REMOVE_CLOSE_SUGGESTIONS_HINT), DOUBLE_TAP_TIMEOUT);
}
});

mCandidateCloseText.setTextColor(closeTextColor);
mCandidateCloseText.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSizePixel);
mCandidateCloseText.setOnClickListener(v -> {
mKeyboardHandler.removeMessages(KeyboardUIStateHandler.MSG_REMOVE_CLOSE_SUGGESTIONS_HINT);
mCandidateCloseText.setVisibility(View.GONE);
abortCorrectionAndResetPredictionState(true);
});

setCandidatesTheme(getCurrentKeyboardTheme());
}

@Override
protected void setCandidatesTheme(KeyboardTheme theme) {
if (mCandidateView == null) return;

mCandidateView.setKeyboardTheme(theme);

final int[] attrs = theme.getResourceMapping().getRemoteStyleableArrayFromLocal(R.styleable.AnyKeyboardViewTheme);
final TypedArray a = theme.getPackageContext().obtainStyledAttributes(theme.getThemeResId(), attrs);
int closeTextColor = ContextCompat.getColor(this, R.color.candidate_other);
float fontSizePixel = getResources().getDimensionPixelSize(R.dimen.candidate_font_height);
Drawable suggestionCloseDrawable = null;
try {
closeTextColor = a.getColor(R.styleable.AnyKeyboardViewTheme_suggestionOthersTextColor, closeTextColor);
fontSizePixel = a.getDimension(R.styleable.AnyKeyboardViewTheme_suggestionTextSize, fontSizePixel);
suggestionCloseDrawable = a.getDrawable(R.styleable.AnyKeyboardViewTheme_suggestionCloseImage);
} catch (Exception e) {
Logger.w(TAG, e, "Failed to retrieve styleable values.");
}
a.recycle();

if (suggestionCloseDrawable != null) mCandidatesCloseIcon.setImageDrawable(suggestionCloseDrawable);


mCandidateCloseText.setTextColor(closeTextColor);
mCandidateCloseText.setTextSize(TypedValue.COMPLEX_UNIT_PX, fontSizePixel);
}

private void clearSuggestions() {
Expand Down Expand Up @@ -1330,22 +1341,14 @@ private void sendEscape() {
@Override
public void onAlphabetKeyboardSet(@NonNull AnyKeyboard keyboard) {
super.onAlphabetKeyboardSet(keyboard);
setKeyboardForView(keyboard);
setKeyboardFinalStuff();
mFrenchSpacePunctuationBehavior = mSwapPunctuationAndSpace && keyboard.getLocale().toString().toLowerCase(Locale.US).startsWith("fr");
}

@Override
public void onSymbolsKeyboardSet(@NonNull AnyKeyboard keyboard) {
super.onSymbolsKeyboardSet(keyboard);
setKeyboardForView(keyboard);
}

private void setKeyboardForView(AnyKeyboard currentKeyboard) {
protected void setKeyboardForView(@NonNull AnyKeyboard currentKeyboard) {
currentKeyboard.setCondensedKeys(mKeyboardInCondensedMode);
if (getInputView() != null) {
getInputView().setKeyboard(currentKeyboard, getKeyboardSwitcher().peekNextAlphabetKeyboard(), getKeyboardSwitcher().peekNextSymbolsKeyboard());
}
super.setKeyboardForView(currentKeyboard);
}

private void showLanguageSelectionDialog() {
Expand Down Expand Up @@ -2315,15 +2318,13 @@ public void onSharedPreferenceChange(String key) {
key.equals("zoom_factor_keys_in_landscape") ||
key.equals(getString(R.string.settings_key_smiley_icon_on_smileys_key)) ||
key.equals(getString(R.string.settings_key_always_hide_language_key))) {
//this will recreate the keyboard view AND flush the keyboards cache.
resetAddOnsCaches(true);
onAddOnsCriticalChange(true);
} else if (key.startsWith(KeyboardFactory.PREF_ID_PREFIX) ||
key.startsWith(QuickTextKeyFactory.PREF_ID_PREFIX) ||
key.startsWith(KeyboardExtensionFactory.EXT_PREF_ID_PREFIX) ||
key.startsWith(KeyboardExtensionFactory.BOTTOM_ROW_PREF_ID_PREFIX) ||
key.startsWith(KeyboardExtensionFactory.TOP_ROW_PREF_ID_PREFIX)) {
//this will recreate the keyboard view AND flush the keyboards cache.
resetAddOnsCaches(true);
onAddOnsCriticalChange(true);
}
}

Expand Down Expand Up @@ -2364,17 +2365,6 @@ public void onCancel() {
//the user released their finger outside of any key... okay. I have nothing to do about that.
}

public void resetAddOnsCaches(boolean recreateView) {
hideWindow();
getKeyboardSwitcher().flushKeyboardsCache();
if (recreateView) {
// also recreate keyboard view
setInputView(onCreateInputView());
setCandidatesView(onCreateCandidatesView());
setCandidatesViewShown(false);
}
}

private void updateShiftStateNow() {
final InputConnection ic = getCurrentInputConnection();
EditorInfo ei = getCurrentInputEditorInfo();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,7 @@ public static void onExternalPackChanged(Intent eventIntent, AnySoftKeyboard ime
Logger.w("AddOnsFactory", e, "Failed to notify onExternalPackChanged on %s", factory);
}
}
if (cleared) ime.resetAddOnsCaches(recreateView);
if (cleared) ime.onAddOnsCriticalChange(recreateView);
}

public final List<E> getEnabledAddOns() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -164,7 +164,7 @@ public static Iterable<String> getLocalesFromDictionaryAddOns(@NonNull Context c
return Observable.fromIterable(AnyApplication.getExternalDictionaryFactory(context).getAllAddOns())
.filter(addOn -> !TextUtils.isEmpty(addOn.getLanguage()))
.map(DictionaryAddOnAndBuilder::getLanguage)
.distinct()
.distinct()//will not return any previously seen value
.blockingIterable();
}
}
12 changes: 12 additions & 0 deletions app/src/main/java/com/anysoftkeyboard/ime/AnySoftKeyboardBase.java
Original file line number Diff line number Diff line change
Expand Up @@ -180,6 +180,14 @@ protected void showOptionsDialogWithData(CharSequence title, @DrawableRes int ic
showNewOptionDialog(builder.create());
}

@CallSuper
public void onAddOnsCriticalChange(boolean recreateView) {
hideWindow();
if (recreateView) {
resetInputViews();
}
}

@Override
public View onCreateInputView() {
if (getInputView() != null) getInputView().onViewNotRequired();
Expand Down Expand Up @@ -211,6 +219,10 @@ public void setInputView(View view) {
updateSoftInputWindowLayoutParameters();
}

protected void resetInputViews() {
hideWindow();
}

@Override
public void updateFullscreenMode() {
super.updateFullscreenMode();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,16 +25,16 @@
import android.view.View;
import android.view.inputmethod.InputMethodSubtype;

import com.anysoftkeyboard.base.utils.Logger;
import com.anysoftkeyboard.keyboards.AnyKeyboard;
import com.anysoftkeyboard.keyboards.Keyboard;
import com.anysoftkeyboard.keyboards.KeyboardAddOnAndBuilder;
import com.anysoftkeyboard.keyboards.KeyboardSwitcher;
import com.anysoftkeyboard.base.utils.Logger;
import com.menny.android.anysoftkeyboard.AnyApplication;

import java.util.List;

public abstract class AnySoftKeyboardKeyboardSwitchedListener extends AnySoftKeyboardRxPrefs
public abstract class AnySoftKeyboardKeyboardSwitchedListener extends AnySoftKeyboardPowerSaving
implements KeyboardSwitcher.KeyboardSwitchedListener {

private KeyboardSwitcher mKeyboardSwitcher;
Expand Down Expand Up @@ -81,6 +81,12 @@ protected final KeyboardSwitcher getKeyboardSwitcher() {
return mKeyboardSwitcher;
}

@Override
public void onAddOnsCriticalChange(boolean recreateView) {
mKeyboardSwitcher.flushKeyboardsCache();
super.onAddOnsCriticalChange(recreateView);
}

@Override
public void onAlphabetKeyboardSet(@NonNull AnyKeyboard keyboard) {
mCurrentAlphabetKeyboard = keyboard;
Expand All @@ -99,6 +105,7 @@ public void onAlphabetKeyboardSet(@NonNull AnyKeyboard keyboard) {
public void onSymbolsKeyboardSet(@NonNull AnyKeyboard keyboard) {
mCurrentSymbolsKeyboard = keyboard;
mInAlphabetKeyboardMode = false;
setKeyboardForView(keyboard);
}

@Override
Expand Down Expand Up @@ -137,13 +144,21 @@ protected final AnyKeyboard getCurrentKeyboard() {
return mInAlphabetKeyboardMode ? mCurrentAlphabetKeyboard : mCurrentSymbolsKeyboard;
}

protected void setKeyboardForView(@NonNull AnyKeyboard keyboard) {
final InputViewBinder inputView = getInputView();
if (inputView != null) {
inputView.setKeyboard(keyboard, mKeyboardSwitcher.peekNextAlphabetKeyboard(), mKeyboardSwitcher.peekNextSymbolsKeyboard());
}
}

@TargetApi(Build.VERSION_CODES.HONEYCOMB)
@Override
protected void onCurrentInputMethodSubtypeChanged(InputMethodSubtype newSubtype) {
super.onCurrentInputMethodSubtypeChanged(newSubtype);
final String newSubtypeExtraValue = newSubtype.getExtraValue();
if (TextUtils.isEmpty(newSubtypeExtraValue))
if (TextUtils.isEmpty(newSubtypeExtraValue)) {
return;//this might mean this is NOT AnySoftKeyboard subtype.
}

if (shouldConsumeSubtypeChangedEvent(newSubtypeExtraValue)) {
mKeyboardSwitcher.nextAlphabetKeyboard(getCurrentInputEditorInfo(), newSubtypeExtraValue);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,73 @@
package com.anysoftkeyboard.ime;

import android.support.annotation.CallSuper;
import android.support.annotation.NonNull;
import android.view.View;

import com.anysoftkeyboard.powersave.PowerSaving;
import com.anysoftkeyboard.rx.GenericOnError;
import com.anysoftkeyboard.theme.KeyboardTheme;
import com.anysoftkeyboard.theme.KeyboardThemeFactory;

public abstract class AnySoftKeyboardPowerSaving extends AnySoftKeyboardRxPrefs {

private KeyboardTheme mCurrentKeyboardTheme;
private boolean mPowerState;

@Override
public void onCreate() {
super.onCreate();

addDisposable(PowerSaving.observePowerSavingState(getApplicationContext(), 0).subscribe(
powerState -> {
mPowerState = powerState;
setupInputViewWatermark();
},
GenericOnError.onError("Power-Saving icon")
));

addDisposable(KeyboardThemeFactory.observeCurrentTheme(getApplicationContext())
.subscribe(this::onKeyboardThemeChanged, GenericOnError.onError("Failed to observeCurrentTheme")));
}

@NonNull
@Override
protected String generateWatermark() {
return super.generateWatermark() + (mPowerState ? "\uD83D\uDD0B" : "");
}

@CallSuper
protected void onKeyboardThemeChanged(@NonNull KeyboardTheme theme) {
mCurrentKeyboardTheme = theme;

final InputViewBinder inputView = getInputView();
if (inputView != null) {
inputView.setKeyboardTheme(mCurrentKeyboardTheme);
setCandidatesTheme(mCurrentKeyboardTheme);
}
}

@Override
protected void resetInputViews() {
super.resetInputViews();
final InputViewBinder inputView = getInputView();
if (inputView != null) {
inputView.setKeyboardTheme(mCurrentKeyboardTheme);
setCandidatesTheme(mCurrentKeyboardTheme);
}
}

protected abstract void setCandidatesTheme(KeyboardTheme theme);

@Override
public View onCreateInputView() {
final View view = super.onCreateInputView();
getInputView().setKeyboardTheme(mCurrentKeyboardTheme);
return view;
}

protected final KeyboardTheme getCurrentKeyboardTheme() {
return mCurrentKeyboardTheme;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@
import com.anysoftkeyboard.base.utils.Logger;
import com.anysoftkeyboard.keyboards.Keyboard;
import com.anysoftkeyboard.powersave.PowerSaving;
import com.anysoftkeyboard.rx.GenericOnError;
import com.github.karczews.rxbroadcastreceiver.RxBroadcastReceivers;
import com.menny.android.anysoftkeyboard.R;

Expand All @@ -28,7 +27,6 @@ public abstract class AnySoftKeyboardPressEffects extends AnySoftKeyboardClipboa
private Vibrator mVibrator;
private int mVibrationDuration;
private int mVibrationDurationForLongPress;
private boolean mPowerState;

@Override
public void onCreate() {
Expand All @@ -37,14 +35,6 @@ public void onCreate() {
mAudioManager = (AudioManager) getSystemService(Context.AUDIO_SERVICE);
mVibrator = (Vibrator) getSystemService(Context.VIBRATOR_SERVICE);

addDisposable(PowerSaving.observePowerSavingState(getApplicationContext(), 0).subscribe(
powerState -> {
mPowerState = powerState;
setupInputViewWatermark();
},
GenericOnError.onError("Power-Saving icon")
));

addDisposable(Observable.combineLatest(
PowerSaving.observePowerSavingState(getApplicationContext(), R.string.settings_key_power_save_mode_sound_control),
RxBroadcastReceivers.fromIntentFilter(getApplicationContext(), new IntentFilter(AudioManager.RINGER_MODE_CHANGED_ACTION)).startWith(new Intent()),
Expand Down Expand Up @@ -94,12 +84,6 @@ public void onCreate() {
}, t -> Logger.w(TAG, t, "Failed to get vibrate duration")));
}

@NonNull
@Override
protected String generateWatermark() {
return super.generateWatermark() + (mPowerState ? "\uD83D\uDD0B" : "");
}

private void performKeySound(int primaryCode) {
if (mCustomSoundVolume != SILENT && primaryCode != 0) {
final int keyFX;
Expand Down
Loading

0 comments on commit 6fa9315

Please sign in to comment.