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