summaryrefslogtreecommitdiffstats
path: root/src/com/android/packageinstaller/permission/ui/handheld/SettingsWithLargeHeader.java
blob: a613b2af5f1e5f35e7410a6a7e7e8d0bc98ab987 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.packageinstaller.permission.ui.handheld;

import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.os.UserHandle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.android.packageinstaller.DeviceUtils;
import com.android.permissioncontroller.R;

/**
 * A class that contains a header.
 */
public abstract class SettingsWithLargeHeader extends PermissionsFrameFragment  {

    private View mHeader;
    protected Intent mInfoIntent;
    protected UserHandle mUserHandle;
    protected Drawable mIcon;
    protected CharSequence mLabel;
    protected boolean mSmallIcon;

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
            Bundle savedInstanceState) {
        ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState);

        if (!DeviceUtils.isTelevision(getContext())) {
            if (mHeader == null) {
                mHeader = inflater.inflate(R.layout.header_large, root, false);
                getPreferencesContainer().addView(mHeader, 0);
            } else if (mHeader.getVisibility() == View.VISIBLE) {
                ((ViewGroup) mHeader.getParent()).removeView(mHeader);
                getPreferencesContainer().addView(mHeader, 0);
                updateHeader(mHeader);
                mHeader.requireViewById(R.id.header_link).setVisibility(View.VISIBLE);
            }
        }

        return root;
    }

    /**
     * Set the icon and label to use in the header.
     *
     * @param icon the icon
     * @param label the label
     * @param infoIntent the intent to show on click
     * @param smallIcon whether the icon should be small
     */
    public void setHeader(@NonNull Drawable icon, @NonNull CharSequence label,
            Intent infoIntent, @Nullable UserHandle userHandle, boolean smallIcon) {
        mIcon = icon;
        mLabel = label;
        mInfoIntent = infoIntent;
        mUserHandle = userHandle;
        mSmallIcon = smallIcon;
        updateHeader(mHeader);
    }

    /**
     * Updates the header to use the correct icon and title.
     *
     * @param header the View that contains the components.
     */
    protected void updateHeader(@NonNull View header) {
        if (header != null) {
            header.setVisibility(View.VISIBLE);

            ImageView appIcon = header.requireViewById(R.id.entity_header_icon);
            appIcon.setImageDrawable(mIcon);
            if (mSmallIcon) {
                int size = getContext().getResources().getDimensionPixelSize(
                        R.dimen.permission_icon_header_size);
                appIcon.getLayoutParams().width = size;
                appIcon.getLayoutParams().height = size;
            }
            if (mInfoIntent != null) {
                appIcon.setOnClickListener(v -> getActivity().startActivityAsUser(mInfoIntent,
                        mUserHandle));
                appIcon.setContentDescription(mLabel);
            }

            TextView appName = header.requireViewById(R.id.entity_header_title);
            appName.setText(mLabel);

            header.requireViewById(R.id.entity_header_summary).setVisibility(View.GONE);
            header.requireViewById(R.id.entity_header_second_summary).setVisibility(View.GONE);
            header.requireViewById(R.id.header_link).setVisibility(View.GONE);
        }
    }

    /**
     * Hide the entire header.
     */
    public void hideHeader() {
        mHeader.setVisibility(View.GONE);
    }

    /**
     * Set the summary text in the header.
     *
     * @param summary the text to display
     * @param listener the click listener if the summary should be clickable
     */
    public void setSummary(@NonNull CharSequence summary, @Nullable View.OnClickListener listener) {
        TextView textView = mHeader.requireViewById(R.id.header_text);
        TextView linkView = mHeader.requireViewById(R.id.header_link);
        if (listener != null) {
            linkView.setOnClickListener(listener);
            linkView.setVisibility(View.VISIBLE);
            linkView.setText(summary);
            textView.setVisibility(View.GONE);
        } else {
            textView.setVisibility(View.VISIBLE);
            textView.setText(summary);
            linkView.setVisibility(View.GONE);
        }
    }
}