blob: f78cde5b8b007e99a4764ad29bcdca8b296744b1 [file] [log] [blame]
Winson Chung4c98d922011-05-31 16:50:48 -07001/*
2 * Copyright (C) 2011 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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung4c98d922011-05-31 16:50:48 -070018
Sunny Goyald5bd67d2016-03-11 01:10:19 -080019import android.content.ActivityNotFoundException;
Winson Chung4c98d922011-05-31 16:50:48 -070020import android.content.ComponentName;
21import android.content.Context;
Tonye3c59252017-05-02 21:32:27 -070022import android.graphics.Rect;
23import android.os.Bundle;
Tony Wickhamdf238372016-05-17 12:19:07 -070024import android.provider.Settings;
Winson Chung4c98d922011-05-31 16:50:48 -070025import android.util.AttributeSet;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080026import android.util.Log;
27import android.widget.Toast;
28
29import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal1f3f07d2017-02-10 16:52:16 -080030import com.android.launcher3.util.Themes;
Winson Chung4c98d922011-05-31 16:50:48 -070031
Tony Wickham734dfbe2015-09-16 16:22:36 -070032public class InfoDropTarget extends UninstallDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070033
Sunny Goyald5bd67d2016-03-11 01:10:19 -080034 private static final String TAG = "InfoDropTarget";
35
Winson Chung4c98d922011-05-31 16:50:48 -070036 public InfoDropTarget(Context context, AttributeSet attrs) {
37 this(context, attrs, 0);
38 }
39
40 public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
41 super(context, attrs, defStyle);
42 }
43
44 @Override
Sunny Goyalda1dfa32017-04-26 22:34:49 -070045 protected void setupUi() {
Winson Chung4c98d922011-05-31 16:50:48 -070046 // Get the hover color
Sunny Goyal1f3f07d2017-02-10 16:52:16 -080047 mHoverColor = Themes.getColorAccent(getContext());
Sunny Goyalda1dfa32017-04-26 22:34:49 -070048 setDrawable(R.drawable.ic_info_shadow);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049 }
50
Sunny Goyald5bd67d2016-03-11 01:10:19 -080051 @Override
Sunny Goyal3dce5f32017-10-05 11:40:05 -070052 protected ComponentName performDropAction(DragObject d) {
53 return performDropAction(mLauncher, d.dragInfo, null, null);
Sunny Goyald5bd67d2016-03-11 01:10:19 -080054 }
55
Tony Wickham734dfbe2015-09-16 16:22:36 -070056 /**
57 * @return Whether the activity was started.
58 */
Sunny Goyald5bd67d2016-03-11 01:10:19 -080059 public static boolean startDetailsActivityForInfo(
Sunny Goyal3dce5f32017-10-05 11:40:05 -070060 ItemInfo info, Launcher launcher, Rect sourceBounds, Bundle opts) {
61 return performDropAction(launcher, info, sourceBounds, opts) != null;
Tonye3c59252017-05-02 21:32:27 -070062 }
63
Sunny Goyal3dce5f32017-10-05 11:40:05 -070064 /**
65 * Performs the drop action and returns the target component for the dragObject or null if
66 * the action was not performed.
67 */
68 private static ComponentName performDropAction(Context context, ItemInfo info,
69 Rect sourceBounds, Bundle opts) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -070070 if (info instanceof PromiseAppInfo) {
71 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
Sunny Goyal3dce5f32017-10-05 11:40:05 -070072 context.startActivity(promiseAppInfo.getMarketIntent());
73 return null;
Mario Bertschler08ffaae2017-03-20 11:30:27 -070074 }
Winson Chung4c98d922011-05-31 16:50:48 -070075 ComponentName componentName = null;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080076 if (info instanceof AppInfo) {
77 componentName = ((AppInfo) info).componentName;
78 } else if (info instanceof ShortcutInfo) {
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -070079 componentName = info.getTargetComponent();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080080 } else if (info instanceof PendingAddItemInfo) {
81 componentName = ((PendingAddItemInfo) info).componentName;
Tony Wickhamce86e192015-09-18 16:06:55 -070082 } else if (info instanceof LauncherAppWidgetInfo) {
83 componentName = ((LauncherAppWidgetInfo) info).providerName;
Winson Chung4c98d922011-05-31 16:50:48 -070084 }
85 if (componentName != null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080086 try {
Sunny Goyal3dce5f32017-10-05 11:40:05 -070087 LauncherAppsCompat.getInstance(context)
Tonye3c59252017-05-02 21:32:27 -070088 .showAppDetailsForProfile(componentName, info.user, sourceBounds, opts);
Sunny Goyal3dce5f32017-10-05 11:40:05 -070089 return componentName;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080090 } catch (SecurityException | ActivityNotFoundException e) {
Sunny Goyal3dce5f32017-10-05 11:40:05 -070091 Toast.makeText(context, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
Sunny Goyald5bd67d2016-03-11 01:10:19 -080092 Log.e(TAG, "Unable to launch settings", e);
93 }
Winson Chung4c98d922011-05-31 16:50:48 -070094 }
Sunny Goyal3dce5f32017-10-05 11:40:05 -070095 return null;
Winson Chung4c98d922011-05-31 16:50:48 -070096 }
97
98 @Override
Sunny Goyal1ce9c472017-10-03 16:17:32 -070099 protected boolean supportsDrop(ItemInfo info) {
100 return supportsDrop(getContext(), info);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700101 }
102
Sunny Goyal8ad02b82016-12-29 13:31:43 -0800103 public static boolean supportsDrop(Context context, ItemInfo info) {
Tony Wickhamdf238372016-05-17 12:19:07 -0700104 // Only show the App Info drop target if developer settings are enabled.
Sunny Goyal8ad02b82016-12-29 13:31:43 -0800105 boolean developmentSettingsEnabled = Settings.Global.getInt(context.getContentResolver(),
Tony Wickhamdf238372016-05-17 12:19:07 -0700106 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1;
Sunny Goyale6e72002017-01-12 16:55:36 -0800107 if (!developmentSettingsEnabled) {
108 return false;
109 }
110 return info.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT &&
111 (info instanceof AppInfo ||
112 (info instanceof ShortcutInfo && !((ShortcutInfo) info).isPromise()) ||
113 (info instanceof LauncherAppWidgetInfo &&
114 ((LauncherAppWidgetInfo) info).restoreStatus == 0) ||
115 info instanceof PendingAddItemInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700116 }
Winson Chung4c98d922011-05-31 16:50:48 -0700117}