1 /*
2  * Copyright (C) 2022 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 @file:Suppress("DEPRECATION")
17 
18 package com.android.permissioncontroller.permission.ui.television
19 
20 import android.app.Application
21 import android.os.Bundle
22 import android.os.UserHandle
23 import androidx.preference.Preference
24 import androidx.preference.PreferenceCategory
25 import com.android.permissioncontroller.R
26 import com.android.permissioncontroller.hibernation.isHibernationEnabled
27 import com.android.permissioncontroller.permission.ui.UnusedAppsFragment
28 import com.android.permissioncontroller.permission.ui.UnusedAppsFragment.Companion.INFO_MSG_CATEGORY
29 
30 /** TV wrapper, with customizations, around [UnusedAppsFragment]. */
31 class TvUnusedAppsFragment :
32     SettingsWithHeader(), UnusedAppsFragment.Parent<TvUnusedAppsPreference> {
33 
34     companion object {
35         private const val UNUSED_PREFERENCE_KEY = "unused_pref_row_key"
36 
37         /** Create a new instance of this fragment. */
38         @JvmStatic
newInstancenull39         fun newInstance(): TvUnusedAppsFragment {
40             return TvUnusedAppsFragment()
41         }
42     }
43 
onCreatePreferencesnull44     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
45         // Preferences will be added via shared logic in [UnusedAppsFragment].
46     }
47 
onActivityCreatednull48     override fun onActivityCreated(savedInstanceState: Bundle?) {
49         super.onActivityCreated(savedInstanceState)
50         if (savedInstanceState == null) {
51             val fragment: UnusedAppsFragment<TvUnusedAppsFragment, TvUnusedAppsPreference> =
52                 UnusedAppsFragment.newInstance()
53             fragment.arguments = arguments
54             // child fragment does not have its own UI - it will add to the preferences of this
55             // parent fragment
56             childFragmentManager.beginTransaction().add(fragment, null).commit()
57         }
58     }
59 
createFooterPreferencenull60     override fun createFooterPreference(): Preference {
61         val preference = com.android.settingslib.widget.FooterPreference(requireContext())
62         if (isHibernationEnabled()) {
63             preference.summary = getString(R.string.unused_apps_page_tv_summary)
64         } else {
65             preference.summary = getString(R.string.auto_revoked_apps_page_summary)
66         }
67         preference.setIcon(R.drawable.ic_info_outline)
68         preference.isSelectable = false
69         return preference
70     }
71 
setLoadingStatenull72     override fun setLoadingState(loading: Boolean, animate: Boolean) {
73         setLoading(loading, animate)
74     }
75 
createUnusedAppPrefnull76     override fun createUnusedAppPref(
77         app: Application,
78         packageName: String,
79         user: UserHandle
80     ): TvUnusedAppsPreference {
81         return TvUnusedAppsPreference(app, packageName, user, requireContext())
82     }
83 
setTitlenull84     override fun setTitle(title: CharSequence) {
85         setHeader(null, null, null, title)
86     }
87 
setEmptyStatenull88     override fun setEmptyState(empty: Boolean) {
89         val infoMsgCategory =
90             preferenceScreen.findPreference<PreferenceCategory>(INFO_MSG_CATEGORY)!!
91         val noUnusedAppsPreference: Preference? =
92             infoMsgCategory.findPreference<Preference>(UNUSED_PREFERENCE_KEY)
93         if (empty && noUnusedAppsPreference == null) {
94             infoMsgCategory.addPreference(createNoUnusedAppsPreference())
95         } else if (noUnusedAppsPreference != null) {
96             noUnusedAppsPreference.setVisible(empty)
97         }
98     }
99 
createNoUnusedAppsPreferencenull100     private fun createNoUnusedAppsPreference(): Preference {
101         val preference = Preference(requireContext())
102         preference.title = getString(R.string.zero_unused_apps)
103         preference.key = UNUSED_PREFERENCE_KEY
104         preference.isSelectable = false
105         preference.order = 0
106         return preference
107     }
108 }
109