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