| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2018 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.google.android.setupcompat; |
| 18 | |
| 19 | import android.annotation.TargetApi; |
| 20 | import android.app.Activity; |
| 21 | import android.content.Context; |
| 22 | import android.content.ContextWrapper; |
| 23 | import android.content.res.TypedArray; |
| Setup Wizard Team | de9b52a | 2019-04-01 10:27:47 +0800 | [diff] [blame] | 24 | import android.os.Build; |
| Setup Wizard Team | 1ed3073 | 2019-03-14 10:00:10 +0800 | [diff] [blame] | 25 | import android.os.Build.VERSION; |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 26 | import android.os.Build.VERSION_CODES; |
| Setup Wizard Team | 1d79d00 | 2018-12-13 14:30:13 +0800 | [diff] [blame] | 27 | import android.os.PersistableBundle; |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 28 | import android.util.AttributeSet; |
| Setup Wizard Team | d41e3be | 2019-01-17 16:07:58 +0800 | [diff] [blame] | 29 | import android.util.Log; |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 30 | import android.view.LayoutInflater; |
| 31 | import android.view.View; |
| 32 | import android.view.ViewGroup; |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 33 | import android.view.WindowManager; |
| Setup Wizard Team | de9b52a | 2019-04-01 10:27:47 +0800 | [diff] [blame] | 34 | import com.google.android.setupcompat.internal.LifecycleFragment; |
| Setup Wizard Team | d6fc4af | 2019-12-31 20:58:11 +0800 | [diff] [blame] | 35 | import com.google.android.setupcompat.internal.PersistableBundles; |
| Maurice Lam | c452932 | 2019-02-14 22:04:56 +0000 | [diff] [blame] | 36 | import com.google.android.setupcompat.internal.TemplateLayout; |
| Setup Wizard Team | 1d79d00 | 2018-12-13 14:30:13 +0800 | [diff] [blame] | 37 | import com.google.android.setupcompat.logging.CustomEvent; |
| 38 | import com.google.android.setupcompat.logging.MetricKey; |
| 39 | import com.google.android.setupcompat.logging.SetupMetricsLogger; |
| Setup Wizard Team | 1ed3073 | 2019-03-14 10:00:10 +0800 | [diff] [blame] | 40 | import com.google.android.setupcompat.partnerconfig.PartnerConfigHelper; |
| Setup Wizard Team | d41e3be | 2019-01-17 16:07:58 +0800 | [diff] [blame] | 41 | import com.google.android.setupcompat.template.FooterBarMixin; |
| 42 | import com.google.android.setupcompat.template.FooterButton; |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 43 | import com.google.android.setupcompat.template.StatusBarMixin; |
| 44 | import com.google.android.setupcompat.template.SystemNavBarMixin; |
| 45 | import com.google.android.setupcompat.util.WizardManagerHelper; |
| 46 | |
| 47 | /** A templatization layout with consistent style used in Setup Wizard or app itself. */ |
| 48 | public class PartnerCustomizationLayout extends TemplateLayout { |
| Setup Wizard Team | d41e3be | 2019-01-17 16:07:58 +0800 | [diff] [blame] | 49 | // Log tags can have at most 23 characters on N or before. |
| 50 | private static final String TAG = "PartnerCustomizedLayout"; |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 51 | |
| Setup Wizard Team | 67e7fef | 2019-03-23 08:06:48 +0800 | [diff] [blame] | 52 | /** |
| 53 | * Attribute indicating whether usage of partner theme resources is allowed. This corresponds to |
| 54 | * the {@code app:sucUsePartnerResource} XML attribute. Note that when running in setup wizard, |
| 55 | * this is always overridden to true. |
| 56 | */ |
| Setup Wizard Team | de9b52a | 2019-04-01 10:27:47 +0800 | [diff] [blame] | 57 | private boolean usePartnerResourceAttr; |
| Setup Wizard Team | 1ed3073 | 2019-03-14 10:00:10 +0800 | [diff] [blame] | 58 | |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 59 | private Activity activity; |
| 60 | |
| 61 | public PartnerCustomizationLayout(Context context) { |
| 62 | this(context, 0, 0); |
| 63 | } |
| 64 | |
| 65 | public PartnerCustomizationLayout(Context context, int template) { |
| 66 | this(context, template, 0); |
| 67 | } |
| 68 | |
| 69 | public PartnerCustomizationLayout(Context context, int template, int containerId) { |
| 70 | super(context, template, containerId); |
| 71 | init(null, R.attr.sucLayoutTheme); |
| 72 | } |
| 73 | |
| 74 | public PartnerCustomizationLayout(Context context, AttributeSet attrs) { |
| 75 | super(context, attrs); |
| 76 | init(attrs, R.attr.sucLayoutTheme); |
| 77 | } |
| 78 | |
| 79 | @TargetApi(VERSION_CODES.HONEYCOMB) |
| 80 | public PartnerCustomizationLayout(Context context, AttributeSet attrs, int defStyleAttr) { |
| 81 | super(context, attrs, defStyleAttr); |
| 82 | init(attrs, defStyleAttr); |
| 83 | } |
| 84 | |
| 85 | private void init(AttributeSet attrs, int defStyleAttr) { |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 86 | |
| 87 | TypedArray a = |
| 88 | getContext() |
| 89 | .obtainStyledAttributes( |
| 90 | attrs, R.styleable.SucPartnerCustomizationLayout, defStyleAttr, 0); |
| 91 | |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 92 | boolean layoutFullscreen = |
| 93 | a.getBoolean(R.styleable.SucPartnerCustomizationLayout_sucLayoutFullscreen, true); |
| Setup Wizard Team | ff4fecb | 2018-12-19 11:31:37 +0800 | [diff] [blame] | 94 | |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 95 | a.recycle(); |
| 96 | |
| Setup Wizard Team | 1ed3073 | 2019-03-14 10:00:10 +0800 | [diff] [blame] | 97 | if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP && layoutFullscreen) { |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 98 | setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN); |
| 99 | } |
| 100 | |
| Setup Wizard Team | ff4fecb | 2018-12-19 11:31:37 +0800 | [diff] [blame] | 101 | registerMixin( |
| Setup Wizard Team | 67e7fef | 2019-03-23 08:06:48 +0800 | [diff] [blame] | 102 | StatusBarMixin.class, new StatusBarMixin(this, activity.getWindow(), attrs, defStyleAttr)); |
| 103 | registerMixin(SystemNavBarMixin.class, new SystemNavBarMixin(this, activity.getWindow())); |
| 104 | registerMixin(FooterBarMixin.class, new FooterBarMixin(this, attrs, defStyleAttr)); |
| Setup Wizard Team | ff4fecb | 2018-12-19 11:31:37 +0800 | [diff] [blame] | 105 | |
| Setup Wizard Team | 1ed3073 | 2019-03-14 10:00:10 +0800 | [diff] [blame] | 106 | getMixin(SystemNavBarMixin.class).applyPartnerCustomizations(attrs, defStyleAttr); |
| 107 | |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 108 | // Override the FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS, FLAG_TRANSLUCENT_STATUS, |
| 109 | // FLAG_TRANSLUCENT_NAVIGATION and SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN attributes of window forces |
| 110 | // showing status bar and navigation bar. |
| Setup Wizard Team | de9b52a | 2019-04-01 10:27:47 +0800 | [diff] [blame] | 111 | if (Build.VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP) { |
| 112 | activity.getWindow().addFlags(WindowManager.LayoutParams.FLAG_DRAWS_SYSTEM_BAR_BACKGROUNDS); |
| 113 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_STATUS); |
| 114 | activity.getWindow().clearFlags(WindowManager.LayoutParams.FLAG_TRANSLUCENT_NAVIGATION); |
| 115 | } |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 116 | } |
| 117 | |
| 118 | @Override |
| 119 | protected View onInflateTemplate(LayoutInflater inflater, int template) { |
| 120 | if (template == 0) { |
| 121 | template = R.layout.partner_customization_layout; |
| 122 | } |
| 123 | return inflateTemplate(inflater, 0, template); |
| 124 | } |
| 125 | |
| Setup Wizard Team | de9b52a | 2019-04-01 10:27:47 +0800 | [diff] [blame] | 126 | /** |
| 127 | * {@inheritDoc} |
| 128 | * |
| 129 | * <p>This method sets all these flags before onTemplateInflated since it will be too late and get |
| 130 | * incorrect flag value on PartnerCustomizationLayout if sets them after onTemplateInflated. |
| 131 | */ |
| 132 | @Override |
| 133 | protected void onBeforeTemplateInflated(AttributeSet attrs, int defStyleAttr) { |
| 134 | |
| Setup Wizard Team | de9b52a | 2019-04-01 10:27:47 +0800 | [diff] [blame] | 135 | // Sets default value to true since this timing |
| 136 | // before PartnerCustomization members initialization |
| 137 | usePartnerResourceAttr = true; |
| 138 | |
| 139 | activity = lookupActivityFromContext(getContext()); |
| 140 | |
| Setup Wizard Team | 75de590 | 2020-09-22 20:34:53 +0800 | [diff] [blame] | 141 | boolean isSetupFlow = WizardManagerHelper.isAnySetupWizard(activity.getIntent()); |
| Setup Wizard Team | de9b52a | 2019-04-01 10:27:47 +0800 | [diff] [blame] | 142 | |
| 143 | TypedArray a = |
| 144 | getContext() |
| 145 | .obtainStyledAttributes( |
| 146 | attrs, R.styleable.SucPartnerCustomizationLayout, defStyleAttr, 0); |
| 147 | |
| 148 | if (!a.hasValue(R.styleable.SucPartnerCustomizationLayout_sucUsePartnerResource)) { |
| Setup Wizard Team | 5b6b1b3 | 2019-04-02 13:42:22 +0800 | [diff] [blame] | 149 | // TODO: Enable Log.WTF after other client already set sucUsePartnerResource. |
| Setup Wizard Team | de9b52a | 2019-04-01 10:27:47 +0800 | [diff] [blame] | 150 | Log.e(TAG, "Attribute sucUsePartnerResource not found in " + activity.getComponentName()); |
| 151 | } |
| 152 | |
| 153 | usePartnerResourceAttr = |
| 154 | isSetupFlow |
| 155 | || a.getBoolean(R.styleable.SucPartnerCustomizationLayout_sucUsePartnerResource, true); |
| 156 | |
| 157 | a.recycle(); |
| 158 | |
| 159 | if (Log.isLoggable(TAG, Log.DEBUG)) { |
| 160 | Log.d( |
| 161 | TAG, |
| 162 | "activity=" |
| 163 | + activity.getClass().getSimpleName() |
| 164 | + " isSetupFlow=" |
| 165 | + isSetupFlow |
| 166 | + " enablePartnerResourceLoading=" |
| 167 | + enablePartnerResourceLoading() |
| 168 | + " usePartnerResourceAttr=" |
| 169 | + usePartnerResourceAttr); |
| 170 | } |
| 171 | } |
| 172 | |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 173 | @Override |
| 174 | protected ViewGroup findContainer(int containerId) { |
| 175 | if (containerId == 0) { |
| 176 | containerId = R.id.suc_layout_content; |
| 177 | } |
| 178 | return super.findContainer(containerId); |
| 179 | } |
| 180 | |
| 181 | @Override |
| 182 | protected void onAttachedToWindow() { |
| 183 | super.onAttachedToWindow(); |
| Setup Wizard Team | 1d79d00 | 2018-12-13 14:30:13 +0800 | [diff] [blame] | 184 | LifecycleFragment.attachNow(activity); |
| Setup Wizard Team | d41e3be | 2019-01-17 16:07:58 +0800 | [diff] [blame] | 185 | getMixin(FooterBarMixin.class).onAttachedToWindow(); |
| Setup Wizard Team | 1d79d00 | 2018-12-13 14:30:13 +0800 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | @Override |
| 189 | protected void onDetachedFromWindow() { |
| 190 | super.onDetachedFromWindow(); |
| Setup Wizard Team | de9b52a | 2019-04-01 10:27:47 +0800 | [diff] [blame] | 191 | if (VERSION.SDK_INT >= VERSION_CODES.LOLLIPOP |
| Setup Wizard Team | 3266cd8 | 2019-05-09 19:31:02 +0800 | [diff] [blame] | 192 | && Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q |
| Setup Wizard Team | de9b52a | 2019-04-01 10:27:47 +0800 | [diff] [blame] | 193 | && WizardManagerHelper.isAnySetupWizard(activity.getIntent())) { |
| Setup Wizard Team | d41e3be | 2019-01-17 16:07:58 +0800 | [diff] [blame] | 194 | FooterBarMixin footerBarMixin = getMixin(FooterBarMixin.class); |
| 195 | footerBarMixin.onDetachedFromWindow(); |
| 196 | FooterButton primaryButton = footerBarMixin.getPrimaryButton(); |
| 197 | FooterButton secondaryButton = footerBarMixin.getSecondaryButton(); |
| 198 | PersistableBundle primaryButtonMetrics = |
| 199 | primaryButton != null |
| 200 | ? primaryButton.getMetrics("PrimaryFooterButton") |
| 201 | : PersistableBundle.EMPTY; |
| 202 | PersistableBundle secondaryButtonMetrics = |
| 203 | secondaryButton != null |
| 204 | ? secondaryButton.getMetrics("SecondaryFooterButton") |
| 205 | : PersistableBundle.EMPTY; |
| 206 | |
| Setup Wizard Team | d6fc4af | 2019-12-31 20:58:11 +0800 | [diff] [blame] | 207 | PersistableBundle persistableBundle = |
| 208 | PersistableBundles.mergeBundles( |
| Setup Wizard Team | d41e3be | 2019-01-17 16:07:58 +0800 | [diff] [blame] | 209 | footerBarMixin.getLoggingMetrics(), primaryButtonMetrics, secondaryButtonMetrics); |
| 210 | |
| Setup Wizard Team | 1d79d00 | 2018-12-13 14:30:13 +0800 | [diff] [blame] | 211 | SetupMetricsLogger.logCustomEvent( |
| 212 | getContext(), |
| Setup Wizard Team | 24bb1e2 | 2019-11-08 15:22:51 +0800 | [diff] [blame] | 213 | CustomEvent.create(MetricKey.get("SetupCompatMetrics", activity), persistableBundle)); |
| Setup Wizard Team | 1d79d00 | 2018-12-13 14:30:13 +0800 | [diff] [blame] | 214 | } |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 215 | } |
| 216 | |
| Setup Wizard Team | 642e096 | 2020-12-30 19:23:06 +0800 | [diff] [blame^] | 217 | public static Activity lookupActivityFromContext(Context context) { |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 218 | if (context instanceof Activity) { |
| 219 | return (Activity) context; |
| 220 | } else if (context instanceof ContextWrapper) { |
| 221 | return lookupActivityFromContext(((ContextWrapper) context).getBaseContext()); |
| 222 | } else { |
| 223 | throw new IllegalArgumentException("Cannot find instance of Activity in parent tree"); |
| 224 | } |
| 225 | } |
| 226 | |
| 227 | /** |
| Setup Wizard Team | 67e7fef | 2019-03-23 08:06:48 +0800 | [diff] [blame] | 228 | * Returns true if partner resource loading is enabled. If true, and other necessary conditions |
| 229 | * for loading theme attributes are met, this layout will use customized theme attributes from OEM |
| 230 | * overlays. This is intended to be used with flag-based development, to allow a flag to control |
| 231 | * the rollout of partner resource loading. |
| Setup Wizard Team | d41e3be | 2019-01-17 16:07:58 +0800 | [diff] [blame] | 232 | */ |
| Setup Wizard Team | 67e7fef | 2019-03-23 08:06:48 +0800 | [diff] [blame] | 233 | protected boolean enablePartnerResourceLoading() { |
| Setup Wizard Team | d41e3be | 2019-01-17 16:07:58 +0800 | [diff] [blame] | 234 | return true; |
| 235 | } |
| 236 | |
| Setup Wizard Team | 1ed3073 | 2019-03-14 10:00:10 +0800 | [diff] [blame] | 237 | /** Returns if the current layout/activity applies partner customized configurations or not. */ |
| Setup Wizard Team | 67e7fef | 2019-03-23 08:06:48 +0800 | [diff] [blame] | 238 | public boolean shouldApplyPartnerResource() { |
| 239 | if (!enablePartnerResourceLoading()) { |
| 240 | return false; |
| 241 | } |
| 242 | if (!usePartnerResourceAttr) { |
| 243 | return false; |
| 244 | } |
| Setup Wizard Team | 3266cd8 | 2019-05-09 19:31:02 +0800 | [diff] [blame] | 245 | if (Build.VERSION.SDK_INT < Build.VERSION_CODES.Q) { |
| Setup Wizard Team | 67e7fef | 2019-03-23 08:06:48 +0800 | [diff] [blame] | 246 | return false; |
| 247 | } |
| 248 | if (!PartnerConfigHelper.get(getContext()).isAvailable()) { |
| 249 | return false; |
| 250 | } |
| 251 | return true; |
| Setup Wizard Team | 1ed3073 | 2019-03-14 10:00:10 +0800 | [diff] [blame] | 252 | } |
| Setup Wizard Team | 8ccc9e6 | 2018-11-28 13:33:48 +0800 | [diff] [blame] | 253 | } |