Add AppNotificationPreference to App Info Settings

This is only for the App Info Settings page, which is planed to be
migrated to SPA in Android U.

Currently protected by flag settings_enable_spa, default false.

This will not impacted the AppNotificationSettings, which has no plan
to be migrated.

Bug: 236346018
Test: Unit test
Test: Manual with Settings App
Change-Id: I4ae669ce36eb6ab73f1adf99585f424e03bf90fb
This commit is contained in:
Chaohui Wang
2022-09-26 20:09:36 +08:00
parent b760ffdc1a
commit dcd965b816
5 changed files with 341 additions and 4 deletions

View File

@@ -95,7 +95,7 @@ private fun AppInfoSettings(packageInfoPresenter: PackageInfoPresenter) {
AppSettingsPreference(app)
AppAllServicesPreference(app)
// TODO: notification_settings
AppNotificationPreference(app)
AppPermissionPreference(app)
AppStoragePreference(app)
InstantAppDomainsPreference(app)

View File

@@ -0,0 +1,67 @@
/*
* Copyright (C) 2022 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.settings.spa.app.appinfo
import android.content.Context
import android.content.pm.ApplicationInfo
import androidx.compose.runtime.Composable
import androidx.compose.runtime.remember
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import androidx.lifecycle.compose.ExperimentalLifecycleComposeApi
import androidx.lifecycle.compose.collectAsStateWithLifecycle
import com.android.settings.R
import com.android.settings.applications.appinfo.AppInfoDashboardFragment
import com.android.settings.notification.app.AppNotificationSettings
import com.android.settings.spa.notification.AppNotificationRepository
import com.android.settings.spa.notification.IAppNotificationRepository
import com.android.settingslib.spa.framework.compose.rememberContext
import com.android.settingslib.spa.widget.preference.Preference
import com.android.settingslib.spa.widget.preference.PreferenceModel
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.flowOn
@OptIn(ExperimentalLifecycleComposeApi::class)
@Composable
fun AppNotificationPreference(
app: ApplicationInfo,
repository: IAppNotificationRepository = rememberContext(::AppNotificationRepository),
) {
val context = LocalContext.current
val summaryFlow = remember(app) {
flow {
emit(repository.getNotificationSummary(app))
}.flowOn(Dispatchers.IO)
}
Preference(object : PreferenceModel {
override val title = stringResource(R.string.notifications_label)
override val summary = summaryFlow.collectAsStateWithLifecycle(
initialValue = stringResource(R.string.summary_placeholder)
)
override val onClick = { navigateToAppNotificationSettings(context, app) }
})
}
private fun navigateToAppNotificationSettings(context: Context, app: ApplicationInfo) {
AppInfoDashboardFragment.startAppInfoFragment(
AppNotificationSettings::class.java,
app,
context,
AppInfoSettingsProvider.METRICS_CATEGORY,
)
}

View File

@@ -31,8 +31,10 @@ import android.os.RemoteException
import android.os.ServiceManager
import android.util.Log
import com.android.settings.R
import com.android.settingslib.spa.framework.util.formatString
import com.android.settingslib.spaprivileged.model.app.IPackageManagers
import com.android.settingslib.spaprivileged.model.app.PackageManagers
import com.android.settingslib.spaprivileged.model.app.userId
import java.util.concurrent.TimeUnit
import kotlin.math.max
import kotlin.math.roundToInt
@@ -50,6 +52,11 @@ data class NotificationSentState(
var sentCount: Int = 0,
)
interface IAppNotificationRepository {
/** Gets the notification summary for the given application. */
fun getNotificationSummary(app: ApplicationInfo): String
}
class AppNotificationRepository(
private val context: Context,
private val packageManagers: IPackageManagers = PackageManagers,
@@ -59,7 +66,7 @@ class AppNotificationRepository(
private val notificationManager: INotificationManager = INotificationManager.Stub.asInterface(
ServiceManager.getService(Context.NOTIFICATION_SERVICE)
),
) {
) : IAppNotificationRepository {
fun getAggregatedUsageEvents(userIdFlow: Flow<Int>): Flow<Map<String, NotificationSentState>> =
userIdFlow.map { userId ->
val aggregatedStats = mutableMapOf<String, NotificationSentState>()
@@ -115,6 +122,58 @@ class AppNotificationRepository(
}
}
override fun getNotificationSummary(app: ApplicationInfo): String {
if (!isEnabled(app)) return context.getString(R.string.off)
val channelCount = getChannelCount(app)
if (channelCount == 0) {
return calculateFrequencySummary(getSentCount(app))
}
val blockedChannelCount = getBlockedChannelCount(app)
if (channelCount == blockedChannelCount) return context.getString(R.string.off)
val frequencySummary = calculateFrequencySummary(getSentCount(app))
if (blockedChannelCount == 0) return frequencySummary
return context.getString(
R.string.notifications_enabled_with_info,
frequencySummary,
context.formatString(
R.string.notifications_categories_off, "count" to blockedChannelCount
)
)
}
private fun getSentCount(app: ApplicationInfo): Int {
var sentCount = 0
queryEventsForPackageForUser(app).forEachNotificationEvent { sentCount++ }
return sentCount
}
private fun queryEventsForPackageForUser(app: ApplicationInfo): UsageEvents? {
val now = System.currentTimeMillis()
val startTime = now - TimeUnit.DAYS.toMillis(DAYS_TO_CHECK)
return try {
usageStatsManager.queryEventsForPackageForUser(
startTime, now, app.userId, app.packageName, context.packageName
)
} catch (e: RemoteException) {
Log.e(TAG, "Failed IUsageStatsManager.queryEventsForPackageForUser(): ", e)
null
}
}
private fun getChannelCount(app: ApplicationInfo): Int = try {
notificationManager.getNumNotificationChannelsForPackage(app.packageName, app.uid, false)
} catch (e: Exception) {
Log.w(TAG, "Error calling INotificationManager", e)
0
}
private fun getBlockedChannelCount(app: ApplicationInfo): Int = try {
notificationManager.getBlockedChannelCount(app.packageName, app.uid)
} catch (e: Exception) {
Log.w(TAG, "Error calling INotificationManager", e)
0
}
fun calculateFrequencySummary(sentCount: Int): String {
val dailyFrequency = (sentCount.toFloat() / DAYS_TO_CHECK).roundToInt()
return if (dailyFrequency > 0) {