1 /* 2 * Copyright (C) 2023 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.safetycenter.ui 18 19 import android.content.Context 20 import android.content.Intent 21 import android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE 22 import android.util.AttributeSet 23 import android.view.View 24 import androidx.annotation.RequiresApi 25 import androidx.fragment.app.FragmentActivity 26 import androidx.preference.Preference 27 import androidx.preference.PreferenceViewHolder 28 import com.android.permissioncontroller.Constants.EXTRA_SESSION_ID 29 import com.android.permissioncontroller.R 30 import com.android.permissioncontroller.safetycenter.SafetyCenterConstants 31 import com.android.settingslib.widget.GroupSectionDividerMixin 32 33 /** A preference that displays the Security and Privacy brand name on a Safety Center subpage. */ 34 @RequiresApi(UPSIDE_DOWN_CAKE) 35 internal class SafetyBrandChipPreference(context: Context, attrs: AttributeSet) : 36 Preference(context, attrs), GroupSectionDividerMixin { 37 38 init { 39 setLayoutResource(R.layout.preference_brand_chip) 40 } 41 42 private var brandChipClickListener: View.OnClickListener? = null 43 onBindViewHoldernull44 override fun onBindViewHolder(holder: PreferenceViewHolder) { 45 super.onBindViewHolder(holder) 46 val brandChipButton = holder.findViewById(R.id.brand_chip)!! 47 brandChipButton.setOnClickListener(brandChipClickListener) 48 SafetyCenterTouchTarget.configureSize( 49 brandChipButton, 50 R.dimen.sc_icon_button_touch_target_size, 51 useWidthHeightFix = true, 52 ) 53 } 54 55 /** 56 * Sets the listener that handles clicks for the brand chip 57 * 58 * @param activity represents the parent activity of the fragment 59 * @param sessionId identifier for the current session 60 */ setupListenernull61 fun setupListener(activity: FragmentActivity, sessionId: Long) { 62 brandChipClickListener = View.OnClickListener { closeSubpage(activity, context, sessionId) } 63 } 64 65 companion object { 66 /** 67 * Closes the subpage and optionally opens up the homepage 68 * 69 * @param fragmentActivity represents the parent activity of the fragment 70 * @param fragmentContext represents the context associated with the fragment 71 * @param sessionId identifier for the current session 72 */ closeSubpagenull73 fun closeSubpage( 74 fragmentActivity: FragmentActivity, 75 fragmentContext: Context, 76 sessionId: Long, 77 ) { 78 val openedFromHomepage = 79 fragmentActivity 80 .getIntent() 81 .getBooleanExtra(SafetyCenterConstants.EXTRA_OPENED_FROM_HOMEPAGE, false) 82 if (!openedFromHomepage) { 83 val intent = Intent(Intent.ACTION_SAFETY_CENTER) 84 intent.putExtra(EXTRA_SESSION_ID, sessionId) 85 NavigationSource.SAFETY_CENTER.addToIntent(intent) 86 fragmentContext.startActivity(intent) 87 } 88 fragmentActivity.finish() 89 } 90 } 91 } 92