1 /* 2 * Copyright (C) 2020 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.tv.twopanelsettings.slices; 18 19 20 import android.content.res.Resources; 21 import android.graphics.drawable.Icon; 22 import android.os.Bundle; 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.fragment.app.Fragment; 30 31 import com.android.tv.twopanelsettings.R; 32 33 /** 34 * Fragment to display informational image and description text for slice. 35 */ 36 public class InfoFragment extends Fragment { 37 public static final String EXTRA_INFO_HAS_STATUS = "extra_info_has_status"; 38 39 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)40 public View onCreateView( 41 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 42 View view = inflater.inflate(R.layout.info_fragment, container, false); 43 updateInfo(view); 44 return view; 45 } 46 47 /** Update the infos in InfoFragment **/ updateInfoFragment()48 public void updateInfoFragment() { 49 updateInfo(getView()); 50 } 51 updateInfo(View view)52 private void updateInfo(View view) { 53 Icon image = getArguments().getParcelable(SlicesConstants.EXTRA_PREFERENCE_INFO_IMAGE); 54 Icon titleIcon = getArguments().getParcelable( 55 SlicesConstants.EXTRA_PREFERENCE_INFO_TITLE_ICON); 56 String title = getArguments().getString(SlicesConstants.EXTRA_PREFERENCE_INFO_TEXT); 57 String summary = getArguments().getString(SlicesConstants.EXTRA_PREFERENCE_INFO_SUMMARY); 58 boolean hasStatus = getArguments().getBoolean(EXTRA_INFO_HAS_STATUS); 59 boolean status = getArguments().getBoolean(SlicesConstants.EXTRA_PREFERENCE_INFO_STATUS); 60 61 Resources res = getResources(); 62 ImageView infoImage = view.findViewById(R.id.info_image); 63 ImageView infoIcon = view.findViewById(R.id.info_title_icon); 64 TextView infoTitle = view.findViewById(R.id.info_title); 65 TextView infoStatus = view.findViewById(R.id.info_status); 66 TextView infoSummary = view.findViewById(R.id.info_summary); 67 68 if (image != null) { 69 infoImage.setImageDrawable(image.loadDrawable(getContext())); 70 } 71 if (titleIcon != null) { 72 infoIcon.setImageDrawable(titleIcon.loadDrawable(getContext())); 73 infoIcon.setVisibility(View.VISIBLE); 74 } else { 75 infoIcon.setVisibility(View.GONE); 76 } 77 if (title != null) { 78 infoTitle.setText(title); 79 infoTitle.setVisibility(View.VISIBLE); 80 if (hasStatus) { 81 infoStatus.setVisibility(View.VISIBLE); 82 if (infoIcon.getVisibility() == View.VISIBLE) { 83 infoTitle.setMaxWidth( 84 res.getDimensionPixelSize( 85 R.dimen.settings_info_fragment_title_text_max_width_short)); 86 } else { 87 infoTitle.setMaxWidth( 88 res.getDimensionPixelSize( 89 R.dimen.settings_info_fragment_title_text_max_width_long)); 90 } 91 if (status) { 92 infoStatus.setTextColor(getResources().getColor(R.color.info_status_on)); 93 infoStatus.setText(getString(R.string.info_status_on)); 94 } else { 95 infoStatus.setTextColor(getResources().getColor(R.color.info_status_off)); 96 infoStatus.setText(getString(R.string.info_status_off)); 97 } 98 } else { 99 infoStatus.setVisibility(View.GONE); 100 // Reset maxWidth to allow the TextView to utilize the full width of its container. 101 infoTitle.setMaxWidth(Integer.MAX_VALUE); 102 } 103 } else { 104 infoTitle.setVisibility(View.GONE); 105 infoStatus.setVisibility(View.GONE); 106 } 107 if (summary != null) { 108 infoSummary.setText(summary); 109 } 110 } 111 } 112