Use some Compat classes

This commit is contained in:
TacoTheDank 2021-01-05 00:05:22 -05:00 committed by Niedermann IT-Dienstleistungen
parent 42f02172cb
commit 56d7782513
9 changed files with 28 additions and 38 deletions

View file

@ -13,6 +13,8 @@ import android.widget.EditText;
import androidx.annotation.ColorInt;
import androidx.annotation.IdRes;
import androidx.annotation.NonNull;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.preference.PreferenceManager;
@ -43,7 +45,7 @@ public class BrandingUtil {
Log.v(TAG, "--- Read: shared_preference_theme_main");
return sharedPreferences.getInt(pref_key_branding_main, context.getApplicationContext().getResources().getColor(R.color.defaultBrand));
} else {
return context.getResources().getColor(R.color.defaultBrand);
return ContextCompat.getColor(context, R.color.defaultBrand);
}
}
@ -70,7 +72,7 @@ public class BrandingUtil {
if (isBrandingEnabled(context) && context instanceof BrandedActivity) {
if (mainColor != previousMainColor || textColor != previousTextColor) {
final BrandedActivity activity = (BrandedActivity) context;
activity.runOnUiThread(activity::recreate);
activity.runOnUiThread(() -> ActivityCompat.recreate(activity));
}
}
}
@ -80,7 +82,7 @@ public class BrandingUtil {
*/
@ColorInt
public static int getSecondaryForegroundColorDependingOnTheme(@NonNull Context context, @ColorInt int mainColor) {
final int primaryColor = context.getResources().getColor(R.color.primary);
final int primaryColor = ContextCompat.getColor(context, R.color.primary);
final boolean isDarkTheme = NotesApplication.isDarkThemeActive(context);
if (isDarkTheme && !contrastRatioIsSufficient(mainColor, primaryColor)) {
Log.v(TAG, "Contrast ratio between brand color " + String.format("#%06X", (0xFFFFFF & mainColor)) + " and dark theme is too low. Falling back to WHITE as brand color.");

View file

@ -9,6 +9,7 @@ import android.widget.TextView;
import androidx.annotation.NonNull;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.RecyclerView;
@ -50,17 +51,17 @@ public class CategoryAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolde
switch (category.id) {
case addItemId:
Drawable wrapDrawable = DrawableCompat.wrap(context.getResources().getDrawable(category.icon));
DrawableCompat.setTint(wrapDrawable, context.getResources().getColor(R.color.icon_color_default));
Drawable wrapDrawable = DrawableCompat.wrap(ContextCompat.getDrawable(context, category.icon));
DrawableCompat.setTint(wrapDrawable, ContextCompat.getColor(context, R.color.icon_color_default));
categoryViewHolder.getIcon().setImageDrawable(wrapDrawable);
categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryAdded());
break;
case clearItemId:
categoryViewHolder.getIcon().setImageDrawable(context.getResources().getDrawable(category.icon));
categoryViewHolder.getIcon().setImageDrawable(ContextCompat.getDrawable(context, category.icon));
categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryCleared());
break;
default:
categoryViewHolder.getIcon().setImageDrawable(context.getResources().getDrawable(category.icon));
categoryViewHolder.getIcon().setImageDrawable(ContextCompat.getDrawable(context, category.icon));
categoryViewHolder.getCategoryWrapper().setOnClickListener((v) -> listener.onCategoryChosen(category.label));
break;
}

View file

@ -22,6 +22,7 @@ import androidx.annotation.NonNull;
import androidx.appcompat.view.ActionMode;
import androidx.appcompat.widget.SearchView;
import androidx.coordinatorlayout.widget.CoordinatorLayout;
import androidx.core.app.ActivityCompat;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.core.view.GravityCompat;
@ -770,7 +771,7 @@ public class MainActivity extends LockedActivity implements NoteClickListener, V
case server_settings: {
// Recreate activity completely, because theme switching makes problems when only invalidating the views.
// @see https://github.com/stefan-niedermann/nextcloud-notes/issues/529
recreate();
ActivityCompat.recreate(this);
break;
}
case manage_account: {

View file

@ -12,6 +12,7 @@ import androidx.annotation.ColorInt;
import androidx.annotation.IntRange;
import androidx.annotation.NonNull;
import androidx.annotation.Px;
import androidx.core.content.ContextCompat;
import androidx.preference.PreferenceManager;
import androidx.recyclerview.widget.RecyclerView;
@ -63,7 +64,7 @@ public class ItemAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> i
public <T extends Context & NoteClickListener> ItemAdapter(@NonNull T context, boolean gridView) {
this.noteClickListener = context;
this.gridView = gridView;
this.mainColor = context.getResources().getColor(R.color.defaultBrand);
this.mainColor = ContextCompat.getColor(context, R.color.defaultBrand);
this.textColor = Color.WHITE;
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context.getApplicationContext());
this.fontSize = getFontSizeFromPreferences(context, sp);

View file

@ -18,6 +18,7 @@ import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.appcompat.widget.AppCompatImageView;
import androidx.core.content.ContextCompat;
import androidx.core.graphics.drawable.DrawableCompat;
import androidx.recyclerview.widget.RecyclerView;
@ -112,7 +113,7 @@ public abstract class NoteViewHolder extends RecyclerView.ViewHolder {
protected void bindSearchableContent(@NonNull Context context, @NonNull TextView textView, @Nullable CharSequence searchQuery, @NonNull String content, int mainColor) {
CharSequence processedContent = content;
if (!TextUtils.isEmpty(searchQuery)) {
@ColorInt final int searchBackground = context.getResources().getColor(R.color.bg_highlighted);
@ColorInt final int searchBackground = ContextCompat.getColor(context, R.color.bg_highlighted);
@ColorInt final int searchForeground = BrandingUtil.getSecondaryForegroundColorDependingOnTheme(context, mainColor);
// The Pattern.quote method will add \Q to the very beginning of the string and \E to the end of the string

View file

@ -7,6 +7,7 @@ import android.util.Log;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.preference.ListPreference;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
@ -49,7 +50,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Bra
final Boolean branding = (Boolean) newValue;
Log.v(TAG, "branding: " + branding);
requireActivity().setResult(Activity.RESULT_OK);
requireActivity().recreate();
ActivityCompat.recreate(requireActivity());
return true;
});
} else {
@ -94,7 +95,7 @@ public class PreferencesFragment extends PreferenceFragmentCompat implements Bra
themePref.setOnPreferenceChangeListener((preference, newValue) -> {
NotesApplication.setAppTheme(DarkModeSetting.valueOf((String) newValue));
requireActivity().setResult(Activity.RESULT_OK);
requireActivity().recreate();
ActivityCompat.recreate(requireActivity());
return true;
});

View file

@ -29,6 +29,7 @@ import android.text.style.MetricAffectingSpan;
import androidx.annotation.ColorInt;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.content.ContextCompat;
import java.util.regex.Matcher;
import java.util.regex.Pattern;
@ -89,7 +90,7 @@ public class DisplayUtils {
this.mainColor = mainColor;
this.textColor = textColor;
this.current = current;
this.highlightColor = context.getResources().getColor(R.color.bg_highlighted);
this.highlightColor = ContextCompat.getColor(context, R.color.bg_highlighted);
}
@Override

View file

@ -1,14 +1,10 @@
package it.niedermann.owncloud.notes.shared.util;
import android.os.Build;
import android.text.Html;
import android.text.Spanned;
import android.text.method.LinkMovementMethod;
import android.widget.TextView;
import androidx.annotation.NonNull;
import static android.os.Build.VERSION_CODES.N;
import androidx.core.text.HtmlCompat;
/**
* Some helper functionality in alike the Android support library.
@ -28,24 +24,8 @@ public class SupportUtil {
* @param formatArgs Arguments for the string resource.
*/
public static void setHtml(@NonNull TextView view, int stringId, Object... formatArgs) {
view.setText(SupportUtil.fromHtml(view.getResources().getString(stringId, formatArgs)));
view.setText(HtmlCompat.fromHtml(
view.getResources().getString(stringId, formatArgs), HtmlCompat.FROM_HTML_MODE_LEGACY));
view.setMovementMethod(LinkMovementMethod.getInstance());
}
/**
* Creates a {@link Spanned} from a HTML string on all SDK versions.
*
* @param source Source string with HTML markup
* @return Spannable for using in a {@link TextView}
* @see Html#fromHtml(String)
* @see Html#fromHtml(String, int)
*/
private static Spanned fromHtml(String source) {
if (Build.VERSION.SDK_INT >= N) {
return Html.fromHtml(source, Html.FROM_HTML_MODE_LEGACY);
} else {
//noinspection deprecation
return Html.fromHtml(source);
}
}
}

View file

@ -11,6 +11,8 @@ import android.net.Uri;
import android.util.Log;
import android.widget.RemoteViews;
import androidx.core.content.ContextCompat;
import java.util.NoSuchElementException;
import it.niedermann.owncloud.notes.R;
@ -94,7 +96,7 @@ public class NoteListWidget extends AppWidgetProvider {
views.setInt(R.id.widget_note_list_create_icon_dark, "setColorFilter", localAccount.getTextColor());
views.setTextColor(R.id.widget_note_list_title_tv_dark, localAccount.getTextColor());
} else {
views.setInt(R.id.widget_note_header_dark, "setBackgroundColor", context.getResources().getColor(R.color.defaultBrand));
views.setInt(R.id.widget_note_header_dark, "setBackgroundColor", ContextCompat.getColor(context, R.color.defaultBrand));
views.setInt(R.id.widget_note_header_icon_dark, "setColorFilter", Color.WHITE);
views.setInt(R.id.widget_note_list_create_icon_dark, "setColorFilter", Color.WHITE);
views.setTextColor(R.id.widget_note_list_title_tv_dark, Color.WHITE);
@ -115,7 +117,7 @@ public class NoteListWidget extends AppWidgetProvider {
views.setInt(R.id.widget_note_list_create_icon, "setColorFilter", localAccount.getTextColor());
views.setTextColor(R.id.widget_note_list_title_tv, localAccount.getTextColor());
} else {
views.setInt(R.id.widget_note_header, "setBackgroundColor", context.getResources().getColor(R.color.defaultBrand));
views.setInt(R.id.widget_note_header, "setBackgroundColor", ContextCompat.getColor(context, R.color.defaultBrand));
views.setInt(R.id.widget_note_header_icon, "setColorFilter", Color.WHITE);
views.setInt(R.id.widget_note_list_create_icon, "setColorFilter", Color.WHITE);
views.setTextColor(R.id.widget_note_list_title_tv, Color.WHITE);