blob: 35edaf46681946286fcda7282a41d2b7e81b4e19 [file] [log] [blame]
Sunny Goyal0b0847b2018-03-14 12:30:11 -07001/*
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.launcher3;
18
19import android.app.ActivityOptions;
20import android.content.ActivityNotFoundException;
21import android.content.Context;
22import android.content.ContextWrapper;
23import android.content.Intent;
24import android.graphics.Rect;
25import android.os.Bundle;
26import android.os.Process;
27import android.os.StrictMode;
28import android.os.UserHandle;
29import android.util.Log;
30import android.view.ActionMode;
31import android.view.View;
32import android.widget.Toast;
33
34import com.android.launcher3.LauncherSettings.Favorites;
35import com.android.launcher3.badge.BadgeInfo;
36import com.android.launcher3.compat.LauncherAppsCompat;
37import com.android.launcher3.shortcuts.DeepShortcutManager;
38import com.android.launcher3.views.BaseDragLayer;
39
40/**
41 * Extension of BaseActivity allowing support for drag-n-drop
42 */
43public abstract class BaseDraggingActivity extends BaseActivity {
44
45 private static final String TAG = "BaseDraggingActivity";
46
47 // The Intent extra that defines whether to ignore the launch animation
48 private static final String INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION =
49 "com.android.launcher3.intent.extra.shortcut.INGORE_LAUNCH_ANIMATION";
50
51 // When starting an action mode, setting this tag will cause the action mode to be cancelled
52 // automatically when user interacts with the launcher.
53 public static final Object AUTO_CANCEL_ACTION_MODE = new Object();
54
55 private ActionMode mCurrentActionMode;
56 protected boolean mIsSafeModeEnabled;
57
58 @Override
59 protected void onCreate(Bundle savedInstanceState) {
60 super.onCreate(savedInstanceState);
61 mIsSafeModeEnabled = getPackageManager().isSafeMode();
62 }
63
64 @Override
65 public void onActionModeStarted(ActionMode mode) {
66 super.onActionModeStarted(mode);
67 mCurrentActionMode = mode;
68 }
69
70 @Override
71 public void onActionModeFinished(ActionMode mode) {
72 super.onActionModeFinished(mode);
73 mCurrentActionMode = null;
74 }
75
76 public boolean finishAutoCancelActionMode() {
77 if (mCurrentActionMode != null && AUTO_CANCEL_ACTION_MODE == mCurrentActionMode.getTag()) {
78 mCurrentActionMode.finish();
79 return true;
80 }
81 return false;
82 }
83
84 public abstract BaseDragLayer getDragLayer();
85
86 public abstract <T extends View> T getOverviewPanel();
87
88 public abstract BadgeInfo getBadgeInfoForItem(ItemInfo info);
89
90 public abstract void invalidateParent(ItemInfo info);
91
92 public static BaseDraggingActivity fromContext(Context context) {
93 if (context instanceof BaseDraggingActivity) {
94 return (BaseDraggingActivity) context;
95 }
96 return ((BaseDraggingActivity) ((ContextWrapper) context).getBaseContext());
97 }
98
99 public Rect getViewBounds(View v) {
100 int[] pos = new int[2];
101 v.getLocationOnScreen(pos);
102 return new Rect(pos[0], pos[1], pos[0] + v.getWidth(), pos[1] + v.getHeight());
103 }
104
105 public final Bundle getActivityLaunchOptionsAsBundle(View v, boolean useDefaultLaunchOptions) {
106 ActivityOptions activityOptions = getActivityLaunchOptions(v, useDefaultLaunchOptions);
107 return activityOptions == null ? null : activityOptions.toBundle();
108 }
109
110 public abstract ActivityOptions getActivityLaunchOptions(
111 View v, boolean useDefaultLaunchOptions);
112
113 public boolean startActivitySafely(View v, Intent intent, ItemInfo item) {
114 if (mIsSafeModeEnabled && !Utilities.isSystemApp(this, intent)) {
115 Toast.makeText(this, R.string.safemode_shortcut_error, Toast.LENGTH_SHORT).show();
116 return false;
117 }
118
119 // Only launch using the new animation if the shortcut has not opted out (this is a
120 // private contract between launcher and may be ignored in the future).
121 boolean useLaunchAnimation = (v != null) &&
122 !intent.hasExtra(INTENT_EXTRA_IGNORE_LAUNCH_ANIMATION);
123 Bundle optsBundle = useLaunchAnimation
124 ? getActivityLaunchOptionsAsBundle(v, isInMultiWindowModeCompat())
125 : null;
126
127 UserHandle user = item == null ? null : item.user;
128
129 // Prepare intent
130 intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
131 if (v != null) {
132 intent.setSourceBounds(getViewBounds(v));
133 }
134 try {
135 boolean isShortcut = Utilities.ATLEAST_MARSHMALLOW
136 && (item instanceof ShortcutInfo)
137 && (item.itemType == Favorites.ITEM_TYPE_SHORTCUT
138 || item.itemType == Favorites.ITEM_TYPE_DEEP_SHORTCUT)
139 && !((ShortcutInfo) item).isPromise();
140 if (isShortcut) {
141 // Shortcuts need some special checks due to legacy reasons.
142 startShortcutIntentSafely(intent, optsBundle, item);
143 } else if (user == null || user.equals(Process.myUserHandle())) {
144 // Could be launching some bookkeeping activity
145 startActivity(intent, optsBundle);
146 } else {
147 LauncherAppsCompat.getInstance(this).startActivityForProfile(
148 intent.getComponent(), user, intent.getSourceBounds(), optsBundle);
149 }
150 getUserEventDispatcher().logAppLaunch(v, intent);
151 return true;
152 } catch (ActivityNotFoundException|SecurityException e) {
153 Toast.makeText(this, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
154 Log.e(TAG, "Unable to launch. tag=" + item + " intent=" + intent, e);
155 }
156 return false;
157 }
158
159 private void startShortcutIntentSafely(Intent intent, Bundle optsBundle, ItemInfo info) {
160 try {
161 StrictMode.VmPolicy oldPolicy = StrictMode.getVmPolicy();
162 try {
163 // Temporarily disable deathPenalty on all default checks. For eg, shortcuts
164 // containing file Uri's would cause a crash as penaltyDeathOnFileUriExposure
165 // is enabled by default on NYC.
166 StrictMode.setVmPolicy(new StrictMode.VmPolicy.Builder().detectAll()
167 .penaltyLog().build());
168
169 if (info.itemType == LauncherSettings.Favorites.ITEM_TYPE_DEEP_SHORTCUT) {
170 String id = ((ShortcutInfo) info).getDeepShortcutId();
171 String packageName = intent.getPackage();
172 DeepShortcutManager.getInstance(this).startShortcut(
173 packageName, id, intent.getSourceBounds(), optsBundle, info.user);
174 } else {
175 // Could be launching some bookkeeping activity
176 startActivity(intent, optsBundle);
177 }
178 } finally {
179 StrictMode.setVmPolicy(oldPolicy);
180 }
181 } catch (SecurityException e) {
182 if (!onErrorStartingShortcut(intent, info)) {
183 throw e;
184 }
185 }
186 }
187
188 protected boolean onErrorStartingShortcut(Intent intent, ItemInfo info) {
189 return false;
190 }
191}