code reformat
This commit is contained in:
@@ -21,140 +21,140 @@ import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater;
|
||||
*/
|
||||
abstract class AbstractCalculatorWidgetProvider extends AppWidgetProvider implements ExternalCalculatorStateUpdater {
|
||||
|
||||
static final String BUTTON_ID_EXTRA = "buttonId";
|
||||
static final String BUTTON_PRESSED_ACTION = "org.solovyev.android.calculator.widget.BUTTON_PRESSED";
|
||||
static final String BUTTON_ID_EXTRA = "buttonId";
|
||||
static final String BUTTON_PRESSED_ACTION = "org.solovyev.android.calculator.widget.BUTTON_PRESSED";
|
||||
|
||||
private static final String TAG = "Calculator++ Widget";
|
||||
private static final String TAG = "Calculator++ Widget";
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* FIELDS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Nullable
|
||||
private String cursorColor;
|
||||
@Nullable
|
||||
private String cursorColor;
|
||||
|
||||
@NotNull
|
||||
private ExternalCalculatorIntentHandler intentHandler = new CalculatorWidgetIntentHandler(this);
|
||||
@NotNull
|
||||
private ExternalCalculatorIntentHandler intentHandler = new CalculatorWidgetIntentHandler(this);
|
||||
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* CONSTRUCTORS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
protected AbstractCalculatorWidgetProvider() {
|
||||
final Class<? extends AppWidgetProvider> componentClass = this.getComponentClass();
|
||||
protected AbstractCalculatorWidgetProvider() {
|
||||
final Class<? extends AppWidgetProvider> componentClass = this.getComponentClass();
|
||||
|
||||
Locator.getInstance().getExternalListenersContainer().addExternalListener(componentClass);
|
||||
}
|
||||
Locator.getInstance().getExternalListenersContainer().addExternalListener(componentClass);
|
||||
}
|
||||
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
/*
|
||||
**********************************************************************
|
||||
*
|
||||
* METHODS
|
||||
*
|
||||
**********************************************************************
|
||||
*/
|
||||
|
||||
@Override
|
||||
public void onEnabled(Context context) {
|
||||
super.onEnabled(context);
|
||||
@Override
|
||||
public void onEnabled(Context context) {
|
||||
super.onEnabled(context);
|
||||
|
||||
getCursorColor(context);
|
||||
}
|
||||
getCursorColor(context);
|
||||
}
|
||||
|
||||
@NotNull
|
||||
private String getCursorColor(@NotNull Context context) {
|
||||
if (cursorColor == null) {
|
||||
cursorColor = Integer.toHexString(context.getResources().getColor(R.color.cpp_widget_cursor_color)).substring(2);
|
||||
}
|
||||
return cursorColor;
|
||||
}
|
||||
@NotNull
|
||||
private String getCursorColor(@NotNull Context context) {
|
||||
if (cursorColor == null) {
|
||||
cursorColor = Integer.toHexString(context.getResources().getColor(R.color.cpp_widget_cursor_color)).substring(2);
|
||||
}
|
||||
return cursorColor;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onUpdate(@NotNull Context context,
|
||||
@NotNull AppWidgetManager appWidgetManager,
|
||||
@NotNull int[] appWidgetIds) {
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||
@Override
|
||||
public void onUpdate(@NotNull Context context,
|
||||
@NotNull AppWidgetManager appWidgetManager,
|
||||
@NotNull int[] appWidgetIds) {
|
||||
super.onUpdate(context, appWidgetManager, appWidgetIds);
|
||||
|
||||
updateWidget(context, appWidgetManager, appWidgetIds, Locator.getInstance().getEditor().getViewState(), Locator.getInstance().getDisplay().getViewState());
|
||||
}
|
||||
updateWidget(context, appWidgetManager, appWidgetIds, Locator.getInstance().getEditor().getViewState(), Locator.getInstance().getDisplay().getViewState());
|
||||
}
|
||||
|
||||
@Override
|
||||
public void updateState(@NotNull Context context,
|
||||
@NotNull CalculatorEditorViewState editorState,
|
||||
@NotNull CalculatorDisplayViewState displayState) {
|
||||
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||
final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getComponentClass()));
|
||||
updateWidget(context, appWidgetManager, appWidgetIds, editorState, displayState);
|
||||
}
|
||||
@Override
|
||||
public void updateState(@NotNull Context context,
|
||||
@NotNull CalculatorEditorViewState editorState,
|
||||
@NotNull CalculatorDisplayViewState displayState) {
|
||||
final AppWidgetManager appWidgetManager = AppWidgetManager.getInstance(context);
|
||||
final int[] appWidgetIds = appWidgetManager.getAppWidgetIds(new ComponentName(context, getComponentClass()));
|
||||
updateWidget(context, appWidgetManager, appWidgetIds, editorState, displayState);
|
||||
}
|
||||
|
||||
|
||||
@NotNull
|
||||
protected Class<? extends AbstractCalculatorWidgetProvider> getComponentClass(){
|
||||
return this.getClass();
|
||||
}
|
||||
@NotNull
|
||||
protected Class<? extends AbstractCalculatorWidgetProvider> getComponentClass() {
|
||||
return this.getClass();
|
||||
}
|
||||
|
||||
private void updateWidget(@NotNull Context context,
|
||||
@NotNull AppWidgetManager appWidgetManager,
|
||||
@NotNull int[] appWidgetIds,
|
||||
@NotNull CalculatorEditorViewState editorState,
|
||||
@NotNull CalculatorDisplayViewState displayState) {
|
||||
for (int appWidgetId : appWidgetIds) {
|
||||
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
|
||||
private void updateWidget(@NotNull Context context,
|
||||
@NotNull AppWidgetManager appWidgetManager,
|
||||
@NotNull int[] appWidgetIds,
|
||||
@NotNull CalculatorEditorViewState editorState,
|
||||
@NotNull CalculatorDisplayViewState displayState) {
|
||||
for (int appWidgetId : appWidgetIds) {
|
||||
final RemoteViews views = new RemoteViews(context.getPackageName(), R.layout.widget_layout);
|
||||
|
||||
for (CalculatorButton button : CalculatorButton.values()) {
|
||||
final Intent onButtonClickIntent = new Intent(context, getComponentClass());
|
||||
onButtonClickIntent.setAction(BUTTON_PRESSED_ACTION);
|
||||
onButtonClickIntent.putExtra(BUTTON_ID_EXTRA, button.getButtonId());
|
||||
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, button.getButtonId(), onButtonClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
if (pendingIntent != null) {
|
||||
views.setOnClickPendingIntent(button.getButtonId(), pendingIntent);
|
||||
}
|
||||
}
|
||||
for (CalculatorButton button : CalculatorButton.values()) {
|
||||
final Intent onButtonClickIntent = new Intent(context, getComponentClass());
|
||||
onButtonClickIntent.setAction(BUTTON_PRESSED_ACTION);
|
||||
onButtonClickIntent.putExtra(BUTTON_ID_EXTRA, button.getButtonId());
|
||||
final PendingIntent pendingIntent = PendingIntent.getBroadcast(context, button.getButtonId(), onButtonClickIntent, PendingIntent.FLAG_UPDATE_CURRENT);
|
||||
if (pendingIntent != null) {
|
||||
views.setOnClickPendingIntent(button.getButtonId(), pendingIntent);
|
||||
}
|
||||
}
|
||||
|
||||
updateEditorState(context, views, editorState);
|
||||
updateDisplayState(context, views, displayState);
|
||||
updateEditorState(context, views, editorState);
|
||||
updateDisplayState(context, views, displayState);
|
||||
|
||||
CalculatorButtons.initMultiplicationButton(views);
|
||||
CalculatorButtons.initMultiplicationButton(views);
|
||||
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
||||
}
|
||||
}
|
||||
appWidgetManager.updateAppWidget(appWidgetId, views);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
super.onReceive(context, intent);
|
||||
@Override
|
||||
public void onReceive(Context context, Intent intent) {
|
||||
super.onReceive(context, intent);
|
||||
|
||||
this.intentHandler.onIntent(context, intent);
|
||||
}
|
||||
this.intentHandler.onIntent(context, intent);
|
||||
}
|
||||
|
||||
private void updateDisplayState(@NotNull Context context, @NotNull RemoteViews views, @NotNull CalculatorDisplayViewState displayState) {
|
||||
if (displayState.isValid()) {
|
||||
views.setTextViewText(R.id.calculator_display, displayState.getText());
|
||||
views.setTextColor(R.id.calculator_display, context.getResources().getColor(R.color.cpp_default_text_color));
|
||||
} else {
|
||||
views.setTextColor(R.id.calculator_display, context.getResources().getColor(R.color.cpp_display_error_text_color));
|
||||
}
|
||||
}
|
||||
private void updateDisplayState(@NotNull Context context, @NotNull RemoteViews views, @NotNull CalculatorDisplayViewState displayState) {
|
||||
if (displayState.isValid()) {
|
||||
views.setTextViewText(R.id.calculator_display, displayState.getText());
|
||||
views.setTextColor(R.id.calculator_display, context.getResources().getColor(R.color.cpp_default_text_color));
|
||||
} else {
|
||||
views.setTextColor(R.id.calculator_display, context.getResources().getColor(R.color.cpp_display_error_text_color));
|
||||
}
|
||||
}
|
||||
|
||||
private void updateEditorState(@NotNull Context context, @NotNull RemoteViews views, @NotNull CalculatorEditorViewState editorState) {
|
||||
String text = editorState.getText();
|
||||
private void updateEditorState(@NotNull Context context, @NotNull RemoteViews views, @NotNull CalculatorEditorViewState editorState) {
|
||||
String text = editorState.getText();
|
||||
|
||||
CharSequence newText = text;
|
||||
int selection = editorState.getSelection();
|
||||
if (selection >= 0 && selection <= text.length()) {
|
||||
// inject cursor
|
||||
newText = Html.fromHtml(text.substring(0, selection) + "<font color=\"#" + getCursorColor(context) + "\">|</font>" + text.substring(selection));
|
||||
}
|
||||
Locator.getInstance().getNotifier().showDebugMessage(TAG, "New editor state: " + text);
|
||||
views.setTextViewText(R.id.calculator_editor, newText);
|
||||
}
|
||||
CharSequence newText = text;
|
||||
int selection = editorState.getSelection();
|
||||
if (selection >= 0 && selection <= text.length()) {
|
||||
// inject cursor
|
||||
newText = Html.fromHtml(text.substring(0, selection) + "<font color=\"#" + getCursorColor(context) + "\">|</font>" + text.substring(selection));
|
||||
}
|
||||
Locator.getInstance().getNotifier().showDebugMessage(TAG, "New editor state: " + text);
|
||||
views.setTextViewText(R.id.calculator_editor, newText);
|
||||
}
|
||||
}
|
||||
|
@@ -15,24 +15,24 @@ import org.solovyev.android.calculator.external.ExternalCalculatorStateUpdater;
|
||||
*/
|
||||
public class CalculatorWidgetIntentHandler extends DefaultExternalCalculatorIntentHandler {
|
||||
|
||||
public CalculatorWidgetIntentHandler(@NotNull ExternalCalculatorStateUpdater stateUpdater) {
|
||||
super(stateUpdater);
|
||||
}
|
||||
public CalculatorWidgetIntentHandler(@NotNull ExternalCalculatorStateUpdater stateUpdater) {
|
||||
super(stateUpdater);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onIntent(@NotNull Context context, @NotNull Intent intent) {
|
||||
super.onIntent(context, intent);
|
||||
@Override
|
||||
public void onIntent(@NotNull Context context, @NotNull Intent intent) {
|
||||
super.onIntent(context, intent);
|
||||
|
||||
if (AbstractCalculatorWidgetProvider.BUTTON_PRESSED_ACTION.equals(intent.getAction())) {
|
||||
final int buttonId = intent.getIntExtra(AbstractCalculatorWidgetProvider.BUTTON_ID_EXTRA, 0);
|
||||
if (AbstractCalculatorWidgetProvider.BUTTON_PRESSED_ACTION.equals(intent.getAction())) {
|
||||
final int buttonId = intent.getIntExtra(AbstractCalculatorWidgetProvider.BUTTON_ID_EXTRA, 0);
|
||||
|
||||
final CalculatorButton button = CalculatorButton.getById(buttonId);
|
||||
if (button != null) {
|
||||
button.onClick(context);
|
||||
}
|
||||
} else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(intent.getAction())) {
|
||||
updateState(context, Locator.getInstance().getEditor().getViewState(), Locator.getInstance().getDisplay().getViewState());
|
||||
}
|
||||
}
|
||||
final CalculatorButton button = CalculatorButton.getById(buttonId);
|
||||
if (button != null) {
|
||||
button.onClick(context);
|
||||
}
|
||||
} else if (Intent.ACTION_CONFIGURATION_CHANGED.equals(intent.getAction())) {
|
||||
updateState(context, Locator.getInstance().getEditor().getViewState(), Locator.getInstance().getDisplay().getViewState());
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
Reference in New Issue
Block a user