Plug ResourceNotFoundException on Lenovo K910 phones

This commit is contained in:
serso 2017-07-24 10:42:38 +02:00
parent 20ba48b475
commit 43870fb9d3
2 changed files with 34 additions and 10 deletions

View File

@ -36,6 +36,7 @@ import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.IBinder; import android.os.IBinder;
import android.os.Looper; import android.os.Looper;
import android.support.annotation.ColorInt;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.v4.app.DialogFragment; import android.support.v4.app.DialogFragment;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
@ -124,7 +125,7 @@ public final class App {
} }
@NonNull @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); final SpannableString spannable = new SpannableString(s);
spannable.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE); spannable.setSpan(new ForegroundColorSpan(color), 0, s.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
return spannable; return spannable;

View File

@ -22,6 +22,16 @@
package org.solovyev.android.calculator.widget; 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.annotation.TargetApi;
import android.app.PendingIntent; import android.app.PendingIntent;
import android.appwidget.AppWidgetManager; import android.appwidget.AppWidgetManager;
@ -33,6 +43,7 @@ import android.content.res.Resources;
import android.graphics.Typeface; import android.graphics.Typeface;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.support.annotation.ColorInt;
import android.support.v4.content.ContextCompat; import android.support.v4.content.ContextCompat;
import android.text.SpannableString; import android.text.SpannableString;
import android.text.SpannableStringBuilder; import android.text.SpannableStringBuilder;
@ -40,21 +51,23 @@ import android.text.SpannedString;
import android.text.TextUtils; import android.text.TextUtils;
import android.text.style.StyleSpan; import android.text.style.StyleSpan;
import android.widget.RemoteViews; import android.widget.RemoteViews;
import org.solovyev.android.Check; 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.Preferences.SimpleTheme;
import org.solovyev.android.calculator.R;
import org.solovyev.android.calculator.buttons.CppButton; import org.solovyev.android.calculator.buttons.CppButton;
import java.util.EnumMap;
import javax.annotation.Nonnull; import javax.annotation.Nonnull;
import javax.annotation.Nullable; import javax.annotation.Nullable;
import javax.inject.Inject; 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 { public class CalculatorWidget extends AppWidgetProvider {
@ -88,7 +101,7 @@ public class CalculatorWidget extends AppWidgetProvider {
@Nonnull @Nonnull
private SpannedString initCursorString(@Nonnull Context context) { private SpannedString initCursorString(@Nonnull Context context) {
if (cursorString == null) { 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) // this will override any other style span (f.e. italic)
s.setSpan(new StyleSpan(Typeface.NORMAL), 0, 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE); s.setSpan(new StyleSpan(Typeface.NORMAL), 0, 1, SpannableString.SPAN_EXCLUSIVE_EXCLUSIVE);
cursorString = new SpannedString(s); cursorString = new SpannedString(s);
@ -96,6 +109,16 @@ public class CalculatorWidget extends AppWidgetProvider {
return cursorString; 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 @Override
public void onUpdate(@Nonnull Context context, public void onUpdate(@Nonnull Context context,
@Nonnull AppWidgetManager appWidgetManager, @Nonnull AppWidgetManager appWidgetManager,