blob: 0ef478074cafbcc48f02c1ec5748b3c3eb1e7d94 [file] [log] [blame]
Winson Chung55cef262010-10-28 14:14:18 -07001/*
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
Daniel Sandler325dc232013-06-05 22:57:57 -040017package com.android.launcher3;
Winson Chung55cef262010-10-28 14:14:18 -070018
Winson Chung68846fd2010-10-29 11:00:27 -070019import java.util.List;
20
21import android.appwidget.AppWidgetProviderInfo;
22import android.content.ClipData;
23import android.content.Context;
24import android.content.DialogInterface;
25import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.database.DataSetObserver;
28import android.graphics.drawable.Drawable;
29import android.view.LayoutInflater;
30import android.view.View;
31import android.view.ViewGroup;
32import android.widget.ImageView;
33import android.widget.ListAdapter;
34import android.widget.TextView;
35
Daniel Sandler325dc232013-06-05 22:57:57 -040036import com.android.launcher3.R;
Winson Chung68846fd2010-10-29 11:00:27 -070037
Winson Chung55cef262010-10-28 14:14:18 -070038
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 */
43public class InstallWidgetReceiver {
44 public static final String ACTION_INSTALL_WIDGET =
Daniel Sandler325dc232013-06-05 22:57:57 -040045 "com.android.launcher3.action.INSTALL_WIDGET";
Winson Chung68846fd2010-10-29 11:00:27 -070046 public static final String ACTION_SUPPORTS_CLIPDATA_MIMETYPE =
Daniel Sandler325dc232013-06-05 22:57:57 -040047 "com.android.launcher3.action.SUPPORTS_CLIPDATA_MIMETYPE";
Winson Chung55cef262010-10-28 14:14:18 -070048
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 =
Daniel Sandler325dc232013-06-05 22:57:57 -040052 "com.android.launcher3.extra.widget.COMPONENT";
Winson Chung68846fd2010-10-29 11:00:27 -070053 public static final String EXTRA_APPWIDGET_CONFIGURATION_DATA_MIME_TYPE =
Daniel Sandler325dc232013-06-05 22:57:57 -040054 "com.android.launcher3.extra.widget.CONFIGURATION_DATA_MIME_TYPE";
Winson Chung55cef262010-10-28 14:14:18 -070055 public static final String EXTRA_APPWIDGET_CONFIGURATION_DATA =
Daniel Sandler325dc232013-06-05 22:57:57 -040056 "com.android.launcher3.extra.widget.CONFIGURATION_DATA";
Winson Chung68846fd2010-10-29 11:00:27 -070057
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;
Winson Chung68846fd2010-10-29 11:00:27 -070080 private int mTargetLayoutScreen;
81 private int[] mTargetLayoutPos;
82
83 public WidgetListAdapter(Launcher l, String mimeType, ClipData data,
Michael Jurka414300a2013-08-27 15:42:35 +020084 List<WidgetMimeTypeHandlerData> list, int targetScreen, int[] targetPos) {
Winson Chung68846fd2010-10-29 11:00:27 -070085 mLauncher = l;
86 mMimeType = mimeType;
87 mClipData = data;
88 mActivities = list;
Winson Chung68846fd2010-10-29 11:00:27 -070089 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];
Michael Jurka414300a2013-08-27 15:42:35 +0200149 CellLayout.rectToCell(widgetInfo.minWidth, widgetInfo.minHeight, widgetSpan);
Winson Chung68846fd2010-10-29 11:00:27 -0700150 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) {
Winson Chung68846fd2010-10-29 11:00:27 -0700184 final AppWidgetProviderInfo widgetInfo = mActivities.get(which).widgetInfo;
185
186 final PendingAddWidgetInfo createInfo = new PendingAddWidgetInfo(widgetInfo, mMimeType,
Michael Jurkac9d95c52011-08-29 14:03:34 -0700187 mClipData);
Winson Chung3d503fb2011-07-13 17:25:49 -0700188 mLauncher.addAppWidgetFromDrop(createInfo, LauncherSettings.Favorites.CONTAINER_DESKTOP,
Adam Cohend41fbf52012-02-16 23:53:59 -0800189 mTargetLayoutScreen, null, null, mTargetLayoutPos);
Winson Chung68846fd2010-10-29 11:00:27 -0700190 }
191 }
Winson Chung55cef262010-10-28 14:14:18 -0700192}