1 /* 2 * Copyright (C) 2010 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 java.util.List; 20 21 import android.appwidget.AppWidgetProviderInfo; 22 import android.content.ClipData; 23 import android.content.Context; 24 import android.content.DialogInterface; 25 import android.content.pm.PackageManager; 26 import android.content.pm.ResolveInfo; 27 import android.database.DataSetObserver; 28 import android.graphics.drawable.Drawable; 29 import android.view.LayoutInflater; 30 import android.view.View; 31 import android.view.ViewGroup; 32 import android.widget.ImageView; 33 import android.widget.ListAdapter; 34 import android.widget.TextView; 35 36 import com.android.launcher3.R; 37 38 39 /** 40 * We will likely flesh this out later, to handle allow external apps to place widgets, but for now, 41 * we just want to expose the action around for checking elsewhere. 42 */ 43 public class InstallWidgetReceiver { 44 public static final String ACTION_INSTALL_WIDGET = 45 "com.android.launcher3.action.INSTALL_WIDGET"; 46 public static final String ACTION_SUPPORTS_CLIPDATA_MIMETYPE = 47 "com.android.launcher3.action.SUPPORTS_CLIPDATA_MIMETYPE"; 48 49 // Currently not exposed. Put into Intent when we want to make it public. 50 // TEMP: Should we call this "EXTRA_APPWIDGET_PROVIDER"? 51 public static final String EXTRA_APPWIDGET_COMPONENT = 52 "com.android.launcher3.extra.widget.COMPONENT"; 53 public static final String EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE = 54 "com.android.launcher3.extra.widget.CONFIGURATION_DATA_MIME_TYPE"; 55 public static final String EXTRA_APPWIDGET_CONFIGURATION_DATA = 56 "com.android.launcher3.extra.widget.CONFIGURATION_DATA"; 57 58 /** 59 * A simple data class that contains per-item information that the adapter below can reference. 60 */ 61 public static class WidgetMimeTypeHandlerData { 62 public ResolveInfo resolveInfo; 63 public AppWidgetProviderInfo widgetInfo; 64 65 public WidgetMimeTypeHandlerData(ResolveInfo rInfo, AppWidgetProviderInfo wInfo) { 66 resolveInfo = rInfo; 67 widgetInfo = wInfo; 68 } 69 } 70 71 /** 72 * The ListAdapter which presents all the valid widgets that can be created for a given drop. 73 */ 74 public static class WidgetListAdapter implements ListAdapter, DialogInterface.OnClickListener { 75 private LayoutInflater mInflater; 76 private Launcher mLauncher; 77 private String mMimeType; 78 private ClipData mClipData; 79 private List<WidgetMimeTypeHandlerData> mActivities; 80 private int mTargetLayoutScreen; 81 private int[] mTargetLayoutPos; 82 83 public WidgetListAdapter(Launcher l, String mimeType, ClipData data, 84 List<WidgetMimeTypeHandlerData> list, int targetScreen, int[] targetPos) { 85 mLauncher = l; 86 mMimeType = mimeType; 87 mClipData = data; 88 mActivities = list; 89 mTargetLayoutScreen = targetScreen; 90 mTargetLayoutPos = targetPos; 91 } 92 93 @Override 94 public void registerDataSetObserver(DataSetObserver observer) { 95 } 96 97 @Override 98 public void unregisterDataSetObserver(DataSetObserver observer) { 99 } 100 101 @Override 102 public int getCount() { 103 return mActivities.size(); 104 } 105 106 @Override 107 public Object getItem(int position) { 108 return null; 109 } 110 111 @Override 112 public long getItemId(int position) { 113 return position; 114 } 115 116 @Override 117 public boolean hasStableIds() { 118 return true; 119 } 120 121 @Override 122 public View getView(int position, View convertView, ViewGroup parent) { 123 final Context context = parent.getContext(); 124 final PackageManager packageManager = context.getPackageManager(); 125 126 // Lazy-create inflater 127 if (mInflater == null) { 128 mInflater = LayoutInflater.from(context); 129 } 130 131 // Use the convert-view where possible 132 if (convertView == null) { 133 convertView = mInflater.inflate(R.layout.external_widget_drop_list_item, parent, 134 false); 135 } 136 137 final WidgetMimeTypeHandlerData data = mActivities.get(position); 138 final ResolveInfo resolveInfo = data.resolveInfo; 139 final AppWidgetProviderInfo widgetInfo = data.widgetInfo; 140 141 // Set the icon 142 Drawable d = resolveInfo.loadIcon(packageManager); 143 ImageView i = (ImageView) convertView.findViewById(R.id.provider_icon); 144 i.setImageDrawable(d); 145 146 // Set the text 147 final CharSequence component = resolveInfo.loadLabel(packageManager); 148 final int[] widgetSpan = new int[2]; 149 CellLayout.rectToCell(widgetInfo.minWidth, widgetInfo.minHeight, widgetSpan); 150 TextView t = (TextView) convertView.findViewById(R.id.provider); 151 t.setText(context.getString(R.string.external_drop_widget_pick_format, 152 component, widgetSpan[0], widgetSpan[1])); 153 154 return convertView; 155 } 156 157 @Override 158 public int getItemViewType(int position) { 159 return 0; 160 } 161 162 @Override 163 public int getViewTypeCount() { 164 return 1; 165 } 166 167 @Override 168 public boolean isEmpty() { 169 return mActivities.isEmpty(); 170 } 171 172 @Override 173 public boolean areAllItemsEnabled() { 174 return false; 175 } 176 177 @Override 178 public boolean isEnabled(int position) { 179 return true; 180 } 181 182 @Override 183 public void onClick(DialogInterface dialog, int which) { 184 final AppWidgetProviderInfo widgetInfo = mActivities.get(which).widgetInfo; 185 186 final PendingAddWidgetInfo createInfo = new PendingAddWidgetInfo(widgetInfo, mMimeType, 187 mClipData); 188 mLauncher.addAppWidgetFromDrop(createInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP, 189 mTargetLayoutScreen, null, null, mTargetLayoutPos); 190 } 191 } 192 } 193