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.launcher2; 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.launcher.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.launcher.action.INSTALL_WIDGET"; 46 public static final String ACTION_SUPPORTS_CLIPDATA_MIMETYPE = 47 "com.android.launcher.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.launcher.extra.widget.COMPONENT"; 53 public static final String EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE = 54 "com.android.launcher.extra.widget.CONFIGURATION_DATA_MIME_TYPE"; 55 public static final String EXTRA_APPWIDGET_CONFIGURATION_DATA = 56 "com.android.launcher.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 CellLayout mTargetLayout; 81 private int mTargetLayoutScreen; 82 private int[] mTargetLayoutPos; 83 84 public WidgetListAdapter(Launcher l, String mimeType, ClipData data, 85 List<WidgetMimeTypeHandlerData> list, CellLayout target, 86 int targetScreen, int[] targetPos) { 87 mLauncher = l; 88 mMimeType = mimeType; 89 mClipData = data; 90 mActivities = list; 91 mTargetLayout = target; 92 mTargetLayoutScreen = targetScreen; 93 mTargetLayoutPos = targetPos; 94 } 95 96 @Override 97 public void registerDataSetObserver(DataSetObserver observer) { 98 } 99 100 @Override 101 public void unregisterDataSetObserver(DataSetObserver observer) { 102 } 103 104 @Override 105 public int getCount() { 106 return mActivities.size(); 107 } 108 109 @Override 110 public Object getItem(int position) { 111 return null; 112 } 113 114 @Override 115 public long getItemId(int position) { 116 return position; 117 } 118 119 @Override 120 public boolean hasStableIds() { 121 return true; 122 } 123 124 @Override 125 public View getView(int position, View convertView, ViewGroup parent) { 126 final Context context = parent.getContext(); 127 final PackageManager packageManager = context.getPackageManager(); 128 129 // Lazy-create inflater 130 if (mInflater == null) { 131 mInflater = LayoutInflater.from(context); 132 } 133 134 // Use the convert-view where possible 135 if (convertView == null) { 136 convertView = mInflater.inflate(R.layout.external_widget_drop_list_item, parent, 137 false); 138 } 139 140 final WidgetMimeTypeHandlerData data = mActivities.get(position); 141 final ResolveInfo resolveInfo = data.resolveInfo; 142 final AppWidgetProviderInfo widgetInfo = data.widgetInfo; 143 144 // Set the icon 145 Drawable d = resolveInfo.loadIcon(packageManager); 146 ImageView i = (ImageView) convertView.findViewById(R.id.provider_icon); 147 i.setImageDrawable(d); 148 149 // Set the text 150 final CharSequence component = resolveInfo.loadLabel(packageManager); 151 final int[] widgetSpan = new int[2]; 152 mTargetLayout.rectToCell(widgetInfo.minWidth, widgetInfo.minHeight, widgetSpan); 153 TextView t = (TextView) convertView.findViewById(R.id.provider); 154 t.setText(context.getString(R.string.external_drop_widget_pick_format, 155 component, widgetSpan[0], widgetSpan[1])); 156 157 return convertView; 158 } 159 160 @Override 161 public int getItemViewType(int position) { 162 return 0; 163 } 164 165 @Override 166 public int getViewTypeCount() { 167 return 1; 168 } 169 170 @Override 171 public boolean isEmpty() { 172 return mActivities.isEmpty(); 173 } 174 175 @Override 176 public boolean areAllItemsEnabled() { 177 return false; 178 } 179 180 @Override 181 public boolean isEnabled(int position) { 182 return true; 183 } 184 185 @Override 186 public void onClick(DialogInterface dialog, int which) { 187 final AppWidgetProviderInfo widgetInfo = mActivities.get(which).widgetInfo; 188 189 final PendingAddWidgetInfo createInfo = new PendingAddWidgetInfo(widgetInfo, mMimeType, 190 mClipData); 191 mLauncher.addAppWidgetFromDrop(createInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP, 192 mTargetLayoutScreen, null, null, mTargetLayoutPos); 193 } 194 } 195 } 196