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 
17 package com.android.permissioncontroller.permission.ui.handheld
18 
19 import android.app.Application
20 import android.content.Context
21 import android.os.Bundle
22 import android.os.UserHandle
23 import android.view.MenuItem
24 import androidx.preference.Preference
25 import androidx.preference.PreferenceCategory
26 import com.android.permissioncontroller.R
27 import com.android.permissioncontroller.hibernation.isHibernationEnabled
28 import com.android.permissioncontroller.permission.ui.UnusedAppsFragment
29 import com.android.permissioncontroller.permission.ui.UnusedAppsFragment.Companion.INFO_MSG_CATEGORY
30 
31 /**
32  * Handheld wrapper, with customizations, around [UnusedAppsFragment].
33  */
34 class HandheldUnusedAppsFragment : PermissionsFrameFragment(),
35     UnusedAppsFragment.Parent<UnusedAppPreference> {
36 
37     companion object {
38         /** Create a new instance of this fragment.  */
39         @JvmStatic
40         fun newInstance(): HandheldUnusedAppsFragment {
41             return HandheldUnusedAppsFragment()
42         }
43     }
44 
45     override fun onCreate(savedInstanceState: Bundle?) {
46         super.onCreate(savedInstanceState)
47         setHasOptionsMenu(true)
48     }
49 
50     override fun onStart() {
51         super.onStart()
52         mUseShadowController = false
53     }
54 
55     override fun onActivityCreated(savedInstanceState: Bundle?) {
56         super.onActivityCreated(savedInstanceState)
57         if (savedInstanceState == null) {
58             val fragment:
59                 UnusedAppsFragment<HandheldUnusedAppsFragment, UnusedAppPreference> =
60                 UnusedAppsFragment.newInstance()
61             fragment.arguments = arguments
62             // child fragment does not have its own UI - it will add to the preferences of this
63             // parent fragment
64             childFragmentManager.beginTransaction()
65                 .add(fragment, null)
66                 .commit()
67         }
68     }
69 
70     override fun onOptionsItemSelected(item: MenuItem): Boolean {
71         if (item.itemId == android.R.id.home) {
72             this.pressBack()
73             return true
74         }
75         return super.onOptionsItemSelected(item)
76     }
77 
78     override fun getEmptyViewString(): Int {
79         return if (isHibernationEnabled()) R.string.no_unused_apps else super.getEmptyViewString()
80     }
81 
82     override fun createFooterPreference(context: Context): Preference {
83         var preference: Preference
84         if (isHibernationEnabled()) {
85             preference = Preference(context)
86             preference.summary = getString(R.string.unused_apps_page_summary)
87         } else {
88             preference = FooterPreference(context)
89 
90             preference.summary = getString(R.string.auto_revoked_apps_page_summary)
91             preference.secondSummary = getString(R.string.auto_revoke_open_app_message)
92         }
93         preference.setIcon(R.drawable.ic_info_outline)
94         preference.isSelectable = false
95         return preference
96     }
97 
98     override fun setLoadingState(loading: Boolean, animate: Boolean) {
99         setLoading(loading, animate)
100     }
101 
102     override fun createUnusedAppPref(
103         app: Application,
104         packageName: String,
105         user: UserHandle,
106         context: Context
107     ): UnusedAppPreference {
108         return UnusedAppPreference(app, packageName, user, context)
109     }
110 
111     override fun setTitle(title: CharSequence) {
112         requireActivity().setTitle(title)
113     }
114 
115     override fun setEmptyState(empty: Boolean) {
116         val infoMsgCategory =
117                 preferenceScreen.findPreference<PreferenceCategory>(INFO_MSG_CATEGORY)!!
118         infoMsgCategory.isVisible = !empty
119     }
120 }