From 3bcffff44460fb9e35364499519da25f179a5890 Mon Sep 17 00:00:00 2001 From: Amine Bou Date: Sun, 25 Mar 2018 20:54:33 +0200 Subject: [PATCH] Fixing crashlytics issues. --- CHANGELOG.md | 2 +- .../bou/readerforselfoss/HomeActivity.kt | 15 +-- .../bou/readerforselfoss/ReaderActivity.kt | 8 -- .../fragments/ArticleFragment.kt | 91 ++++++++++++++----- 4 files changed, 75 insertions(+), 41 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 59b1d3e..eeba08b 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,7 +6,7 @@ - Added an option to use a webview in the article viewer (see #149) -- Fixes (#151 #152 #155 #157 #160 #174) +- Fixes (#151 #152 #155 #157 #160 #174) and more. - New year fixes !!! 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 d07c62e..e0e2e04 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/HomeActivity.kt @@ -204,11 +204,12 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { ): Boolean = false override fun onSwiped(viewHolder: RecyclerView.ViewHolder, swipeDir: Int) { - try { - val i = items[viewHolder.adapterPosition] - val position = items.indexOf(i) + val position = viewHolder.adapterPosition + val i = items.elementAtOrNull(position) + if (i != null) { val adapter = recyclerView.adapter + when (adapter) { is ItemCardAdapter -> adapter.removeItemAtIndex(position) is ItemListAdapter -> adapter.removeItemAtIndex(position) @@ -242,14 +243,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener { offsetOverride = lastVisibleItem ) } - } catch (e: IndexOutOfBoundsException) { - Crashlytics.setUserIdentifier(userIdentifier) - Crashlytics.log( - 100, - "SWIPE_INDEX_OUT_OF_BOUND", - "IndexOutOfBoundsException when swiping" - ) - Crashlytics.logException(e) } } } diff --git a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt index 834675b..94aa9a3 100644 --- a/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt +++ b/app/src/main/java/apps/amine/bou/readerforselfoss/ReaderActivity.kt @@ -67,14 +67,6 @@ class ReaderActivity : AppCompatActivity() { markOnScroll = sharedPref.getBoolean("mark_on_scroll", false) if (allItems.isEmpty()) { - Crashlytics.setUserIdentifier(userIdentifier) - Crashlytics.log( - 100, - "READER_ITEMS_EMPTY", - "Items empty when trying to open the Article Reader. Was (static) companion object field set ?" - ) - Crashlytics.logException(Exception("Empty items on Reader Activity.")) - finish() } 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 852492d..c4962a5 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 @@ -1,6 +1,10 @@ package apps.amine.bou.readerforselfoss.fragments +import android.content.Context +import android.content.DialogInterface +import android.content.Intent import android.content.SharedPreferences +import android.net.Uri import android.os.Build import android.os.Bundle import android.preference.PreferenceManager @@ -9,6 +13,8 @@ import android.support.design.widget.FloatingActionButton import android.support.v4.app.Fragment import android.support.v4.content.ContextCompat import android.support.v4.widget.NestedScrollView +import android.support.v7.app.AlertDialog +import android.support.v7.view.ContextThemeWrapper import android.view.LayoutInflater import android.view.MenuItem import android.view.View @@ -49,6 +55,8 @@ class ArticleFragment : Fragment() { private lateinit var contentSource: String private lateinit var contentImage: String private lateinit var contentTitle: String + private var showMalformedUrl: Boolean = false + private lateinit var editor: SharedPreferences.Editor private lateinit var fab: FloatingActionButton override fun onStop() { @@ -58,12 +66,14 @@ class ArticleFragment : Fragment() { override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) + pageNumber = arguments!!.getInt(ARG_POSITION) allItems = arguments!!.getParcelableArrayList(ARG_ITEMS) } private lateinit var rootView: ViewGroup + override fun onCreateView( inflater: LayoutInflater, container: ViewGroup?, @@ -72,6 +82,8 @@ class ArticleFragment : Fragment() { rootView = inflater .inflate(R.layout.fragment_article, container, false) as ViewGroup + val context: Context = activity!! + url = allItems[pageNumber.toInt()].getLinkDecoded() contentText = allItems[pageNumber.toInt()].content contentTitle = allItems[pageNumber.toInt()].title @@ -87,14 +99,16 @@ class ArticleFragment : Fragment() { mCustomTabActivityHelper.bindCustomTabsService(activity) val prefs = PreferenceManager.getDefaultSharedPreferences(activity) + editor = prefs.edit() fontSize = prefs.getString("reader_font_size", "14").toInt() + showMalformedUrl = prefs.getBoolean("show_error_malformed_url", true) mFloatingToolbar.setClickListener( object : FloatingToolbar.ItemClickListener { override fun onItemClick(item: MenuItem) { when (item.itemId) { - R.id.more_action -> getContentFromMercury(customTabsIntent, prefs) + R.id.more_action -> getContentFromMercury(customTabsIntent, prefs, context) R.id.share_action -> activity!!.shareLink(url) R.id.open_action -> activity!!.openItemUrl( allItems, @@ -116,17 +130,17 @@ class ArticleFragment : Fragment() { if (contentText.isEmptyOrNullOrNullString()) { - getContentFromMercury(customTabsIntent, prefs) + getContentFromMercury(customTabsIntent, prefs, context) } else { rootView.source.text = contentSource rootView.titleView.text = contentTitle - htmlToWebview(contentText, prefs) + htmlToWebview(contentText, prefs, context) if (!contentImage.isEmptyOrNullOrNullString()) { rootView.imageView.visibility = View.VISIBLE Glide - .with(activity!!.baseContext) + .with(context) .asBitmap() .load(contentImage) .apply(RequestOptions.fitCenterTransform()) @@ -151,7 +165,8 @@ class ArticleFragment : Fragment() { private fun getContentFromMercury( customTabsIntent: CustomTabsIntent, - prefs: SharedPreferences + prefs: SharedPreferences, + context: Context ) { rootView.progressBar.visibility = View.VISIBLE val parser = MercuryApi( @@ -183,13 +198,13 @@ class ArticleFragment : Fragment() { } try { - htmlToWebview(response.body()!!.content.orEmpty(), prefs) + htmlToWebview(response.body()!!.content.orEmpty(), prefs, context) } catch (e: Exception) { Crashlytics.setUserIdentifier(prefs.getString("unique_id", "")) Crashlytics.log( 100, "MERCURY_CONTENT_EXCEPTION", - "Webview issue" + "Webview issue ${e.message}" ) Crashlytics.logException(e) } @@ -199,7 +214,7 @@ class ArticleFragment : Fragment() { rootView.imageView.visibility = View.VISIBLE try { Glide - .with(activity!!.baseContext) + .with(context) .asBitmap() .load(response.body()!!.lead_image_url) .apply(RequestOptions.fitCenterTransform()) @@ -214,7 +229,7 @@ class ArticleFragment : Fragment() { Crashlytics.log( 100, "MERCURY_CONTENT_EXCEPTION", - "Glide issue" + "Glide issue with image ${response.body()!!.lead_image_url}" ) Crashlytics.logException(e) } @@ -276,28 +291,28 @@ class ArticleFragment : Fragment() { ) } - private fun htmlToWebview(c: String, prefs: SharedPreferences) { + private fun htmlToWebview(c: String, prefs: SharedPreferences, context: Context) { - val accentColor = ContextCompat.getColor(activity!!.baseContext, R.color.accent) + val accentColor = ContextCompat.getColor(context, R.color.accent) val stringColor = String.format("#%06X", 0xFFFFFF and accentColor) rootView.webcontent.visibility = View.VISIBLE val textColor = if (Scoop.getInstance().currentFlavor.isDayNight) { rootView.webcontent.setBackgroundColor( ContextCompat.getColor( - activity!!.baseContext, + context, R.color.dark_webview ) ) - ContextCompat.getColor(activity!!.baseContext, R.color.dark_webview_text) + ContextCompat.getColor(context, R.color.dark_webview_text) } else { rootView.webcontent.setBackgroundColor( ContextCompat.getColor( - activity!!.baseContext, + context, R.color.light_webview ) ) - ContextCompat.getColor(activity!!.baseContext, R.color.light_webview_text) + ContextCompat.getColor(context, R.color.light_webview_text) } val stringTextColor = String.format("#%06X", 0xFFFFFF and textColor) @@ -319,10 +334,44 @@ class ArticleFragment : Fragment() { val itemUrl = URL(url) baseUrl = itemUrl.protocol + "://" + itemUrl.host } catch (e: MalformedURLException) { - Toast.makeText(activity!!.baseContext, "You are encountering a bug that I can't solve. Can you please contact me to solve the issue, please ?", Toast.LENGTH_LONG).show() - Crashlytics.setUserIdentifier(prefs.getString("unique_id", "")) - Crashlytics.log(100, "BASE_URL_MALFORMED", e.message) - Crashlytics.logException(e) + if (showMalformedUrl) { + val alertDialog = AlertDialog.Builder(context).create() + alertDialog.setTitle("Error") + alertDialog.setMessage("You are encountering a bug that I can't solve. Can you please contact me to solve the issue, please ?") + alertDialog.setButton( + AlertDialog.BUTTON_POSITIVE, + "Send mail", + { dialog, _ -> + + // This won't be translated because it should only be temporary. + val to = BuildConfig.FEEDBACK_EMAIL + val subject= "[MalformedURLException]" + val body= "Please specify the source, item and spout you are using for the url below : \n ${e.message}" + val mailTo = "mailto:" + to + "?&subject=" + Uri.encode(subject) + "&body=" + Uri.encode(body) + + val emailIntent = Intent(Intent.ACTION_VIEW) + emailIntent.data = Uri.parse(mailTo) + startActivity(emailIntent) + + dialog.dismiss() + } + ) + alertDialog.setButton( + AlertDialog.BUTTON_NEUTRAL, + "Not now", + { dialog, _ -> dialog.dismiss() } + ) + alertDialog.setButton( + AlertDialog.BUTTON_NEGATIVE, + "Don't show anymore.", + { dialog, _ -> + editor.putBoolean("show_error_malformed_url", false) + editor.apply() + dialog.dismiss() + } + ) + alertDialog.show() + } } rootView.webcontent.loadDataWithBaseURL( @@ -374,8 +423,8 @@ class ArticleFragment : Fragment() { } companion object { - private val ARG_POSITION = "position" - private val ARG_ITEMS = "items" + private const val ARG_POSITION = "position" + private const val ARG_ITEMS = "items" fun newInstance( position: Int,