From edfea45936c5fb25b2362eaa75c6305e45a542e8 Mon Sep 17 00:00:00 2001 From: davidoskky Date: Wed, 27 Oct 2021 15:22:35 +0200 Subject: [PATCH] Migration --- .../bou/readerforselfoss/HomeActivity.kt | 2 +- .../apps/amine/bou/readerforselfoss/MyApp.kt | 4 +- .../fragments/ArticleFragment.kt | 49 +++++----- .../settings/SettingsActivity.java | 91 +++++++------------ .../bou/readerforselfoss/themes/AppColors.kt | 69 +++++++++----- .../bou/readerforselfoss/utils/AppUtils.kt | 2 +- .../utils/customtabs/ServiceConnection.java | 2 +- app/src/main/res/layout/activity_sources.xml | 4 +- app/src/main/res/layout/fragment_article.xml | 4 +- 9 files changed, 106 insertions(+), 121 deletions(-) diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt index bf61874..cbfefec 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -349,7 +349,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { if(response.body() != null) { val version = response.body() as ApiVersion apiVersionMajor = version.getApiMajorVersion() - sharedPref.edit().putInt("apiVersionMajor", apiVersionMajor).commit() + sharedPref.edit().putInt("apiVersionMajor", apiVersionMajor).apply() if (apiVersionMajor >= 4) { Config.dateTimeFormatter = "yyyy-MM-dd'T'HH:mm:ssXXX" diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt index 1414b74..7c2b623 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/MyApp.kt @@ -61,14 +61,14 @@ class MyApp : MultiDexApplication() { private fun initDrawerImageLoader() { DrawerImageLoader.init(object : AbstractDrawerImageLoader() { override fun set(imageView: ImageView, uri: Uri, placeholder: Drawable, tag: String?) { - Glide.with(imageView?.context) + Glide.with(imageView.context) .loadMaybeBasicAuth(config, uri.toString()) .apply(RequestOptions.fitCenterTransform().placeholder(placeholder)) .into(imageView) } override fun cancel(imageView: ImageView) { - Glide.with(imageView?.context).clear(imageView) + Glide.with(imageView.context).clear(imageView) } override fun placeholder(ctx: Context, tag: String?): Drawable { diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt index 11d8503..55c473a 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/fragments/ArticleFragment.kt @@ -49,6 +49,7 @@ import retrofit2.Callback import retrofit2.Response import java.net.MalformedURLException import java.net.URL +import java.util.* import java.util.concurrent.ExecutionException import kotlin.collections.ArrayList @@ -56,7 +57,7 @@ class ArticleFragment : Fragment() { private lateinit var pageNumber: Number private var fontSize: Int = 16 private lateinit var allItems: ArrayList - private var mCustomTabActivityHelper: CustomTabActivityHelper? = null; + private var mCustomTabActivityHelper: CustomTabActivityHelper? = null private lateinit var url: String private lateinit var contentText: String private lateinit var contentSource: String @@ -105,7 +106,7 @@ class ArticleFragment : Fragment() { inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle? - ): View? { + ): View { try { _binding = FragmentArticleBinding.inflate(inflater, container, false) @@ -164,7 +165,7 @@ class ArticleFragment : Fragment() { object : FloatingToolbar.ItemClickListener { override fun onItemClick(item: MenuItem) { when (item.itemId) { - R.id.more_action -> getContentFromMercury(customTabsIntent, prefs) + R.id.more_action -> getContentFromMercury(customTabsIntent) R.id.share_action -> requireActivity().shareLink(url, contentTitle) R.id.open_action -> requireActivity().openItemUrl( allItems, @@ -224,7 +225,7 @@ class ArticleFragment : Fragment() { } if (contentText.isEmptyOrNullOrNullString()) { - getContentFromMercury(customTabsIntent, prefs) + getContentFromMercury(customTabsIntent) } else { binding.titleView.text = contentTitle if (typeface != null) { @@ -266,11 +267,11 @@ class ArticleFragment : Fragment() { .setMessage(requireContext().getString(R.string.webview_dialog_issue_message)) .setTitle(requireContext().getString(R.string.webview_dialog_issue_title)) .setPositiveButton(android.R.string.ok - ) { dialog, which -> + ) { _, _ -> val sharedPref = PreferenceManager.getDefaultSharedPreferences(requireContext()) val editor = sharedPref.edit() editor.putBoolean("prefer_article_viewer", false) - editor.commit() + editor.apply() requireActivity().finish() } .create() @@ -294,8 +295,7 @@ class ArticleFragment : Fragment() { } private fun getContentFromMercury( - customTabsIntent: CustomTabsIntent, - prefs: SharedPreferences + customTabsIntent: CustomTabsIntent ) { if ((context != null && requireContext().isNetworkAccessible(null)) || context == null) { binding.progressBar.visibility = View.VISIBLE @@ -346,31 +346,19 @@ class ArticleFragment : Fragment() { } else { binding.imageView.visibility = View.GONE } - } catch (e: Exception) { - if (context != null) { - } - } + } catch (e: Exception) { } try { binding.nestedScrollView.scrollTo(0, 0) binding.progressBar.visibility = View.GONE - } catch (e: Exception) { - if (context != null) { - } - } + } catch (e: Exception) { } } else { try { openInBrowserAfterFailing(customTabsIntent) - } catch (e: Exception) { - if (context != null) { - } - } + } catch (e: Exception) { } } - } catch (e: Exception) { - if (context != null) { - } - } + } catch (e: Exception) { } } override fun onFailure( @@ -443,22 +431,27 @@ class ArticleFragment : Fragment() { override fun shouldInterceptRequest(view: WebView?, url: String): WebResourceResponse? { val glideOptions = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL) - if (url.toLowerCase().contains(".jpg") || url.toLowerCase().contains(".jpeg")) { + if (url.lowercase(Locale.ROOT).contains(".jpg") || url.lowercase(Locale.ROOT).contains(".jpeg")) { try { val image = Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get() return WebResourceResponse("image/jpg", "UTF-8", getBitmapInputStream(image, Bitmap.CompressFormat.JPEG)) }catch ( e : ExecutionException) {} } - else if (url.toLowerCase().contains(".png")) { + else if (url.lowercase(Locale.ROOT).contains(".png")) { try { val image = Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get() return WebResourceResponse("image/jpg", "UTF-8", getBitmapInputStream(image, Bitmap.CompressFormat.PNG)) }catch ( e : ExecutionException) {} } - else if (url.toLowerCase().contains(".webp")) { + else if (url.lowercase(Locale.ROOT).contains(".webp")) { try { val image = Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get() - return WebResourceResponse("image/jpg", "UTF-8", getBitmapInputStream(image, Bitmap.CompressFormat.WEBP)) + val compressFormat = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.R) { + Bitmap.CompressFormat.WEBP_LOSSLESS + } else { + Bitmap.CompressFormat.WEBP + } + return WebResourceResponse("image/jpg", "UTF-8", getBitmapInputStream(image, compressFormat)) }catch ( e : ExecutionException) {} } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java b/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java index d7ef47b..5be6b5c 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/settings/SettingsActivity.java @@ -2,28 +2,21 @@ package apps.amine.bou.readerforselfoss.settings; import android.annotation.TargetApi; -import android.content.ClipData; -import android.content.ClipboardManager; import android.content.Context; import android.content.Intent; import android.content.SharedPreferences; import android.content.res.Configuration; -import android.content.res.Resources; import android.net.Uri; import android.os.Build; import android.os.Bundle; import android.preference.EditTextPreference; import android.preference.Preference; -import android.preference.Preference.OnPreferenceChangeListener; -import android.preference.Preference.OnPreferenceClickListener; import android.preference.PreferenceActivity; import android.preference.PreferenceFragment; import android.preference.PreferenceManager; -import android.preference.SwitchPreference; import androidx.appcompat.app.ActionBar; import android.text.Editable; import android.text.InputFilter; -import android.text.Spanned; import android.text.TextWatcher; import android.util.Log; import android.view.Menu; @@ -54,13 +47,10 @@ public class SettingsActivity extends AppCompatPreferenceActivity { * A preference value change listener that updates the preference's summary * to reflect its new value. */ - private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { - @Override - public boolean onPreferenceChange(Preference preference, Object value) { - String stringValue = value.toString(); - preference.setSummary(stringValue); - return true; - } + private static final Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> { + String stringValue = value.toString(); + preference.setSummary(stringValue); + return true; }; /** @@ -128,7 +118,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity { loadHeadersFromResource(R.xml.pref_headers, target); AppColors appColors = new AppColors(this); - if (appColors != null && appColors.isDarkTheme()) { + if (appColors.isDarkTheme()) { for (Header header : target) { tryLoadIconDark(header); } @@ -178,19 +168,15 @@ public class SettingsActivity extends AppCompatPreferenceActivity { EditTextPreference itemsNumber = (EditTextPreference) findPreference("prefer_api_items_number"); itemsNumber.getEditText().setFilters(new InputFilter[]{ - new InputFilter() { - - @Override - public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { - try { - int input = Integer.parseInt(dest.toString() + source.toString()); - if (input <= 200 && input > 0) - return null; - } catch (NumberFormatException nfe) { - Toast.makeText(getActivity(), R.string.items_number_should_be_number, Toast.LENGTH_LONG).show(); - } - return ""; + (source, start, end, dest, dstart, dend) -> { + try { + int input = Integer.parseInt(dest.toString() + source.toString()); + if (input <= 200 && input > 0) + return null; + } catch (NumberFormatException nfe) { + Toast.makeText(getActivity(), R.string.items_number_should_be_number, Toast.LENGTH_LONG).show(); } + return ""; } }); @@ -214,24 +200,18 @@ public class SettingsActivity extends AppCompatPreferenceActivity { public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} @Override - public void afterTextChanged(Editable editable) { - try { - fontSize.getEditText().setTextSize(Integer.parseInt(editable.toString())); - } catch (NumberFormatException e) {} + public void afterTextChanged(Editable editable) throws NumberFormatException { + fontSize.getEditText().setTextSize(Integer.parseInt(editable.toString())); } }); fontSize.getEditText().setFilters(new InputFilter[]{ - new InputFilter() { - - @Override - public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { - try { - int input = Integer.parseInt(dest.toString() + source.toString()); - if (input > 0) - return null; - } catch (NumberFormatException nfe) {} - return ""; - } + (source, start, end, dest, dstart, dend) -> { + try { + int input = Integer.parseInt(dest.toString() + source.toString()); + if (input > 0) + return null; + } catch (NumberFormatException ignored) {} + return ""; } }); } @@ -254,28 +234,19 @@ public class SettingsActivity extends AppCompatPreferenceActivity { addPreferencesFromResource(R.xml.pref_links); setHasOptionsMenu(true); - findPreference("trackerLink").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - openUrl(Uri.parse(Config.trackerUrl)); - return true; - } + findPreference("trackerLink").setOnPreferenceClickListener(preference -> { + openUrl(Uri.parse(Config.trackerUrl)); + return true; }); - findPreference("sourceLink").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - openUrl(Uri.parse(Config.sourceUrl)); - return false; - } + findPreference("sourceLink").setOnPreferenceClickListener(preference -> { + openUrl(Uri.parse(Config.sourceUrl)); + return false; }); - findPreference("translation").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { - @Override - public boolean onPreferenceClick(Preference preference) { - openUrl(Uri.parse(Config.translationUrl)); - return false; - } + findPreference("translation").setOnPreferenceClickListener(preference -> { + openUrl(Uri.parse(Config.translationUrl)); + return false; }); } } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/themes/AppColors.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/themes/AppColors.kt index 85edf56..504c228 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/themes/AppColors.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/themes/AppColors.kt @@ -2,13 +2,11 @@ package apps.amine.bou.readerforselfoss.themes import android.app.Activity import android.content.Context +import android.os.Build import android.preference.PreferenceManager import androidx.annotation.ColorInt -import androidx.appcompat.view.ContextThemeWrapper import android.util.TypedValue import apps.amine.bou.readerforselfoss.R -import android.view.LayoutInflater -import android.view.ViewGroup class AppColors(a: Activity) { @@ -24,26 +22,49 @@ class AppColors(a: Activity) { init { val sharedPref = PreferenceManager.getDefaultSharedPreferences(a) - colorPrimary = - sharedPref.getInt( - "color_primary", - a.resources.getColor(R.color.colorPrimary) - ) - colorPrimaryDark = - sharedPref.getInt( - "color_primary_dark", - a.resources.getColor(R.color.colorPrimaryDark) - ) - colorAccent = - sharedPref.getInt( - "color_accent", - a.resources.getColor(R.color.colorAccent) - ) - colorAccentDark = - sharedPref.getInt( - "color_accent_dark", - a.resources.getColor(R.color.colorAccentDark) - ) + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) { + colorPrimary = + sharedPref.getInt( + "color_primary", + a.resources.getColor(R.color.colorPrimary, a.theme) + ) + colorPrimaryDark = + sharedPref.getInt( + "color_primary_dark", + a.resources.getColor(R.color.colorPrimaryDark, a.theme) + ) + colorAccent = + sharedPref.getInt( + "color_accent", + a.resources.getColor(R.color.colorAccent, a.theme) + ) + colorAccentDark = + sharedPref.getInt( + "color_accent_dark", + a.resources.getColor(R.color.colorAccentDark, a.theme) + ) + } else { + colorPrimary = + sharedPref.getInt( + "color_primary", + a.resources.getColor(R.color.colorPrimary) + ) + colorPrimaryDark = + sharedPref.getInt( + "color_primary_dark", + a.resources.getColor(R.color.colorPrimaryDark) + ) + colorAccent = + sharedPref.getInt( + "color_accent", + a.resources.getColor(R.color.colorAccent) + ) + colorAccentDark = + sharedPref.getInt( + "color_accent_dark", + a.resources.getColor(R.color.colorAccentDark) + ) + } isDarkTheme = sharedPref.getBoolean( "dark_theme", @@ -65,7 +86,7 @@ class AppColors(a: Activity) { } val wrapper = Context::class.java - val method = wrapper!!.getMethod("getThemeResId") + val method = wrapper.getMethod("getThemeResId") method.isAccessible = true val typedCardBackground = TypedValue() diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/AppUtils.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/AppUtils.kt index 35e5a72..f206294 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/AppUtils.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/AppUtils.kt @@ -13,7 +13,7 @@ fun String.longHash(): Long { val chars = this.toCharArray() for (i in 0 until l) { - h = 31 * h + chars[i].toLong() + h = 31 * h + chars[i].code.toLong() } return h } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/customtabs/ServiceConnection.java b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/customtabs/ServiceConnection.java index f035dff..f8e972e 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/utils/customtabs/ServiceConnection.java +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/utils/customtabs/ServiceConnection.java @@ -13,7 +13,7 @@ import java.lang.ref.WeakReference; */ public class ServiceConnection extends CustomTabsServiceConnection { // A weak reference to the ServiceConnectionCallback to avoid leaking it. - private WeakReference mConnectionCallback; + private final WeakReference mConnectionCallback; public ServiceConnection(ServiceConnectionCallback connectionCallback) { mConnectionCallback = new WeakReference<>(connectionCallback); diff --git a/app/src/main/res/layout/activity_sources.xml b/app/src/main/res/layout/activity_sources.xml index 1097e6b..48523d2 100644 --- a/app/src/main/res/layout/activity_sources.xml +++ b/app/src/main/res/layout/activity_sources.xml @@ -31,8 +31,8 @@ android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_gravity="end|bottom|right" - android:src="@drawable/ic_add_white_24dp" + android:layout_gravity="end|bottom|end" + app:srcCompat="@drawable/ic_add_white_24dp" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin" android:layout_alignParentBottom="true" diff --git a/app/src/main/res/layout/fragment_article.xml b/app/src/main/res/layout/fragment_article.xml index 5ddf9ea..daccaa9 100644 --- a/app/src/main/res/layout/fragment_article.xml +++ b/app/src/main/res/layout/fragment_article.xml @@ -94,13 +94,13 @@ android:id="@+id/fab" android:layout_width="wrap_content" android:layout_height="wrap_content" - android:layout_gravity="end|bottom|right" + android:layout_gravity="end|bottom|end" android:layout_marginBottom="16dp" android:layout_marginEnd="16dp" android:layout_marginRight="16dp" android:paddingBottom="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin" - android:src="@drawable/ic_add_white_24dp" + app:srcCompat="@drawable/ic_add_white_24dp" app:backgroundTint="?attr/colorAccent" app:fabSize="mini" app:rippleColor="?attr/colorAccentDark" />