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 
32 /** A preference that displays the Security and Privacy brand name on a Safety Center subpage. */
33 @RequiresApi(UPSIDE_DOWN_CAKE)
34 internal class SafetyBrandChipPreference(context: Context, attrs: AttributeSet) :
35     Preference(context, attrs) {
36 
37     init {
38         setLayoutResource(R.layout.preference_brand_chip)
39     }
40 
41     private var brandChipClickListener: View.OnClickListener? = null
42 
onBindViewHoldernull43     override fun onBindViewHolder(holder: PreferenceViewHolder) {
44         super.onBindViewHolder(holder)
45         val brandChipButton = holder.findViewById(R.id.brand_chip)!!
46         brandChipButton.setOnClickListener(brandChipClickListener)
47     }
48 
49     /**
50      * Sets the listener that handles clicks for the brand chip
51      *
52      * @param activity represents the parent activity of the fragment
53      * @param sessionId identifier for the current session
54      */
setupListenernull55     fun setupListener(activity: FragmentActivity, sessionId: Long) {
56         brandChipClickListener = View.OnClickListener { closeSubpage(activity, context, sessionId) }
57     }
58 
59     companion object {
60         /**
61          * Closes the subpage and optionally opens up the homepage
62          *
63          * @param fragmentActivity represents the parent activity of the fragment
64          * @param fragmentContext represents the context associated with the fragment
65          * @param sessionId identifier for the current session
66          */
closeSubpagenull67         fun closeSubpage(
68             fragmentActivity: FragmentActivity,
69             fragmentContext: Context,
70             sessionId: Long
71         ) {
72             val openedFromHomepage =
73                 fragmentActivity
74                     .getIntent()
75                     .getBooleanExtra(SafetyCenterConstants.EXTRA_OPENED_FROM_HOMEPAGE, false)
76             if (!openedFromHomepage) {
77                 val intent = Intent(Intent.ACTION_SAFETY_CENTER)
78                 intent.putExtra(EXTRA_SESSION_ID, sessionId)
79                 NavigationSource.SAFETY_CENTER.addToIntent(intent)
80                 fragmentContext.startActivity(intent)
81             }
82             fragmentActivity.finish()
83         }
84     }
85 }
86