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 /** Handheld wrapper, with customizations, around [UnusedAppsFragment]. */ 32 class HandheldUnusedAppsFragment : 33 PermissionsFrameFragment(), UnusedAppsFragment.Parent<UnusedAppPreference> { 34 35 companion object { 36 /** Create a new instance of this fragment. */ 37 @JvmStatic newInstancenull38 fun newInstance(): HandheldUnusedAppsFragment { 39 return HandheldUnusedAppsFragment() 40 } 41 } 42 onCreatenull43 override fun onCreate(savedInstanceState: Bundle?) { 44 super.onCreate(savedInstanceState) 45 setHasOptionsMenu(true) 46 } 47 onStartnull48 override fun onStart() { 49 super.onStart() 50 mUseShadowController = false 51 } 52 onActivityCreatednull53 override fun onActivityCreated(savedInstanceState: Bundle?) { 54 super.onActivityCreated(savedInstanceState) 55 if (savedInstanceState == null) { 56 val fragment: UnusedAppsFragment<HandheldUnusedAppsFragment, UnusedAppPreference> = 57 UnusedAppsFragment.newInstance() 58 fragment.arguments = arguments 59 // child fragment does not have its own UI - it will add to the preferences of this 60 // parent fragment 61 childFragmentManager.beginTransaction().add(fragment, null).commit() 62 } 63 } 64 onOptionsItemSelectednull65 override fun onOptionsItemSelected(item: MenuItem): Boolean { 66 if (item.itemId == android.R.id.home) { 67 this.pressBack() 68 return true 69 } 70 return super.onOptionsItemSelected(item) 71 } 72 getEmptyViewStringnull73 override fun getEmptyViewString(): Int { 74 return if (isHibernationEnabled()) R.string.no_unused_apps else super.getEmptyViewString() 75 } 76 createFooterPreferencenull77 override fun createFooterPreference(): Preference { 78 var preference: Preference 79 if (isHibernationEnabled()) { 80 preference = com.android.settingslib.widget.FooterPreference(requireContext()) 81 preference.summary = getString(R.string.unused_apps_page_summary) 82 } else { 83 preference = FooterPreference(requireContext()) 84 85 preference.summary = getString(R.string.auto_revoked_apps_page_summary) 86 preference.secondSummary = getString(R.string.auto_revoke_open_app_message) 87 } 88 preference.setIcon(R.drawable.ic_info_outline) 89 preference.isSelectable = false 90 return preference 91 } 92 setLoadingStatenull93 override fun setLoadingState(loading: Boolean, animate: Boolean) { 94 setLoading(loading, animate) 95 } 96 createUnusedAppPrefnull97 override fun createUnusedAppPref( 98 app: Application, 99 packageName: String, 100 user: UserHandle 101 ): UnusedAppPreference { 102 return UnusedAppPreference(app, packageName, user, requireContext()) 103 } 104 setTitlenull105 override fun setTitle(title: CharSequence) { 106 requireActivity().setTitle(title) 107 } 108 setEmptyStatenull109 override fun setEmptyState(empty: Boolean) { 110 val infoMsgCategory = 111 preferenceScreen.findPreference<PreferenceCategory>(INFO_MSG_CATEGORY)!! 112 infoMsgCategory.isVisible = !empty 113 } 114 } 115