2018-03-14 12:30:11 -07:00
|
|
|
/*
|
|
|
|
|
* Copyright (C) 2018 The Android Open Source Project
|
|
|
|
|
*
|
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
|
*
|
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
*
|
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
|
* limitations under the License.
|
|
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
package com.android.launcher3;
|
|
|
|
|
|
2020-04-27 01:12:01 -07:00
|
|
|
import static com.android.launcher3.logging.StatsLogManager.LauncherEvent.LAUNCHER_APP_LAUNCH_TAP;
|
2020-08-11 12:06:49 -07:00
|
|
|
import static com.android.launcher3.util.DisplayController.DisplayHolder.CHANGE_ROTATION;
|
2020-02-03 14:22:09 -08:00
|
|
|
|
2018-03-14 12:30:11 -07:00
|
|
|
import android.app.ActivityOptions;
|
|
|
|
|
import android.content.ActivityNotFoundException;
|
|
|
|
|
import android.content.Intent;
|
2019-10-02 16:13:34 -07:00
|
|
|
import android.content.pm.LauncherApps;
|
2018-08-11 15:53:40 -07:00
|
|
|
import android.content.res.Configuration;
|
2020-05-27 17:52:03 -07:00
|
|
|
import android.graphics.Insets;
|
2020-04-28 14:17:35 -07:00
|
|
|
import android.graphics.Point;
|
2018-03-14 12:30:11 -07:00
|
|
|
import android.graphics.Rect;
|
|
|
|
|
import android.os.Bundle;
|
|
|
|
|
import android.os.Process;
|
|
|
|
|
import android.os.StrictMode;
|
|
|
|
|
import android.os.UserHandle;
|
|
|
|
|
import android.util.Log;
|
|
|
|
|
import android.view.ActionMode;
|
2020-04-28 14:17:35 -07:00
|
|
|
import android.view.Display;
|
2018-03-14 12:30:11 -07:00
|
|
|
import android.view.View;
|
2020-01-07 13:07:55 -08:00
|
|
|
import android.view.View.OnClickListener;
|
2020-05-27 17:52:03 -07:00
|
|
|
import android.view.WindowInsets.Type;
|
|
|
|
|
import android.view.WindowMetrics;
|
2018-03-14 12:30:11 -07:00
|
|
|
import android.widget.Toast;
|
|
|
|
|
|
2019-09-06 11:46:19 -07:00
|
|
|
import androidx.annotation.Nullable;
|
|
|
|
|
|
2018-03-14 12:30:11 -07:00
|
|
|
import com.android.launcher3.LauncherSettings.Favorites;
|
2020-06-14 22:39:51 -07:00
|
|
|
import com.android.launcher3.logging.InstanceId;
|
|
|
|
|
import com.android.launcher3.logging.InstanceIdSequence;
|
2020-04-06 15:11:17 -07:00
|
|
|
import com.android.launcher3.model.data.ItemInfo;
|
|
|
|
|
import com.android.launcher3.model.data.WorkspaceItemInfo;
|
2020-01-07 13:07:55 -08:00
|
|
|
import com.android.launcher3.touch.ItemClickHandler;
|
2018-05-03 16:58:41 -07:00
|
|
|
import com.android.launcher3.uioverrides.WallpaperColorInfo;
|
2020-08-11 12:06:49 -07:00
|
|
|
import com.android.launcher3.util.DisplayController;
|
|
|
|
|
import com.android.launcher3.util.DisplayController.DisplayInfoChangeListener;
|
|
|
|
|
import com.android.launcher3.util.DisplayController.Info;
|
2019-07-17 15:12:56 -07:00
|
|
|
import com.android.launcher3.util.PackageManagerHelper;
|
2019-05-01 13:15:29 -07:00
|
|
|
import com.android.launcher3.util.Themes;
|
2019-10-14 14:06:38 -07:00
|
|
|
import com.android.launcher3.util.TraceHelper;
|
2020-05-27 17:52:03 -07:00
|
|
|
import com.android.launcher3.util.WindowBounds;
|
2018-03-14 12:30:11 -07:00
|
|
|
|
|
|
|
|
/**
|
|
|
|
|
* Extension of BaseActivity allowing support for drag-n-drop
|
|
|
|
|
*/
|
2018-03-27 17:35:54 -07:00
|
|
|
public abstract class BaseDraggingActivity extends BaseActivity
|
2020-02-03 14:22:09 -08:00
|
|
|
implements WallpaperColorInfo.OnChangeListener, DisplayInfoChangeListener {
|
2018-03-14 12:30:11 -07:00
|
|
|
|
|
|
|
|
private static final String TAG = "BaseDraggingActivity";
|
|
|
|
|
|
|
|
|
|
// When starting an action mode, setting this tag will cause the action mode to be cancelled
|
|
|
|
|
// automatically when user interacts with the launcher.
|
|
|
|
|
public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
|
|
|
|
|
|
|
|
|
|
private ActionMode mCurrentActionMode;
|
|
|
|
|
protected boolean mIsSafeModeEnabled;
|
|
|
|
|
|
2019-10-14 16:30:38 -07:00
|
|
|
private Runnable mOnStartCallback;
|
2018-03-19 13:41:31 -07:00
|
|
|
|
2018-06-07 15:42:57 -07:00
|
|
|
private int mThemeRes = R.style.AppTheme;
|
2018-03-27 17:35:54 -07:00
|
|
|
|
2018-03-14 12:30:11 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onCreate(Bundle savedInstanceState) {
|
|
|
|
|
super.onCreate(savedInstanceState);
|
2019-10-14 14:06:38 -07:00
|
|
|
|
|
|
|
|
|
2020-07-09 12:21:40 -07:00
|
|
|
mIsSafeModeEnabled = TraceHelper.allowIpcs("isSafeMode",
|
2019-10-14 14:06:38 -07:00
|
|
|
() -> getPackageManager().isSafeMode());
|
2020-08-11 12:06:49 -07:00
|
|
|
DisplayController.getDefaultDisplay(this).addChangeListener(this);
|
2018-03-27 17:35:54 -07:00
|
|
|
|
|
|
|
|
// Update theme
|
2019-12-09 14:55:56 -08:00
|
|
|
WallpaperColorInfo.INSTANCE.get(this).addOnChangeListener(this);
|
2019-05-01 13:15:29 -07:00
|
|
|
int themeRes = Themes.getActivityThemeRes(this);
|
2018-03-27 17:35:54 -07:00
|
|
|
if (themeRes != mThemeRes) {
|
|
|
|
|
mThemeRes = themeRes;
|
|
|
|
|
setTheme(themeRes);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onExtractedColorsChanged(WallpaperColorInfo wallpaperColorInfo) {
|
2018-08-11 15:53:40 -07:00
|
|
|
updateTheme();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onConfigurationChanged(Configuration newConfig) {
|
|
|
|
|
super.onConfigurationChanged(newConfig);
|
|
|
|
|
updateTheme();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void updateTheme() {
|
2019-05-01 13:15:29 -07:00
|
|
|
if (mThemeRes != Themes.getActivityThemeRes(this)) {
|
2020-10-28 00:13:03 -07:00
|
|
|
recreate();
|
2018-03-27 17:35:54 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 12:30:11 -07:00
|
|
|
@Override
|
|
|
|
|
public void onActionModeStarted(ActionMode mode) {
|
|
|
|
|
super.onActionModeStarted(mode);
|
|
|
|
|
mCurrentActionMode = mode;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
public void onActionModeFinished(ActionMode mode) {
|
|
|
|
|
super.onActionModeFinished(mode);
|
|
|
|
|
mCurrentActionMode = null;
|
|
|
|
|
}
|
|
|
|
|
|
2018-07-03 15:53:39 -07:00
|
|
|
@Override
|
2018-03-14 12:30:11 -07:00
|
|
|
public boolean finishAutoCancelActionMode() {
|
|
|
|
|
if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
|
|
|
|
|
mCurrentActionMode.finish();
|
|
|
|
|
return true;
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public abstract <T extends View> T getOverviewPanel();
|
|
|
|
|
|
2018-03-19 13:41:31 -07:00
|
|
|
public abstract View getRootView();
|
|
|
|
|
|
2019-07-10 14:25:32 -07:00
|
|
|
public void returnToHomescreen() {
|
|
|
|
|
// no-op
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-14 12:30:11 -07:00
|
|
|
public Rect getViewBounds(View v) {
|
|
|
|
|
int[] pos = new int[2];
|
|
|
|
|
v.getLocationOnScreen(pos);
|
|
|
|
|
return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 18:06:23 -07:00
|
|
|
public final Bundle getActivityLaunchOptionsAsBundle(View v) {
|
|
|
|
|
ActivityOptions activityOptions = getActivityLaunchOptions(v);
|
2018-03-14 12:30:11 -07:00
|
|
|
return activityOptions == null ? null : activityOptions.toBundle();
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-16 18:06:23 -07:00
|
|
|
public abstract ActivityOptions getActivityLaunchOptions(View v);
|
2018-03-14 12:30:11 -07:00
|
|
|
|
2020-07-15 17:02:16 -07:00
|
|
|
public boolean startActivitySafely(View v, Intent intent, @Nullable ItemInfo item) {
|
2019-07-17 15:12:56 -07:00
|
|
|
if (mIsSafeModeEnabled && !PackageManagerHelper.isSystemApp(this, intent)) {
|
2018-03-14 12:30:11 -07:00
|
|
|
Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2019-01-31 12:37:41 -08:00
|
|
|
Bundle optsBundle = (v != null) ? getActivityLaunchOptionsAsBundle(v) : null;
|
2018-03-14 12:30:11 -07:00
|
|
|
UserHandle user = item == null ? null : item.user;
|
|
|
|
|
|
|
|
|
|
// Prepare intent
|
|
|
|
|
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
|
|
|
|
if (v != null) {
|
|
|
|
|
intent.setSourceBounds(getViewBounds(v));
|
|
|
|
|
}
|
|
|
|
|
try {
|
2019-03-27 16:03:06 -07:00
|
|
|
boolean isShortcut = (item instanceof WorkspaceItemInfo)
|
2018-03-14 12:30:11 -07:00
|
|
|
&& (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
|
|
|
|
|
|| item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
|
2019-03-27 16:03:06 -07:00
|
|
|
&& !((WorkspaceItemInfo) item).isPromise();
|
2018-03-14 12:30:11 -07:00
|
|
|
if (isShortcut) {
|
|
|
|
|
// Shortcuts need some special checks due to legacy reasons.
|
2020-07-15 17:02:16 -07:00
|
|
|
startShortcutIntentSafely(intent, optsBundle, item);
|
2018-03-14 12:30:11 -07:00
|
|
|
} else if (user == null || user.equals(Process.myUserHandle())) {
|
|
|
|
|
// Could be launching some bookkeeping activity
|
|
|
|
|
startActivity(intent, optsBundle);
|
|
|
|
|
} else {
|
2019-10-02 16:13:34 -07:00
|
|
|
getSystemService(LauncherApps.class).startMainActivity(
|
2018-03-14 12:30:11 -07:00
|
|
|
intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
|
|
|
|
|
}
|
2020-06-06 14:44:27 -07:00
|
|
|
if (item != null) {
|
2020-06-14 22:39:51 -07:00
|
|
|
InstanceId instanceId = new InstanceIdSequence().newInstanceId();
|
|
|
|
|
logAppLaunch(item, instanceId);
|
2020-06-06 14:44:27 -07:00
|
|
|
}
|
2018-03-14 12:30:11 -07:00
|
|
|
return true;
|
2019-08-01 15:44:55 -07:00
|
|
|
} catch (NullPointerException|ActivityNotFoundException|SecurityException e) {
|
2018-03-14 12:30:11 -07:00
|
|
|
Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
|
|
|
|
|
Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
|
|
|
|
|
}
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-06-14 22:39:51 -07:00
|
|
|
protected void logAppLaunch(ItemInfo info, InstanceId instanceId) {
|
2020-06-16 18:58:13 -07:00
|
|
|
getStatsLogManager().logger().withItemInfo(info).withInstanceId(instanceId)
|
|
|
|
|
.log(LAUNCHER_APP_LAUNCH_TAP);
|
2020-06-14 22:39:51 -07:00
|
|
|
}
|
|
|
|
|
|
2020-07-15 17:02:16 -07:00
|
|
|
private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
|
2018-03-14 12:30:11 -07:00
|
|
|
try {
|
|
|
|
|
StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
|
|
|
|
|
try {
|
|
|
|
|
// Temporarily disable deathPenalty on all default checks. For eg, shortcuts
|
|
|
|
|
// containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
|
|
|
|
|
// is enabled by default on NYC.
|
|
|
|
|
StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
|
|
|
|
|
.penaltyLog().build());
|
|
|
|
|
|
|
|
|
|
if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
|
2019-03-27 16:03:06 -07:00
|
|
|
String id = ((WorkspaceItemInfo) info).getDeepShortcutId();
|
2018-03-14 12:30:11 -07:00
|
|
|
String packageName = intent.getPackage();
|
2019-12-11 10:00:47 -08:00
|
|
|
startShortcut(packageName, id, intent.getSourceBounds(), optsBundle, info.user);
|
2018-03-14 12:30:11 -07:00
|
|
|
} else {
|
|
|
|
|
// Could be launching some bookkeeping activity
|
|
|
|
|
startActivity(intent, optsBundle);
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
StrictMode.setVmPolicy(oldPolicy);
|
|
|
|
|
}
|
|
|
|
|
} catch (SecurityException e) {
|
|
|
|
|
if (!onErrorStartingShortcut(intent, info)) {
|
|
|
|
|
throw e;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
2018-03-19 13:41:31 -07:00
|
|
|
|
|
|
|
|
@Override
|
|
|
|
|
protected void onStart() {
|
|
|
|
|
super.onStart();
|
|
|
|
|
|
|
|
|
|
if (mOnStartCallback != null) {
|
2019-10-14 16:30:38 -07:00
|
|
|
mOnStartCallback.run();
|
2018-03-19 13:41:31 -07:00
|
|
|
mOnStartCallback = null;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2018-03-27 17:35:54 -07:00
|
|
|
@Override
|
|
|
|
|
protected void onDestroy() {
|
|
|
|
|
super.onDestroy();
|
2019-12-09 14:55:56 -08:00
|
|
|
WallpaperColorInfo.INSTANCE.get(this).removeOnChangeListener(this);
|
2020-08-11 12:06:49 -07:00
|
|
|
DisplayController.getDefaultDisplay(this).removeChangeListener(this);
|
2018-03-27 17:35:54 -07:00
|
|
|
}
|
|
|
|
|
|
2019-10-14 16:30:38 -07:00
|
|
|
public void runOnceOnStart(Runnable action) {
|
|
|
|
|
mOnStartCallback = action;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void clearRunOnceOnStartCallback() {
|
|
|
|
|
mOnStartCallback = null;
|
2018-03-19 13:41:31 -07:00
|
|
|
}
|
|
|
|
|
|
2018-05-07 17:31:40 -07:00
|
|
|
protected void onDeviceProfileInitiated() {
|
|
|
|
|
if (mDeviceProfile.isVerticalBarLayout()) {
|
2019-09-06 11:46:19 -07:00
|
|
|
mDeviceProfile.updateIsSeascape(this);
|
2018-05-07 17:31:40 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-02-03 14:22:09 -08:00
|
|
|
@Override
|
|
|
|
|
public void onDisplayInfoChanged(Info info, int flags) {
|
|
|
|
|
if ((flags & CHANGE_ROTATION) != 0 && mDeviceProfile.updateIsSeascape(this)) {
|
2018-05-07 17:31:40 -07:00
|
|
|
reapplyUi();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-01-07 13:07:55 -08:00
|
|
|
public OnClickListener getItemOnClickListener() {
|
|
|
|
|
return ItemClickHandler.INSTANCE;
|
|
|
|
|
}
|
|
|
|
|
|
2018-05-07 17:31:40 -07:00
|
|
|
protected abstract void reapplyUi();
|
2020-04-28 14:17:35 -07:00
|
|
|
|
2020-05-27 17:52:03 -07:00
|
|
|
protected WindowBounds getMultiWindowDisplaySize() {
|
2020-04-28 14:17:35 -07:00
|
|
|
if (Utilities.ATLEAST_R) {
|
2020-05-27 17:52:03 -07:00
|
|
|
WindowMetrics wm = getWindowManager().getCurrentWindowMetrics();
|
|
|
|
|
|
|
|
|
|
Insets insets = wm.getWindowInsets().getInsets(Type.systemBars());
|
|
|
|
|
return new WindowBounds(wm.getBounds(),
|
|
|
|
|
new Rect(insets.left, insets.top, insets.right, insets.bottom));
|
2020-04-28 14:17:35 -07:00
|
|
|
}
|
|
|
|
|
// Note: Calls to getSize() can't rely on our cached DefaultDisplay since it can return
|
|
|
|
|
// the app window size
|
|
|
|
|
Display display = getWindowManager().getDefaultDisplay();
|
|
|
|
|
Point mwSize = new Point();
|
|
|
|
|
display.getSize(mwSize);
|
2020-05-27 17:52:03 -07:00
|
|
|
return new WindowBounds(new Rect(0, 0, mwSize.x, mwSize.y), new Rect());
|
2020-04-28 14:17:35 -07:00
|
|
|
}
|
2018-03-14 12:30:11 -07:00
|
|
|
}
|