From 43870fb9d3685cb78d8c13e24ed6d7c8e58632a7 Mon Sep 17 00:00:00 2001 From: serso Date: Mon, 24 Jul 2017 10:42:38 +0200 Subject: [PATCH] Plug ResourceNotFoundException on Lenovo K910 phones --- .../org/solovyev/android/calculator/App.java | 3 +- .../calculator/widget/CalculatorWidget.java | 41 +++++++++++++++---- 2 files changed, 34 insertions(+), 10 deletions(-) diff --git a/app/src/main/java/org/solovyev/android/calculator/App.java b/app/src/main/java/org/solovyev/android/calculator/App.java index 9fed051d..9698125e 100644 --- a/app/src/main/java/org/solovyev/android/calculator/App.java +++ b/app/src/main/java/org/solovyev/android/calculator/App.java @@ -36,6 +36,7 @@ import android.net.Uri; import android.os.Build; import android.os.IBinder; import android.os.Looper; +import android.support.annotation.ColorInt; import android.support.annotation.NonNull; import android.support.v4.app.DialogFragment; import android.support.v4.app.Fragment; @@ -124,7 +125,7 @@ public final class App { } @NonNull - public static SpannableString colorString(@Nonnull String s, int color) { + public static SpannableString colorString(@Nonnull String s, @ColorInt int color) { final SpannableString spannable = new SpannableString(s); spannable.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); return spannable; 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 aab6a20c..e635b501 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 @@ -22,6 +22,16 @@ package org.solovyev.android.calculator.widget; +import static android.content.Intent.ACTION_CONFIGURATION_CHANGED; +import static android.os.Build.VERSION_CODES.JELLY_BEAN; + +import static org.solovyev.android.calculator.App.cast; +import static org.solovyev.android.calculator.Broadcaster.ACTION_DISPLAY_STATE_CHANGED; +import static org.solovyev.android.calculator.Broadcaster.ACTION_EDITOR_STATE_CHANGED; +import static org.solovyev.android.calculator.Broadcaster.ACTION_INIT; +import static org.solovyev.android.calculator.Broadcaster.ACTION_THEME_CHANGED; +import static org.solovyev.android.calculator.WidgetReceiver.newButtonClickedIntent; + import android.annotation.TargetApi; import android.app.PendingIntent; import android.appwidget.AppWidgetManager; @@ -33,6 +43,7 @@ import android.content.res.Resources; import android.graphics.Typeface; import android.os.Build; import android.os.Bundle; +import android.support.annotation.ColorInt; import android.support.v4.content.ContextCompat; import android.text.SpannableString; import android.text.SpannableStringBuilder; @@ -40,21 +51,23 @@ import android.text.SpannedString; import android.text.TextUtils; import android.text.style.StyleSpan; import android.widget.RemoteViews; + import org.solovyev.android.Check; -import org.solovyev.android.calculator.*; +import org.solovyev.android.calculator.App; +import org.solovyev.android.calculator.Display; +import org.solovyev.android.calculator.DisplayState; +import org.solovyev.android.calculator.Editor; +import org.solovyev.android.calculator.EditorState; +import org.solovyev.android.calculator.Engine; import org.solovyev.android.calculator.Preferences.SimpleTheme; +import org.solovyev.android.calculator.R; import org.solovyev.android.calculator.buttons.CppButton; +import java.util.EnumMap; + import javax.annotation.Nonnull; import javax.annotation.Nullable; import javax.inject.Inject; -import java.util.EnumMap; - -import static android.content.Intent.ACTION_CONFIGURATION_CHANGED; -import static android.os.Build.VERSION_CODES.JELLY_BEAN; -import static org.solovyev.android.calculator.App.cast; -import static org.solovyev.android.calculator.Broadcaster.*; -import static org.solovyev.android.calculator.WidgetReceiver.newButtonClickedIntent; public class CalculatorWidget extends AppWidgetProvider { @@ -88,7 +101,7 @@ public class CalculatorWidget extends AppWidgetProvider { @Nonnull private SpannedString initCursorString(@Nonnull Context context) { if (cursorString == null) { - final SpannableString s = App.colorString("|", ContextCompat.getColor(context, R.color.cpp_widget_cursor)); + final SpannableString s = App.colorString("|", readWidgetCursorColor(context)); // this will override any other style span (f.e. italic) s.setSpan(new StyleSpan(Typeface.NORMAL), 0, 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); cursorString = new SpannedString(s); @@ -96,6 +109,16 @@ public class CalculatorWidget extends AppWidgetProvider { return cursorString; } + @ColorInt + private int readWidgetCursorColor(@Nonnull Context context) { + try { + return ContextCompat.getColor(context, R.color.cpp_widget_cursor); + } catch (Resources.NotFoundException e) { + // on Lenovo K910 the color for some reason can't be found + return 0x757575; + } + } + @Override public void onUpdate(@Nonnull Context context, @Nonnull AppWidgetManager appWidgetManager,