Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 1 | /* |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 2 | * Copyright (C) 2016 The Android Open Source Project |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 3 | * |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 4 | * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file |
| 5 | * except in compliance with the License. You may obtain a copy of the License at |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 6 | * |
| 7 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | * |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 9 | * Unless required by applicable law or agreed to in writing, software distributed under the |
| 10 | * License is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY |
| 11 | * KIND, either express or implied. See the License for the specific language governing |
| 12 | * permissions and limitations under the License. |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | package com.android.systemui.qs.customize; |
| 16 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 17 | import android.content.Context; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 18 | import android.graphics.Canvas; |
| 19 | import android.graphics.drawable.ColorDrawable; |
| 20 | import android.support.v4.view.ViewCompat; |
| 21 | import android.support.v7.widget.GridLayoutManager.SpanSizeLookup; |
| 22 | import android.support.v7.widget.RecyclerView; |
| 23 | import android.support.v7.widget.RecyclerView.ItemDecoration; |
| 24 | import android.support.v7.widget.RecyclerView.State; |
| 25 | import android.support.v7.widget.RecyclerView.ViewHolder; |
| 26 | import android.support.v7.widget.helper.ItemTouchHelper; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 27 | import android.view.LayoutInflater; |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 28 | import android.view.MotionEvent; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 29 | import android.view.View; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 30 | import android.view.ViewGroup; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 31 | import android.widget.FrameLayout; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 32 | import com.android.systemui.R; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 33 | import com.android.systemui.qs.QSIconView; |
| 34 | import com.android.systemui.qs.QSTileView; |
| 35 | import com.android.systemui.qs.customize.TileAdapter.Holder; |
| 36 | import com.android.systemui.qs.customize.TileQueryHelper.TileInfo; |
| 37 | import com.android.systemui.qs.customize.TileQueryHelper.TileStateListener; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 38 | import com.android.systemui.statusbar.phone.QSTileHost; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 39 | |
| 40 | import java.util.ArrayList; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 41 | import java.util.List; |
| 42 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 43 | public class TileAdapter extends RecyclerView.Adapter<Holder> implements TileStateListener { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 44 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 45 | private static final long DRAG_LENGTH = 100; |
| 46 | private static final float DRAG_SCALE = 1.2f; |
| 47 | public static final long MOVE_DURATION = 150; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 48 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 49 | private static final int TYPE_TILE = 0; |
| 50 | private static final int TYPE_EDIT = 1; |
| 51 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 52 | private final Context mContext; |
| 53 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 54 | private final List<TileInfo> mTiles = new ArrayList<>(); |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 55 | private final ItemTouchHelper mItemTouchHelper; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 56 | private int mDividerIndex; |
| 57 | private List<String> mCurrentSpecs; |
| 58 | private List<TileInfo> mOtherTiles; |
| 59 | private List<TileInfo> mAllTiles; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 60 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 61 | private Holder mCurrentDrag; |
| 62 | |
| 63 | public TileAdapter(Context context) { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 64 | mContext = context; |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 65 | mItemTouchHelper = new ItemTouchHelper(mCallbacks); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 66 | setHasStableIds(true); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | @Override |
| 70 | public long getItemId(int position) { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 71 | return mTiles.get(position) != null ? mAllTiles.indexOf(mTiles.get(position)) : -1; |
| 72 | } |
| 73 | |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 74 | public ItemTouchHelper getItemTouchHelper() { |
| 75 | return mItemTouchHelper; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 76 | } |
| 77 | |
| 78 | public ItemDecoration getItemDecoration() { |
| 79 | return mDecoration; |
| 80 | } |
| 81 | |
| 82 | public void saveSpecs(QSTileHost host) { |
| 83 | List<String> newSpecs = new ArrayList<>(); |
| 84 | for (int i = 0; mTiles.get(i) != null; i++) { |
| 85 | newSpecs.add(mTiles.get(i).spec); |
| 86 | } |
| 87 | host.changeTiles(mCurrentSpecs, newSpecs); |
| 88 | setTileSpecs(newSpecs); |
| 89 | } |
| 90 | |
| 91 | public void setTileSpecs(List<String> currentSpecs) { |
| 92 | mCurrentSpecs = currentSpecs; |
| 93 | recalcSpecs(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 94 | } |
| 95 | |
| 96 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 97 | public void onTilesChanged(List<TileInfo> tiles) { |
| 98 | mAllTiles = tiles; |
| 99 | recalcSpecs(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 100 | } |
| 101 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 102 | private void recalcSpecs() { |
| 103 | if (mCurrentSpecs == null || mAllTiles == null) { |
| 104 | return; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 105 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 106 | mOtherTiles = new ArrayList<TileInfo>(mAllTiles); |
| 107 | mTiles.clear(); |
| 108 | for (int i = 0; i < mCurrentSpecs.size(); i++) { |
Jason Monk | cb654cb | 2016-02-25 15:43:07 -0500 | [diff] [blame] | 109 | final TileInfo tile = getAndRemoveOther(mCurrentSpecs.get(i)); |
| 110 | if (tile != null) { |
| 111 | mTiles.add(tile); |
| 112 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 113 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 114 | mTiles.add(null); |
| 115 | mTiles.addAll(mOtherTiles); |
| 116 | mDividerIndex = mTiles.indexOf(null); |
| 117 | notifyDataSetChanged(); |
| 118 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 119 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 120 | private TileInfo getAndRemoveOther(String s) { |
| 121 | for (int i = 0; i < mOtherTiles.size(); i++) { |
| 122 | if (mOtherTiles.get(i).spec.equals(s)) { |
| 123 | return mOtherTiles.remove(i); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 124 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 125 | } |
| 126 | return null; |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public int getItemViewType(int position) { |
| 131 | if (mTiles.get(position) == null) { |
| 132 | return TYPE_EDIT; |
| 133 | } |
| 134 | return TYPE_TILE; |
| 135 | } |
| 136 | |
| 137 | @Override |
| 138 | public Holder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 139 | final Context context = parent.getContext(); |
| 140 | LayoutInflater inflater = LayoutInflater.from(context); |
| 141 | if (viewType == 1) { |
| 142 | return new Holder(inflater.inflate(R.layout.qs_customize_divider, parent, false)); |
| 143 | } |
| 144 | FrameLayout frame = (FrameLayout) inflater.inflate(R.layout.qs_customize_tile_frame, parent, |
| 145 | false); |
| 146 | frame.addView(new QSTileView(context, new QSIconView(context))); |
| 147 | return new Holder(frame); |
| 148 | } |
| 149 | |
| 150 | @Override |
| 151 | public int getItemCount() { |
| 152 | return mTiles.size(); |
| 153 | } |
| 154 | |
| 155 | @Override |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 156 | public void onBindViewHolder(final Holder holder, int position) { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 157 | if (holder.getItemViewType() == TYPE_EDIT) return; |
| 158 | |
| 159 | TileInfo info = mTiles.get(position); |
| 160 | holder.mTileView.onStateChanged(info.state); |
| 161 | } |
| 162 | |
| 163 | public SpanSizeLookup getSizeLookup() { |
| 164 | return mSizeLookup; |
| 165 | } |
| 166 | |
| 167 | public class Holder extends ViewHolder { |
| 168 | private QSTileView mTileView; |
| 169 | |
| 170 | public Holder(View itemView) { |
| 171 | super(itemView); |
| 172 | if (itemView instanceof FrameLayout) { |
| 173 | mTileView = (QSTileView) ((FrameLayout) itemView).getChildAt(0); |
Jason Monk | 04fd249 | 2016-02-29 14:29:26 -0500 | [diff] [blame] | 174 | mTileView.setBackground(null); |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 175 | mTileView.getIcon().disableAnimation(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 176 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 177 | } |
| 178 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 179 | public void startDrag() { |
| 180 | itemView.animate() |
| 181 | .setDuration(DRAG_LENGTH) |
| 182 | .scaleX(DRAG_SCALE) |
| 183 | .scaleY(DRAG_SCALE); |
| 184 | mTileView.findViewById(R.id.tile_label).animate() |
| 185 | .setDuration(DRAG_LENGTH) |
| 186 | .alpha(0); |
| 187 | } |
| 188 | |
| 189 | public void stopDrag() { |
| 190 | itemView.animate() |
| 191 | .setDuration(DRAG_LENGTH) |
| 192 | .scaleX(1) |
| 193 | .scaleY(1); |
| 194 | mTileView.findViewById(R.id.tile_label).animate() |
| 195 | .setDuration(DRAG_LENGTH) |
| 196 | .alpha(1); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 197 | } |
| 198 | } |
| 199 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 200 | private final SpanSizeLookup mSizeLookup = new SpanSizeLookup() { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 201 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 202 | public int getSpanSize(int position) { |
| 203 | return getItemViewType(position) == TYPE_EDIT ? 3 : 1; |
| 204 | } |
| 205 | }; |
| 206 | |
| 207 | private final ItemDecoration mDecoration = new ItemDecoration() { |
| 208 | // TODO: Move this to resource. |
| 209 | private final ColorDrawable mDrawable = new ColorDrawable(0xff384248); |
| 210 | |
| 211 | @Override |
| 212 | public void onDraw(Canvas c, RecyclerView parent, State state) { |
| 213 | super.onDraw(c, parent, state); |
| 214 | |
| 215 | final int childCount = parent.getChildCount(); |
| 216 | final int width = parent.getWidth(); |
| 217 | final int bottom = parent.getBottom(); |
| 218 | for (int i = 0; i < childCount; i++) { |
| 219 | final View child = parent.getChildAt(i); |
| 220 | final ViewHolder holder = parent.getChildViewHolder(child); |
| 221 | if (holder.getAdapterPosition() < mDividerIndex) { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 222 | continue; |
| 223 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 224 | |
| 225 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child |
| 226 | .getLayoutParams(); |
| 227 | final int top = child.getTop() + params.topMargin + |
| 228 | Math.round(ViewCompat.getTranslationY(child)); |
| 229 | // Draw full width, in case there aren't tiles all the way across. |
| 230 | mDrawable.setBounds(0, top, width, bottom); |
| 231 | mDrawable.draw(c); |
| 232 | break; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 233 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 234 | } |
| 235 | }; |
| 236 | |
| 237 | private final ItemTouchHelper.Callback mCallbacks = new ItemTouchHelper.Callback() { |
| 238 | |
| 239 | @Override |
| 240 | public boolean isLongPressDragEnabled() { |
| 241 | return true; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 242 | } |
| 243 | |
| 244 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 245 | public boolean isItemViewSwipeEnabled() { |
| 246 | return false; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 247 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 248 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 249 | @Override |
| 250 | public void onSelectedChanged(ViewHolder viewHolder, int actionState) { |
| 251 | super.onSelectedChanged(viewHolder, actionState); |
| 252 | if (mCurrentDrag != null) { |
| 253 | mCurrentDrag.stopDrag(); |
| 254 | } |
| 255 | if (viewHolder != null) { |
| 256 | mCurrentDrag = (Holder) viewHolder; |
| 257 | mCurrentDrag.startDrag(); |
| 258 | } |
| 259 | } |
| 260 | |
| 261 | @Override |
| 262 | public int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder) { |
| 263 | if (viewHolder.getItemViewType() == TYPE_EDIT) { |
| 264 | return makeMovementFlags(0, 0); |
| 265 | } |
| 266 | int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.RIGHT |
| 267 | | ItemTouchHelper.LEFT; |
| 268 | return makeMovementFlags(dragFlags, 0); |
| 269 | } |
| 270 | |
| 271 | @Override |
| 272 | public boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder target) { |
| 273 | int from = viewHolder.getAdapterPosition(); |
| 274 | int to = target.getAdapterPosition(); |
| 275 | if (to > mDividerIndex) { |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 276 | if (from >= mDividerIndex) { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 277 | return false; |
| 278 | } |
| 279 | } |
| 280 | if (target.getItemViewType() == TYPE_EDIT && from < mDividerIndex) { |
| 281 | to++; |
| 282 | } |
| 283 | move(from, to, mTiles); |
| 284 | mDividerIndex = mTiles.indexOf(null); |
| 285 | notifyItemMoved(from, to); |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | private <T> void move(int from, int to, List<T> list) { |
| 290 | list.add(from > to ? to : to + 1, list.get(from)); |
| 291 | list.remove(from > to ? from + 1 : from); |
| 292 | } |
| 293 | |
| 294 | @Override |
| 295 | public void onSwiped(ViewHolder viewHolder, int direction) { |
| 296 | } |
| 297 | }; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 298 | } |