Migration

This commit is contained in:
davidoskky 2021-10-27 15:22:35 +02:00
parent 62a82b01b8
commit edfea45936
9 changed files with 106 additions and 121 deletions

View File

@ -349,7 +349,7 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
if(response.body() != null) { if(response.body() != null) {
val version = response.body() as ApiVersion val version = response.body() as ApiVersion
apiVersionMajor = version.getApiMajorVersion() apiVersionMajor = version.getApiMajorVersion()
sharedPref.edit().putInt("apiVersionMajor", apiVersionMajor).commit() sharedPref.edit().putInt("apiVersionMajor", apiVersionMajor).apply()
if (apiVersionMajor >= 4) { if (apiVersionMajor >= 4) {
Config.dateTimeFormatter = "yyyy-MM-dd'T'HH:mm:ssXXX" Config.dateTimeFormatter = "yyyy-MM-dd'T'HH:mm:ssXXX"

View File

@ -61,14 +61,14 @@ class MyApp : MultiDexApplication() {
private fun initDrawerImageLoader() { private fun initDrawerImageLoader() {
DrawerImageLoader.init(object : AbstractDrawerImageLoader() { DrawerImageLoader.init(object : AbstractDrawerImageLoader() {
override fun set(imageView: ImageView, uri: Uri, placeholder: Drawable, tag: String?) { override fun set(imageView: ImageView, uri: Uri, placeholder: Drawable, tag: String?) {
Glide.with(imageView?.context) Glide.with(imageView.context)
.loadMaybeBasicAuth(config, uri.toString()) .loadMaybeBasicAuth(config, uri.toString())
.apply(RequestOptions.fitCenterTransform().placeholder(placeholder)) .apply(RequestOptions.fitCenterTransform().placeholder(placeholder))
.into(imageView) .into(imageView)
} }
override fun cancel(imageView: 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 { override fun placeholder(ctx: Context, tag: String?): Drawable {

View File

@ -49,6 +49,7 @@ import retrofit2.Callback
import retrofit2.Response import retrofit2.Response
import java.net.MalformedURLException import java.net.MalformedURLException
import java.net.URL import java.net.URL
import java.util.*
import java.util.concurrent.ExecutionException import java.util.concurrent.ExecutionException
import kotlin.collections.ArrayList import kotlin.collections.ArrayList
@ -56,7 +57,7 @@ class ArticleFragment : Fragment() {
private lateinit var pageNumber: Number private lateinit var pageNumber: Number
private var fontSize: Int = 16 private var fontSize: Int = 16
private lateinit var allItems: ArrayList<Item> private lateinit var allItems: ArrayList<Item>
private var mCustomTabActivityHelper: CustomTabActivityHelper? = null; private var mCustomTabActivityHelper: CustomTabActivityHelper? = null
private lateinit var url: String private lateinit var url: String
private lateinit var contentText: String private lateinit var contentText: String
private lateinit var contentSource: String private lateinit var contentSource: String
@ -105,7 +106,7 @@ class ArticleFragment : Fragment() {
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
savedInstanceState: Bundle? savedInstanceState: Bundle?
): View? { ): View {
try { try {
_binding = FragmentArticleBinding.inflate(inflater, container, false) _binding = FragmentArticleBinding.inflate(inflater, container, false)
@ -164,7 +165,7 @@ class ArticleFragment : Fragment() {
object : FloatingToolbar.ItemClickListener { object : FloatingToolbar.ItemClickListener {
override fun onItemClick(item: MenuItem) { override fun onItemClick(item: MenuItem) {
when (item.itemId) { 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.share_action -> requireActivity().shareLink(url, contentTitle)
R.id.open_action -> requireActivity().openItemUrl( R.id.open_action -> requireActivity().openItemUrl(
allItems, allItems,
@ -224,7 +225,7 @@ class ArticleFragment : Fragment() {
} }
if (contentText.isEmptyOrNullOrNullString()) { if (contentText.isEmptyOrNullOrNullString()) {
getContentFromMercury(customTabsIntent, prefs) getContentFromMercury(customTabsIntent)
} else { } else {
binding.titleView.text = contentTitle binding.titleView.text = contentTitle
if (typeface != null) { if (typeface != null) {
@ -266,11 +267,11 @@ class ArticleFragment : Fragment() {
.setMessage(requireContext().getString(R.string.webview_dialog_issue_message)) .setMessage(requireContext().getString(R.string.webview_dialog_issue_message))
.setTitle(requireContext().getString(R.string.webview_dialog_issue_title)) .setTitle(requireContext().getString(R.string.webview_dialog_issue_title))
.setPositiveButton(android.R.string.ok .setPositiveButton(android.R.string.ok
) { dialog, which -> ) { _, _ ->
val sharedPref = PreferenceManager.getDefaultSharedPreferences(requireContext()) val sharedPref = PreferenceManager.getDefaultSharedPreferences(requireContext())
val editor = sharedPref.edit() val editor = sharedPref.edit()
editor.putBoolean("prefer_article_viewer", false) editor.putBoolean("prefer_article_viewer", false)
editor.commit() editor.apply()
requireActivity().finish() requireActivity().finish()
} }
.create() .create()
@ -294,8 +295,7 @@ class ArticleFragment : Fragment() {
} }
private fun getContentFromMercury( private fun getContentFromMercury(
customTabsIntent: CustomTabsIntent, customTabsIntent: CustomTabsIntent
prefs: SharedPreferences
) { ) {
if ((context != null && requireContext().isNetworkAccessible(null)) || context == null) { if ((context != null && requireContext().isNetworkAccessible(null)) || context == null) {
binding.progressBar.visibility = View.VISIBLE binding.progressBar.visibility = View.VISIBLE
@ -346,31 +346,19 @@ class ArticleFragment : Fragment() {
} else { } else {
binding.imageView.visibility = View.GONE binding.imageView.visibility = View.GONE
} }
} catch (e: Exception) { } catch (e: Exception) { }
if (context != null) {
}
}
try { try {
binding.nestedScrollView.scrollTo(0, 0) binding.nestedScrollView.scrollTo(0, 0)
binding.progressBar.visibility = View.GONE binding.progressBar.visibility = View.GONE
} catch (e: Exception) { } catch (e: Exception) { }
if (context != null) {
}
}
} else { } else {
try { try {
openInBrowserAfterFailing(customTabsIntent) openInBrowserAfterFailing(customTabsIntent)
} catch (e: Exception) { } catch (e: Exception) { }
if (context != null) {
}
}
} }
} catch (e: Exception) { } catch (e: Exception) { }
if (context != null) {
}
}
} }
override fun onFailure( override fun onFailure(
@ -443,22 +431,27 @@ class ArticleFragment : Fragment() {
override fun shouldInterceptRequest(view: WebView?, url: String): WebResourceResponse? { override fun shouldInterceptRequest(view: WebView?, url: String): WebResourceResponse? {
val glideOptions = RequestOptions.diskCacheStrategyOf(DiskCacheStrategy.ALL) 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 { try {
val image = Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get() val image = Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get()
return WebResourceResponse("image/jpg", "UTF-8", getBitmapInputStream(image, Bitmap.CompressFormat.JPEG)) return WebResourceResponse("image/jpg", "UTF-8", getBitmapInputStream(image, Bitmap.CompressFormat.JPEG))
}catch ( e : ExecutionException) {} }catch ( e : ExecutionException) {}
} }
else if (url.toLowerCase().contains(".png")) { else if (url.lowercase(Locale.ROOT).contains(".png")) {
try { try {
val image = Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get() val image = Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get()
return WebResourceResponse("image/jpg", "UTF-8", getBitmapInputStream(image, Bitmap.CompressFormat.PNG)) return WebResourceResponse("image/jpg", "UTF-8", getBitmapInputStream(image, Bitmap.CompressFormat.PNG))
}catch ( e : ExecutionException) {} }catch ( e : ExecutionException) {}
} }
else if (url.toLowerCase().contains(".webp")) { else if (url.lowercase(Locale.ROOT).contains(".webp")) {
try { try {
val image = Glide.with(view).asBitmap().apply(glideOptions).load(url).submit().get() 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) {} }catch ( e : ExecutionException) {}
} }

View File

@ -2,28 +2,21 @@ package apps.amine.bou.readerforselfoss.settings;
import android.annotation.TargetApi; import android.annotation.TargetApi;
import android.content.ClipData;
import android.content.ClipboardManager;
import android.content.Context; import android.content.Context;
import android.content.Intent; import android.content.Intent;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.content.res.Configuration; import android.content.res.Configuration;
import android.content.res.Resources;
import android.net.Uri; import android.net.Uri;
import android.os.Build; import android.os.Build;
import android.os.Bundle; import android.os.Bundle;
import android.preference.EditTextPreference; import android.preference.EditTextPreference;
import android.preference.Preference; import android.preference.Preference;
import android.preference.Preference.OnPreferenceChangeListener;
import android.preference.Preference.OnPreferenceClickListener;
import android.preference.PreferenceActivity; import android.preference.PreferenceActivity;
import android.preference.PreferenceFragment; import android.preference.PreferenceFragment;
import android.preference.PreferenceManager; import android.preference.PreferenceManager;
import android.preference.SwitchPreference;
import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.ActionBar;
import android.text.Editable; import android.text.Editable;
import android.text.InputFilter; import android.text.InputFilter;
import android.text.Spanned;
import android.text.TextWatcher; import android.text.TextWatcher;
import android.util.Log; import android.util.Log;
import android.view.Menu; import android.view.Menu;
@ -54,13 +47,10 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
* A preference value change listener that updates the preference's summary * A preference value change listener that updates the preference's summary
* to reflect its new value. * to reflect its new value.
*/ */
private static Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = new Preference.OnPreferenceChangeListener() { private static final Preference.OnPreferenceChangeListener sBindPreferenceSummaryToValueListener = (preference, value) -> {
@Override String stringValue = value.toString();
public boolean onPreferenceChange(Preference preference, Object value) { preference.setSummary(stringValue);
String stringValue = value.toString(); return true;
preference.setSummary(stringValue);
return true;
}
}; };
/** /**
@ -128,7 +118,7 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
loadHeadersFromResource(R.xml.pref_headers, target); loadHeadersFromResource(R.xml.pref_headers, target);
AppColors appColors = new AppColors(this); AppColors appColors = new AppColors(this);
if (appColors != null && appColors.isDarkTheme()) { if (appColors.isDarkTheme()) {
for (Header header : target) { for (Header header : target) {
tryLoadIconDark(header); tryLoadIconDark(header);
} }
@ -178,19 +168,15 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
EditTextPreference itemsNumber = (EditTextPreference) findPreference("prefer_api_items_number"); EditTextPreference itemsNumber = (EditTextPreference) findPreference("prefer_api_items_number");
itemsNumber.getEditText().setFilters(new InputFilter[]{ itemsNumber.getEditText().setFilters(new InputFilter[]{
new InputFilter() { (source, start, end, dest, dstart, dend) -> {
try {
@Override int input = Integer.parseInt(dest.toString() + source.toString());
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (input <= 200 && input > 0)
try { return null;
int input = Integer.parseInt(dest.toString() + source.toString()); } catch (NumberFormatException nfe) {
if (input <= 200 && input > 0) Toast.makeText(getActivity(), R.string.items_number_should_be_number, Toast.LENGTH_LONG).show();
return null;
} catch (NumberFormatException nfe) {
Toast.makeText(getActivity(), R.string.items_number_should_be_number, Toast.LENGTH_LONG).show();
}
return "";
} }
return "";
} }
}); });
@ -214,24 +200,18 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {} public void onTextChanged(CharSequence charSequence, int i, int i1, int i2) {}
@Override @Override
public void afterTextChanged(Editable editable) { public void afterTextChanged(Editable editable) throws NumberFormatException {
try { fontSize.getEditText().setTextSize(Integer.parseInt(editable.toString()));
fontSize.getEditText().setTextSize(Integer.parseInt(editable.toString()));
} catch (NumberFormatException e) {}
} }
}); });
fontSize.getEditText().setFilters(new InputFilter[]{ fontSize.getEditText().setFilters(new InputFilter[]{
new InputFilter() { (source, start, end, dest, dstart, dend) -> {
try {
@Override int input = Integer.parseInt(dest.toString() + source.toString());
public CharSequence filter(CharSequence source, int start, int end, Spanned dest, int dstart, int dend) { if (input > 0)
try { return null;
int input = Integer.parseInt(dest.toString() + source.toString()); } catch (NumberFormatException ignored) {}
if (input > 0) return "";
return null;
} catch (NumberFormatException nfe) {}
return "";
}
} }
}); });
} }
@ -254,28 +234,19 @@ public class SettingsActivity extends AppCompatPreferenceActivity {
addPreferencesFromResource(R.xml.pref_links); addPreferencesFromResource(R.xml.pref_links);
setHasOptionsMenu(true); setHasOptionsMenu(true);
findPreference("trackerLink").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { findPreference("trackerLink").setOnPreferenceClickListener(preference -> {
@Override openUrl(Uri.parse(Config.trackerUrl));
public boolean onPreferenceClick(Preference preference) { return true;
openUrl(Uri.parse(Config.trackerUrl));
return true;
}
}); });
findPreference("sourceLink").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { findPreference("sourceLink").setOnPreferenceClickListener(preference -> {
@Override openUrl(Uri.parse(Config.sourceUrl));
public boolean onPreferenceClick(Preference preference) { return false;
openUrl(Uri.parse(Config.sourceUrl));
return false;
}
}); });
findPreference("translation").setOnPreferenceClickListener(new Preference.OnPreferenceClickListener() { findPreference("translation").setOnPreferenceClickListener(preference -> {
@Override openUrl(Uri.parse(Config.translationUrl));
public boolean onPreferenceClick(Preference preference) { return false;
openUrl(Uri.parse(Config.translationUrl));
return false;
}
}); });
} }
} }

View File

@ -2,13 +2,11 @@ package apps.amine.bou.readerforselfoss.themes
import android.app.Activity import android.app.Activity
import android.content.Context import android.content.Context
import android.os.Build
import android.preference.PreferenceManager import android.preference.PreferenceManager
import androidx.annotation.ColorInt import androidx.annotation.ColorInt
import androidx.appcompat.view.ContextThemeWrapper
import android.util.TypedValue import android.util.TypedValue
import apps.amine.bou.readerforselfoss.R import apps.amine.bou.readerforselfoss.R
import android.view.LayoutInflater
import android.view.ViewGroup
class AppColors(a: Activity) { class AppColors(a: Activity) {
@ -24,26 +22,49 @@ class AppColors(a: Activity) {
init { init {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(a) val sharedPref = PreferenceManager.getDefaultSharedPreferences(a)
colorPrimary = if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
sharedPref.getInt( colorPrimary =
"color_primary", sharedPref.getInt(
a.resources.getColor(R.color.colorPrimary) "color_primary",
) a.resources.getColor(R.color.colorPrimary, a.theme)
colorPrimaryDark = )
sharedPref.getInt( colorPrimaryDark =
"color_primary_dark", sharedPref.getInt(
a.resources.getColor(R.color.colorPrimaryDark) "color_primary_dark",
) a.resources.getColor(R.color.colorPrimaryDark, a.theme)
colorAccent = )
sharedPref.getInt( colorAccent =
"color_accent", sharedPref.getInt(
a.resources.getColor(R.color.colorAccent) "color_accent",
) a.resources.getColor(R.color.colorAccent, a.theme)
colorAccentDark = )
sharedPref.getInt( colorAccentDark =
"color_accent_dark", sharedPref.getInt(
a.resources.getColor(R.color.colorAccentDark) "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 = isDarkTheme =
sharedPref.getBoolean( sharedPref.getBoolean(
"dark_theme", "dark_theme",
@ -65,7 +86,7 @@ class AppColors(a: Activity) {
} }
val wrapper = Context::class.java val wrapper = Context::class.java
val method = wrapper!!.getMethod("getThemeResId") val method = wrapper.getMethod("getThemeResId")
method.isAccessible = true method.isAccessible = true
val typedCardBackground = TypedValue() val typedCardBackground = TypedValue()

View File

@ -13,7 +13,7 @@ fun String.longHash(): Long {
val chars = this.toCharArray() val chars = this.toCharArray()
for (i in 0 until l) { for (i in 0 until l) {
h = 31 * h + chars[i].toLong() h = 31 * h + chars[i].code.toLong()
} }
return h return h
} }

View File

@ -13,7 +13,7 @@ import java.lang.ref.WeakReference;
*/ */
public class ServiceConnection extends CustomTabsServiceConnection { public class ServiceConnection extends CustomTabsServiceConnection {
// A weak reference to the ServiceConnectionCallback to avoid leaking it. // A weak reference to the ServiceConnectionCallback to avoid leaking it.
private WeakReference<ServiceConnectionCallback> mConnectionCallback; private final WeakReference<ServiceConnectionCallback> mConnectionCallback;
public ServiceConnection(ServiceConnectionCallback connectionCallback) { public ServiceConnection(ServiceConnectionCallback connectionCallback) {
mConnectionCallback = new WeakReference<>(connectionCallback); mConnectionCallback = new WeakReference<>(connectionCallback);

View File

@ -31,8 +31,8 @@
android:id="@+id/fab" android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="wrap_content" android:layout_height="wrap_content"
android:layout_gravity="end|bottom|right" android:layout_gravity="end|bottom|end"
android:src="@drawable/ic_add_white_24dp" app:srcCompat="@drawable/ic_add_white_24dp"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@dimen/activity_vertical_margin" android:paddingTop="@dimen/activity_vertical_margin"
android:layout_alignParentBottom="true" android:layout_alignParentBottom="true"

View File

@ -94,13 +94,13 @@
android:id="@+id/fab" android:id="@+id/fab"
android:layout_width="wrap_content" android:layout_width="wrap_content"
android:layout_height="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_marginBottom="16dp"
android:layout_marginEnd="16dp" android:layout_marginEnd="16dp"
android:layout_marginRight="16dp" android:layout_marginRight="16dp"
android:paddingBottom="@dimen/activity_vertical_margin" android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingTop="@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:backgroundTint="?attr/colorAccent"
app:fabSize="mini" app:fabSize="mini"
app:rippleColor="?attr/colorAccentDark" /> app:rippleColor="?attr/colorAccentDark" />