Widget fix

This commit is contained in:
serso 2016-01-07 17:25:46 +01:00
parent 0467fd9b4f
commit 40390b537d

View File

@ -34,11 +34,19 @@ import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.text.*; import android.text.SpannableStringBuilder;
import android.text.SpannedString;
import android.text.TextUtils;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import org.solovyev.android.Views; import org.solovyev.android.Views;
import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.App;
import org.solovyev.android.calculator.CalculatorButton;
import org.solovyev.android.calculator.CalculatorDisplayViewState;
import org.solovyev.android.calculator.CalculatorEditorViewState;
import org.solovyev.android.calculator.Locator;
import org.solovyev.android.calculator.Preferences.SimpleTheme; import org.solovyev.android.calculator.Preferences.SimpleTheme;
import org.solovyev.android.calculator.R;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
@ -46,7 +54,9 @@ import javax.annotation.Nullable;
import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT; import static android.appwidget.AppWidgetManager.OPTION_APPWIDGET_MIN_HEIGHT;
import static android.content.Intent.ACTION_CONFIGURATION_CHANGED; import static android.content.Intent.ACTION_CONFIGURATION_CHANGED;
import static android.os.Build.VERSION_CODES.JELLY_BEAN; import static android.os.Build.VERSION_CODES.JELLY_BEAN;
import static org.solovyev.android.calculator.CalculatorBroadcaster.*; import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_DISPLAY_STATE_CHANGED;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_EDITOR_STATE_CHANGED;
import static org.solovyev.android.calculator.CalculatorBroadcaster.ACTION_THEME_CHANGED;
import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent; import static org.solovyev.android.calculator.CalculatorReceiver.newButtonClickedIntent;
public class CalculatorWidget extends AppWidgetProvider { public class CalculatorWidget extends AppWidgetProvider {
@ -197,22 +207,28 @@ public class CalculatorWidget extends AppWidgetProvider {
private void updateEditorState(@Nonnull Context context, @Nonnull RemoteViews views, @Nonnull CalculatorEditorViewState editorState, @Nonnull SimpleTheme theme) { private void updateEditorState(@Nonnull Context context, @Nonnull RemoteViews views, @Nonnull CalculatorEditorViewState editorState, @Nonnull SimpleTheme theme) {
final CharSequence text = editorState.getTextAsCharSequence(); final CharSequence text = editorState.getTextAsCharSequence();
final boolean unspan = App.getTheme().light != theme.light; final boolean unspan = App.getTheme().light != theme.light;
CharSequence newText = text;
int selection = editorState.getSelection(); final int selection = editorState.getSelection();
if (selection >= 0 && selection <= text.length()) { if (selection < 0 || selection > text.length()) {
views.setTextViewText(R.id.calculator_editor, unspan ? unspan(text) : text);
return;
}
final SpannableStringBuilder result;
// inject cursor // inject cursor
final SpannableStringBuilder result = new SpannableStringBuilder(); if (unspan) {
result = new SpannableStringBuilder();
final CharSequence beforeCursor = text.subSequence(0, selection); final CharSequence beforeCursor = text.subSequence(0, selection);
result.append(unspan ? unspan(beforeCursor) : beforeCursor); result.append(unspan(beforeCursor));
result.append(getCursorString(context)); result.append(getCursorString(context));
final CharSequence afterCursor = text.subSequence(selection, text.length()); final CharSequence afterCursor = text.subSequence(selection, text.length());
result.append(unspan ? unspan(afterCursor) : afterCursor); result.append(unspan(afterCursor));
newText = result; } else {
result = new SpannableStringBuilder(text);
result.insert(selection, getCursorString(context));
} }
Locator.getInstance().getNotifier().showDebugMessage(TAG, "New editor state: " + text); views.setTextViewText(R.id.calculator_editor, result);
views.setTextViewText(R.id.calculator_editor, newText);
} }
@NonNull @NonNull