| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.widget; |
| 18 |
|
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.util.SparseIntArray; |
| 22 |
import android.view.View; |
| 23 |
import android.view.ViewGroup; |
| 24 |
import android.widget.RemoteViews.ColorResources; |
| 25 |
import android.widget.RemoteViews.InteractionHandler; |
| 26 |
import android.widget.RemoteViews.RemoteCollectionItems; |
| 27 |
|
| 28 |
import com.android.internal.R; |
| 29 |
|
| 30 |
import java.util.stream.IntStream; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
class RemoteCollectionItemsAdapter extends BaseAdapter { |
| 38 |
|
| 39 |
private final int mViewTypeCount; |
| 40 |
|
| 41 |
private RemoteCollectionItems mItems; |
| 42 |
private InteractionHandler mInteractionHandler; |
| 43 |
private ColorResources mColorResources; |
| 44 |
|
| 45 |
private SparseIntArray mLayoutIdToViewType; |
| 46 |
|
| 47 |
RemoteCollectionItemsAdapter( |
| 48 |
@NonNull RemoteCollectionItems items, |
| 49 |
@NonNull InteractionHandler interactionHandler, |
| 50 |
@NonNull ColorResources colorResources) { |
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
mViewTypeCount = items.getViewTypeCount(); |
| 56 |
|
| 57 |
mItems = items; |
| 58 |
mInteractionHandler = interactionHandler; |
| 59 |
mColorResources = colorResources; |
| 60 |
|
| 61 |
initLayoutIdToViewType(); |
| 62 |
} |
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
void setData( |
| 70 |
@NonNull RemoteCollectionItems items, |
| 71 |
@NonNull InteractionHandler interactionHandler, |
| 72 |
@NonNull ColorResources colorResources) { |
| 73 |
if (mViewTypeCount < items.getViewTypeCount()) { |
| 74 |
throw new IllegalArgumentException( |
| 75 |
"RemoteCollectionItemsAdapter cannot increase view type count after creation"); |
| 76 |
} |
| 77 |
|
| 78 |
mItems = items; |
| 79 |
mInteractionHandler = interactionHandler; |
| 80 |
mColorResources = colorResources; |
| 81 |
|
| 82 |
initLayoutIdToViewType(); |
| 83 |
|
| 84 |
notifyDataSetChanged(); |
| 85 |
} |
| 86 |
|
| 87 |
private void initLayoutIdToViewType() { |
| 88 |
SparseIntArray previousLayoutIdToViewType = mLayoutIdToViewType; |
| 89 |
mLayoutIdToViewType = new SparseIntArray(mViewTypeCount); |
| 90 |
|
| 91 |
int[] layoutIds = IntStream.range(0, mItems.getItemCount()) |
| 92 |
.map(position -> mItems.getItemView(position).getLayoutId()) |
| 93 |
.distinct() |
| 94 |
.toArray(); |
| 95 |
if (layoutIds.length > mViewTypeCount) { |
| 96 |
throw new IllegalArgumentException( |
| 97 |
"Collection items uses " + layoutIds.length + " distinct layouts, which is " |
| 98 |
+ "more than view type count of " + mViewTypeCount); |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
boolean[] processedLayoutIdIndices = new boolean[layoutIds.length]; |
| 103 |
|
| 104 |
boolean[] assignedViewTypes = new boolean[mViewTypeCount]; |
| 105 |
|
| 106 |
if (previousLayoutIdToViewType != null) { |
| 107 |
for (int i = 0; i < layoutIds.length; i++) { |
| 108 |
int layoutId = layoutIds[i]; |
| 109 |
|
| 110 |
|
| 111 |
int previousViewType = previousLayoutIdToViewType.get(layoutId, -1); |
| 112 |
|
| 113 |
if (previousViewType < 0) continue; |
| 114 |
|
| 115 |
mLayoutIdToViewType.put(layoutId, previousViewType); |
| 116 |
processedLayoutIdIndices[i] = true; |
| 117 |
assignedViewTypes[previousViewType] = true; |
| 118 |
} |
| 119 |
} |
| 120 |
|
| 121 |
int lastViewType = -1; |
| 122 |
for (int i = 0; i < layoutIds.length; i++) { |
| 123 |
|
| 124 |
if (processedLayoutIdIndices[i]) continue; |
| 125 |
|
| 126 |
int layoutId = layoutIds[i]; |
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
int viewType = IntStream.range(lastViewType + 1, layoutIds.length) |
| 131 |
.filter(type -> !assignedViewTypes[type]) |
| 132 |
.findFirst() |
| 133 |
.orElseThrow( |
| 134 |
() -> new IllegalStateException( |
| 135 |
"RemoteCollectionItems has more distinct layout ids than its " |
| 136 |
+ "view type count")); |
| 137 |
mLayoutIdToViewType.put(layoutId, viewType); |
| 138 |
processedLayoutIdIndices[i] = true; |
| 139 |
assignedViewTypes[viewType] = true; |
| 140 |
lastViewType = viewType; |
| 141 |
} |
| 142 |
} |
| 143 |
|
| 144 |
@Override |
| 145 |
public int getCount() { |
| 146 |
return mItems.getItemCount(); |
| 147 |
} |
| 148 |
|
| 149 |
@Override |
| 150 |
public RemoteViews getItem(int position) { |
| 151 |
return mItems.getItemView(position); |
| 152 |
} |
| 153 |
|
| 154 |
@Override |
| 155 |
public long getItemId(int position) { |
| 156 |
return mItems.getItemId(position); |
| 157 |
} |
| 158 |
|
| 159 |
@Override |
| 160 |
public int getItemViewType(int position) { |
| 161 |
return mLayoutIdToViewType.get(mItems.getItemView(position).getLayoutId()); |
| 162 |
} |
| 163 |
|
| 164 |
@Override |
| 165 |
public int getViewTypeCount() { |
| 166 |
return mViewTypeCount; |
| 167 |
} |
| 168 |
|
| 169 |
@Override |
| 170 |
public boolean hasStableIds() { |
| 171 |
return mItems.hasStableIds(); |
| 172 |
} |
| 173 |
|
| 174 |
@Nullable |
| 175 |
@Override |
| 176 |
public View getView(int position, @Nullable View convertView, @NonNull ViewGroup parent) { |
| 177 |
if (position >= getCount()) return null; |
| 178 |
|
| 179 |
RemoteViews item = mItems.getItemView(position); |
| 180 |
item.addFlags(RemoteViews.FLAG_WIDGET_IS_COLLECTION_CHILD); |
| 181 |
View reapplyView = getViewToReapply(convertView, item); |
| 182 |
|
| 183 |
|
| 184 |
if (reapplyView != null) { |
| 185 |
try { |
| 186 |
item.reapply( |
| 187 |
parent.getContext(), |
| 188 |
reapplyView, |
| 189 |
mInteractionHandler, |
| 190 |
null , |
| 191 |
mColorResources); |
| 192 |
return reapplyView; |
| 193 |
} catch (RuntimeException e) { |
| 194 |
|
| 195 |
|
| 196 |
} |
| 197 |
} |
| 198 |
|
| 199 |
return item.apply( |
| 200 |
parent.getContext(), |
| 201 |
parent, |
| 202 |
mInteractionHandler, |
| 203 |
null , |
| 204 |
mColorResources); |
| 205 |
} |
| 206 |
|
| 207 |
|
| 208 |
@Nullable |
| 209 |
private static View getViewToReapply(@Nullable View convertView, @NonNull RemoteViews item) { |
| 210 |
if (convertView == null) return null; |
| 211 |
|
| 212 |
Object layoutIdTag = convertView.getTag(R.id.widget_frame); |
| 213 |
if (!(layoutIdTag instanceof Integer)) return null; |
| 214 |
|
| 215 |
return item.getLayoutId() == (Integer) layoutIdTag ? convertView : null; |
| 216 |
} |
| 217 |
} |