blob: 901c7aed2aa09efb8c6b69591cd9025ad127245b [file] [log] [blame]
Tony Wickhamfb63fe82018-01-16 12:14:06 -08001/*
2 * Copyright (C) 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
17package com.android.systemui.recents;
18
Tony Wickhamfb63fe82018-01-16 12:14:06 -080019import static android.app.WindowConfiguration.ACTIVITY_TYPE_STANDARD;
20import static android.app.WindowConfiguration.ACTIVITY_TYPE_UNDEFINED;
21
Tony Wickhamfb63fe82018-01-16 12:14:06 -080022import android.annotation.TargetApi;
23import android.app.ActivityManager;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080024import android.content.Context;
25import android.content.res.Configuration;
Tony Wickham0239d5f2018-01-22 18:40:05 -080026import android.content.res.Resources;
Tracy Zhou559d97c2018-04-07 23:54:46 -070027import android.graphics.CornerPathEffect;
28import android.graphics.Paint;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080029import android.graphics.PixelFormat;
Tracy Zhou559d97c2018-04-07 23:54:46 -070030import android.graphics.drawable.ShapeDrawable;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080031import android.os.Build;
Tony Wickham96f35e52018-02-16 15:36:09 -080032import android.os.SystemProperties;
33import android.os.UserManager;
Tony Wickham05c1f852018-02-06 12:32:54 -080034import android.text.TextUtils;
35import android.util.Log;
Tracy Zhou559d97c2018-04-07 23:54:46 -070036import android.util.TypedValue;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080037import android.view.Gravity;
38import android.view.LayoutInflater;
39import android.view.View;
40import android.view.ViewGroup;
41import android.view.WindowManager;
42import android.view.animation.AccelerateInterpolator;
43import android.view.animation.DecelerateInterpolator;
Tony Wickham0239d5f2018-01-22 18:40:05 -080044import android.widget.ImageView;
45import android.widget.TextView;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080046
Tony Wickham05c1f852018-02-06 12:32:54 -080047import com.android.systemui.OverviewProxyService;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080048import com.android.systemui.Prefs;
49import com.android.systemui.R;
50import com.android.systemui.recents.misc.SysUiTaskStackChangeListener;
51import com.android.systemui.shared.system.ActivityManagerWrapper;
52
Tony Wickham78f8eee2018-02-27 15:40:33 -080053import java.util.Collections;
54import java.util.HashSet;
55import java.util.Set;
56
Tony Wickhamfb63fe82018-01-16 12:14:06 -080057/**
58 * Shows onboarding for the new recents interaction in P (codenamed quickstep).
59 */
60@TargetApi(Build.VERSION_CODES.P)
Tony Wickham05c1f852018-02-06 12:32:54 -080061public class RecentsOnboarding {
Tony Wickhamfb63fe82018-01-16 12:14:06 -080062
Tony Wickham05c1f852018-02-06 12:32:54 -080063 private static final String TAG = "RecentsOnboarding";
Tony Wickhamfb63fe82018-01-16 12:14:06 -080064 private static final boolean RESET_PREFS_FOR_DEBUG = false;
Tracy Zhoubeaa9752018-04-09 10:37:44 -070065 private static final boolean ONBOARDING_ENABLED = false;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080066 private static final long SHOW_DELAY_MS = 500;
67 private static final long SHOW_HIDE_DURATION_MS = 300;
68 // Don't show the onboarding until the user has launched this number of apps.
Tony Wickham64f1cd52018-01-23 16:05:53 -080069 private static final int SHOW_ON_APP_LAUNCH = 2;
Tony Wickham2f0d0152018-03-01 15:45:04 -080070 // After explicitly dismissing, show again after launching this number of apps.
71 private static final int SHOW_ON_APP_LAUNCH_AFTER_DISMISS = 5;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080072
73 private final Context mContext;
74 private final WindowManager mWindowManager;
Tony Wickham05c1f852018-02-06 12:32:54 -080075 private final OverviewProxyService mOverviewProxyService;
Tony Wickham78f8eee2018-02-27 15:40:33 -080076 private Set<String> mBlacklistedPackages;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080077 private final View mLayout;
Tony Wickham0239d5f2018-01-22 18:40:05 -080078 private final TextView mTextView;
79 private final ImageView mDismissView;
Tracy Zhou559d97c2018-04-07 23:54:46 -070080 private final View mArrowView;
81 private final int mOnboardingToastColor;
82 private final int mOnboardingToastArrowRadius;
83 private int mNavBarHeight;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080084
85 private boolean mTaskListenerRegistered;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080086 private boolean mLayoutAttachedToWindow;
Tony Wickham2f0d0152018-03-01 15:45:04 -080087 private int mLastTaskId;
88 private boolean mHasDismissed;
89 private int mNumAppsLaunchedSinceDismiss;
Tony Wickhamfb63fe82018-01-16 12:14:06 -080090
91 private final SysUiTaskStackChangeListener mTaskListener = new SysUiTaskStackChangeListener() {
92 @Override
93 public void onTaskStackChanged() {
94 ActivityManager.RunningTaskInfo info = ActivityManagerWrapper.getInstance()
95 .getRunningTask(ACTIVITY_TYPE_UNDEFINED /* ignoreActivityType */);
Winson Chung475f20f2018-03-19 15:04:56 -070096 if (info == null) {
97 return;
98 }
Tony Wickham78f8eee2018-02-27 15:40:33 -080099 if (mBlacklistedPackages.contains(info.baseActivity.getPackageName())) {
100 hide(true);
101 return;
102 }
Tony Wickham2f0d0152018-03-01 15:45:04 -0800103 if (info.id == mLastTaskId) {
104 // We only count launches that go to a new task.
105 return;
106 }
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800107 int activityType = info.configuration.windowConfiguration.getActivityType();
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800108 if (activityType == ACTIVITY_TYPE_STANDARD) {
Tony Wickham2f0d0152018-03-01 15:45:04 -0800109 mLastTaskId = info.id;
110 int numAppsLaunched = mHasDismissed ? mNumAppsLaunchedSinceDismiss
111 : Prefs.getInt(mContext, Prefs.Key.NUM_APPS_LAUNCHED, 0);
112 int showOnAppLaunch = mHasDismissed ? SHOW_ON_APP_LAUNCH_AFTER_DISMISS
113 : SHOW_ON_APP_LAUNCH;
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800114 numAppsLaunched++;
Tony Wickham2f0d0152018-03-01 15:45:04 -0800115 if (numAppsLaunched >= showOnAppLaunch) {
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800116 show();
117 } else {
Tony Wickham2f0d0152018-03-01 15:45:04 -0800118 if (mHasDismissed) {
119 mNumAppsLaunchedSinceDismiss = numAppsLaunched;
120 } else {
121 Prefs.putInt(mContext, Prefs.Key.NUM_APPS_LAUNCHED, numAppsLaunched);
122 }
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800123 }
124 } else {
Winson Chung8ae17be2018-01-24 19:50:12 -0800125 hide(false);
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800126 }
127 }
128 };
129
130 private final View.OnAttachStateChangeListener mOnAttachStateChangeListener
131 = new View.OnAttachStateChangeListener() {
132 @Override
133 public void onViewAttachedToWindow(View view) {
134 if (view == mLayout) {
135 mLayoutAttachedToWindow = true;
Tony Wickham2f0d0152018-03-01 15:45:04 -0800136 mHasDismissed = false;
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800137 }
138 }
139
140 @Override
141 public void onViewDetachedFromWindow(View view) {
142 if (view == mLayout) {
143 mLayoutAttachedToWindow = false;
144 }
145 }
146 };
147
Tony Wickham05c1f852018-02-06 12:32:54 -0800148 public RecentsOnboarding(Context context, OverviewProxyService overviewProxyService) {
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800149 mContext = context;
Tony Wickham05c1f852018-02-06 12:32:54 -0800150 mOverviewProxyService = overviewProxyService;
Tony Wickham0239d5f2018-01-22 18:40:05 -0800151 final Resources res = context.getResources();
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800152 mWindowManager = (WindowManager) mContext.getSystemService(Context.WINDOW_SERVICE);
Tony Wickham78f8eee2018-02-27 15:40:33 -0800153 mBlacklistedPackages = new HashSet<>();
154 Collections.addAll(mBlacklistedPackages, res.getStringArray(
155 R.array.recents_onboarding_blacklisted_packages));
Tony Wickham05c1f852018-02-06 12:32:54 -0800156 mLayout = LayoutInflater.from(mContext).inflate(R.layout.recents_onboarding, null);
Winson Chung8ae17be2018-01-24 19:50:12 -0800157 mTextView = mLayout.findViewById(R.id.onboarding_text);
158 mDismissView = mLayout.findViewById(R.id.dismiss);
Tracy Zhou559d97c2018-04-07 23:54:46 -0700159 mArrowView = mLayout.findViewById(R.id.arrow);
160
161 TypedValue typedValue = new TypedValue();
162 context.getTheme().resolveAttribute(android.R.attr.colorAccent, typedValue, true);
163 mOnboardingToastColor = res.getColor(typedValue.resourceId);
164 mOnboardingToastArrowRadius = res.getDimensionPixelSize(
165 R.dimen.recents_onboarding_toast_arrow_corner_radius);
Tony Wickham0239d5f2018-01-22 18:40:05 -0800166
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800167 mLayout.addOnAttachStateChangeListener(mOnAttachStateChangeListener);
Tony Wickham2f0d0152018-03-01 15:45:04 -0800168 mDismissView.setOnClickListener(v -> {
169 hide(true);
170 mHasDismissed = true;
171 mNumAppsLaunchedSinceDismiss = 0;
172 });
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800173
Tracy Zhou559d97c2018-04-07 23:54:46 -0700174 ViewGroup.LayoutParams arrowLp = mArrowView.getLayoutParams();
175 ShapeDrawable arrowDrawable = new ShapeDrawable(TriangleShape.create(
176 arrowLp.width, arrowLp.height, false));
177 Paint arrowPaint = arrowDrawable.getPaint();
178 arrowPaint.setColor(mOnboardingToastColor);
179 // The corner path effect won't be reflected in the shadow, but shouldn't be noticeable.
180 arrowPaint.setPathEffect(new CornerPathEffect(mOnboardingToastArrowRadius));
181 mArrowView.setBackground(arrowDrawable);
182
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800183 if (RESET_PREFS_FOR_DEBUG) {
Tony Wickham05c1f852018-02-06 12:32:54 -0800184 Prefs.putBoolean(mContext, Prefs.Key.HAS_SEEN_RECENTS_ONBOARDING, false);
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800185 Prefs.putInt(mContext, Prefs.Key.NUM_APPS_LAUNCHED, 0);
186 }
187 }
188
Winson Chung8ae17be2018-01-24 19:50:12 -0800189 public void onConnectedToLauncher() {
Tracy Zhoubeaa9752018-04-09 10:37:44 -0700190 if (!ONBOARDING_ENABLED) {
191 return;
192 }
Tony Wickham05c1f852018-02-06 12:32:54 -0800193 boolean alreadySeenRecentsOnboarding = Prefs.getBoolean(mContext,
194 Prefs.Key.HAS_SEEN_RECENTS_ONBOARDING, false);
195 if (!mTaskListenerRegistered && !alreadySeenRecentsOnboarding) {
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800196 ActivityManagerWrapper.getInstance().registerTaskStackListener(mTaskListener);
197 mTaskListenerRegistered = true;
198 }
199 }
200
Matthew Ng2ea93b72018-03-14 19:43:18 +0000201 public void onQuickStepStarted() {
Tony Wickham05c1f852018-02-06 12:32:54 -0800202 boolean alreadySeenRecentsOnboarding = Prefs.getBoolean(mContext,
203 Prefs.Key.HAS_SEEN_RECENTS_ONBOARDING, false);
204 if (!alreadySeenRecentsOnboarding) {
205 Prefs.putBoolean(mContext, Prefs.Key.HAS_SEEN_RECENTS_ONBOARDING, true);
Winson Chung8ae17be2018-01-24 19:50:12 -0800206 onDisconnectedFromLauncher();
207 }
208 }
209
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800210 public void onDisconnectedFromLauncher() {
211 if (mTaskListenerRegistered) {
212 ActivityManagerWrapper.getInstance().unregisterTaskStackListener(mTaskListener);
213 mTaskListenerRegistered = false;
214 }
Tony Wickham2f0d0152018-03-01 15:45:04 -0800215 mHasDismissed = false;
216 mNumAppsLaunchedSinceDismiss = 0;
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800217 hide(false);
218 }
219
220 public void onConfigurationChanged(Configuration newConfiguration) {
221 if (newConfiguration.orientation != Configuration.ORIENTATION_PORTRAIT) {
222 hide(false);
223 }
224 }
225
226 public void show() {
Tony Wickham96f35e52018-02-16 15:36:09 -0800227 if (!shouldShow()) {
228 return;
229 }
Tracy Zhou0ad27632018-04-11 20:40:53 -0700230 mTextView.setText(R.string.recents_swipe_up_onboarding);
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800231 // Only show in portrait.
232 int orientation = mContext.getResources().getConfiguration().orientation;
233 if (!mLayoutAttachedToWindow && orientation == Configuration.ORIENTATION_PORTRAIT) {
234 mLayout.setSystemUiVisibility(View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
Tracy Zhou559d97c2018-04-07 23:54:46 -0700235
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800236 mWindowManager.addView(mLayout, getWindowLayoutParams());
237 int layoutHeight = mLayout.getHeight();
238 if (layoutHeight == 0) {
239 mLayout.measure(View.MeasureSpec.UNSPECIFIED, View.MeasureSpec.UNSPECIFIED);
240 layoutHeight = mLayout.getMeasuredHeight();
241 }
242 mLayout.setTranslationY(layoutHeight);
243 mLayout.setAlpha(0);
244 mLayout.animate()
245 .translationY(0)
246 .alpha(1f)
247 .withLayer()
248 .setStartDelay(SHOW_DELAY_MS)
249 .setDuration(SHOW_HIDE_DURATION_MS)
250 .setInterpolator(new DecelerateInterpolator())
251 .start();
252 }
253 }
254
Tony Wickham96f35e52018-02-16 15:36:09 -0800255 /**
Robb Glassere8f1d362018-02-20 13:28:53 -0800256 * @return True unless setprop has been set to false, we're in demo mode, or running tests in
257 * automation.
Tony Wickham96f35e52018-02-16 15:36:09 -0800258 */
259 private boolean shouldShow() {
260 return SystemProperties.getBoolean("persist.quickstep.onboarding.enabled",
Robb Glassere8f1d362018-02-20 13:28:53 -0800261 !(mContext.getSystemService(UserManager.class)).isDemoUser() &&
262 !ActivityManager.isRunningInTestHarness());
Tony Wickham96f35e52018-02-16 15:36:09 -0800263 }
264
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800265 public void hide(boolean animate) {
266 if (mLayoutAttachedToWindow) {
267 if (animate) {
268 mLayout.animate()
269 .translationY(mLayout.getHeight())
270 .alpha(0f)
271 .withLayer()
272 .setDuration(SHOW_HIDE_DURATION_MS)
273 .setInterpolator(new AccelerateInterpolator())
274 .withEndAction(() -> mWindowManager.removeView(mLayout))
275 .start();
276 } else {
Tony Wickham96f35e52018-02-16 15:36:09 -0800277 mLayout.animate().cancel();
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800278 mWindowManager.removeView(mLayout);
279 }
280 }
281 }
282
Tracy Zhou559d97c2018-04-07 23:54:46 -0700283 public void setNavBarHeight(int navBarHeight) {
284 mNavBarHeight = navBarHeight;
Tony Wickham0239d5f2018-01-22 18:40:05 -0800285 }
286
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800287 private WindowManager.LayoutParams getWindowLayoutParams() {
Tracy Zhou559d97c2018-04-07 23:54:46 -0700288 int flags = WindowManager.LayoutParams.FLAG_LAYOUT_NO_LIMITS
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800289 | WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE;
290 final WindowManager.LayoutParams lp = new WindowManager.LayoutParams(
291 ViewGroup.LayoutParams.MATCH_PARENT,
292 ViewGroup.LayoutParams.WRAP_CONTENT,
Tracy Zhou559d97c2018-04-07 23:54:46 -0700293 0, -mNavBarHeight / 2,
294 WindowManager.LayoutParams.TYPE_APPLICATION_OVERLAY,
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800295 flags,
296 PixelFormat.TRANSLUCENT);
297 lp.privateFlags |= WindowManager.LayoutParams.PRIVATE_FLAG_SHOW_FOR_ALL_USERS;
Tony Wickham05c1f852018-02-06 12:32:54 -0800298 lp.setTitle("RecentsOnboarding");
Tony Wickhamfb63fe82018-01-16 12:14:06 -0800299 lp.gravity = Gravity.BOTTOM;
300 return lp;
301 }
302}