summaryrefslogtreecommitdiff
path: root/PermissionController/src/com/android/permissioncontroller/permission/ui/auto/AutoUnusedAppsFragment.kt
blob: d798291e0727909aebb17a321aa404f8dbdf2dbd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
/*
 * Copyright (C) 2021 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.
 */
@file:Suppress("DEPRECATION")

package com.android.permissioncontroller.permission.ui.auto

import android.app.Application
import android.os.Bundle
import android.os.UserHandle
import androidx.preference.Preference
import androidx.preference.PreferenceCategory
import com.android.car.ui.utils.ViewUtils
import com.android.car.ui.utils.ViewUtils.LazyLayoutView
import com.android.permissioncontroller.R
import com.android.permissioncontroller.auto.AutoSettingsFrameFragment
import com.android.permissioncontroller.hibernation.isHibernationEnabled
import com.android.permissioncontroller.permission.ui.UnusedAppsFragment
import com.android.permissioncontroller.permission.ui.UnusedAppsFragment.Companion.INFO_MSG_CATEGORY

/** Auto wrapper, with customizations, around [UnusedAppsFragment]. */
class AutoUnusedAppsFragment :
    AutoSettingsFrameFragment(), UnusedAppsFragment.Parent<AutoUnusedAppsPreference> {

    companion object {
        private const val UNUSED_PREFERENCE_KEY = "unused_pref_row_key"

        /** Create a new instance of this fragment. */
        @JvmStatic
        fun newInstance(): AutoUnusedAppsFragment {
            return AutoUnusedAppsFragment()
        }
    }

    override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
        // Preferences will be added via shared logic in [UnusedAppsFragment].
    }

    override fun onActivityCreated(savedInstanceState: Bundle?) {
        super.onActivityCreated(savedInstanceState)
        if (savedInstanceState == null) {
            val fragment: UnusedAppsFragment<AutoUnusedAppsFragment, AutoUnusedAppsPreference> =
                UnusedAppsFragment.newInstance()
            fragment.arguments = arguments
            // child fragment does not have its own UI - it will add to the preferences of this
            // parent fragment
            childFragmentManager.beginTransaction().add(fragment, null).commit()
        }

        // initially focus on focus parking view and then shift focus to recyclerview once it has
        // loaded
        ViewUtils.hideFocus(getCarUiRecyclerView().getView().getRootView())
        val lazyLayoutView = getCarUiRecyclerView() as LazyLayoutView
        ViewUtils.initFocus(lazyLayoutView)
    }

    override fun createFooterPreference(): Preference {
        val preference = Preference(context!!)
        if (isHibernationEnabled()) {
            preference.summary = getString(R.string.unused_apps_page_summary)
        } else {
            preference.summary =
                """
            ${getString(R.string.auto_revoked_apps_page_summary)}
            ${getString(R.string.auto_revoke_open_app_message)}
            """
                    .trimIndent()
        }
        preference.setIcon(R.drawable.ic_info_outline)
        preference.isSelectable = false
        return preference
    }

    override fun setLoadingState(loading: Boolean, animate: Boolean) {
        setLoading(false)
    }

    override fun createUnusedAppPref(
        app: Application,
        packageName: String,
        user: UserHandle
    ): AutoUnusedAppsPreference {
        return AutoUnusedAppsPreference(app, packageName, user, requireContext())
    }

    override fun setTitle(title: CharSequence) {
        headerLabel = title
    }

    override fun setEmptyState(empty: Boolean) {
        val infoMsgCategory =
            preferenceScreen.findPreference<PreferenceCategory>(INFO_MSG_CATEGORY)!!
        val noUnusedAppsPreference: Preference? =
            infoMsgCategory.findPreference<Preference>(UNUSED_PREFERENCE_KEY)
        if (empty && noUnusedAppsPreference == null) {
            infoMsgCategory.addPreference(createNoUnusedAppsPreference())
        } else if (noUnusedAppsPreference != null) {
            noUnusedAppsPreference.setVisible(empty)
        }
    }

    private fun createNoUnusedAppsPreference(): Preference {
        val preference = Preference(context!!)
        preference.title = getString(R.string.zero_unused_apps)
        preference.key = UNUSED_PREFERENCE_KEY
        preference.isSelectable = false
        preference.order = 0
        return preference
    }
}