1 /* 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 17 package com.android.launcher3; 18 19 import android.content.ActivityNotFoundException; 20 import android.content.ComponentName; 21 import android.content.Context; 22 import android.graphics.Rect; 23 import android.os.Bundle; 24 import android.provider.Settings; 25 import android.util.AttributeSet; 26 import android.util.Log; 27 import android.widget.Toast; 28 29 import com.android.launcher3.compat.LauncherAppsCompat; 30 import com.android.launcher3.util.Themes; 31 32 public class InfoDropTarget extends UninstallDropTarget { 33 34 private static final String TAG = "InfoDropTarget"; 35 36 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 45 protected void onFinishInflate() { 46 super.onFinishInflate(); 47 // Get the hover color 48 mHoverColor = Themes.getColorAccent(getContext()); 49 50 setDrawable(R.drawable.ic_info_launcher); 51 } 52 53 @Override 54 public void completeDrop(DragObject d) { 55 DropTargetResultCallback callback = d.dragSource instanceof DropTargetResultCallback 56 ? (DropTargetResultCallback) d.dragSource : null; 57 startDetailsActivityForInfo(d.dragInfo, mLauncher, callback); 58 } 59 60 /** 61 * @return Whether the activity was started. 62 */ 63 public static boolean startDetailsActivityForInfo( 64 ItemInfo info, Launcher launcher, DropTargetResultCallback callback) { 65 return startDetailsActivityForInfo(info, launcher, callback, null, null); 66 } 67 68 public static boolean startDetailsActivityForInfo(ItemInfo info, Launcher launcher, 69 DropTargetResultCallback callback, Rect sourceBounds, Bundle opts) { 70 boolean result = false; 71 ComponentName componentName = null; 72 if (info instanceof AppInfo) { 73 componentName = ((AppInfo) info).componentName; 74 } else if (info instanceof ShortcutInfo) { 75 componentName = ((ShortcutInfo) info).intent.getComponent(); 76 } else if (info instanceof PendingAddItemInfo) { 77 componentName = ((PendingAddItemInfo) info).componentName; 78 } else if (info instanceof LauncherAppWidgetInfo) { 79 componentName = ((LauncherAppWidgetInfo) info).providerName; 80 } 81 if (componentName != null) { 82 try { 83 LauncherAppsCompat.getInstance(launcher) 84 .showAppDetailsForProfile(componentName, info.user, sourceBounds, opts); 85 result = true; 86 } catch (SecurityException | ActivityNotFoundException e) { 87 Toast.makeText(launcher, R.string.activity_not_found, Toast.LENGTH_SHORT).show(); 88 Log.e(TAG, "Unable to launch settings", e); 89 } 90 } 91 92 if (callback != null) { 93 sendUninstallResult(launcher, result, componentName, info.user, callback); 94 } 95 return result; 96 } 97 98 @Override 99 protected boolean supportsDrop(DragSource source, ItemInfo info) { 100 return source.supportsAppInfoDropTarget() && supportsDrop(getContext(), info); 101 } 102 103 public static boolean supportsDrop(Context context, ItemInfo info) { 104 // Only show the App Info drop target if developer settings are enabled. 105 boolean developmentSettingsEnabled = Settings.Global.getInt(context.getContentResolver(), 106 Settings.Global.DEVELOPMENT_SETTINGS_ENABLED, 0) == 1; 107 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); 116 } 117 } 118