catching IllegalStateException when adding OnAccountsUpdatedListener twice which shouldn't happen

This commit is contained in:
Mariotaku Lee 2016-12-13 09:12:55 +08:00
parent d137d8846d
commit 54306c2d17
8 changed files with 68 additions and 62 deletions

View File

@ -0,0 +1,29 @@
package org.mariotaku.ktextension
import android.accounts.AccountManager
import android.accounts.OnAccountsUpdateListener
import android.os.Handler
/**
* Created by mariotaku on 2016/12/13.
*/
fun AccountManager.addOnAccountsUpdatedListenerSafe(listener: OnAccountsUpdateListener,
handler: Handler? = null,
updateImmediately: Boolean = false): Boolean {
try {
this.addOnAccountsUpdatedListener(listener, handler, updateImmediately)
return true
} catch (e: IllegalStateException) {
return false
}
}
fun AccountManager.removeOnAccountsUpdatedListenerSafe(listener: OnAccountsUpdateListener): Boolean {
try {
this.removeOnAccountsUpdatedListener(listener)
return true
} catch (e: IllegalStateException) {
return false
}
}

View File

@ -57,7 +57,9 @@ import kotlinx.android.synthetic.main.activity_home_content.*
import kotlinx.android.synthetic.main.layout_empty_tab_hint.*
import org.mariotaku.abstask.library.AbstractTask
import org.mariotaku.abstask.library.TaskStarter
import org.mariotaku.ktextension.addOnAccountsUpdatedListenerSafe
import org.mariotaku.ktextension.convert
import org.mariotaku.ktextension.removeOnAccountsUpdatedListenerSafe
import org.mariotaku.twidere.Constants.*
import org.mariotaku.twidere.R
import org.mariotaku.twidere.activity.iface.IControlBarActivity
@ -389,7 +391,7 @@ class HomeActivity : BaseActivity(), OnClickListener, OnPageChangeListener, Supp
override fun onStart() {
super.onStart()
multiSelectHandler.dispatchOnStart()
AccountManager.get(this).addOnAccountsUpdatedListener(accountUpdatedListener, null, false)
AccountManager.get(this).addOnAccountsUpdatedListenerSafe(accountUpdatedListener, updateImmediately = false)
bus.register(this)
readStateManager.registerOnSharedPreferenceChangeListener(readStateChangeListener)
@ -406,7 +408,7 @@ class HomeActivity : BaseActivity(), OnClickListener, OnPageChangeListener, Supp
multiSelectHandler.dispatchOnStop()
readStateManager.unregisterOnSharedPreferenceChangeListener(readStateChangeListener)
bus.unregister(this)
AccountManager.get(this).removeOnAccountsUpdatedListener(accountUpdatedListener)
AccountManager.get(this).removeOnAccountsUpdatedListenerSafe(accountUpdatedListener)
preferences.edit().putInt(SharedPreferenceConstants.KEY_SAVED_TAB_POSITION, mainPager.currentItem).apply()
super.onStop()

View File

@ -55,10 +55,7 @@ import android.view.View.OnClickListener
import android.view.animation.DecelerateInterpolator
import android.widget.ImageView
import kotlinx.android.synthetic.main.header_drawer_account_selector.view.*
import org.mariotaku.ktextension.convert
import org.mariotaku.ktextension.setItemAvailability
import org.mariotaku.ktextension.setMenuItemIcon
import org.mariotaku.ktextension.setMenuItemTitle
import org.mariotaku.ktextension.*
import org.mariotaku.twidere.R
import org.mariotaku.twidere.TwidereConstants.*
import org.mariotaku.twidere.activity.*
@ -728,7 +725,9 @@ class AccountsDashboardFragment : BaseSupportFragment(), LoaderCallbacks<Account
class AccountsInfoLoader(context: Context) : AsyncTaskLoader<AccountsInfo>(context) {
private var contentObserver: ContentObserver? = null
private var accountListener: OnAccountsUpdateListener? = null
private val accountListener = OnAccountsUpdateListener {
onContentChanged()
}
private var firstLoad: Boolean
@ -756,10 +755,7 @@ class AccountsDashboardFragment : BaseSupportFragment(), LoaderCallbacks<Account
context.contentResolver.unregisterContentObserver(contentObserver)
contentObserver = null
}
if (accountListener != null) {
AccountManager.get(context).removeOnAccountsUpdatedListener(accountListener)
accountListener = null
}
AccountManager.get(context).removeOnAccountsUpdatedListenerSafe(accountListener)
}
/**
@ -780,12 +776,7 @@ class AccountsDashboardFragment : BaseSupportFragment(), LoaderCallbacks<Account
}
context.contentResolver.registerContentObserver(Drafts.CONTENT_URI, true, contentObserver)
}
if (accountListener == null) {
accountListener = OnAccountsUpdateListener { accounts ->
onContentChanged()
}
AccountManager.get(context).addOnAccountsUpdatedListener(accountListener, null, false)
}
AccountManager.get(context).addOnAccountsUpdatedListenerSafe(accountListener, updateImmediately = false)
if (takeContentChanged() || firstLoad) {
firstLoad = false

View File

@ -29,6 +29,8 @@ import android.os.Bundle
import android.os.Handler
import android.support.v4.content.Loader
import com.squareup.otto.Subscribe
import org.mariotaku.ktextension.addOnAccountsUpdatedListenerSafe
import org.mariotaku.ktextension.removeOnAccountsUpdatedListenerSafe
import org.mariotaku.library.objectcursor.ObjectCursor
import org.mariotaku.sqliteqb.library.ArgsArray
import org.mariotaku.sqliteqb.library.Columns.Column
@ -73,7 +75,9 @@ abstract class CursorActivitiesFragment : AbsActivitiesFragment() {
protected abstract val errorInfoKey: String
private var contentObserver: ContentObserver? = null
private var accountListener: OnAccountsUpdateListener? = null
private val accountListener: OnAccountsUpdateListener = OnAccountsUpdateListener { accounts ->
reloadActivities()
}
abstract val contentUri: Uri
@ -122,12 +126,7 @@ abstract class CursorActivitiesFragment : AbsActivitiesFragment() {
}
context.contentResolver.registerContentObserver(Filters.CONTENT_URI, true, contentObserver)
}
if (accountListener == null) {
accountListener = OnAccountsUpdateListener { accounts ->
reloadActivities()
}
AccountManager.get(context).addOnAccountsUpdatedListener(accountListener, null, false)
}
AccountManager.get(context).addOnAccountsUpdatedListenerSafe(accountListener, updateImmediately = false)
updateRefreshState()
reloadActivities()
}
@ -137,10 +136,7 @@ abstract class CursorActivitiesFragment : AbsActivitiesFragment() {
context.contentResolver.unregisterContentObserver(contentObserver)
contentObserver = null
}
if (accountListener != null) {
AccountManager.get(context).removeOnAccountsUpdatedListener(accountListener)
accountListener = null
}
AccountManager.get(context).removeOnAccountsUpdatedListenerSafe(accountListener)
super.onStop()
}

View File

@ -29,6 +29,8 @@ import android.os.Handler
import android.support.v4.content.Loader
import com.squareup.otto.Subscribe
import kotlinx.android.synthetic.main.fragment_content_recyclerview.*
import org.mariotaku.ktextension.addOnAccountsUpdatedListenerSafe
import org.mariotaku.ktextension.removeOnAccountsUpdatedListenerSafe
import org.mariotaku.sqliteqb.library.ArgsArray
import org.mariotaku.sqliteqb.library.Columns.Column
import org.mariotaku.sqliteqb.library.Expression
@ -54,7 +56,9 @@ import org.mariotaku.twidere.util.Utils
abstract class CursorStatusesFragment : AbsStatusesFragment() {
private var contentObserver: ContentObserver? = null
private var accountListener: OnAccountsUpdateListener? = null
private val accountListener: OnAccountsUpdateListener = OnAccountsUpdateListener { accounts ->
reloadStatuses()
}
abstract val errorInfoKey: String
abstract val isFilterEnabled: Boolean
@ -129,12 +133,7 @@ abstract class CursorStatusesFragment : AbsStatusesFragment() {
}
context.contentResolver.registerContentObserver(Filters.CONTENT_URI, true, contentObserver)
}
if (accountListener == null) {
accountListener = OnAccountsUpdateListener { accounts ->
reloadStatuses()
}
AccountManager.get(context).addOnAccountsUpdatedListener(accountListener, null, false)
}
AccountManager.get(context).addOnAccountsUpdatedListenerSafe(accountListener, updateImmediately = false)
updateRefreshState()
reloadStatuses()
}
@ -144,10 +143,7 @@ abstract class CursorStatusesFragment : AbsStatusesFragment() {
context.contentResolver.unregisterContentObserver(contentObserver)
contentObserver = null
}
if (accountListener != null) {
AccountManager.get(context).removeOnAccountsUpdatedListener(accountListener)
accountListener = null
}
AccountManager.get(context).removeOnAccountsUpdatedListenerSafe(accountListener)
super.onStop()
}

View File

@ -37,6 +37,8 @@ import android.view.MenuInflater
import android.view.MenuItem
import com.squareup.otto.Subscribe
import kotlinx.android.synthetic.main.fragment_content_recyclerview.*
import org.mariotaku.ktextension.addOnAccountsUpdatedListenerSafe
import org.mariotaku.ktextension.removeOnAccountsUpdatedListenerSafe
import org.mariotaku.sqliteqb.library.ArgsArray
import org.mariotaku.sqliteqb.library.Columns.Column
import org.mariotaku.sqliteqb.library.Expression
@ -93,20 +95,12 @@ class DirectMessagesFragment : AbsContentListRecyclerViewFragment<MessageEntries
override fun onStart() {
super.onStart()
bus.register(this)
try {
AccountManager.get(context).addOnAccountsUpdatedListener(accountListener, null, false)
} catch (e: IllegalStateException) {
// This really shouldn't happen.
}
AccountManager.get(context).addOnAccountsUpdatedListenerSafe(accountListener, updateImmediately = false)
adapter.updateReadState()
}
override fun onStop() {
try {
AccountManager.get(context).removeOnAccountsUpdatedListener(accountListener)
} catch (e: IllegalStateException) {
// This really shouldn't happen.
}
AccountManager.get(context).removeOnAccountsUpdatedListenerSafe(accountListener)
bus.unregister(this)
super.onStop()
}

View File

@ -4,6 +4,8 @@ import android.accounts.AccountManager
import android.accounts.OnAccountsUpdateListener
import android.content.Context
import android.support.v4.content.AsyncTaskLoader
import org.mariotaku.ktextension.addOnAccountsUpdatedListenerSafe
import org.mariotaku.ktextension.removeOnAccountsUpdatedListenerSafe
import org.mariotaku.twidere.model.AccountDetails
import org.mariotaku.twidere.model.util.AccountUtils
@ -15,7 +17,9 @@ class AccountDetailsLoader(
val filter: (AccountDetails.() -> Boolean)? = null
) : AsyncTaskLoader<List<AccountDetails>>(context) {
private val am: AccountManager
private var accountUpdateListener: OnAccountsUpdateListener? = null
private val accountUpdateListener = OnAccountsUpdateListener {
onContentChanged()
}
init {
am = AccountManager.get(context)
@ -30,19 +34,11 @@ class AccountDetailsLoader(
override fun onReset() {
super.onReset()
onStopLoading()
if (accountUpdateListener != null) {
am.removeOnAccountsUpdatedListener(accountUpdateListener)
accountUpdateListener = null
}
am.removeOnAccountsUpdatedListenerSafe(accountUpdateListener)
}
override fun onStartLoading() {
if (accountUpdateListener == null) {
accountUpdateListener = OnAccountsUpdateListener {
onContentChanged()
}
am.addOnAccountsUpdatedListener(accountUpdateListener, null, true)
}
am.addOnAccountsUpdatedListenerSafe(accountUpdateListener, updateImmediately = true)
if (takeContentChanged()) {
forceLoad()
}

View File

@ -13,6 +13,8 @@ import android.support.v4.app.NotificationCompat
import android.support.v4.util.SimpleArrayMap
import android.text.TextUtils
import android.util.Log
import org.mariotaku.ktextension.addOnAccountsUpdatedListenerSafe
import org.mariotaku.ktextension.removeOnAccountsUpdatedListenerSafe
import org.mariotaku.microblog.library.MicroBlogException
import org.mariotaku.microblog.library.twitter.TwitterUserStream
import org.mariotaku.microblog.library.twitter.UserStreamCallback
@ -58,12 +60,12 @@ class StreamingService : Service() {
Log.d(LOGTAG, "Stream service started.")
}
initStreaming()
AccountManager.get(this).addOnAccountsUpdatedListener(accountChangeObserver, null, false)
AccountManager.get(this).addOnAccountsUpdatedListenerSafe(accountChangeObserver, updateImmediately = false)
}
override fun onDestroy() {
clearTwitterInstances()
AccountManager.get(this).removeOnAccountsUpdatedListener(accountChangeObserver)
AccountManager.get(this).removeOnAccountsUpdatedListenerSafe(accountChangeObserver)
if (BuildConfig.DEBUG) {
Log.d(LOGTAG, "Stream service stopped.")
}