1 /*
2  * Copyright (C) 2021 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.auto
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.car.ui.utils.ViewUtils
26 import com.android.car.ui.utils.ViewUtils.LazyLayoutView
27 import com.android.permissioncontroller.R
28 import com.android.permissioncontroller.auto.AutoSettingsFrameFragment
29 import com.android.permissioncontroller.hibernation.isHibernationEnabled
30 import com.android.permissioncontroller.permission.ui.UnusedAppsFragment
31 import com.android.permissioncontroller.permission.ui.UnusedAppsFragment.Companion.INFO_MSG_CATEGORY
32 
33 /** Auto wrapper, with customizations, around [UnusedAppsFragment]. */
34 class AutoUnusedAppsFragment :
35     AutoSettingsFrameFragment(), UnusedAppsFragment.Parent<AutoUnusedAppsPreference> {
36 
37     companion object {
38         private const val UNUSED_PREFERENCE_KEY = "unused_pref_row_key"
39 
40         /** Create a new instance of this fragment. */
41         @JvmStatic
newInstancenull42         fun newInstance(): AutoUnusedAppsFragment {
43             return AutoUnusedAppsFragment()
44         }
45     }
46 
onCreatePreferencesnull47     override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
48         // Preferences will be added via shared logic in [UnusedAppsFragment].
49     }
50 
onActivityCreatednull51     override fun onActivityCreated(savedInstanceState: Bundle?) {
52         super.onActivityCreated(savedInstanceState)
53         if (savedInstanceState == null) {
54             val fragment: UnusedAppsFragment<AutoUnusedAppsFragment, AutoUnusedAppsPreference> =
55                 UnusedAppsFragment.newInstance()
56             fragment.arguments = arguments
57             // child fragment does not have its own UI - it will add to the preferences of this
58             // parent fragment
59             childFragmentManager.beginTransaction().add(fragment, null).commit()
60         }
61 
62         // initially focus on focus parking view and then shift focus to recyclerview once it has
63         // loaded
64         ViewUtils.hideFocus(getCarUiRecyclerView().getView().getRootView())
65         val lazyLayoutView = getCarUiRecyclerView() as LazyLayoutView
66         ViewUtils.initFocus(lazyLayoutView)
67     }
68 
createFooterPreferencenull69     override fun createFooterPreference(): Preference {
70         val preference = Preference(context!!)
71         if (isHibernationEnabled()) {
72             preference.summary = getString(R.string.unused_apps_page_summary)
73         } else {
74             preference.summary =
75                 """
76             ${getString(R.string.auto_revoked_apps_page_summary)}
77             ${getString(R.string.auto_revoke_open_app_message)}
78             """
79                     .trimIndent()
80         }
81         preference.setIcon(R.drawable.ic_info_outline)
82         preference.isSelectable = false
83         return preference
84     }
85 
setLoadingStatenull86     override fun setLoadingState(loading: Boolean, animate: Boolean) {
87         setLoading(false)
88     }
89 
createUnusedAppPrefnull90     override fun createUnusedAppPref(
91         app: Application,
92         packageName: String,
93         user: UserHandle
94     ): AutoUnusedAppsPreference {
95         return AutoUnusedAppsPreference(app, packageName, user, requireContext())
96     }
97 
setTitlenull98     override fun setTitle(title: CharSequence) {
99         headerLabel = title
100     }
101 
setEmptyStatenull102     override fun setEmptyState(empty: Boolean) {
103         val infoMsgCategory =
104             preferenceScreen.findPreference<PreferenceCategory>(INFO_MSG_CATEGORY)!!
105         val noUnusedAppsPreference: Preference? =
106             infoMsgCategory.findPreference<Preference>(UNUSED_PREFERENCE_KEY)
107         if (empty && noUnusedAppsPreference == null) {
108             infoMsgCategory.addPreference(createNoUnusedAppsPreference())
109         } else if (noUnusedAppsPreference != null) {
110             noUnusedAppsPreference.setVisible(empty)
111         }
112     }
113 
createNoUnusedAppsPreferencenull114     private fun createNoUnusedAppsPreference(): Preference {
115         val preference = Preference(context!!)
116         preference.title = getString(R.string.zero_unused_apps)
117         preference.key = UNUSED_PREFERENCE_KEY
118         preference.isSelectable = false
119         preference.order = 0
120         return preference
121     }
122 }
123