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.permissioncontroller.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.Nullable; 30 31 import com.android.permissioncontroller.DeviceUtils; 32 import com.android.permissioncontroller.R; 33 34 public abstract class SettingsWithHeader extends PermissionsFrameFragment { 35 36 private View mHeader; 37 protected Intent mInfoIntent; 38 protected Drawable mIcon; 39 protected CharSequence mLabel; 40 protected UserHandle mUserHandle; 41 42 @Override onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)43 public View onCreateView(LayoutInflater inflater, ViewGroup container, 44 Bundle savedInstanceState) { 45 ViewGroup root = (ViewGroup) super.onCreateView(inflater, container, savedInstanceState); 46 47 if (!DeviceUtils.isTelevision(getContext())) { 48 mHeader = inflater.inflate(R.layout.header, root, false); 49 getPreferencesContainer().addView(mHeader, 0); 50 updateHeader(); 51 } 52 53 return root; 54 } 55 setHeader(Drawable icon, CharSequence label, Intent infoIntent, @Nullable UserHandle userHandle)56 public void setHeader(Drawable icon, CharSequence label, Intent infoIntent, 57 @Nullable UserHandle userHandle) { 58 mIcon = icon; 59 mLabel = label; 60 mInfoIntent = infoIntent; 61 mUserHandle = userHandle; 62 updateHeader(); 63 } 64 updateHeader()65 private void updateHeader() { 66 if (mHeader != null) { 67 final ImageView appIcon = (ImageView) mHeader.findViewById(R.id.icon); 68 appIcon.setImageDrawable(mIcon); 69 70 final TextView appName = (TextView) mHeader.findViewById(R.id.name); 71 appName.setText(mLabel); 72 73 final View info = mHeader.findViewById(R.id.info); 74 if (mInfoIntent == null) { 75 info.setVisibility(View.GONE); 76 } else { 77 info.setVisibility(View.VISIBLE); 78 info.setClickable(true); 79 info.setOnClickListener(v -> getActivity().startActivityAsUser(mInfoIntent, 80 mUserHandle)); 81 } 82 } 83 } 84 } 85