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     }
49 
50     /**
51      * Sets the listener that handles clicks for the brand chip
52      *
53      * @param activity represents the parent activity of the fragment
54      * @param sessionId identifier for the current session
55      */
setupListenernull56     fun setupListener(activity: FragmentActivity, sessionId: Long) {
57         brandChipClickListener = View.OnClickListener { closeSubpage(activity, context, sessionId) }
58     }
59 
60     companion object {
61         /**
62          * Closes the subpage and optionally opens up the homepage
63          *
64          * @param fragmentActivity represents the parent activity of the fragment
65          * @param fragmentContext represents the context associated with the fragment
66          * @param sessionId identifier for the current session
67          */
closeSubpagenull68         fun closeSubpage(
69             fragmentActivity: FragmentActivity,
70             fragmentContext: Context,
71             sessionId: Long,
72         ) {
73             val openedFromHomepage =
74                 fragmentActivity
75                     .getIntent()
76                     .getBooleanExtra(SafetyCenterConstants.EXTRA_OPENED_FROM_HOMEPAGE, false)
77             if (!openedFromHomepage) {
78                 val intent = Intent(Intent.ACTION_SAFETY_CENTER)
79                 intent.putExtra(EXTRA_SESSION_ID, sessionId)
80                 NavigationSource.SAFETY_CENTER.addToIntent(intent)
81                 fragmentContext.startActivity(intent)
82             }
83             fragmentActivity.finish()
84         }
85     }
86 }
87