Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2020 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 | package com.android.launcher3.hybridhotseat; |
| 17 | |
Samuel Fufa | b641ac2 | 2020-02-10 17:58:11 -0800 | [diff] [blame] | 18 | import static com.android.launcher3.logging.LoggerUtils.newLauncherEvent; |
Samuel Fufa | f667a13 | 2020-05-29 14:47:42 -0700 | [diff] [blame] | 19 | import static com.android.launcher3.userevent.nano.LauncherLogProto.ControlType |
| 20 | .HYBRID_HOTSEAT_CANCELED; |
Samuel Fufa | b641ac2 | 2020-02-10 17:58:11 -0800 | [diff] [blame] | 21 | |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 22 | import android.animation.PropertyValuesHolder; |
| 23 | import android.content.Context; |
| 24 | import android.content.res.Configuration; |
| 25 | import android.graphics.Rect; |
| 26 | import android.util.AttributeSet; |
| 27 | import android.view.LayoutInflater; |
| 28 | import android.view.View; |
| 29 | import android.widget.Button; |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 30 | import android.widget.TextView; |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 31 | |
Samuel Fufa | 5b2da14 | 2020-05-28 16:55:26 -0700 | [diff] [blame^] | 32 | import com.android.launcher3.AbstractFloatingView; |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 33 | import com.android.launcher3.CellLayout; |
| 34 | import com.android.launcher3.DeviceProfile; |
| 35 | import com.android.launcher3.Insettable; |
| 36 | import com.android.launcher3.Launcher; |
| 37 | import com.android.launcher3.R; |
Samuel Fufa | 40846c6 | 2020-04-12 16:26:16 -0700 | [diff] [blame] | 38 | import com.android.launcher3.Workspace; |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 39 | import com.android.launcher3.anim.Interpolators; |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 40 | import com.android.launcher3.config.FeatureFlags; |
Samuel Fufa | b641ac2 | 2020-02-10 17:58:11 -0800 | [diff] [blame] | 41 | import com.android.launcher3.logging.UserEventDispatcher; |
Sunny Goyal | e396abf | 2020-04-06 15:11:17 -0700 | [diff] [blame] | 42 | import com.android.launcher3.model.data.WorkspaceItemInfo; |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 43 | import com.android.launcher3.uioverrides.PredictedAppIcon; |
| 44 | import com.android.launcher3.userevent.nano.LauncherLogProto; |
| 45 | import com.android.launcher3.views.AbstractSlideInView; |
| 46 | |
| 47 | import java.util.List; |
| 48 | |
| 49 | /** |
| 50 | * User education dialog for hybrid hotseat. Allows user to migrate hotseat items to a new page in |
| 51 | * the workspace and shows predictions on the whole hotseat |
| 52 | */ |
| 53 | public class HotseatEduDialog extends AbstractSlideInView implements Insettable { |
| 54 | |
| 55 | private static final int DEFAULT_CLOSE_DURATION = 200; |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 56 | protected static final int FINAL_SCRIM_BG_COLOR = 0x88000000; |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 57 | |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 58 | |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 59 | // we use this value to keep track of migration logs as we experiment with different migrations |
| 60 | private static final int MIGRATION_EXPERIMENT_IDENTIFIER = 1; |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 61 | |
| 62 | private final Rect mInsets = new Rect(); |
| 63 | private View mHotseatWrapper; |
| 64 | private CellLayout mSampleHotseat; |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 65 | private Button mDismissBtn; |
| 66 | |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 67 | public void setHotseatEduController(HotseatEduController hotseatEduController) { |
| 68 | mHotseatEduController = hotseatEduController; |
| 69 | } |
| 70 | |
| 71 | private HotseatEduController mHotseatEduController; |
| 72 | |
| 73 | public HotseatEduDialog(Context context, AttributeSet attr) { |
| 74 | this(context, attr, 0); |
| 75 | } |
| 76 | |
| 77 | public HotseatEduDialog(Context context, AttributeSet attrs, |
| 78 | int defStyleAttr) { |
| 79 | super(context, attrs, defStyleAttr); |
| 80 | mContent = this; |
| 81 | } |
| 82 | |
| 83 | |
| 84 | @Override |
| 85 | protected void onFinishInflate() { |
| 86 | super.onFinishInflate(); |
| 87 | mHotseatWrapper = findViewById(R.id.hotseat_wrapper); |
| 88 | mSampleHotseat = findViewById(R.id.sample_prediction); |
| 89 | |
| 90 | DeviceProfile grid = mLauncher.getDeviceProfile(); |
| 91 | Rect padding = grid.getHotseatLayoutPadding(); |
| 92 | |
| 93 | mSampleHotseat.getLayoutParams().height = grid.cellHeightPx; |
| 94 | mSampleHotseat.setGridSize(grid.inv.numHotseatIcons, 1); |
| 95 | mSampleHotseat.setPadding(padding.left, 0, padding.right, 0); |
| 96 | |
| 97 | Button turnOnBtn = findViewById(R.id.turn_predictions_on); |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 98 | turnOnBtn.setOnClickListener(this::onAccept); |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 99 | |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 100 | mDismissBtn = findViewById(R.id.no_thanks); |
| 101 | mDismissBtn.setOnClickListener(this::onDismiss); |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 102 | |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 103 | // update ui to reflect which migration method is going to be used |
| 104 | if (FeatureFlags.HOTSEAT_MIGRATE_TO_FOLDER.get()) { |
| 105 | ((TextView) findViewById(R.id.hotseat_edu_content)).setText( |
| 106 | R.string.hotseat_edu_message_migrate_alt); |
| 107 | } |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 108 | } |
| 109 | |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 110 | private void onAccept(View v) { |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 111 | mHotseatEduController.migrate(); |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 112 | handleClose(true); |
Samuel Fufa | aa2aff5 | 2020-03-24 18:34:12 -0700 | [diff] [blame] | 113 | |
| 114 | mHotseatEduController.moveHotseatItems(); |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 115 | mHotseatEduController.finishOnboarding(); |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 116 | //TODO: pass actual page index here. |
| 117 | // Temporarily we're passing 1 for folder migration and 2 for page migration |
| 118 | logUserAction(true, FeatureFlags.HOTSEAT_MIGRATE_TO_FOLDER.get() ? 1 : 2); |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 119 | } |
| 120 | |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 121 | private void onDismiss(View v) { |
Samuel Fufa | aa2aff5 | 2020-03-24 18:34:12 -0700 | [diff] [blame] | 122 | mHotseatEduController.showDimissTip(); |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 123 | mHotseatEduController.finishOnboarding(); |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 124 | logUserAction(false, -1); |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 125 | handleClose(true); |
| 126 | } |
| 127 | |
| 128 | @Override |
| 129 | public void logActionCommand(int command) { |
| 130 | // Since this is on-boarding popup, it is not a user controlled action. |
| 131 | } |
| 132 | |
| 133 | @Override |
| 134 | public int getLogContainerType() { |
| 135 | return LauncherLogProto.ContainerType.TIP; |
| 136 | } |
| 137 | |
| 138 | @Override |
| 139 | protected boolean isOfType(int type) { |
| 140 | return (type & TYPE_ON_BOARD_POPUP) != 0; |
| 141 | } |
| 142 | |
| 143 | @Override |
| 144 | public void setInsets(Rect insets) { |
| 145 | int leftInset = insets.left - mInsets.left; |
| 146 | int rightInset = insets.right - mInsets.right; |
| 147 | int bottomInset = insets.bottom - mInsets.bottom; |
| 148 | mInsets.set(insets); |
Samuel Fufa | be40b29 | 2020-05-18 12:50:29 -0700 | [diff] [blame] | 149 | if (mLauncher.getOrientation() == Configuration.ORIENTATION_PORTRAIT) { |
| 150 | setPadding(leftInset, getPaddingTop(), rightInset, 0); |
| 151 | mHotseatWrapper.setPadding(mHotseatWrapper.getPaddingLeft(), getPaddingTop(), |
| 152 | mHotseatWrapper.getPaddingRight(), bottomInset); |
| 153 | mHotseatWrapper.getLayoutParams().height = |
| 154 | mLauncher.getDeviceProfile().hotseatBarSizePx + insets.bottom; |
| 155 | |
| 156 | } else { |
| 157 | setPadding(0, getPaddingTop(), 0, 0); |
| 158 | mHotseatWrapper.setPadding(mHotseatWrapper.getPaddingLeft(), getPaddingTop(), |
| 159 | mHotseatWrapper.getPaddingRight(), |
| 160 | (int) getResources().getDimension(R.dimen.bottom_sheet_edu_padding)); |
| 161 | ((TextView) findViewById(R.id.hotseat_edu_heading)).setText( |
| 162 | R.string.hotseat_edu_title_migrate_landscape); |
| 163 | ((TextView) findViewById(R.id.hotseat_edu_content)).setText( |
| 164 | R.string.hotseat_edu_message_migrate_landscape); |
| 165 | } |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 166 | } |
| 167 | |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 168 | private void logUserAction(boolean migrated, int pageIndex) { |
Samuel Fufa | b641ac2 | 2020-02-10 17:58:11 -0800 | [diff] [blame] | 169 | LauncherLogProto.Action action = new LauncherLogProto.Action(); |
| 170 | LauncherLogProto.Target target = new LauncherLogProto.Target(); |
Samuel Fufa | 40846c6 | 2020-04-12 16:26:16 -0700 | [diff] [blame] | 171 | |
| 172 | int hotseatItemsCount = mLauncher.getHotseat().getShortcutsAndWidgets().getChildCount(); |
| 173 | // -1 to exclude smart space |
| 174 | int workspaceItemCount = mLauncher.getWorkspace().getScreenWithId( |
| 175 | Workspace.FIRST_SCREEN_ID).getShortcutsAndWidgets().getChildCount() - 1; |
| 176 | |
Samuel Fufa | b641ac2 | 2020-02-10 17:58:11 -0800 | [diff] [blame] | 177 | action.type = LauncherLogProto.Action.Type.TOUCH; |
| 178 | action.touch = LauncherLogProto.Action.Touch.TAP; |
| 179 | target.containerType = LauncherLogProto.ContainerType.TIP; |
| 180 | target.tipType = LauncherLogProto.TipType.HYBRID_HOTSEAT; |
| 181 | target.controlType = migrated ? LauncherLogProto.ControlType.HYBRID_HOTSEAT_ACCEPTED |
| 182 | : HYBRID_HOTSEAT_CANCELED; |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 183 | target.rank = MIGRATION_EXPERIMENT_IDENTIFIER; |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 184 | // encoding migration type on pageIndex |
Samuel Fufa | 82bbdac | 2020-03-09 18:24:47 -0700 | [diff] [blame] | 185 | target.pageIndex = pageIndex; |
Samuel Fufa | 40846c6 | 2020-04-12 16:26:16 -0700 | [diff] [blame] | 186 | target.cardinality = (workspaceItemCount * 1000) + hotseatItemsCount; |
Samuel Fufa | b641ac2 | 2020-02-10 17:58:11 -0800 | [diff] [blame] | 187 | LauncherLogProto.LauncherEvent event = newLauncherEvent(action, target); |
| 188 | UserEventDispatcher.newInstance(getContext()).dispatchUserEvent(event, null); |
| 189 | } |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 190 | |
Samuel Fufa | b641ac2 | 2020-02-10 17:58:11 -0800 | [diff] [blame] | 191 | private void logOnBoardingSeen() { |
| 192 | LauncherLogProto.Action action = new LauncherLogProto.Action(); |
| 193 | LauncherLogProto.Target target = new LauncherLogProto.Target(); |
| 194 | action.type = LauncherLogProto.Action.Type.TIP; |
| 195 | target.containerType = LauncherLogProto.ContainerType.TIP; |
| 196 | target.tipType = LauncherLogProto.TipType.HYBRID_HOTSEAT; |
| 197 | LauncherLogProto.LauncherEvent event = newLauncherEvent(action, target); |
| 198 | UserEventDispatcher.newInstance(getContext()).dispatchUserEvent(event, null); |
| 199 | } |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 200 | |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 201 | private void animateOpen() { |
| 202 | if (mIsOpen || mOpenCloseAnimator.isRunning()) { |
| 203 | return; |
| 204 | } |
| 205 | mIsOpen = true; |
| 206 | mOpenCloseAnimator.setValues( |
| 207 | PropertyValuesHolder.ofFloat(TRANSLATION_SHIFT, TRANSLATION_SHIFT_OPENED)); |
| 208 | mOpenCloseAnimator.setInterpolator(Interpolators.FAST_OUT_SLOW_IN); |
| 209 | mOpenCloseAnimator.start(); |
| 210 | } |
| 211 | |
| 212 | @Override |
| 213 | protected void handleClose(boolean animate) { |
| 214 | handleClose(true, DEFAULT_CLOSE_DURATION); |
| 215 | } |
| 216 | |
| 217 | @Override |
| 218 | protected void onConfigurationChanged(Configuration newConfig) { |
| 219 | super.onConfigurationChanged(newConfig); |
| 220 | handleClose(false); |
| 221 | } |
| 222 | |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 223 | @Override |
| 224 | protected int getScrimColor(Context context) { |
| 225 | return FINAL_SCRIM_BG_COLOR; |
| 226 | } |
| 227 | |
| 228 | private void populatePreview(List<WorkspaceItemInfo> predictions) { |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 229 | for (int i = 0; i < mLauncher.getDeviceProfile().inv.numHotseatIcons; i++) { |
| 230 | WorkspaceItemInfo info = predictions.get(i); |
| 231 | PredictedAppIcon icon = PredictedAppIcon.createIcon(mSampleHotseat, info); |
| 232 | icon.setEnabled(false); |
Samuel Fufa | 6eaf989 | 2020-04-01 11:40:40 -0700 | [diff] [blame] | 233 | icon.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_NO); |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 234 | icon.verifyHighRes(); |
| 235 | CellLayout.LayoutParams lp = new CellLayout.LayoutParams(i, 0, 1, 1); |
| 236 | mSampleHotseat.addViewToCellLayout(icon, i, info.getViewId(), lp, true); |
| 237 | } |
| 238 | } |
| 239 | |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 240 | /** |
| 241 | * Opens User education dialog with a list of suggested apps |
| 242 | */ |
| 243 | public void show(List<WorkspaceItemInfo> predictions) { |
| 244 | if (getParent() != null |
| 245 | || predictions.size() < mLauncher.getDeviceProfile().inv.numHotseatIcons |
| 246 | || mHotseatEduController == null) { |
| 247 | return; |
| 248 | } |
Samuel Fufa | 5b2da14 | 2020-05-28 16:55:26 -0700 | [diff] [blame^] | 249 | AbstractFloatingView.closeAllOpenViews(mLauncher); |
Samuel Fufa | a421143 | 2020-02-25 18:47:54 -0800 | [diff] [blame] | 250 | attachToContainer(); |
| 251 | logOnBoardingSeen(); |
| 252 | animateOpen(); |
| 253 | populatePreview(predictions); |
| 254 | } |
| 255 | |
Samuel Fufa | aed008d | 2019-12-19 10:57:40 -0800 | [diff] [blame] | 256 | /** |
| 257 | * Factory method for HotseatPredictionUserEdu dialog |
| 258 | */ |
| 259 | public static HotseatEduDialog getDialog(Launcher launcher) { |
| 260 | LayoutInflater layoutInflater = LayoutInflater.from(launcher); |
| 261 | return (HotseatEduDialog) layoutInflater.inflate( |
| 262 | R.layout.predicted_hotseat_edu, launcher.getDragLayer(), |
| 263 | false); |
| 264 | |
| 265 | } |
| 266 | } |