blob: 2f61a01ed7cfa4f25796119c40e746fc7d53b713 [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;
Tony Wickhamdf238372016-05-17 12:19:07 -070022import android.provider.Settings;
Winson Chung4c98d922011-05-31 16:50:48 -070023import android.util.AttributeSet;
Sunny Goyald5bd67d2016-03-11 01:10:19 -080024import android.util.Log;
25import android.widget.Toast;
26
27import com.android.launcher3.compat.LauncherAppsCompat;
Sunny Goyal1f3f07d2017-02-10 16:52:16 -080028import com.android.launcher3.util.Themes;
Winson Chung4c98d922011-05-31 16:50:48 -070029
Tony Wickham734dfbe2015-09-16 16:22:36 -070030public class InfoDropTarget extends UninstallDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070031
Sunny Goyald5bd67d2016-03-11 01:10:19 -080032 private static final String TAG = "InfoDropTarget";
33
Winson Chung4c98d922011-05-31 16:50:48 -070034 public InfoDropTarget(Context context, AttributeSet attrs) {
35 this(context, attrs, 0);
36 }
37
38 public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
39 super(context, attrs, defStyle);
40 }
41
42 @Override
43 protected void onFinishInflate() {
44 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070045 // Get the hover color
Sunny Goyal1f3f07d2017-02-10 16:52:16 -080046 mHoverColor = Themes.getColorAccent(getContext());
Adam Cohen18bbc6a2014-06-03 21:43:24 -070047
Sunny Goyal40b626b2015-06-04 22:40:14 -070048 setDrawable(R.drawable.ic_info_launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080049 }
50
Sunny Goyald5bd67d2016-03-11 01:10:19 -080051 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -080052 public void completeDrop(DragObject d) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080053 DropTargetResultCallback callback = d.dragSource instanceof DropTargetResultCallback
54 ? (DropTargetResultCallback) d.dragSource : null;
55 startDetailsActivityForInfo(d.dragInfo, mLauncher, callback);
56 }
57
Tony Wickham734dfbe2015-09-16 16:22:36 -070058 /**
59 * @return Whether the activity was started.
60 */
Sunny Goyald5bd67d2016-03-11 01:10:19 -080061 public static boolean startDetailsActivityForInfo(
62 ItemInfo info, Launcher launcher, DropTargetResultCallback callback) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -070063 if (info instanceof PromiseAppInfo) {
64 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
65 launcher.startActivity(promiseAppInfo.getMarketIntent());
66 return true;
67 }
68
Sunny Goyald5bd67d2016-03-11 01:10:19 -080069 boolean result = false;
Winson Chung4c98d922011-05-31 16:50:48 -070070 ComponentName componentName = null;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080071 if (info instanceof AppInfo) {
72 componentName = ((AppInfo) info).componentName;
73 } else if (info instanceof ShortcutInfo) {
74 componentName = ((ShortcutInfo) info).intent.getComponent();
75 } else if (info instanceof PendingAddItemInfo) {
76 componentName = ((PendingAddItemInfo) info).componentName;
Tony Wickhamce86e192015-09-18 16:06:55 -070077 } else if (info instanceof LauncherAppWidgetInfo) {
78 componentName = ((LauncherAppWidgetInfo) info).providerName;
Winson Chung4c98d922011-05-31 16:50:48 -070079 }
80 if (componentName != null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080081 try {
82 LauncherAppsCompat.getInstance(launcher)
83 .showAppDetailsForProfile(componentName, info.user);
84 result = true;
85 } catch (SecurityException | ActivityNotFoundException e) {
86 Toast.makeText(launcher, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
87 Log.e(TAG, "Unable to launch settings", e);
88 }
Winson Chung4c98d922011-05-31 16:50:48 -070089 }
Tony Wickham734dfbe2015-09-16 16:22:36 -070090
Sunny Goyald5bd67d2016-03-11 01:10:19 -080091 if (callback != null) {
92 sendUninstallResult(launcher, result, componentName, info.user, callback);
93 }
94 return result;
Winson Chung4c98d922011-05-31 16:50:48 -070095 }
96
97 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -070098 protected boolean supportsDrop(DragSource source, ItemInfo info) {
Sunny Goyal8ad02b82016-12-29 13:31:43 -080099 return source.supportsAppInfoDropTarget() && supportsDrop(getContext(), info);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700100 }
101
Sunny Goyal8ad02b82016-12-29 13:31:43 -0800102 public static boolean supportsDrop(Context context, ItemInfo info) {
Tony Wickhamdf238372016-05-17 12:19:07 -0700103 // Only show the App Info drop target if developer settings are enabled.
Sunny Goyal8ad02b82016-12-29 13:31:43 -0800104 boolean developmentSettingsEnabled = Settings.Global.getInt(context.getContentResolver(),
Tony Wickhamdf238372016-05-17 12:19:07 -0700105 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1;
Sunny Goyale6e72002017-01-12 16:55:36 -0800106 if (!developmentSettingsEnabled) {
107 return false;
108 }
109 return info.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT &&
110 (info instanceof AppInfo ||
111 (info instanceof ShortcutInfo && !((ShortcutInfo) info).isPromise()) ||
112 (info instanceof LauncherAppWidgetInfo &&
113 ((LauncherAppWidgetInfo) info).restoreStatus == 0) ||
114 info instanceof PendingAddItemInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700115 }
Winson Chung4c98d922011-05-31 16:50:48 -0700116}