• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright 2016, 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.managedprovisioning.common;
18 
19 import android.annotation.Nullable;
20 import android.content.res.Resources;
21 import android.os.Bundle;
22 import android.text.Editable;
23 import android.text.Layout;
24 import android.text.TextWatcher;
25 import android.widget.TextView;
26 
27 import androidx.annotation.VisibleForTesting;
28 
29 import com.android.managedprovisioning.R;
30 
31 import com.google.android.setupdesign.GlifLayout;
32 
33 
34 /**
35  * Base class for setting up the layout.
36  */
37 public abstract class SetupGlifLayoutActivity extends SetupLayoutActivity {
38 
39     private int mInitialHeaderMaxLines;
40 
SetupGlifLayoutActivity()41     public SetupGlifLayoutActivity() {
42         super();
43     }
44 
45     @VisibleForTesting
SetupGlifLayoutActivity( Utils utils, SettingsFacade settingsFacade, ThemeHelper themeHelper)46     protected SetupGlifLayoutActivity(
47             Utils utils, SettingsFacade settingsFacade, ThemeHelper themeHelper) {
48         super(utils, settingsFacade, themeHelper);
49     }
50 
51     @Override
onCreate(Bundle savedInstanceState)52     protected void onCreate(Bundle savedInstanceState) {
53         super.onCreate(savedInstanceState);
54     }
55 
56     @Override
onApplyThemeResource(Resources.Theme theme, int resid, boolean first)57     protected void onApplyThemeResource(Resources.Theme theme, int resid, boolean first) {
58         theme.applyStyle(R.style.SetupWizardPartnerResource, true);
59         super.onApplyThemeResource(theme, resid, first);
60     }
61 
initializeLayoutParams( int layoutResourceId, @Nullable Integer headerResourceId)62     protected void initializeLayoutParams(
63             int layoutResourceId, @Nullable Integer headerResourceId) {
64         setContentView(layoutResourceId);
65         GlifLayout layout = findViewById(R.id.setup_wizard_layout);
66 
67         if (headerResourceId != null) {
68             layout.setHeaderText(headerResourceId);
69         }
70 
71         TextView header = findViewById(R.id.suc_layout_title);
72         if (header != null) {
73             mInitialHeaderMaxLines = header.getMaxLines();
74             header.addTextChangedListener(new TextWatcher() {
75                 @Override
76                 public void beforeTextChanged(CharSequence s, int start, int count, int after) {
77                 }
78 
79                 @Override
80                 public void onTextChanged(CharSequence s, int start, int before, int count) {
81                 }
82 
83                 @Override
84                 public void afterTextChanged(Editable s) {
85                     increaseMaxLinesIfNecessary(header, mInitialHeaderMaxLines);
86                 }
87             });
88 
89             increaseMaxLinesIfNecessary(header, mInitialHeaderMaxLines);
90         }
91 
92         layout.setIcon(getDrawable(R.drawable.ic_enterprise_blue_24dp));
93     }
94 
95     /**
96      * If the text takes more than its {@code textView}'s {@code initialMaxLines}, expand it one
97      * more line.
98      */
increaseMaxLinesIfNecessary(TextView textView, int initialMaxLines)99     private void increaseMaxLinesIfNecessary(TextView textView, int initialMaxLines) {
100         textView.setMaxLines(initialMaxLines);
101         textView.post(() -> {
102             Layout layout = textView.getLayout();
103             if (layout == null) {
104                 return;
105             }
106             int lineCount = layout.getLineCount();
107             if (lineCount > 0 && layout.getEllipsisCount(lineCount - 1) > 0) {
108                 textView.setMaxLines(initialMaxLines + 1);
109             }
110         });
111     }
112 }
113