Widget fixes

This commit is contained in:
Sergey Solovyev 2012-10-20 17:18:46 +04:00
parent 5f800daa25
commit a76fe608b7
4 changed files with 40 additions and 28 deletions

View File

@ -0,0 +1,3 @@
<resources>
<dimen name="widget_margin">0dp</dimen>
</resources>

View File

@ -18,6 +18,7 @@
<dimen name="widget_editor_text_size">25sp</dimen> <dimen name="widget_editor_text_size">25sp</dimen>
<dimen name="widget_keyboard_button_text_size">20dp</dimen> <dimen name="widget_keyboard_button_text_size">20dp</dimen>
<dimen name="widget_display_text_size">25sp</dimen> <dimen name="widget_display_text_size">25sp</dimen>
<dimen name="widget_margin">6dp</dimen>
<!--only for not multipane--> <!--only for not multipane-->
<dimen name="editor_padding">5dp</dimen> <dimen name="editor_padding">5dp</dimen>

View File

@ -82,6 +82,7 @@
<style name="widget_main_layout_style" parent="default_main_layout_style"> <style name="widget_main_layout_style" parent="default_main_layout_style">
<item name="android:padding">1dp</item> <item name="android:padding">1dp</item>
<item name="android:layout_margin">@dimen/widget_margin</item>
</style> </style>
<style name="default_actionbar_style" parent="@style/Widget.Sherlock.ActionBar"> <style name="default_actionbar_style" parent="@style/Widget.Sherlock.ActionBar">

View File

