1 /*
2  * Copyright (C) 2022 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.providers.media.photopicker.ui;
18 
19 import android.annotation.Nullable;
20 import android.content.Context;
21 import android.text.Html;
22 import android.util.AttributeSet;
23 import android.view.Gravity;
24 import android.view.LayoutInflater;
25 import android.view.View;
26 import android.widget.LinearLayout;
27 import android.widget.TextView;
28 
29 import com.android.providers.media.R;
30 import com.android.providers.media.photopicker.util.SafetyProtectionUtils;
31 
32 /**
33  * A custom view class for Safety Protection widget.
34  */
35 public class SafetyProtectionSectionView extends LinearLayout {
SafetyProtectionSectionView(Context context)36     public SafetyProtectionSectionView(Context context) {
37         super(context);
38         init(context);
39     }
40 
SafetyProtectionSectionView(Context context, @Nullable AttributeSet attrs)41     public SafetyProtectionSectionView(Context context, @Nullable AttributeSet attrs) {
42         super(context, attrs);
43         init(context);
44     }
45 
SafetyProtectionSectionView(Context context, @Nullable AttributeSet attrs, int defStyleAttr)46     public SafetyProtectionSectionView(Context context, @Nullable AttributeSet attrs,
47             int defStyleAttr) {
48         super(context, attrs, defStyleAttr);
49         init(context);
50     }
51 
SafetyProtectionSectionView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)52     public SafetyProtectionSectionView(Context context, AttributeSet attrs,
53             int defStyleAttr, int defStyleRes) {
54         super(context, attrs, defStyleAttr, defStyleRes);
55         init(context);
56     }
57 
init(Context context)58     private void init(Context context) {
59         setGravity(Gravity.CENTER);
60         setOrientation(HORIZONTAL);
61         int visibility = SafetyProtectionUtils.shouldShowSafetyProtectionResources(context)
62                 ? View.VISIBLE : View.GONE;
63         setVisibility(visibility);
64     }
65 
66     @Override
onFinishInflate()67     protected void onFinishInflate() {
68         super.onFinishInflate();
69         Context context = getContext();
70         if (SafetyProtectionUtils.shouldShowSafetyProtectionResources(context)) {
71             LayoutInflater.from(context).inflate(R.layout.safety_protection_section, this);
72             TextView safetyProtectionDisplayTextView =
73                     requireViewById(R.id.safety_protection_display_text);
74             safetyProtectionDisplayTextView.setText(Html.fromHtml(
75                     context.getString(android.R.string.safety_protection_display_text), 0));
76         }
77     }
78 }
79