blob: d831a3a0301befa507dfcb0d56daf3b71e306678 [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;
Winson Chung4c98d922011-05-31 16:50:48 -070028
Tony Wickham734dfbe2015-09-16 16:22:36 -070029public class InfoDropTarget extends UninstallDropTarget {
Winson Chung4c98d922011-05-31 16:50:48 -070030
Sunny Goyald5bd67d2016-03-11 01:10:19 -080031 private static final String TAG = "InfoDropTarget";
32
Winson Chung4c98d922011-05-31 16:50:48 -070033 public InfoDropTarget(Context context, AttributeSet attrs) {
34 this(context, attrs, 0);
35 }
36
37 public InfoDropTarget(Context context, AttributeSet attrs, int defStyle) {
38 super(context, attrs, defStyle);
39 }
40
41 @Override
42 protected void onFinishInflate() {
43 super.onFinishInflate();
Winson Chung4c98d922011-05-31 16:50:48 -070044 // Get the hover color
Andrew Sappersteinabef55a2016-06-19 12:49:00 -070045 mHoverColor = Utilities.getColorAccent(getContext());
Adam Cohen18bbc6a2014-06-03 21:43:24 -070046
Sunny Goyal40b626b2015-06-04 22:40:14 -070047 setDrawable(R.drawable.ic_info_launcher);
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080048 }
49
Sunny Goyald5bd67d2016-03-11 01:10:19 -080050 @Override
Sunny Goyal0f76b562016-12-13 19:37:10 -080051 public void completeDrop(DragObject d) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080052 DropTargetResultCallback callback = d.dragSource instanceof DropTargetResultCallback
53 ? (DropTargetResultCallback) d.dragSource : null;
54 startDetailsActivityForInfo(d.dragInfo, mLauncher, callback);
55 }
56
Tony Wickham734dfbe2015-09-16 16:22:36 -070057 /**
58 * @return Whether the activity was started.
59 */
Sunny Goyald5bd67d2016-03-11 01:10:19 -080060 public static boolean startDetailsActivityForInfo(
61 ItemInfo info, Launcher launcher, DropTargetResultCallback callback) {
62 boolean result = false;
Winson Chung4c98d922011-05-31 16:50:48 -070063 ComponentName componentName = null;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080064 if (info instanceof AppInfo) {
65 componentName = ((AppInfo) info).componentName;
66 } else if (info instanceof ShortcutInfo) {
67 componentName = ((ShortcutInfo) info).intent.getComponent();
68 } else if (info instanceof PendingAddItemInfo) {
69 componentName = ((PendingAddItemInfo) info).componentName;
Tony Wickhamce86e192015-09-18 16:06:55 -070070 } else if (info instanceof LauncherAppWidgetInfo) {
71 componentName = ((LauncherAppWidgetInfo) info).providerName;
Winson Chung4c98d922011-05-31 16:50:48 -070072 }
73 if (componentName != null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080074 try {
75 LauncherAppsCompat.getInstance(launcher)
76 .showAppDetailsForProfile(componentName, info.user);
77 result = true;
78 } catch (SecurityException | ActivityNotFoundException e) {
79 Toast.makeText(launcher, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
80 Log.e(TAG, "Unable to launch settings", e);
81 }
Winson Chung4c98d922011-05-31 16:50:48 -070082 }
Tony Wickham734dfbe2015-09-16 16:22:36 -070083
Sunny Goyald5bd67d2016-03-11 01:10:19 -080084 if (callback != null) {
85 sendUninstallResult(launcher, result, componentName, info.user, callback);
86 }
87 return result;
Winson Chung4c98d922011-05-31 16:50:48 -070088 }
89
90 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -070091 protected boolean supportsDrop(DragSource source, ItemInfo info) {
Sunny Goyal8ad02b82016-12-29 13:31:43 -080092 return source.supportsAppInfoDropTarget() && supportsDrop(getContext(), info);
Sunny Goyal1a70cef2015-04-22 11:29:51 -070093 }
94
Sunny Goyal8ad02b82016-12-29 13:31:43 -080095 public static boolean supportsDrop(Context context, ItemInfo info) {
Tony Wickhamdf238372016-05-17 12:19:07 -070096 // Only show the App Info drop target if developer settings are enabled.
Sunny Goyal8ad02b82016-12-29 13:31:43 -080097 boolean developmentSettingsEnabled = Settings.Global.getInt(context.getContentResolver(),
Tony Wickhamdf238372016-05-17 12:19:07 -070098 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1;
Sunny Goyale6e72002017-01-12 16:55:36 -080099 if (!developmentSettingsEnabled) {
100 return false;
101 }
102 return info.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT &&
103 (info instanceof AppInfo ||
104 (info instanceof ShortcutInfo && !((ShortcutInfo) info).isPromise()) ||
105 (info instanceof LauncherAppWidgetInfo &&
106 ((LauncherAppWidgetInfo) info).restoreStatus == 0) ||
107 info instanceof PendingAddItemInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700108 }
Winson Chung4c98d922011-05-31 16:50:48 -0700109}