@ -70,7 +70,7 @@ public class CalculatorWidgetProvider extends AppWidgetProvider {
@NotNull @NotNull
private String getCursorColor(@NotNull Context context) { private String getCursorColor(@NotNull Context context) {
if ( cursorColor == null ) { if (cursorColor == null) {
cursorColor = Integer.toHexString(context.getResources().getColor(R.color.widget_cursor_color)).substring(2); cursorColor = Integer.toHexString(context.getResources().getColor(R.color.widget_cursor_color)).substring(2);
} }
return cursorColor; return cursorColor;
@ -82,6 +82,22 @@ public class CalculatorWidgetProvider extends AppWidgetProvider {
@NotNull int[] appWidgetIds) { @NotNull int[] appWidgetIds) {
super.onUpdate(context, appWidgetManager, appWidgetIds); super.onUpdate(context, appWidgetManager, appWidgetIds);
updateWidget(context, appWidgetManager, appWidgetIds, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), CalculatorLocatorImpl.getInstance().getDisplay().getViewState());
}
private void updateWidget(@NotNull Context context,
@NotNull CalculatorEditorViewState editorState,
@NotNull CalculatorDisplayViewState displayState) {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, CalculatorWidgetProvider.class));
updateWidget(context, appWidgetManager, appWidgetIds, editorState, displayState);
}
private void updateWidget(@NotNull Context context,
@NotNull AppWidgetManager appWidgetManager,
@NotNull int[] appWidgetIds,
@NotNull CalculatorEditorViewState editorState,
@NotNull CalculatorDisplayViewState displayState) {
for (int appWidgetId : appWidgetIds) { for (int appWidgetId : appWidgetIds) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout); final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
@ -95,6 +111,9 @@ public class CalculatorWidgetProvider extends AppWidgetProvider {
} }
} }
updateEditorState(context, views, editorState);
updateDisplayState(context, views, displayState);
appWidgetManager.updateAppWidget(appWidgetId, views); appWidgetManager.updateAppWidget(appWidgetId, views);
} }
} }
@ -115,60 +134,48 @@ public class CalculatorWidgetProvider extends AppWidgetProvider {
final Serializable object = intent.getSerializableExtra(EDITOR_STATE_EXTRA); final Serializable object = intent.getSerializableExtra(EDITOR_STATE_EXTRA);
if (object instanceof CalculatorEditorViewState) { if (object instanceof CalculatorEditorViewState) {
updateEditorState(context, (CalculatorEditorViewState) object); updateWidget(context, (CalculatorEditorViewState) object, CalculatorLocatorImpl.getInstance().getDisplay().getViewState());
} }
} else if (DISPLAY_STATE_CHANGED_ACTION.equals(intent.getAction())) { } else if (DISPLAY_STATE_CHANGED_ACTION.equals(intent.getAction())) {
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast received!"); CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "Display state changed broadcast received!");
final Serializable object = intent.getSerializableExtra(DISPLAY_STATE_EXTRA); final Serializable object = intent.getSerializableExtra(DISPLAY_STATE_EXTRA);
if (object instanceof CalculatorDisplayViewState) { if (object instanceof CalculatorDisplayViewState) {
updateDisplayState(context, (CalculatorDisplayViewState) object); updateWidget(context, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), (CalculatorDisplayViewState) object);
} }
} else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(intent.getAction())) {
updateWidget(context, CalculatorLocatorImpl.getInstance().getEditor().getViewState(), CalculatorLocatorImpl.getInstance().getDisplay().getViewState());
} }
} }
private void updateDisplayState(@NotNull Context context, @NotNull CalculatorDisplayViewState displayState) { private void updateDisplayState(@NotNull Context context, @NotNull RemoteViews views, @NotNull CalculatorDisplayViewState displayState) {
if (displayState.isValid()) { if (displayState.isValid()) {
setText(context, R.id.calculatorDisplay, displayState.getText()); setText(views, R.id.calculatorDisplay, displayState.getText());
setTextColor(context, R.id.calculatorDisplay, context.getResources().getColor(R.color.default_text_color)); setTextColor(views, R.id.calculatorDisplay, context.getResources().getColor(R.color.default_text_color));
} else { } else {
setTextColor(context, R.id.calculatorDisplay, context.getResources().getColor(R.color.display_error_text_color)); setTextColor(views, R.id.calculatorDisplay, context.getResources().getColor(R.color.display_error_text_color));
} }
} }
private void setText(@NotNull Context context, int textViewId, @Nullable CharSequence text) { private void setText(@NotNull RemoteViews views, int textViewId, @Nullable CharSequence text) {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); views.setTextViewText(textViewId, text);
final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, CalculatorWidgetProvider.class));
for (int appWidgetId : appWidgetIds) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setTextViewText(textViewId, text);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
} }
private void setTextColor(@NotNull Context context, int textViewId, int textColor) { private void setTextColor(@NotNull RemoteViews views, int textViewId, int textColor) {
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context); views.setTextColor(textViewId, textColor);
final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, CalculatorWidgetProvider.class));
for (int appWidgetId : appWidgetIds) {
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
views.setTextColor(textViewId, textColor);
appWidgetManager.updateAppWidget(appWidgetId, views);
}
} }
private void updateEditorState(@NotNull Context context, @NotNull CalculatorEditorViewState editorState) { private void updateEditorState(@NotNull Context context, @NotNull RemoteViews views, @NotNull CalculatorEditorViewState editorState) {
String text = editorState.getText(); String text = editorState.getText();
CharSequence newText = text; CharSequence newText = text;
int selection = editorState.getSelection(); int selection = editorState.getSelection();
if (selection >= 0 && selection <= text.length() ) { if (selection >= 0 && selection <= text.length()) {
// inject cursor // inject cursor
newText = Html.fromHtml(text.substring(0, selection) + "<font color=\"#" + getCursorColor(context) + "\">|</font>" + text.substring(selection)); newText = Html.fromHtml(text.substring(0, selection) + "<font color=\"#" + getCursorColor(context) + "\">|</font>" + text.substring(selection));
} }
CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "New editor state: " + text); CalculatorLocatorImpl.getInstance().getNotifier().showDebugMessage(TAG, "New editor state: " + text);
setText(context, R.id.calculatorEditor, newText); setText(views, R.id.calculatorEditor, newText);
} }
/* /*