blob: f919dd052cac2b0d54c122715afd2960c40eae74 [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 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) {
Tonye3c59252017-05-02 21:32:27 -070063 return startDetailsActivityForInfo(info, launcher, callback, null, null);
64 }
65
66 public static boolean startDetailsActivityForInfo(ItemInfo info, Launcher launcher,
67 DropTargetResultCallback callback, Rect sourceBounds, Bundle opts) {
Mario Bertschler08ffaae2017-03-20 11:30:27 -070068 if (info instanceof PromiseAppInfo) {
69 PromiseAppInfo promiseAppInfo = (PromiseAppInfo) info;
70 launcher.startActivity(promiseAppInfo.getMarketIntent());
71 return true;
72 }
Sunny Goyald5bd67d2016-03-11 01:10:19 -080073 boolean result = false;
Winson Chung4c98d922011-05-31 16:50:48 -070074 ComponentName componentName = null;
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080075 if (info instanceof AppInfo) {
76 componentName = ((AppInfo) info).componentName;
77 } else if (info instanceof ShortcutInfo) {
Mario Bertschler8ff9e1d2017-08-08 16:26:18 -070078 componentName = info.getTargetComponent();
Sunny Goyal71b5c0b2015-01-08 16:59:04 -080079 } else if (info instanceof PendingAddItemInfo) {
80 componentName = ((PendingAddItemInfo) info).componentName;
Tony Wickhamce86e192015-09-18 16:06:55 -070081 } else if (info instanceof LauncherAppWidgetInfo) {
82 componentName = ((LauncherAppWidgetInfo) info).providerName;
Winson Chung4c98d922011-05-31 16:50:48 -070083 }
84 if (componentName != null) {
Sunny Goyald5bd67d2016-03-11 01:10:19 -080085 try {
86 LauncherAppsCompat.getInstance(launcher)
Tonye3c59252017-05-02 21:32:27 -070087 .showAppDetailsForProfile(componentName, info.user, sourceBounds, opts);
Sunny Goyald5bd67d2016-03-11 01:10:19 -080088 result = true;
89 } catch (SecurityException | ActivityNotFoundException e) {
90 Toast.makeText(launcher, R.string.activity_not_found, Toast.LENGTH_SHORT).show();
91 Log.e(TAG, "Unable to launch settings", e);
92 }
Winson Chung4c98d922011-05-31 16:50:48 -070093 }
Tony Wickham734dfbe2015-09-16 16:22:36 -070094
Sunny Goyald5bd67d2016-03-11 01:10:19 -080095 if (callback != null) {
96 sendUninstallResult(launcher, result, componentName, info.user, callback);
97 }
98 return result;
Winson Chung4c98d922011-05-31 16:50:48 -070099 }
100
101 @Override
Sunny Goyalaa8ef112015-06-12 20:04:41 -0700102 protected boolean supportsDrop(DragSource source, ItemInfo info) {
Sunny Goyal8ad02b82016-12-29 13:31:43 -0800103 return source.supportsAppInfoDropTarget() && supportsDrop(getContext(), info);
Sunny Goyal1a70cef2015-04-22 11:29:51 -0700104 }
105
Sunny Goyal8ad02b82016-12-29 13:31:43 -0800106 public static boolean supportsDrop(Context context, ItemInfo info) {
Tony Wickhamdf238372016-05-17 12:19:07 -0700107 // Only show the App Info drop target if developer settings are enabled.
Sunny Goyal8ad02b82016-12-29 13:31:43 -0800108 boolean developmentSettingsEnabled = Settings.Global.getInt(context.getContentResolver(),
Tony Wickhamdf238372016-05-17 12:19:07 -0700109 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1;
Sunny Goyale6e72002017-01-12 16:55:36 -0800110 if (!developmentSettingsEnabled) {
111 return false;
112 }
113 return info.itemType != LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT &&
114 (info instanceof AppInfo ||
115 (info instanceof ShortcutInfo && !((ShortcutInfo) info).isPromise()) ||
116 (info instanceof LauncherAppWidgetInfo &&
117 ((LauncherAppWidgetInfo) info).restoreStatus == 0) ||
118 info instanceof PendingAddItemInfo);
Winson Chung4c98d922011-05-31 16:50:48 -0700119 }
Winson Chung4c98d922011-05-31 16:50:48 -0700120}