Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 306ac67b authored by Chaohui Wang's avatar Chaohui Wang Committed by Android (Google) Code Review
Browse files

Merge "Add AppListNotifications to Spa"

parents 3c0929a7 7d91106e
Loading
Loading
Loading
Loading
+17 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
@@ -235,6 +236,22 @@ public abstract class AppInfoBase extends SettingsPreferenceFragment
                .launch();
    }

    /** Starts app info fragment from SPA pages. */
    public static void startAppInfoFragment(Class<?> fragment, String title, ApplicationInfo app,
            Context context, int sourceMetricsCategory) {
        final Bundle args = new Bundle();
        args.putString(AppInfoBase.ARG_PACKAGE_NAME, app.packageName);
        args.putInt(AppInfoBase.ARG_PACKAGE_UID, app.uid);

        new SubSettingLauncher(context)
                .setDestination(fragment.getName())
                .setSourceMetricsCategory(sourceMetricsCategory)
                .setTitleText(title)
                .setArguments(args)
                .setUserHandle(UserHandle.getUserHandleForUid(app.uid))
                .launch();
    }

    public static class MyAlertDialogFragment extends InstrumentedDialogFragment {

        private static final String ARG_ID = "id";
+4 −0
Original line number Diff line number Diff line
@@ -18,6 +18,8 @@ package com.android.settings.spa

import com.android.settings.spa.app.InstallUnknownAppsListProvider
import com.android.settings.spa.home.HomePageProvider
import com.android.settings.spa.notification.AppListNotificationsPageProvider
import com.android.settings.spa.notification.NotificationMainPageProvider
import com.android.settingslib.spa.framework.common.SettingsPageProviderRepository
import com.android.settingslib.spaprivileged.template.app.TogglePermissionAppListTemplate

@@ -28,6 +30,8 @@ private val togglePermissionAppListTemplate = TogglePermissionAppListTemplate(
val settingsPageProviders = SettingsPageProviderRepository(
    allPagesList = listOf(
        HomePageProvider,
        NotificationMainPageProvider,
        AppListNotificationsPageProvider,
    ) + togglePermissionAppListTemplate.createPageProviders(),
    rootPages = listOf(HomePageProvider.name),
)
+2 −0
Original line number Diff line number Diff line
@@ -21,6 +21,7 @@ import androidx.compose.runtime.Composable
import androidx.compose.ui.res.stringResource
import com.android.settings.R
import com.android.settings.spa.app.InstallUnknownAppsListProvider
import com.android.settings.spa.notification.NotificationMainPageProvider
import com.android.settingslib.spa.framework.common.SettingsPageProvider
import com.android.settingslib.spa.widget.scaffold.HomeScaffold

@@ -37,5 +38,6 @@ object HomePageProvider : SettingsPageProvider {
private fun HomePage() {
    HomeScaffold(title = stringResource(R.string.settings_label)) {
        InstallUnknownAppsListProvider.EntryItem()
        NotificationMainPageProvider.EntryItem()
    }
}
+94 −0
Original line number Diff line number Diff line
/*
 * 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.notification

import android.app.settings.SettingsEnums
import android.content.Context
import android.content.pm.ApplicationInfo
import android.os.Bundle
import androidx.compose.runtime.Composable
import androidx.compose.runtime.livedata.observeAsState
import androidx.compose.runtime.produceState
import androidx.compose.ui.platform.LocalContext
import androidx.compose.ui.res.stringResource
import com.android.settings.R
import com.android.settings.applications.AppInfoBase
import com.android.settings.notification.app.AppNotificationSettings
import com.android.settingslib.spa.framework.common.SettingsPageProvider
import com.android.settingslib.spa.framework.compose.navigator
import com.android.settingslib.spa.framework.compose.rememberContext
import com.android.settingslib.spa.framework.compose.toState
import com.android.settingslib.spa.widget.preference.Preference
import com.android.settingslib.spa.widget.preference.PreferenceModel
import com.android.settingslib.spaprivileged.template.app.AppListItemModel
import com.android.settingslib.spaprivileged.template.app.AppListPage
import com.android.settingslib.spaprivileged.template.app.AppListSwitchItem

object AppListNotificationsPageProvider : SettingsPageProvider {
    override val name = "AppListNotifications"

    @Composable
    override fun Page(arguments: Bundle?) {
        AppListPage(
            title = stringResource(R.string.app_notifications_title),
            listModel = rememberContext(::AppNotificationsListModel),
        ) {
            AppNotificationsItem(it)
        }
    }

    @Composable
    fun EntryItem() {
        Preference(object : PreferenceModel {
            override val title = stringResource(R.string.app_notifications_title)
            override val summary = stringResource(R.string.app_notification_field_summary).toState()
            override val onClick = navigator(name)
        })
    }
}

@Composable
private fun AppNotificationsItem(
    itemModel: AppListItemModel<AppNotificationsRecord>,
) {
    val appNotificationsRepository = rememberContext(::AppNotificationRepository)
    val context = LocalContext.current
    AppListSwitchItem(
        itemModel = itemModel,
        onClick = {
            navigateToAppNotificationSettings(
                context = context,
                app = itemModel.record.app,
            )
        },
        checked = itemModel.record.controller.isEnabled.observeAsState(),
        changeable = produceState(initialValue = false) {
            value = appNotificationsRepository.isChangeable(itemModel.record.app)
        },
        onCheckedChange = itemModel.record.controller::setEnabled,
    )
}

private fun navigateToAppNotificationSettings(context: Context, app: ApplicationInfo) {
    AppInfoBase.startAppInfoFragment(
        AppNotificationSettings::class.java,
        context.getString(R.string.notifications_title),
        app,
        context,
        SettingsEnums.MANAGE_APPLICATIONS_NOTIFICATIONS,
    )
}
+50 −0
Original line number Diff line number Diff line
/*
 * 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.notification

import android.content.pm.ApplicationInfo
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData

class AppNotificationController(
    private val repository: AppNotificationRepository,
    private val app: ApplicationInfo,
) {
    val isEnabled: LiveData<Boolean>
        get() = _isEnabled

    fun getEnabled() = _isEnabled.get()

    fun setEnabled(enabled: Boolean) {
        if (repository.setEnabled(app, enabled)) {
            _isEnabled.postValue(enabled)
        }
    }

    private val _isEnabled = object : MutableLiveData<Boolean>() {
        override fun onActive() {
            postValue(repository.isEnabled(app))
        }

        override fun onInactive() {
        }

        fun get(): Boolean = value ?: repository.isEnabled(app).also {
            postValue(it)
        }
    }
}
Loading