• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2015 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.packageinstaller.permission.ui.handheld;
18 
19 import android.content.Intent;
20 import android.graphics.drawable.Drawable;
21 import android.os.Bundle;
22 import android.os.UserHandle;
23 import android.view.LayoutInflater;
24 import android.view.View;
25 import android.view.ViewGroup;
26 import android.widget.ImageView;
27 import android.widget.TextView;
28 
29 import androidx.annotation.NonNull;
30 import androidx.annotation.Nullable;
31 
32 import com.android.packageinstaller.DeviceUtils;
33 import com.android.permissioncontroller.R;
34 
35 /**
36  * A class that contains a header.
37  */
38 public abstract class SettingsWithLargeHeader extends PermissionsFrameFragment  {
39 
40     private View mHeader;
41     protected Intent mInfoIntent;
42     protected UserHandle mUserHandle;
43     protected Drawable mIcon;
44     protected CharSequence mLabel;
45     protected boolean mSmallIcon;
46 
47     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)48     public View onCreateView(LayoutInflater inflater, ViewGroup container,
49             Bundle savedInstanceState) {
50         ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState);
51 
52         if (!DeviceUtils.isTelevision(getContext())) {
53             if (mHeader == null) {
54                 mHeader = inflater.inflate(R.layout.header_large, root, false);
55                 getPreferencesContainer().addView(mHeader, 0);
56             } else if (mHeader.getVisibility() == View.VISIBLE) {
57                 ((ViewGroup) mHeader.getParent()).removeView(mHeader);
58                 getPreferencesContainer().addView(mHeader, 0);
59                 updateHeader(mHeader);
60                 mHeader.requireViewById(R.id.header_link).setVisibility(View.VISIBLE);
61             }
62         }
63 
64         return root;
65     }
66 
67     /**
68      * Set the icon and label to use in the header.
69      *
70      * @param icon the icon
71      * @param label the label
72      * @param infoIntent the intent to show on click
73      * @param smallIcon whether the icon should be small
74      */
setHeader(@onNull Drawable icon, @NonNull CharSequence label, Intent infoIntent, @Nullable UserHandle userHandle, boolean smallIcon)75     public void setHeader(@NonNull Drawable icon, @NonNull CharSequence label,
76             Intent infoIntent, @Nullable UserHandle userHandle, boolean smallIcon) {
77         mIcon = icon;
78         mLabel = label;
79         mInfoIntent = infoIntent;
80         mUserHandle = userHandle;
81         mSmallIcon = smallIcon;
82         updateHeader(mHeader);
83     }
84 
85     /**
86      * Updates the header to use the correct icon and title.
87      *
88      * @param header the View that contains the components.
89      */
updateHeader(@onNull View header)90     protected void updateHeader(@NonNull View header) {
91         if (header != null) {
92             header.setVisibility(View.VISIBLE);
93 
94             ImageView appIcon = header.requireViewById(R.id.entity_header_icon);
95             appIcon.setImageDrawable(mIcon);
96             if (mSmallIcon) {
97                 int size = getContext().getResources().getDimensionPixelSize(
98                         R.dimen.permission_icon_header_size);
99                 appIcon.getLayoutParams().width = size;
100                 appIcon.getLayoutParams().height = size;
101             }
102             if (mInfoIntent != null) {
103                 appIcon.setOnClickListener(v -> getActivity().startActivityAsUser(mInfoIntent,
104                         mUserHandle));
105                 appIcon.setContentDescription(mLabel);
106             }
107 
108             TextView appName = header.requireViewById(R.id.entity_header_title);
109             appName.setText(mLabel);
110 
111             header.requireViewById(R.id.entity_header_summary).setVisibility(View.GONE);
112             header.requireViewById(R.id.entity_header_second_summary).setVisibility(View.GONE);
113             header.requireViewById(R.id.header_link).setVisibility(View.GONE);
114         }
115     }
116 
117     /**
118      * Hide the entire header.
119      */
hideHeader()120     public void hideHeader() {
121         mHeader.setVisibility(View.GONE);
122     }
123 
124     /**
125      * Set the summary text in the header.
126      *
127      * @param summary the text to display
128      * @param listener the click listener if the summary should be clickable
129      */
setSummary(@onNull CharSequence summary, @Nullable View.OnClickListener listener)130     public void setSummary(@NonNull CharSequence summary, @Nullable View.OnClickListener listener) {
131         TextView textView = mHeader.requireViewById(R.id.header_text);
132         TextView linkView = mHeader.requireViewById(R.id.header_link);
133         if (listener != null) {
134             linkView.setOnClickListener(listener);
135             linkView.setVisibility(View.VISIBLE);
136             linkView.setText(summary);
137             textView.setVisibility(View.GONE);
138         } else {
139             textView.setVisibility(View.VISIBLE);
140             textView.setText(summary);
141             linkView.setVisibility(View.GONE);
142         }
143     }
144 }
145