AddItemActivity.java revision 22ca9ec137ec8eb70026f90591ae01af7669a3b7
1/* 2 * Copyright (C) 2017 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.launcher3.dragndrop; 18 19import static com.android.launcher3.logging.LoggerUtils.newCommandAction; 20import static com.android.launcher3.logging.LoggerUtils.newContainerTarget; 21import static com.android.launcher3.logging.LoggerUtils.newItemTarget; 22import static com.android.launcher3.logging.LoggerUtils.newLauncherEvent; 23 24import android.annotation.TargetApi; 25import android.app.ActivityOptions; 26import android.appwidget.AppWidgetHost; 27import android.appwidget.AppWidgetManager; 28import android.content.ClipData; 29import android.content.ClipDescription; 30import android.content.Intent; 31import android.content.pm.LauncherApps.PinItemRequest; 32import android.content.res.Configuration; 33import android.graphics.Canvas; 34import android.graphics.Point; 35import android.graphics.PointF; 36import android.graphics.Rect; 37import android.os.Build; 38import android.os.Bundle; 39import android.view.MotionEvent; 40import android.view.View; 41import android.view.View.DragShadowBuilder; 42import android.view.View.OnLongClickListener; 43import android.view.View.OnTouchListener; 44 45import com.android.launcher3.BaseActivity; 46import com.android.launcher3.InstallShortcutReceiver; 47import com.android.launcher3.InvariantDeviceProfile; 48import com.android.launcher3.Launcher; 49import com.android.launcher3.LauncherAppState; 50import com.android.launcher3.LauncherAppWidgetProviderInfo; 51import com.android.launcher3.R; 52import com.android.launcher3.Utilities; 53import com.android.launcher3.compat.AppWidgetManagerCompat; 54import com.android.launcher3.compat.LauncherAppsCompatVO; 55import com.android.launcher3.model.WidgetItem; 56import com.android.launcher3.shortcuts.ShortcutInfoCompat; 57import com.android.launcher3.userevent.nano.LauncherLogProto.Action; 58import com.android.launcher3.userevent.nano.LauncherLogProto.ContainerType; 59import com.android.launcher3.widget.PendingAddShortcutInfo; 60import com.android.launcher3.widget.PendingAddWidgetInfo; 61import com.android.launcher3.widget.WidgetHostViewLoader; 62import com.android.launcher3.widget.WidgetImageView; 63 64@TargetApi(Build.VERSION_CODES.O) 65public class AddItemActivity extends BaseActivity implements OnLongClickListener, OnTouchListener { 66 67 private static final int SHADOW_SIZE = 10; 68 69 private static final int REQUEST_BIND_APPWIDGET = 1; 70 private static final String STATE_EXTRA_WIDGET_ID = "state.widget.id"; 71 72 private final PointF mLastTouchPos = new PointF(); 73 74 private PinItemRequest mRequest; 75 private LauncherAppState mApp; 76 private InvariantDeviceProfile mIdp; 77 78 private LivePreviewWidgetCell mWidgetCell; 79 80 // Widget request specific options. 81 private AppWidgetHost mAppWidgetHost; 82 private AppWidgetManagerCompat mAppWidgetManager; 83 private PendingAddWidgetInfo mPendingWidgetInfo; 84 private int mPendingBindWidgetId; 85 private Bundle mWidgetOptions; 86 87 @Override 88 protected void onCreate(Bundle savedInstanceState) { 89 super.onCreate(savedInstanceState); 90 91 mRequest = LauncherAppsCompatVO.getPinItemRequest(getIntent()); 92 if (mRequest == null) { 93 finish(); 94 return; 95 } 96 97 mApp = LauncherAppState.getInstance(this); 98 mIdp = mApp.getInvariantDeviceProfile(); 99 100 // Use the application context to get the device profile, as in multiwindow-mode, the 101 // confirmation activity might be rotated. 102 mDeviceProfile = mIdp.getDeviceProfile(getApplicationContext()); 103 104 setContentView(R.layout.add_item_confirmation_activity); 105 mWidgetCell = findViewById(R.id.widget_cell); 106 107 if (mRequest.getRequestType() == PinItemRequest.REQUEST_TYPE_SHORTCUT) { 108 setupShortcut(); 109 } else { 110 if (!setupWidget()) { 111 // TODO: show error toast? 112 finish(); 113 } 114 } 115 116 mWidgetCell.setOnTouchListener(this); 117 mWidgetCell.setOnLongClickListener(this); 118 119 // savedInstanceState is null when the activity is created the first time (i.e., avoids 120 // duplicate logging during rotation) 121 if (savedInstanceState == null) { 122 logCommand(Action.Command.ENTRY); 123 } 124 } 125 126 @Override 127 public boolean onTouch(View view, MotionEvent motionEvent) { 128 mLastTouchPos.set(motionEvent.getX(), motionEvent.getY()); 129 return false; 130 } 131 132 @Override 133 public boolean onLongClick(View view) { 134 // Find the position of the preview relative to the touch location. 135 WidgetImageView img = mWidgetCell.getWidgetView(); 136 137 // If the ImageView doesn't have a drawable yet, the widget preview hasn't been loaded and 138 // we abort the drag. 139 if (img.getBitmap() == null) { 140 return false; 141 } 142 143 Rect bounds = img.getBitmapBounds(); 144 bounds.offset(img.getLeft() - (int) mLastTouchPos.x, img.getTop() - (int) mLastTouchPos.y); 145 146 // Start home and pass the draw request params 147 PinItemDragListener listener = new PinItemDragListener(mRequest, bounds, 148 img.getBitmap().getWidth(), img.getWidth()); 149 Intent homeIntent = new Intent(Intent.ACTION_MAIN) 150 .addCategory(Intent.CATEGORY_HOME) 151 .setPackage(getPackageName()) 152 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK) 153 .putExtra(PinItemDragListener.EXTRA_PIN_ITEM_DRAG_LISTENER, listener); 154 155 if (!getResources().getBoolean(R.bool.allow_rotation) && 156 !Utilities.isAllowRotationPrefEnabled(this) && 157 (getResources().getConfiguration().orientation == 158 Configuration.ORIENTATION_LANDSCAPE && !isInMultiWindowMode())) { 159 // If we are starting the drag in landscape even though home is locked in portrait, 160 // restart the home activity to temporarily allow rotation. 161 homeIntent.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TASK); 162 } 163 164 startActivity(homeIntent, 165 ActivityOptions.makeCustomAnimation(this, 0, android.R.anim.fade_out).toBundle()); 166 167 // Start a system drag and drop. We use a transparent bitmap as preview for system drag 168 // as the preview is handled internally by launcher. 169 ClipDescription description = new ClipDescription("", new String[]{listener.getMimeType()}); 170 ClipData data = new ClipData(description, new ClipData.Item("")); 171 view.startDragAndDrop(data, new DragShadowBuilder(view) { 172 173 @Override 174 public void onDrawShadow(Canvas canvas) { } 175 176 @Override 177 public void onProvideShadowMetrics(Point outShadowSize, Point outShadowTouchPoint) { 178 outShadowSize.set(SHADOW_SIZE, SHADOW_SIZE); 179 outShadowTouchPoint.set(SHADOW_SIZE / 2, SHADOW_SIZE / 2); 180 } 181 }, null, View.DRAG_FLAG_GLOBAL); 182 return false; 183 } 184 185 private void setupShortcut() { 186 PinShortcutRequestActivityInfo shortcutInfo = 187 new PinShortcutRequestActivityInfo(mRequest, this); 188 WidgetItem item = new WidgetItem(shortcutInfo); 189 mWidgetCell.getWidgetView().setTag(new PendingAddShortcutInfo(shortcutInfo)); 190 mWidgetCell.applyFromCellItem(item, mApp.getWidgetCache()); 191 mWidgetCell.ensurePreview(); 192 } 193 194 private boolean setupWidget() { 195 LauncherAppWidgetProviderInfo widgetInfo = LauncherAppWidgetProviderInfo 196 .fromProviderInfo(this, mRequest.getAppWidgetProviderInfo(this)); 197 if (widgetInfo.minSpanX > mIdp.numColumns || widgetInfo.minSpanY > mIdp.numRows) { 198 // Cannot add widget 199 return false; 200 } 201 mWidgetCell.setPreview(PinItemDragListener.getPreview(mRequest)); 202 203 mAppWidgetManager = AppWidgetManagerCompat.getInstance(this); 204 mAppWidgetHost = new AppWidgetHost(this, Launcher.APPWIDGET_HOST_ID); 205 206 mPendingWidgetInfo = new PendingAddWidgetInfo(widgetInfo); 207 mPendingWidgetInfo.spanX = Math.min(mIdp.numColumns, widgetInfo.spanX); 208 mPendingWidgetInfo.spanY = Math.min(mIdp.numRows, widgetInfo.spanY); 209 mWidgetOptions = WidgetHostViewLoader.getDefaultOptionsForWidget(this, mPendingWidgetInfo); 210 211 WidgetItem item = new WidgetItem(widgetInfo, getPackageManager(), mIdp); 212 mWidgetCell.getWidgetView().setTag(mPendingWidgetInfo); 213 mWidgetCell.applyFromCellItem(item, mApp.getWidgetCache()); 214 mWidgetCell.ensurePreview(); 215 return true; 216 } 217 218 /** 219 * Called when the cancel button is clicked. 220 */ 221 public void onCancelClick(View v) { 222 logCommand(Action.Command.CANCEL); 223 finish(); 224 } 225 226 /** 227 * Called when place-automatically button is clicked. 228 */ 229 public void onPlaceAutomaticallyClick(View v) { 230 if (mRequest.getRequestType() == PinItemRequest.REQUEST_TYPE_SHORTCUT) { 231 InstallShortcutReceiver.queueShortcut( 232 new ShortcutInfoCompat(mRequest.getShortcutInfo()), this); 233 logCommand(Action.Command.CONFIRM); 234 mRequest.accept(); 235 finish(); 236 return; 237 } 238 239 mPendingBindWidgetId = mAppWidgetHost.allocateAppWidgetId(); 240 boolean success = mAppWidgetManager.bindAppWidgetIdIfAllowed( 241 mPendingBindWidgetId, mRequest.getAppWidgetProviderInfo(this), mWidgetOptions); 242 if (success) { 243 acceptWidget(mPendingBindWidgetId); 244 return; 245 } 246 247 // request bind widget 248 Intent intent = new Intent(AppWidgetManager.ACTION_APPWIDGET_BIND); 249 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingBindWidgetId); 250 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER, 251 mPendingWidgetInfo.componentName); 252 intent.putExtra(AppWidgetManager.EXTRA_APPWIDGET_PROVIDER_PROFILE, 253 mRequest.getAppWidgetProviderInfo(this).getProfile()); 254 startActivityForResult(intent, REQUEST_BIND_APPWIDGET); 255 } 256 257 private void acceptWidget(int widgetId) { 258 InstallShortcutReceiver.queueWidget(mRequest.getAppWidgetProviderInfo(this), widgetId, this); 259 mWidgetOptions.putInt(AppWidgetManager.EXTRA_APPWIDGET_ID, widgetId); 260 mRequest.accept(mWidgetOptions); 261 logCommand(Action.Command.CONFIRM); 262 finish(); 263 } 264 265 @Override 266 public void onBackPressed() { 267 logCommand(Action.Command.BACK); 268 super.onBackPressed(); 269 } 270 271 @Override 272 protected void onActivityResult(int requestCode, int resultCode, Intent data) { 273 if (requestCode == REQUEST_BIND_APPWIDGET) { 274 int widgetId = data != null 275 ? data.getIntExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, mPendingBindWidgetId) 276 : mPendingBindWidgetId; 277 if (resultCode == RESULT_OK) { 278 acceptWidget(widgetId); 279 } else { 280 // Simply wait it out. 281 mAppWidgetHost.deleteAppWidgetId(widgetId); 282 mPendingBindWidgetId = -1; 283 } 284 return; 285 } 286 super.onActivityResult(requestCode, resultCode, data); 287 } 288 289 @Override 290 protected void onSaveInstanceState(Bundle outState) { 291 super.onSaveInstanceState(outState); 292 outState.putInt(STATE_EXTRA_WIDGET_ID, mPendingBindWidgetId); 293 } 294 295 @Override 296 protected void onRestoreInstanceState(Bundle savedInstanceState) { 297 super.onRestoreInstanceState(savedInstanceState); 298 mPendingBindWidgetId = savedInstanceState 299 .getInt(STATE_EXTRA_WIDGET_ID, mPendingBindWidgetId); 300 } 301 302 private void logCommand(int command) { 303 getUserEventDispatcher().dispatchUserEvent(newLauncherEvent( 304 newCommandAction(command), 305 newItemTarget(mWidgetCell.getWidgetView()), 306 newContainerTarget(ContainerType.PINITEM)), null); 307 } 308} 309