1 /* 2 * Copyright (C) 2021 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.settings.device.displaysound; 18 19 import android.os.Bundle; 20 import android.util.TypedValue; 21 import android.view.LayoutInflater; 22 import android.view.View; 23 import android.view.View.OnLayoutChangeListener; 24 import android.view.ViewGroup; 25 import android.widget.TextView; 26 27 import androidx.annotation.Keep; 28 29 import com.android.tv.twopanelsettings.R; 30 import com.android.tv.twopanelsettings.slices.InfoFragment; 31 32 /** 33 * The preview Fragment for "Text scaling" screen in TV Settings showing sample text. 34 */ 35 @Keep 36 public class FontScalePreviewFragment extends InfoFragment implements OnLayoutChangeListener { 37 38 private static final String ELLIPSIS = "..."; 39 40 /** Argument key containing the current font scale value. */ 41 public static final String CURRENT_FONT_SCALE_VALUE = "current_font_scale_value"; 42 43 /** Argument key containing the font scale value this fragment will preview. */ 44 public static final String PREVIEW_FONT_SCALE_VALUE = "preview_font_scale_value"; 45 46 @Override onCreateView( LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)47 public View onCreateView( 48 LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) { 49 50 View view = inflater.inflate(R.layout.font_scale_preview, container, false); 51 52 // Use the current font scale and the font scale assigned to this preview to calculate 53 // the adjusted text size for each TextView. Uses px because sp would be scaled up 54 // according to the system FONT_SCALE value. 55 float preview_font_scale = Float.parseFloat( 56 getArguments().getString(PREVIEW_FONT_SCALE_VALUE)); 57 float current_font_scale = getArguments().getFloat(CURRENT_FONT_SCALE_VALUE); 58 59 TextView title = ((TextView) view.findViewById(R.id.preview_title)); 60 float adjustedTextSizeTitle = 61 (title.getTextSize() / current_font_scale) * preview_font_scale; 62 title.setTextSize(TypedValue.COMPLEX_UNIT_PX, adjustedTextSizeTitle); 63 64 TextView subtitle = ((TextView) view.findViewById(R.id.preview_subtitle)); 65 float adjustedTextSizeSubtitle = 66 (subtitle.getTextSize() / current_font_scale) * preview_font_scale; 67 subtitle.setTextSize(TypedValue.COMPLEX_UNIT_PX, adjustedTextSizeSubtitle); 68 69 TextView sampleContent = ((TextView) view.findViewById(R.id.preview_content)); 70 float adjustedTextSizeContent = 71 (sampleContent.getTextSize() / current_font_scale) * preview_font_scale; 72 sampleContent.setTextSize(TypedValue.COMPLEX_UNIT_PX, adjustedTextSizeContent); 73 sampleContent.addOnLayoutChangeListener(this); 74 75 return view; 76 } 77 78 @Override onLayoutChange(View v, int left, int top, int right, int bottom, int oldLeft, int oldTop, int oldRight, int oldBottom)79 public void onLayoutChange(View v, int left, int top, int right, int bottom, 80 int oldLeft, int oldTop, int oldRight, int oldBottom) { 81 82 TextView sampleText = (TextView) v; 83 int sampleTextHeight = sampleText.getHeight(); 84 if (sampleTextHeight > 0) { 85 // Set the max amount of lines to prevent horizontal cut on the text 86 int lines = sampleTextHeight / sampleText.getLineHeight(); 87 sampleText.setMaxLines(lines); 88 89 // Ellipsize after the max line change 90 int lineEndIndex = sampleText.getLayout().getLineEnd(lines - 1); 91 if (lineEndIndex >= ELLIPSIS.length()) { 92 String ellipsizedText = 93 sampleText.getText().subSequence(0, lineEndIndex - ELLIPSIS.length()) 94 + ELLIPSIS; 95 sampleText.setText(ellipsizedText); 96 } 97 } 98 } 99 } 100