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.handheld 19 20 import android.app.Application 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 newInstancenull40 fun newInstance(): HandheldUnusedAppsFragment { 41 return HandheldUnusedAppsFragment() 42 } 43 } 44 onCreatenull45 override fun onCreate(savedInstanceState: Bundle?) { 46 super.onCreate(savedInstanceState) 47 setHasOptionsMenu(true) 48 } 49 onStartnull50 override fun onStart() { 51 super.onStart() 52 mUseShadowController = false 53 } 54 onActivityCreatednull55 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 onOptionsItemSelectednull70 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 getEmptyViewStringnull78 override fun getEmptyViewString(): Int { 79 return if (isHibernationEnabled()) R.string.no_unused_apps else super.getEmptyViewString() 80 } 81 createFooterPreferencenull82 override fun createFooterPreference(): Preference { 83 var preference: Preference 84 if (isHibernationEnabled()) { 85 preference = com.android.settingslib.widget.FooterPreference(requireContext()) 86 preference.summary = getString(R.string.unused_apps_page_summary) 87 } else { 88 preference = FooterPreference(requireContext()) 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 setLoadingStatenull98 override fun setLoadingState(loading: Boolean, animate: Boolean) { 99 setLoading(loading, animate) 100 } 101 createUnusedAppPrefnull102 override fun createUnusedAppPref( 103 app: Application, 104 packageName: String, 105 user: UserHandle 106 ): UnusedAppPreference { 107 return UnusedAppPreference(app, packageName, user, requireContext()) 108 } 109 setTitlenull110 override fun setTitle(title: CharSequence) { 111 requireActivity().setTitle(title) 112 } 113 setEmptyStatenull114 override fun setEmptyState(empty: Boolean) { 115 val infoMsgCategory = 116 preferenceScreen.findPreference<PreferenceCategory>(INFO_MSG_CATEGORY)!! 117 infoMsgCategory.isVisible = !empty 118 } 119 }