Fixing crashlytics issues.

This commit is contained in:
Amine Bou 2018-03-25 20:54:33 +02:00
parent 75e9031fa5
commit 3bcffff444
4 changed files with 75 additions and 41 deletions

View File

@ -6,7 +6,7 @@
- Added an option to use a webview in the article viewer (see #149) - 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 !!! - New year fixes !!!

View File

@ -204,11 +204,12 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
): Boolean = false ): Boolean = false
override fun onSwiped(viewHolder: RecyclerView.ViewHolder, swipeDir: Int) { override fun onSwiped(viewHolder: RecyclerView.ViewHolder, swipeDir: Int) {
try { val position = viewHolder.adapterPosition
val i = items[viewHolder.adapterPosition] val i = items.elementAtOrNull(position)
val position = items.indexOf(i)
if (i != null) {
val adapter = recyclerView.adapter val adapter = recyclerView.adapter
when (adapter) { when (adapter) {
is ItemCardAdapter -> adapter.removeItemAtIndex(position) is ItemCardAdapter -> adapter.removeItemAtIndex(position)
is ItemListAdapter -> adapter.removeItemAtIndex(position) is ItemListAdapter -> adapter.removeItemAtIndex(position)
@ -242,14 +243,6 @@ class HomeActivity : AppCompatActivity(), SearchView.OnQueryTextListener {
offsetOverride = lastVisibleItem offsetOverride = lastVisibleItem
) )
} }
} catch (e: IndexOutOfBoundsException) {
Crashlytics.setUserIdentifier(userIdentifier)
Crashlytics.log(
100,
"SWIPE_INDEX_OUT_OF_BOUND",
"IndexOutOfBoundsException when swiping"
)
Crashlytics.logException(e)
} }
} }
} }

View File

@ -67,14 +67,6 @@ class ReaderActivity : AppCompatActivity() {
markOnScroll = sharedPref.getBoolean("mark_on_scroll", false) markOnScroll = sharedPref.getBoolean("mark_on_scroll", false)
if (allItems.isEmpty()) { 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() finish()
} }

View File

