forked from upstream-mirrors/quik
Fix crash when phone permission not granted
This commit is contained in:
@@ -39,6 +39,8 @@ class PermissionManagerImpl @Inject constructor(private val context: Context) :
|
|||||||
|
|
||||||
override fun hasContacts(): Boolean = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PERMISSION_GRANTED
|
override fun hasContacts(): Boolean = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_CONTACTS) == PERMISSION_GRANTED
|
||||||
|
|
||||||
|
override fun hasPhone(): Boolean = ContextCompat.checkSelfPermission(context, Manifest.permission.READ_PHONE_STATE) == PERMISSION_GRANTED
|
||||||
|
|
||||||
override fun hasStorage(): Boolean = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PERMISSION_GRANTED
|
override fun hasStorage(): Boolean = ContextCompat.checkSelfPermission(context, Manifest.permission.WRITE_EXTERNAL_STORAGE) == PERMISSION_GRANTED
|
||||||
|
|
||||||
}
|
}
|
||||||
@@ -1,59 +0,0 @@
|
|||||||
/*
|
|
||||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
|
||||||
*
|
|
||||||
* This file is part of QKSMS.
|
|
||||||
*
|
|
||||||
* QKSMS is free software: you can redistribute it and/or modify
|
|
||||||
* it under the terms of the GNU General Public License as published by
|
|
||||||
* the Free Software Foundation, either version 3 of the License, or
|
|
||||||
* (at your option) any later version.
|
|
||||||
*
|
|
||||||
* QKSMS is distributed in the hope that it will be useful,
|
|
||||||
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
||||||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
||||||
* GNU General Public License for more details.
|
|
||||||
*
|
|
||||||
* You should have received a copy of the GNU General Public License
|
|
||||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
|
||||||
*/
|
|
||||||
package util
|
|
||||||
|
|
||||||
import android.telephony.SubscriptionInfo
|
|
||||||
import android.telephony.SubscriptionManager
|
|
||||||
import io.reactivex.Observable
|
|
||||||
import io.reactivex.Observer
|
|
||||||
import io.reactivex.disposables.Disposable
|
|
||||||
|
|
||||||
class ActiveSubscriptionObservable(private val subscriptionManager: SubscriptionManager) : Observable<List<SubscriptionInfo>>() {
|
|
||||||
|
|
||||||
override fun subscribeActual(observer: Observer<in List<SubscriptionInfo>>) {
|
|
||||||
observer.onNext(subscriptionManager.activeSubscriptionInfoList ?: listOf())
|
|
||||||
|
|
||||||
val listener = Listener(subscriptionManager, observer)
|
|
||||||
observer.onSubscribe(listener)
|
|
||||||
subscriptionManager.addOnSubscriptionsChangedListener(listener)
|
|
||||||
}
|
|
||||||
|
|
||||||
internal class Listener(
|
|
||||||
private val subscriptionManager: SubscriptionManager,
|
|
||||||
private val observer: Observer<in List<SubscriptionInfo>>
|
|
||||||
) : Disposable, SubscriptionManager.OnSubscriptionsChangedListener() {
|
|
||||||
|
|
||||||
private var disposed: Boolean = false
|
|
||||||
|
|
||||||
override fun onSubscriptionsChanged() {
|
|
||||||
if (!isDisposed) {
|
|
||||||
observer.onNext(subscriptionManager.activeSubscriptionInfoList ?: listOf())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
override fun isDisposed(): Boolean = disposed
|
|
||||||
|
|
||||||
override fun dispose() {
|
|
||||||
disposed = true
|
|
||||||
subscriptionManager.removeOnSubscriptionsChangedListener(this)
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
|
|
||||||
}
|
|
||||||
@@ -5,14 +5,24 @@ import android.telephony.PhoneNumberUtils
|
|||||||
import android.telephony.SubscriptionInfo
|
import android.telephony.SubscriptionInfo
|
||||||
import android.telephony.SubscriptionManager
|
import android.telephony.SubscriptionManager
|
||||||
import io.reactivex.Observable
|
import io.reactivex.Observable
|
||||||
|
import io.reactivex.Observer
|
||||||
|
import io.reactivex.disposables.Disposable
|
||||||
|
import manager.PermissionManager
|
||||||
import javax.inject.Inject
|
import javax.inject.Inject
|
||||||
|
|
||||||
class SubscriptionUtilsImpl @Inject constructor(context: Context) : SubscriptionUtils {
|
class SubscriptionUtilsImpl @Inject constructor(
|
||||||
|
context: Context,
|
||||||
|
private val permissions: PermissionManager
|
||||||
|
) : SubscriptionUtils {
|
||||||
|
|
||||||
private val subscriptionManager = SubscriptionManager.from(context)
|
/**
|
||||||
|
* If we don't have the phone permission, then return a null [SubscriptionManager]
|
||||||
|
*/
|
||||||
|
private val subscriptionManager: SubscriptionManager? = SubscriptionManager.from(context)
|
||||||
|
get() = field?.takeIf { permissions.hasPhone() }
|
||||||
|
|
||||||
override val subscriptions: List<SubscriptionInfo>
|
override val subscriptions: List<SubscriptionInfo>
|
||||||
get() = subscriptionManager.activeSubscriptionInfoList ?: listOf()
|
get() = subscriptionManager?.activeSubscriptionInfoList ?: listOf()
|
||||||
|
|
||||||
override val subscriptionsObservable: Observable<List<SubscriptionInfo>> = ActiveSubscriptionObservable(subscriptionManager)
|
override val subscriptionsObservable: Observable<List<SubscriptionInfo>> = ActiveSubscriptionObservable(subscriptionManager)
|
||||||
|
|
||||||
@@ -21,3 +31,39 @@ class SubscriptionUtilsImpl @Inject constructor(context: Context) : Subscription
|
|||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
|
class ActiveSubscriptionObservable(
|
||||||
|
private val subscriptionManager: SubscriptionManager?
|
||||||
|
) : Observable<List<SubscriptionInfo>>() {
|
||||||
|
|
||||||
|
override fun subscribeActual(observer: Observer<in List<SubscriptionInfo>>) {
|
||||||
|
observer.onNext(subscriptionManager?.activeSubscriptionInfoList ?: listOf())
|
||||||
|
|
||||||
|
val listener = Listener(subscriptionManager, observer)
|
||||||
|
observer.onSubscribe(listener)
|
||||||
|
subscriptionManager?.addOnSubscriptionsChangedListener(listener)
|
||||||
|
}
|
||||||
|
|
||||||
|
internal class Listener(
|
||||||
|
private val subscriptionManager: SubscriptionManager?,
|
||||||
|
private val observer: Observer<in List<SubscriptionInfo>>
|
||||||
|
) : Disposable, SubscriptionManager.OnSubscriptionsChangedListener() {
|
||||||
|
|
||||||
|
private var disposed: Boolean = false
|
||||||
|
|
||||||
|
override fun onSubscriptionsChanged() {
|
||||||
|
if (!isDisposed) {
|
||||||
|
observer.onNext(subscriptionManager?.activeSubscriptionInfoList ?: listOf())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun isDisposed(): Boolean = disposed
|
||||||
|
|
||||||
|
override fun dispose() {
|
||||||
|
disposed = true
|
||||||
|
subscriptionManager?.removeOnSubscriptionsChangedListener(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
||||||
@@ -28,6 +28,8 @@ interface PermissionManager {
|
|||||||
|
|
||||||
fun hasContacts(): Boolean
|
fun hasContacts(): Boolean
|
||||||
|
|
||||||
|
fun hasPhone(): Boolean
|
||||||
|
|
||||||
fun hasStorage(): Boolean
|
fun hasStorage(): Boolean
|
||||||
|
|
||||||
}
|
}
|
||||||
Reference in New Issue
Block a user