From 926909a62dda1cfb9b1a72777789a21e6d4484fb Mon Sep 17 00:00:00 2001 From: serso Date: Wed, 6 Jan 2016 15:07:08 +0100 Subject: [PATCH] Show Widget Settings if settings button was clicked --- .../android/calculator/AndroidCalculator.java | 12 +++++----- .../CalculatorActivityLauncher.java | 6 +++++ .../android/calculator/CalculatorButton.java | 1 + .../calculator/CalculatorEventType.java | 1 + .../calculator/CalculatorSpecialButton.java | 12 +++++++--- .../plot/AbstractCalculatorPlotFragment.java | 24 ++++--------------- .../preferences/PreferencesActivity.java | 8 +++++-- .../calculator/widget/CalculatorWidget.java | 11 +++++++-- 8 files changed, 43 insertions(+), 32 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java index d293e0f0..bb5bee8d 100644 --- a/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java +++ b/app/src/main/java/org/solovyev/android/calculator/AndroidCalculator.java @@ -25,19 +25,16 @@ package org.solovyev.android.calculator; import android.app.Application; import android.content.SharedPreferences; import android.preference.PreferenceManager; - +import jscl.NumeralBase; +import jscl.math.Generic; import org.solovyev.android.calculator.history.CalculatorHistoryState; import org.solovyev.android.calculator.jscl.JsclOperation; import org.solovyev.common.history.HistoryAction; import org.solovyev.common.msg.Message; -import java.util.List; - import javax.annotation.Nonnull; import javax.annotation.Nullable; - -import jscl.NumeralBase; -import jscl.math.Generic; +import java.util.List; /** * User: serso @@ -219,6 +216,9 @@ public class AndroidCalculator implements Calculator, CalculatorEventListener, S case show_settings_detached: CalculatorActivityLauncher.showSettings(CalculatorApplication.getInstance(), true); break; + case show_settings_widget: + CalculatorActivityLauncher.showWidgetSettings(CalculatorApplication.getInstance(), true); + break; case show_like_dialog: CalculatorActivityLauncher.likeButtonPressed(CalculatorApplication.getInstance()); break; diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java index b8d19118..877e45b3 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorActivityLauncher.java @@ -75,6 +75,12 @@ public final class CalculatorActivityLauncher implements CalculatorEventListener showSettings(context, false); } + public static void showWidgetSettings(@Nonnull final Context context, boolean detached) { + final Intent intent = PreferencesActivity.makeIntent(context, R.xml.preferences_widget, R.string.prefs_widget_title); + Activities.addIntentFlags(intent, detached, context); + context.startActivity(intent); + } + public static void showSettings(@Nonnull final Context context, boolean detached) { final Intent intent = new Intent(context, PreferencesActivity.class); Activities.addIntentFlags(intent, detached, context); diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorButton.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorButton.java index e4b60c56..87f54188 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorButton.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorButton.java @@ -59,6 +59,7 @@ public enum CalculatorButton { brackets(R.id.cpp_button_round_brackets, "()"), settings(R.id.cpp_button_settings, settings_detached), + settings_widget(R.id.cpp_button_settings_widget, CalculatorSpecialButton.settings_widget), like(R.id.cpp_button_like, CalculatorSpecialButton.like), /*last row*/ diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java index facdfc3c..aa8fe879 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorEventType.java @@ -174,6 +174,7 @@ public enum CalculatorEventType { show_settings, show_settings_detached, + show_settings_widget, show_like_dialog, diff --git a/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java b/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java index fc78e65b..bfde70d7 100644 --- a/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java +++ b/app/src/main/java/org/solovyev/android/calculator/CalculatorSpecialButton.java @@ -24,11 +24,10 @@ package org.solovyev.android.calculator; import org.solovyev.common.text.Strings; -import java.util.HashMap; -import java.util.Map; - import javax.annotation.Nonnull; import javax.annotation.Nullable; +import java.util.HashMap; +import java.util.Map; /** * User: serso @@ -75,6 +74,13 @@ public enum CalculatorSpecialButton { } }, + settings_widget("settings_widget") { + @Override + public void onClick(@Nonnull CalculatorKeyboard keyboard) { + Locator.getInstance().getCalculator().fireCalculatorEvent(CalculatorEventType.show_settings_widget, null); + } + }, + like("like") { @Override public void onClick(@Nonnull CalculatorKeyboard keyboard) { diff --git a/app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java b/app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java index b332a894..f8936455 100644 --- a/app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java +++ b/app/src/main/java/org/solovyev/android/calculator/plot/AbstractCalculatorPlotFragment.java @@ -39,28 +39,17 @@ import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; import android.view.View; - import org.solovyev.android.Android; import org.solovyev.android.Threads; -import org.solovyev.android.calculator.App; -import org.solovyev.android.calculator.CalculatorApplication; -import org.solovyev.android.calculator.CalculatorEventData; -import org.solovyev.android.calculator.CalculatorEventHolder; -import org.solovyev.android.calculator.CalculatorEventListener; -import org.solovyev.android.calculator.CalculatorEventType; -import org.solovyev.android.calculator.CalculatorFragment; -import org.solovyev.android.calculator.CalculatorUtils; -import org.solovyev.android.calculator.Locator; +import org.solovyev.android.calculator.*; import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.preferences.PreferencesActivity; -import org.solovyev.android.menu.AMenuItem; -import org.solovyev.android.menu.ActivityMenu; -import org.solovyev.android.menu.AndroidMenuHelper; -import org.solovyev.android.menu.IdentifiableMenuItem; -import org.solovyev.android.menu.ListActivityMenu; +import org.solovyev.android.menu.*; import org.solovyev.common.JPredicate; import org.solovyev.common.msg.MessageType; +import javax.annotation.Nonnull; +import javax.annotation.Nullable; import java.io.File; import java.text.SimpleDateFormat; import java.util.ArrayList; @@ -70,9 +59,6 @@ import java.util.List; import java.util.concurrent.Executor; import java.util.concurrent.Executors; -import javax.annotation.Nonnull; -import javax.annotation.Nullable; - import static org.solovyev.android.calculator.model.AndroidCalculatorEngine.Preferences; /** @@ -433,7 +419,7 @@ public abstract class AbstractCalculatorPlotFragment extends CalculatorFragment preferences(R.id.menu_plot_settings) { @Override public void onClick(@Nonnull MenuItem data, @Nonnull Context context) { - PreferencesActivity.start(context, R.xml.preferences_plot, R.string.prefs_graph_screen_title); + PreferencesActivity.showPlotPreferences(context); } }; diff --git a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java index 24469890..b015f9cd 100644 --- a/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java +++ b/app/src/main/java/org/solovyev/android/calculator/preferences/PreferencesActivity.java @@ -51,13 +51,17 @@ public class PreferencesActivity extends BaseActivity implements SharedPreferenc return preferences; } - public static void start(@Nonnull Context context, @XmlRes int preference, @StringRes int title) { + public static void showPlotPreferences(@Nonnull Context context) { + start(context, R.xml.preferences_plot, R.string.prefs_graph_screen_title); + } + + private static void start(@Nonnull Context context, @XmlRes int preference, @StringRes int title) { final Intent intent = makeIntent(context, preference, title); context.startActivity(intent); } @Nonnull - static Intent makeIntent(@Nonnull Context context, @XmlRes int preference, @StringRes int title) { + public static Intent makeIntent(@Nonnull Context context, @XmlRes int preference, @StringRes int title) { final Intent intent = new Intent(context, PreferencesActivity.class); intent.putExtra(EXTRA_PREFERENCE, preference); if (title != 0) { diff --git a/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java b/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java index ee4c6994..425b8df6 100644 --- a/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java +++ b/app/src/main/java/org/solovyev/android/calculator/widget/CalculatorWidget.java @@ -111,9 +111,16 @@ public class CalculatorWidget extends AppWidgetProvider { final RemoteViews views = new RemoteViews(context.getPackageName(), getLayout(manager, widgetId, resources, theme)); for (CalculatorButton button : CalculatorButton.values()) { - final PendingIntent intent = PendingIntent.getBroadcast(context, button.getButtonId(), newButtonClickedIntent(context, button), PendingIntent.FLAG_UPDATE_CURRENT); + final int buttonId; + if (button == CalculatorButton.settings_widget) { + // overriding default settings button behavior + buttonId = CalculatorButton.settings.getButtonId(); + } else { + buttonId = button.getButtonId(); + } + final PendingIntent intent = PendingIntent.getBroadcast(context, buttonId, newButtonClickedIntent(context, button), PendingIntent.FLAG_UPDATE_CURRENT); if (intent != null) { - views.setOnClickPendingIntent(button.getButtonId(), intent); + views.setOnClickPendingIntent(buttonId, intent); } }