@ -1,6 +1,10 @@
package apps.amine.bou.readerforselfoss.fragments package apps.amine.bou.readerforselfoss.fragments
import android.content.Context
import android.content.DialogInterface
import android.content.Intent
import android.content.SharedPreferences import android.content.SharedPreferences
import android.net.Uri
import android.os.Build import android.os.Build
import android.os.Bundle import android.os.Bundle
import android.preference.PreferenceManager import android.preference.PreferenceManager
@ -9,6 +13,8 @@ import android.support.design.widget.FloatingActionButton
import android.support.v4.app.Fragment import android.support.v4.app.Fragment
import android.support.v4.content.ContextCompat import android.support.v4.content.ContextCompat
import android.support.v4.widget.NestedScrollView 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.LayoutInflater
import android.view.MenuItem import android.view.MenuItem
import android.view.View import android.view.View
@ -49,6 +55,8 @@ class ArticleFragment : Fragment() {
private lateinit var contentSource: String private lateinit var contentSource: String
private lateinit var contentImage: String private lateinit var contentImage: String
private lateinit var contentTitle: String private lateinit var contentTitle: String
private var showMalformedUrl: Boolean = false
private lateinit var editor: SharedPreferences.Editor
private lateinit var fab: FloatingActionButton private lateinit var fab: FloatingActionButton
override fun onStop() { override fun onStop() {
@ -58,12 +66,14 @@ class ArticleFragment : Fragment() {
override fun onCreate(savedInstanceState: Bundle?) { override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState) super.onCreate(savedInstanceState)
pageNumber = arguments!!.getInt(ARG_POSITION) pageNumber = arguments!!.getInt(ARG_POSITION)
allItems = arguments!!.getParcelableArrayList(ARG_ITEMS) allItems = arguments!!.getParcelableArrayList(ARG_ITEMS)
} }
private lateinit var rootView: ViewGroup private lateinit var rootView: ViewGroup
override fun onCreateView( override fun onCreateView(
inflater: LayoutInflater, inflater: LayoutInflater,
container: ViewGroup?, container: ViewGroup?,
@ -72,6 +82,8 @@ class ArticleFragment : Fragment() {
rootView = inflater rootView = inflater
.inflate(R.layout.fragment_article, container, false) as ViewGroup .inflate(R.layout.fragment_article, container, false) as ViewGroup
val context: Context = activity!!
url = allItems[pageNumber.toInt()].getLinkDecoded() url = allItems[pageNumber.toInt()].getLinkDecoded()
contentText = allItems[pageNumber.toInt()].content contentText = allItems[pageNumber.toInt()].content
contentTitle = allItems[pageNumber.toInt()].title contentTitle = allItems[pageNumber.toInt()].title
@ -87,14 +99,16 @@ class ArticleFragment : Fragment() {
mCustomTabActivityHelper.bindCustomTabsService(activity) mCustomTabActivityHelper.bindCustomTabsService(activity)
val prefs = PreferenceManager.getDefaultSharedPreferences(activity) val prefs = PreferenceManager.getDefaultSharedPreferences(activity)
editor = prefs.edit()
fontSize = prefs.getString("reader_font_size", "14").toInt() fontSize = prefs.getString("reader_font_size", "14").toInt()
showMalformedUrl = prefs.getBoolean("show_error_malformed_url", true)
mFloatingToolbar.setClickListener( mFloatingToolbar.setClickListener(
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, prefs, context)
R.id.share_action -> activity!!.shareLink(url) R.id.share_action -> activity!!.shareLink(url)
R.id.open_action -> activity!!.openItemUrl( R.id.open_action -> activity!!.openItemUrl(
allItems, allItems,
@ -116,17 +130,17 @@ class ArticleFragment : Fragment() {
if (contentText.isEmptyOrNullOrNullString()) { if (contentText.isEmptyOrNullOrNullString()) {
getContentFromMercury(customTabsIntent, prefs) getContentFromMercury(customTabsIntent, prefs, context)
} else { } else {
rootView.source.text = contentSource rootView.source.text = contentSource
rootView.titleView.text = contentTitle rootView.titleView.text = contentTitle
htmlToWebview(contentText, prefs) htmlToWebview(contentText, prefs, context)
if (!contentImage.isEmptyOrNullOrNullString()) { if (!contentImage.isEmptyOrNullOrNullString()) {
rootView.imageView.visibility = View.VISIBLE rootView.imageView.visibility = View.VISIBLE
Glide Glide
.with(activity!!.baseContext) .with(context)
.asBitmap() .asBitmap()
.load(contentImage) .load(contentImage)
.apply(RequestOptions.fitCenterTransform()) .apply(RequestOptions.fitCenterTransform())
@ -151,7 +165,8 @@ class ArticleFragment : Fragment() {
private fun getContentFromMercury( private fun getContentFromMercury(
customTabsIntent: CustomTabsIntent, customTabsIntent: CustomTabsIntent,
prefs: SharedPreferences prefs: SharedPreferences,
context: Context
) { ) {
rootView.progressBar.visibility = View.VISIBLE rootView.progressBar.visibility = View.VISIBLE
val parser = MercuryApi( val parser = MercuryApi(
@ -183,13 +198,13 @@ class ArticleFragment : Fragment() {
} }
try { try {
htmlToWebview(response.body()!!.content.orEmpty(), prefs) htmlToWebview(response.body()!!.content.orEmpty(), prefs, context)
} catch (e: Exception) { } catch (e: Exception) {
Crashlytics.setUserIdentifier(prefs.getString("unique_id", "")) Crashlytics.setUserIdentifier(prefs.getString("unique_id", ""))
Crashlytics.log( Crashlytics.log(
100, 100,
"MERCURY_CONTENT_EXCEPTION", "MERCURY_CONTENT_EXCEPTION",
"Webview issue" "Webview issue ${e.message}"
) )
Crashlytics.logException(e) Crashlytics.logException(e)
} }
@ -199,7 +214,7 @@ class ArticleFragment : Fragment() {
rootView.imageView.visibility = View.VISIBLE rootView.imageView.visibility = View.VISIBLE
try { try {
Glide Glide
.with(activity!!.baseContext) .with(context)
.asBitmap() .asBitmap()
.load(response.body()!!.lead_image_url) .load(response.body()!!.lead_image_url)
.apply(RequestOptions.fitCenterTransform()) .apply(RequestOptions.fitCenterTransform())
@ -214,7 +229,7 @@ class ArticleFragment : Fragment() {
Crashlytics.log( Crashlytics.log(
100, 100,
"MERCURY_CONTENT_EXCEPTION", "MERCURY_CONTENT_EXCEPTION",
"Glide issue" "Glide issue with image ${response.body()!!.lead_image_url}"
) )
Crashlytics.logException(e) 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) val stringColor = String.format("#%06X", 0xFFFFFF and accentColor)
rootView.webcontent.visibility = View.VISIBLE rootView.webcontent.visibility = View.VISIBLE
val textColor = if (Scoop.getInstance().currentFlavor.isDayNight) { val textColor = if (Scoop.getInstance().currentFlavor.isDayNight) {
rootView.webcontent.setBackgroundColor( rootView.webcontent.setBackgroundColor(
ContextCompat.getColor( ContextCompat.getColor(
activity!!.baseContext, context,
R.color.dark_webview R.color.dark_webview
) )
) )
ContextCompat.getColor(activity!!.baseContext, R.color.dark_webview_text) ContextCompat.getColor(context, R.color.dark_webview_text)
} else { } else {
rootView.webcontent.setBackgroundColor( rootView.webcontent.setBackgroundColor(
ContextCompat.getColor( ContextCompat.getColor(
activity!!.baseContext, context,
R.color.light_webview 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) val stringTextColor = String.format("#%06X", 0xFFFFFF and textColor)
@ -319,10 +334,44 @@ class ArticleFragment : Fragment() {
val itemUrl = URL(url) val itemUrl = URL(url)
baseUrl = itemUrl.protocol + "://" + itemUrl.host baseUrl = itemUrl.protocol + "://" + itemUrl.host
} catch (e: MalformedURLException) { } 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() if (showMalformedUrl) {
Crashlytics.setUserIdentifier(prefs.getString("unique_id", "")) val alertDialog = AlertDialog.Builder(context).create()
Crashlytics.log(100, "BASE_URL_MALFORMED", e.message) alertDialog.setTitle("Error")
Crashlytics.logException(e) 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( rootView.webcontent.loadDataWithBaseURL(
@ -374,8 +423,8 @@ class ArticleFragment : Fragment() {
} }
companion object { companion object {
private val ARG_POSITION = "position" private const val ARG_POSITION = "position"
private val ARG_ITEMS = "items" private const val ARG_ITEMS = "items"
fun newInstance( fun newInstance(
position: Int, position: Int,