Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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 | package com.android.launcher3.widget; |
| 17 | |
| 18 | import android.content.Context; |
| 19 | import android.content.pm.ResolveInfo; |
| 20 | import android.support.v7.widget.RecyclerView.Adapter; |
| 21 | import android.util.Log; |
| 22 | import android.view.LayoutInflater; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewGroup; |
| 25 | import android.widget.ImageView; |
| 26 | import android.widget.TextView; |
| 27 | |
| 28 | import com.android.launcher3.IconCache; |
| 29 | import com.android.launcher3.Launcher; |
| 30 | import com.android.launcher3.LauncherAppState; |
| 31 | import com.android.launcher3.LauncherAppWidgetProviderInfo; |
| 32 | import com.android.launcher3.R; |
| 33 | import com.android.launcher3.WidgetPreviewLoader; |
| 34 | import com.android.launcher3.compat.UserHandleCompat; |
| 35 | |
| 36 | import java.util.List; |
| 37 | |
| 38 | /** |
| 39 | * List view adapter for the widget tray. |
| 40 | * |
| 41 | * <p>Memory vs. Performance: |
| 42 | * The less number of types of views are inserted into a {@link RecyclerView}, the more recycling |
| 43 | * happens and less memory is consumed. {@link #getItemViewType} was not overridden as there is |
| 44 | * only a single type of view. |
| 45 | */ |
| 46 | public class WidgetsListAdapter extends Adapter<WidgetsRowViewHolder> { |
| 47 | |
| 48 | private static final String TAG = "WidgetsListAdapter"; |
| 49 | private static final boolean DEBUG = false; |
| 50 | |
| 51 | private Context mContext; |
| 52 | private Launcher mLauncher; |
| 53 | private LayoutInflater mLayoutInflater; |
| 54 | private IconCache mIconCache; |
| 55 | |
| 56 | private WidgetsModel mWidgetsModel; |
| 57 | private WidgetPreviewLoader mWidgetPreviewLoader; |
| 58 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 59 | private View.OnClickListener mIconClickListener; |
| 60 | private View.OnLongClickListener mIconLongClickListener; |
| 61 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 62 | public WidgetsListAdapter(Context context, |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 63 | View.OnClickListener iconClickListener, |
| 64 | View.OnLongClickListener iconLongClickListener, |
| 65 | Launcher launcher) { |
| 66 | mLayoutInflater = LayoutInflater.from(context); |
| 67 | mContext = context; |
| 68 | |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 69 | mIconClickListener = iconClickListener; |
| 70 | mIconLongClickListener = iconLongClickListener; |
| 71 | |
| 72 | mLauncher = launcher; |
| 73 | mIconCache = LauncherAppState.getInstance().getIconCache(); |
| 74 | } |
| 75 | |
| 76 | public void setWidgetsModel(WidgetsModel w) { |
| 77 | mWidgetsModel = w; |
| 78 | } |
| 79 | |
| 80 | @Override |
| 81 | public int getItemCount() { |
| 82 | return mWidgetsModel.getPackageSize(); |
| 83 | } |
| 84 | |
| 85 | @Override |
| 86 | public void onBindViewHolder(WidgetsRowViewHolder holder, int pos) { |
Hyunyoung Song | 4e8fb91 | 2015-04-11 15:44:32 -0700 | [diff] [blame] | 87 | List<Object> infoList = mWidgetsModel.getSortedWidgets(pos); |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 88 | |
| 89 | ViewGroup row = ((ViewGroup) holder.getContent().findViewById(R.id.widgets_cell_list)); |
| 90 | if (DEBUG) { |
| 91 | Log.d(TAG, String.format( |
Hyunyoung Song | 4e8fb91 | 2015-04-11 15:44:32 -0700 | [diff] [blame] | 92 | "onBindViewHolder [pos=%d, widget#=%d, row.getChildCount=%d]", |
| 93 | pos, infoList.size(), row.getChildCount())); |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | // Add more views. |
| 97 | // if there are too many, hide them. |
| 98 | int diff = infoList.size() - row.getChildCount(); |
| 99 | if (diff > 0) { |
| 100 | for (int i = 0; i < diff; i++) { |
| 101 | WidgetCell widget = new WidgetCell(mContext); |
| 102 | widget = (WidgetCell) mLayoutInflater.inflate( |
| 103 | R.layout.widget_cell, row, false); |
| 104 | |
| 105 | // set up touch. |
| 106 | widget.setOnClickListener(mIconClickListener); |
| 107 | widget.setOnLongClickListener(mIconLongClickListener); |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 108 | row.addView(widget); |
| 109 | } |
| 110 | } else if (diff < 0) { |
| 111 | for (int i=infoList.size() ; i < row.getChildCount(); i++) { |
| 112 | row.getChildAt(i).setVisibility(View.GONE); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | // Bind the views in the application info section. |
Hyunyoung Song | 4e8fb91 | 2015-04-11 15:44:32 -0700 | [diff] [blame] | 117 | PackageItemInfo infoOut = mWidgetsModel.getPackageItemInfo(pos); |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 118 | if (infoOut.usingLowResIcon) { |
Hyunyoung Song | 4e8fb91 | 2015-04-11 15:44:32 -0700 | [diff] [blame] | 119 | // TODO(hyunyoungs): call this in none UI thread in the same way as BubbleTextView. |
| 120 | mIconCache.getTitleAndIconForApp(infoOut.packageName, |
| 121 | UserHandleCompat.myUserHandle(), false /* useLowResIcon */, infoOut); |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 122 | } |
Hyunyoung Song | 559d90d | 2015-04-28 15:06:45 -0700 | [diff] [blame] | 123 | |
| 124 | TextView tv = ((TextView) holder.getContent().findViewById(R.id.section)); |
| 125 | tv.setText(infoOut.title); |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 126 | ImageView iv = (ImageView) holder.getContent().findViewById(R.id.section_image); |
| 127 | iv.setImageBitmap(infoOut.iconBitmap); |
| 128 | |
| 129 | // Bind the view in the widget horizontal tray region. |
| 130 | for (int i=0; i < infoList.size(); i++) { |
| 131 | WidgetCell widget = (WidgetCell) row.getChildAt(i); |
Hyunyoung Song | 4e8fb91 | 2015-04-11 15:44:32 -0700 | [diff] [blame] | 132 | if (getWidgetPreviewLoader() == null) { |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 133 | return; |
| 134 | } |
| 135 | if (infoList.get(i) instanceof LauncherAppWidgetProviderInfo) { |
| 136 | LauncherAppWidgetProviderInfo info = (LauncherAppWidgetProviderInfo) infoList.get(i); |
| 137 | PendingAddWidgetInfo pawi = new PendingAddWidgetInfo(info, null); |
| 138 | widget.setTag(pawi); |
| 139 | widget.applyFromAppWidgetProviderInfo(info, -1, mWidgetPreviewLoader); |
| 140 | } else if (infoList.get(i) instanceof ResolveInfo) { |
| 141 | ResolveInfo info = (ResolveInfo) infoList.get(i); |
| 142 | PendingAddShortcutInfo pasi = new PendingAddShortcutInfo(info.activityInfo); |
| 143 | widget.setTag(pasi); |
| 144 | widget.applyFromResolveInfo(mLauncher.getPackageManager(), info, mWidgetPreviewLoader); |
| 145 | } |
| 146 | widget.setVisibility(View.VISIBLE); |
| 147 | widget.ensurePreview(); |
| 148 | } |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 149 | } |
| 150 | |
| 151 | @Override |
| 152 | public WidgetsRowViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 153 | if (DEBUG) { |
Hyunyoung Song | 559d90d | 2015-04-28 15:06:45 -0700 | [diff] [blame] | 154 | Log.v(TAG, "\nonCreateViewHolder"); |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | ViewGroup container = (ViewGroup) mLayoutInflater.inflate( |
| 158 | R.layout.widgets_list_row_view, parent, false); |
| 159 | return new WidgetsRowViewHolder(container); |
| 160 | } |
| 161 | |
| 162 | @Override |
Hyunyoung Song | f17a1c9 | 2015-04-27 17:01:32 -0700 | [diff] [blame] | 163 | public void onViewRecycled(WidgetsRowViewHolder holder) { |
| 164 | ViewGroup row = ((ViewGroup) holder.getContent().findViewById(R.id.widgets_cell_list)); |
| 165 | |
| 166 | for (int i = 0; i < row.getChildCount(); i++) { |
| 167 | WidgetCell widget = (WidgetCell) row.getChildAt(i); |
Hyunyoung Song | 559d90d | 2015-04-28 15:06:45 -0700 | [diff] [blame] | 168 | widget.clear(); |
Hyunyoung Song | f17a1c9 | 2015-04-27 17:01:32 -0700 | [diff] [blame] | 169 | } |
| 170 | } |
| 171 | |
| 172 | @Override |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 173 | public long getItemId(int pos) { |
| 174 | return pos; |
| 175 | } |
| 176 | |
| 177 | private WidgetPreviewLoader getWidgetPreviewLoader() { |
| 178 | if (mWidgetPreviewLoader == null) { |
| 179 | mWidgetPreviewLoader = LauncherAppState.getInstance().getWidgetCache(); |
| 180 | } |
| 181 | return mWidgetPreviewLoader; |
| 182 | } |
Hyunyoung Song | 3f47144 | 2015-04-08 19:01:34 -0700 | [diff] [blame] | 183 | } |