org.solovyev.android.keyboard.AbstractAndroidKeyboardController Maven / Gradle / Ivy
/*
* Copyright 2013 serso aka se.solovyev
*
* Licensed 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.
*
* ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
* Contact details
*
* Email: [email protected]
* Site: http://se.solovyev.org
*/
package org.solovyev.android.keyboard;
import android.content.Context;
import android.view.View;
import android.view.inputmethod.CompletionInfo;
import android.view.inputmethod.EditorInfo;
import org.solovyev.common.text.Strings;
import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
/**
* User: serso
* Date: 11/3/12
* Time: 1:11 PM
*/
public abstract class AbstractAndroidKeyboardController extends AbstractKeyboardController {
@Override
public void onStartInput(@Nonnull EditorInfo attribute, boolean restarting) {
super.onStartInput(attribute, restarting);
updateCandidates();
getKeyboardView().setCompletions(Collections.emptyList());
}
@Nonnull
@Override
public AKeyboardViewWithSuggestions getKeyboardView() {
return (AKeyboardViewWithSuggestions) super.getKeyboardView();
}
@Override
public void onFinishInput() {
super.onFinishInput();
updateCandidates();
}
@Override
public boolean handleBackspace() {
boolean changed = super.handleBackspace();
if (changed) {
updateCandidates();
}
return changed;
}
@Nonnull
@Override
protected abstract AKeyboardViewWithSuggestions createKeyboardView0(@Nonnull Context context);
public void setSuggestions(@Nonnull List suggestions,
boolean completions,
boolean typedWordValid) {
final AKeyboardViewWithSuggestions keyboardView = getKeyboardView();
if (suggestions.size() > 0) {
keyboardView.setCandidatesViewShown(true);
} else if (keyboardView.isExtractViewShown()) {
keyboardView.setCandidatesViewShown(true);
}
keyboardView.setSuggestions(suggestions, completions, typedWordValid);
}
@Override
public void onDisplayCompletions(@Nullable CompletionInfo[] completions) {
super.onDisplayCompletions(completions);
if (getState().isCompletion()) {
if (completions == null) {
setSuggestions(Collections.emptyList(), false, false);
} else {
final List suggestions = new ArrayList();
for (CompletionInfo completion : Arrays.asList(completions)) {
if (completion != null) {
suggestions.add(completion.getText().toString());
}
}
setSuggestions(suggestions, true, true);
}
}
}
/**
* Update the list of available candidates from the current composing
* text. This will need to be filled in by however you are determining
* candidates.
*/
protected void updateCandidates() {
if (!getState().isCompletion()) {
final CharSequence text = getKeyboardInput().getTypedText();
if (!Strings.isEmpty(text)) {
final List list = new ArrayList();
list.add(text.toString());
setSuggestions(list, true, true);
} else {
setSuggestions(Collections.emptyList(), false, false);
}
}
}
@Override
public void handleClose() {
super.handleClose();
updateCandidates();
}
@Override
public void pickSuggestionManually(int index) {
super.pickSuggestionManually(index);
final AKeyboardViewWithSuggestions keyboardView = getKeyboardView();
final AKeyboardInput keyboardInput = getKeyboardInput();
final List completions = keyboardView.getCompletions();
final CharSequence text = keyboardInput.getTypedText();
if (getState().isCompletion() && index >= 0 && index < completions.size()) {
final CompletionInfo ci = completions.get(index);
keyboardInput.commitCompletion(ci);
keyboardView.clearCandidateView();
updateShiftKeyState(keyboardInput.getCurrentInputEditorInfo());
} else if (!Strings.isEmpty(text)) {
// If we were generating candidate suggestions for the current
// text, we would commit one of them here. But for this sample,
// we will just commit the current text.
keyboardInput.commitTyped();
}
}
@Override
public View onCreateCandidatesView() {
return this.getKeyboardView().onCreateCandidatesView();
}
}
© 2015 - 2024 Weber Informatics LLC | Privacy Policy