Add handling if notification blocking is enabled

This commit is contained in:
Jonas L 2019-03-04 00:00:00 +00:00
parent d7fd43685b
commit 29b919b94f
5 changed files with 25 additions and 14 deletions

View file

@ -93,6 +93,7 @@ class NotificationListener: NotificationListenerService() {
BlockingReason.BlockedAtThisTime -> getString(R.string.lock_reason_short_blocked_time_area) BlockingReason.BlockedAtThisTime -> getString(R.string.lock_reason_short_blocked_time_area)
BlockingReason.MissingNetworkTime -> getString(R.string.lock_reason_short_missing_network_time) BlockingReason.MissingNetworkTime -> getString(R.string.lock_reason_short_missing_network_time)
BlockingReason.RequiresCurrentDevice -> getString(R.string.lock_reason_short_requires_current_device) BlockingReason.RequiresCurrentDevice -> getString(R.string.lock_reason_short_requires_current_device)
BlockingReason.NotificationsAreBlocked -> getString(R.string.lock_reason_short_notification_blocking)
BlockingReason.None -> throw IllegalStateException() BlockingReason.None -> throw IllegalStateException()
} }
) )
@ -115,7 +116,10 @@ class NotificationListener: NotificationListenerService() {
return BlockingReason.None return BlockingReason.None
} }
val blockingReason = blockingReasonUtil.getBlockingReason(sbn.packageName).waitForNonNullValue() val blockingReason = blockingReasonUtil.getBlockingReason(
packageName = sbn.packageName,
forNotification = true
).waitForNonNullValue()
if (blockingReason == BlockingReason.None) { if (blockingReason == BlockingReason.None) {
return BlockingReason.None return BlockingReason.None

View file

@ -39,7 +39,8 @@ enum class BlockingReason {
TimeOver, TimeOver,
TimeOverExtraTimeCanBeUsedLater, TimeOverExtraTimeCanBeUsedLater,
MissingNetworkTime, MissingNetworkTime,
RequiresCurrentDevice RequiresCurrentDevice,
NotificationsAreBlocked
} }
class BlockingReasonUtil(private val appLogic: AppLogic) { class BlockingReasonUtil(private val appLogic: AppLogic) {
@ -47,7 +48,7 @@ class BlockingReasonUtil(private val appLogic: AppLogic) {
private const val LOG_TAG = "BlockingReason" private const val LOG_TAG = "BlockingReason"
} }
fun getBlockingReason(packageName: String): LiveData<BlockingReason> { fun getBlockingReason(packageName: String, forNotification: Boolean): LiveData<BlockingReason> {
// check precondition that the app is running // check precondition that the app is running
return appLogic.enable.switchMap { return appLogic.enable.switchMap {
@ -62,14 +63,14 @@ class BlockingReasonUtil(private val appLogic: AppLogic) {
if (user == null || user.type != UserType.Child) { if (user == null || user.type != UserType.Child) {
liveDataFromValue(BlockingReason.None) liveDataFromValue(BlockingReason.None)
} else { } else {
getBlockingReasonStep2(packageName, user, TimeZone.getTimeZone(user.timeZone)) getBlockingReasonStep2(packageName, user, TimeZone.getTimeZone(user.timeZone), forNotification)
} }
} }
} }
} }
} }
private fun getBlockingReasonStep2(packageName: String, child: User, timeZone: TimeZone): LiveData<BlockingReason> { private fun getBlockingReasonStep2(packageName: String, child: User, timeZone: TimeZone, forNotification: Boolean): LiveData<BlockingReason> {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "step 2") Log.d(LOG_TAG, "step 2")
} }
@ -80,11 +81,11 @@ class BlockingReasonUtil(private val appLogic: AppLogic) {
} else if (AndroidIntegrationApps.ignoredApps.contains(packageName)) { } else if (AndroidIntegrationApps.ignoredApps.contains(packageName)) {
return liveDataFromValue(BlockingReason.None) return liveDataFromValue(BlockingReason.None)
} else { } else {
return getBlockingReasonStep3(packageName, child, timeZone) return getBlockingReasonStep3(packageName, child, timeZone, forNotification)
} }
} }
private fun getBlockingReasonStep3(packageName: String, child: User, timeZone: TimeZone): LiveData<BlockingReason> { private fun getBlockingReasonStep3(packageName: String, child: User, timeZone: TimeZone, forNotification: Boolean): LiveData<BlockingReason> {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "step 3") Log.d(LOG_TAG, "step 3")
} }
@ -102,12 +103,12 @@ class BlockingReasonUtil(private val appLogic: AppLogic) {
if (temporarilyAllowedApps.contains(packageName)) { if (temporarilyAllowedApps.contains(packageName)) {
liveDataFromValue(BlockingReason.None) liveDataFromValue(BlockingReason.None)
} else { } else {
getBlockingReasonStep4(packageName, child, timeZone) getBlockingReasonStep4(packageName, child, timeZone, forNotification)
} }
} }
} }
private fun getBlockingReasonStep4(packageName: String, child: User, timeZone: TimeZone): LiveData<BlockingReason> { private fun getBlockingReasonStep4(packageName: String, child: User, timeZone: TimeZone, forNotification: Boolean): LiveData<BlockingReason> {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "step 4") Log.d(LOG_TAG, "step 4")
} }
@ -137,20 +138,24 @@ class BlockingReasonUtil(private val appLogic: AppLogic) {
if (categoryEntry2 == null) { if (categoryEntry2 == null) {
liveDataFromValue(BlockingReason.NotPartOfAnCategory) liveDataFromValue(BlockingReason.NotPartOfAnCategory)
} else { } else {
getBlockingReasonStep4Point5(categoryEntry2, child, timeZone, false) getBlockingReasonStep4Point5(categoryEntry2, child, timeZone, false, forNotification)
} }
} }
} else { } else {
getBlockingReasonStep4Point5(categoryEntry, child, timeZone, false) getBlockingReasonStep4Point5(categoryEntry, child, timeZone, false, forNotification)
} }
} }
} }
private fun getBlockingReasonStep4Point5(category: Category, child: User, timeZone: TimeZone, isParentCategory: Boolean): LiveData<BlockingReason> { private fun getBlockingReasonStep4Point5(category: Category, child: User, timeZone: TimeZone, isParentCategory: Boolean, forNotification: Boolean): LiveData<BlockingReason> {
if (BuildConfig.DEBUG) { if (BuildConfig.DEBUG) {
Log.d(LOG_TAG, "step 4.5") Log.d(LOG_TAG, "step 4.5")
} }
if (forNotification && category.blockAllNotifications) {
return liveDataFromValue(BlockingReason.NotificationsAreBlocked)
}
if (category.temporarilyBlocked) { if (category.temporarilyBlocked) {
return liveDataFromValue(BlockingReason.TemporarilyBlocked) return liveDataFromValue(BlockingReason.TemporarilyBlocked)
} }
@ -181,7 +186,7 @@ class BlockingReasonUtil(private val appLogic: AppLogic) {
if (parentCategory == null) { if (parentCategory == null) {
liveDataFromValue(BlockingReason.None) liveDataFromValue(BlockingReason.None)
} else { } else {
getBlockingReasonStep4Point5(parentCategory, child, timeZone, true) getBlockingReasonStep4Point5(parentCategory, child, timeZone, true, forNotification)
} }
} }
} else { } else {

View file

@ -72,7 +72,7 @@ class LockFragment : Fragment() {
private val auth: ActivityViewModel by lazy { getActivityViewModel(activity!!) } private val auth: ActivityViewModel by lazy { getActivityViewModel(activity!!) }
private val logic: AppLogic by lazy { DefaultAppLogic.with(context!!) } private val logic: AppLogic by lazy { DefaultAppLogic.with(context!!) }
private val title: String? by lazy { logic.platformIntegration.getLocalAppTitle(packageName) } private val title: String? by lazy { logic.platformIntegration.getLocalAppTitle(packageName) }
private val blockingReason: LiveData<BlockingReason> by lazy { BlockingReasonUtil(logic).getBlockingReason(packageName) } private val blockingReason: LiveData<BlockingReason> by lazy { BlockingReasonUtil(logic).getBlockingReason(packageName, forNotification = false) }
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View { override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
val binding = LockFragmentBinding.inflate(layoutInflater, container, false) val binding = LockFragmentBinding.inflate(layoutInflater, container, false)

View file

@ -82,4 +82,5 @@
<string name="lock_reason_short_blocked_time_area">Sperrzeit</string> <string name="lock_reason_short_blocked_time_area">Sperrzeit</string>
<string name="lock_reason_short_missing_network_time">Netzwerkzeit erforderlich</string> <string name="lock_reason_short_missing_network_time">Netzwerkzeit erforderlich</string>
<string name="lock_reason_short_requires_current_device">nicht als aktuelles Gerät gewählt</string> <string name="lock_reason_short_requires_current_device">nicht als aktuelles Gerät gewählt</string>
<string name="lock_reason_short_notification_blocking">alle Benachrichtigungen werden blockiert</string>
</resources> </resources>

View file

@ -86,4 +86,5 @@
<string name="lock_reason_short_blocked_time_area">blocked time area</string> <string name="lock_reason_short_blocked_time_area">blocked time area</string>
<string name="lock_reason_short_missing_network_time">missing network time</string> <string name="lock_reason_short_missing_network_time">missing network time</string>
<string name="lock_reason_short_requires_current_device">device must be the current device</string> <string name="lock_reason_short_requires_current_device">device must be the current device</string>
<string name="lock_reason_short_notification_blocking">all notifications are blocked</string>
</resources> </resources>