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 | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 17 | import android.app.AlertDialog; |
| 18 | import android.app.AlertDialog.Builder; |
Jason Monk | 96defbe | 2016-03-29 16:51:03 -0400 | [diff] [blame] | 19 | import android.content.ComponentName; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 20 | import android.content.Context; |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 21 | import android.content.DialogInterface; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 22 | import android.graphics.Canvas; |
| 23 | import android.graphics.drawable.ColorDrawable; |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 24 | import android.os.Handler; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 25 | import android.support.v4.view.ViewCompat; |
| 26 | import android.support.v7.widget.GridLayoutManager.SpanSizeLookup; |
| 27 | import android.support.v7.widget.RecyclerView; |
| 28 | import android.support.v7.widget.RecyclerView.ItemDecoration; |
| 29 | import android.support.v7.widget.RecyclerView.State; |
| 30 | import android.support.v7.widget.RecyclerView.ViewHolder; |
| 31 | import android.support.v7.widget.helper.ItemTouchHelper; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 32 | import android.view.LayoutInflater; |
| 33 | import android.view.View; |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 34 | import android.view.View.OnClickListener; |
| 35 | import android.view.View.OnLayoutChangeListener; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 36 | import android.view.ViewGroup; |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 37 | import android.view.accessibility.AccessibilityManager; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 38 | import android.widget.FrameLayout; |
Jason Monk | 24dbd51 | 2016-03-16 14:56:42 -0400 | [diff] [blame] | 39 | import android.widget.TextView; |
Jason Monk | 96defbe | 2016-03-29 16:51:03 -0400 | [diff] [blame] | 40 | import com.android.internal.logging.MetricsLogger; |
| 41 | import com.android.internal.logging.MetricsProto; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 42 | import com.android.systemui.R; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 43 | import com.android.systemui.qs.QSIconView; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 44 | import com.android.systemui.qs.customize.TileAdapter.Holder; |
| 45 | import com.android.systemui.qs.customize.TileQueryHelper.TileInfo; |
| 46 | import com.android.systemui.qs.customize.TileQueryHelper.TileStateListener; |
Jason Monk | 96defbe | 2016-03-29 16:51:03 -0400 | [diff] [blame] | 47 | import com.android.systemui.qs.external.CustomTile; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 48 | import com.android.systemui.statusbar.phone.QSTileHost; |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 49 | import com.android.systemui.statusbar.phone.SystemUIDialog; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 50 | |
| 51 | import java.util.ArrayList; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 52 | import java.util.List; |
| 53 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 54 | public class TileAdapter extends RecyclerView.Adapter<Holder> implements TileStateListener { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 55 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 56 | private static final long DRAG_LENGTH = 100; |
| 57 | private static final float DRAG_SCALE = 1.2f; |
| 58 | public static final long MOVE_DURATION = 150; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 59 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 60 | private static final int TYPE_TILE = 0; |
| 61 | private static final int TYPE_EDIT = 1; |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 62 | private static final int TYPE_ACCESSIBLE_DROP = 2; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 63 | private static final int TYPE_DIVIDER = 4; |
| 64 | |
| 65 | private static final long EDIT_ID = 10000; |
| 66 | private static final long DIVIDER_ID = 20000; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 67 | |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 68 | private final Context mContext; |
| 69 | |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 70 | private final Handler mHandler = new Handler(); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 71 | private final List<TileInfo> mTiles = new ArrayList<>(); |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 72 | private final ItemTouchHelper mItemTouchHelper; |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 73 | private final AccessibilityManager mAccessibilityManager; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 74 | private int mEditIndex; |
| 75 | private int mTileDividerIndex; |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 76 | private boolean mNeedsFocus; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 77 | private List<String> mCurrentSpecs; |
| 78 | private List<TileInfo> mOtherTiles; |
| 79 | private List<TileInfo> mAllTiles; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 80 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 81 | private Holder mCurrentDrag; |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 82 | private boolean mAccessibilityMoving; |
| 83 | private int mAccessibilityFromIndex; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 84 | |
| 85 | public TileAdapter(Context context) { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 86 | mContext = context; |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 87 | mAccessibilityManager = context.getSystemService(AccessibilityManager.class); |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 88 | mItemTouchHelper = new ItemTouchHelper(mCallbacks); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 89 | setHasStableIds(true); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | @Override |
| 93 | public long getItemId(int position) { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 94 | return mTiles.get(position) != null ? mAllTiles.indexOf(mTiles.get(position)) |
| 95 | : position == mEditIndex ? EDIT_ID : DIVIDER_ID; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 96 | } |
| 97 | |
Jason Monk | b53b6c5 | 2016-02-24 17:25:49 -0500 | [diff] [blame] | 98 | public ItemTouchHelper getItemTouchHelper() { |
| 99 | return mItemTouchHelper; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 100 | } |
| 101 | |
| 102 | public ItemDecoration getItemDecoration() { |
| 103 | return mDecoration; |
| 104 | } |
| 105 | |
| 106 | public void saveSpecs(QSTileHost host) { |
| 107 | List<String> newSpecs = new ArrayList<>(); |
| 108 | for (int i = 0; mTiles.get(i) != null; i++) { |
| 109 | newSpecs.add(mTiles.get(i).spec); |
| 110 | } |
| 111 | host.changeTiles(mCurrentSpecs, newSpecs); |
| 112 | setTileSpecs(newSpecs); |
| 113 | } |
| 114 | |
| 115 | public void setTileSpecs(List<String> currentSpecs) { |
| 116 | mCurrentSpecs = currentSpecs; |
| 117 | recalcSpecs(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 118 | } |
| 119 | |
| 120 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 121 | public void onTilesChanged(List<TileInfo> tiles) { |
| 122 | mAllTiles = tiles; |
| 123 | recalcSpecs(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 126 | private void recalcSpecs() { |
| 127 | if (mCurrentSpecs == null || mAllTiles == null) { |
| 128 | return; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 129 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 130 | mOtherTiles = new ArrayList<TileInfo>(mAllTiles); |
| 131 | mTiles.clear(); |
| 132 | for (int i = 0; i < mCurrentSpecs.size(); i++) { |
Jason Monk | cb654cb | 2016-02-25 15:43:07 -0500 | [diff] [blame] | 133 | final TileInfo tile = getAndRemoveOther(mCurrentSpecs.get(i)); |
| 134 | if (tile != null) { |
| 135 | mTiles.add(tile); |
| 136 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 137 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 138 | mTiles.add(null); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 139 | for (int i = 0; i < mOtherTiles.size(); i++) { |
| 140 | final TileInfo tile = mOtherTiles.get(i); |
| 141 | if (tile.isSystem) { |
| 142 | mOtherTiles.remove(i--); |
| 143 | mTiles.add(tile); |
| 144 | } |
| 145 | } |
| 146 | if (mOtherTiles.size() != 0) { |
| 147 | mTileDividerIndex = mTiles.size(); |
| 148 | mTiles.add(null); |
| 149 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 150 | mTiles.addAll(mOtherTiles); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 151 | mEditIndex = mTiles.indexOf(null); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 152 | notifyDataSetChanged(); |
| 153 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 154 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 155 | private TileInfo getAndRemoveOther(String s) { |
| 156 | for (int i = 0; i < mOtherTiles.size(); i++) { |
| 157 | if (mOtherTiles.get(i).spec.equals(s)) { |
| 158 | return mOtherTiles.remove(i); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 159 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 160 | } |
| 161 | return null; |
| 162 | } |
| 163 | |
| 164 | @Override |
| 165 | public int getItemViewType(int position) { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 166 | if (mAccessibilityMoving && position == mEditIndex - 1) { |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 167 | return TYPE_ACCESSIBLE_DROP; |
| 168 | } |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 169 | if (position == mTileDividerIndex) { |
| 170 | return TYPE_DIVIDER; |
| 171 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 172 | if (mTiles.get(position) == null) { |
| 173 | return TYPE_EDIT; |
| 174 | } |
| 175 | return TYPE_TILE; |
| 176 | } |
| 177 | |
| 178 | @Override |
| 179 | public Holder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 180 | final Context context = parent.getContext(); |
| 181 | LayoutInflater inflater = LayoutInflater.from(context); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 182 | if (viewType == TYPE_DIVIDER) { |
| 183 | return new Holder(inflater.inflate(R.layout.qs_customize_tile_divider, parent, false)); |
| 184 | } |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 185 | if (viewType == TYPE_EDIT) { |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 186 | return new Holder(inflater.inflate(R.layout.qs_customize_divider, parent, false)); |
| 187 | } |
| 188 | FrameLayout frame = (FrameLayout) inflater.inflate(R.layout.qs_customize_tile_frame, parent, |
| 189 | false); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 190 | frame.addView(new CustomizeTileView(context, new QSIconView(context))); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 191 | return new Holder(frame); |
| 192 | } |
| 193 | |
| 194 | @Override |
| 195 | public int getItemCount() { |
| 196 | return mTiles.size(); |
| 197 | } |
| 198 | |
| 199 | @Override |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 200 | public void onBindViewHolder(final Holder holder, final int position) { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 201 | if (holder.getItemViewType() == TYPE_DIVIDER) { |
| 202 | return; |
| 203 | } |
Jason Monk | 24dbd51 | 2016-03-16 14:56:42 -0400 | [diff] [blame] | 204 | if (holder.getItemViewType() == TYPE_EDIT) { |
| 205 | ((TextView) holder.itemView.findViewById(android.R.id.title)).setText( |
| 206 | mCurrentDrag != null ? R.string.drag_to_remove_tiles |
| 207 | : R.string.drag_to_add_tiles); |
| 208 | return; |
| 209 | } |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 210 | if (holder.getItemViewType() == TYPE_ACCESSIBLE_DROP) { |
| 211 | holder.mTileView.setClickable(true); |
| 212 | holder.mTileView.setFocusable(true); |
| 213 | holder.mTileView.setFocusableInTouchMode(true); |
| 214 | holder.mTileView.setVisibility(View.VISIBLE); |
| 215 | holder.mTileView.setImportantForAccessibility(View.IMPORTANT_FOR_ACCESSIBILITY_YES); |
| 216 | holder.mTileView.setContentDescription(mContext.getString( |
| 217 | R.string.accessibility_qs_edit_position_label, position + 1)); |
| 218 | holder.mTileView.setOnClickListener(new OnClickListener() { |
| 219 | @Override |
| 220 | public void onClick(View v) { |
| 221 | selectPosition(position, v); |
| 222 | } |
| 223 | }); |
| 224 | if (mNeedsFocus) { |
| 225 | // Wait for this to get laid out then set its focus. |
| 226 | holder.mTileView.addOnLayoutChangeListener(new OnLayoutChangeListener() { |
| 227 | @Override |
| 228 | public void onLayoutChange(View v, int left, int top, int right, int bottom, |
| 229 | int oldLeft, int oldTop, int oldRight, int oldBottom) { |
| 230 | holder.mTileView.removeOnLayoutChangeListener(this); |
| 231 | holder.mTileView.requestFocus(); |
| 232 | } |
| 233 | }); |
| 234 | mNeedsFocus = false; |
| 235 | } |
| 236 | return; |
| 237 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 238 | |
| 239 | TileInfo info = mTiles.get(position); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 240 | |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 241 | if (position > mEditIndex) { |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 242 | info.state.contentDescription = mContext.getString( |
| 243 | R.string.accessibility_qs_edit_add_tile_label, info.state.label); |
| 244 | } else if (mAccessibilityMoving) { |
| 245 | info.state.contentDescription = mContext.getString( |
| 246 | R.string.accessibility_qs_edit_position_label, position + 1); |
| 247 | } else { |
| 248 | info.state.contentDescription = mContext.getString( |
| 249 | R.string.accessibility_qs_edit_tile_label, position + 1, info.state.label); |
| 250 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 251 | holder.mTileView.onStateChanged(info.state); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 252 | holder.mTileView.setAppLabel(info.appLabel); |
| 253 | holder.mTileView.setShowAppLabel(position > mTileDividerIndex); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 254 | |
| 255 | if (mAccessibilityManager.isTouchExplorationEnabled()) { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 256 | final boolean selectable = !mAccessibilityMoving || position < mEditIndex; |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 257 | holder.mTileView.setClickable(selectable); |
| 258 | holder.mTileView.setFocusable(selectable); |
| 259 | holder.mTileView.setImportantForAccessibility(selectable |
| 260 | ? View.IMPORTANT_FOR_ACCESSIBILITY_YES |
| 261 | : View.IMPORTANT_FOR_ACCESSIBILITY_NO_HIDE_DESCENDANTS); |
| 262 | if (selectable) { |
| 263 | holder.mTileView.setOnClickListener(new OnClickListener() { |
| 264 | @Override |
| 265 | public void onClick(View v) { |
| 266 | if (mAccessibilityMoving) { |
| 267 | selectPosition(position, v); |
| 268 | } else { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 269 | if (position < mEditIndex) { |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 270 | showAccessibilityDialog(position, v); |
| 271 | } else { |
| 272 | startAccessibleDrag(position); |
| 273 | } |
| 274 | } |
| 275 | } |
| 276 | }); |
| 277 | } |
| 278 | } |
| 279 | } |
| 280 | |
| 281 | private void selectPosition(int position, View v) { |
| 282 | // Remove the placeholder. |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 283 | mTiles.remove(mEditIndex--); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 284 | mAccessibilityMoving = false; |
| 285 | move(mAccessibilityFromIndex, position, v); |
| 286 | notifyDataSetChanged(); |
| 287 | } |
| 288 | |
| 289 | private void showAccessibilityDialog(final int position, final View v) { |
| 290 | TileInfo info = mTiles.get(position); |
| 291 | CharSequence[] options = new CharSequence[] { |
| 292 | mContext.getString(R.string.accessibility_qs_edit_move_tile, info.state.label), |
| 293 | mContext.getString(R.string.accessibility_qs_edit_remove_tile, info.state.label), |
| 294 | }; |
| 295 | AlertDialog dialog = new Builder(mContext) |
| 296 | .setItems(options, new DialogInterface.OnClickListener() { |
| 297 | @Override |
| 298 | public void onClick(DialogInterface dialog, int which) { |
| 299 | if (which == 0) { |
| 300 | startAccessibleDrag(position); |
| 301 | } else { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 302 | move(position, mEditIndex, v); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 303 | } |
| 304 | } |
| 305 | }).setNegativeButton(android.R.string.cancel, null) |
| 306 | .create(); |
| 307 | SystemUIDialog.setShowForAllUsers(dialog, true); |
| 308 | SystemUIDialog.applyFlags(dialog); |
| 309 | dialog.show(); |
| 310 | } |
| 311 | |
| 312 | private void startAccessibleDrag(int position) { |
| 313 | mAccessibilityMoving = true; |
| 314 | mNeedsFocus = true; |
| 315 | mAccessibilityFromIndex = position; |
| 316 | // Add placeholder for last slot. |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 317 | mTiles.add(mEditIndex++, null); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 318 | notifyDataSetChanged(); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 319 | } |
| 320 | |
| 321 | public SpanSizeLookup getSizeLookup() { |
| 322 | return mSizeLookup; |
| 323 | } |
| 324 | |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 325 | private boolean move(int from, int to, View v) { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 326 | if (to >= mEditIndex) { |
| 327 | if (from >= mEditIndex) { |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 328 | return false; |
| 329 | } |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 330 | // Sort tiles into system/non-system groups. |
| 331 | TileInfo tile = mTiles.get(from); |
| 332 | if (tile.isSystem) { |
| 333 | if (to > mTileDividerIndex) { |
| 334 | to = mTileDividerIndex; |
| 335 | } |
| 336 | } else { |
| 337 | if (mTileDividerIndex == mTiles.size()) { |
| 338 | mTiles.add(null); |
| 339 | } |
| 340 | if (to <= mTileDividerIndex) { |
| 341 | to = mTileDividerIndex; |
| 342 | } |
| 343 | } |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 344 | } |
| 345 | CharSequence fromLabel = mTiles.get(from).state.label; |
| 346 | move(from, to, mTiles); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 347 | notifyDataSetChanged(); |
| 348 | updateDividerLocations(); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 349 | CharSequence announcement; |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 350 | if (to >= mEditIndex) { |
Jason Monk | 96defbe | 2016-03-29 16:51:03 -0400 | [diff] [blame] | 351 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_QS_EDIT_REMOVE_SPEC, |
| 352 | strip(mTiles.get(to))); |
| 353 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_QS_EDIT_REMOVE, |
| 354 | from); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 355 | announcement = mContext.getString(R.string.accessibility_qs_edit_tile_removed, |
| 356 | fromLabel); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 357 | } else if (from >= mEditIndex) { |
Jason Monk | 96defbe | 2016-03-29 16:51:03 -0400 | [diff] [blame] | 358 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_QS_EDIT_ADD_SPEC, |
| 359 | strip(mTiles.get(to))); |
| 360 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_QS_EDIT_ADD, |
| 361 | to); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 362 | announcement = mContext.getString(R.string.accessibility_qs_edit_tile_added, |
| 363 | fromLabel, (to + 1)); |
| 364 | } else { |
Jason Monk | 96defbe | 2016-03-29 16:51:03 -0400 | [diff] [blame] | 365 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_QS_EDIT_MOVE_SPEC, |
| 366 | strip(mTiles.get(to))); |
| 367 | MetricsLogger.action(mContext, MetricsProto.MetricsEvent.ACTION_QS_EDIT_MOVE, |
| 368 | to); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 369 | announcement = mContext.getString(R.string.accessibility_qs_edit_tile_moved, |
| 370 | fromLabel, (to + 1)); |
| 371 | } |
| 372 | v.announceForAccessibility(announcement); |
| 373 | return true; |
| 374 | } |
| 375 | |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 376 | private void updateDividerLocations() { |
| 377 | // The first null is the edit tiles label, the second null is the tile divider. |
| 378 | // If there is no second null, then there are no non-system tiles. |
| 379 | mEditIndex = -1; |
| 380 | mTileDividerIndex = mTiles.size(); |
| 381 | for (int i = 0; i < mTiles.size(); i++) { |
| 382 | if (mTiles.get(i) == null) { |
| 383 | if (mEditIndex == -1) { |
| 384 | mEditIndex = i; |
| 385 | } else { |
| 386 | mTileDividerIndex = i; |
| 387 | } |
| 388 | } |
| 389 | } |
| 390 | if (mTiles.get(mTiles.size() - 1) == null) { |
| 391 | mTiles.remove(mTiles.size() - 1); |
| 392 | } |
| 393 | } |
| 394 | |
Jason Monk | 96defbe | 2016-03-29 16:51:03 -0400 | [diff] [blame] | 395 | private String strip(TileInfo tileInfo) { |
| 396 | String spec = tileInfo.spec; |
| 397 | if (spec.startsWith(CustomTile.PREFIX)) { |
| 398 | ComponentName component = CustomTile.getComponentFromSpec(spec); |
| 399 | return component.getPackageName(); |
| 400 | } |
| 401 | return spec; |
| 402 | } |
| 403 | |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 404 | private <T> void move(int from, int to, List<T> list) { |
| 405 | list.add(from > to ? to : to + 1, list.get(from)); |
| 406 | list.remove(from > to ? from + 1 : from); |
| 407 | } |
| 408 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 409 | public class Holder extends ViewHolder { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 410 | private CustomizeTileView mTileView; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 411 | |
| 412 | public Holder(View itemView) { |
| 413 | super(itemView); |
| 414 | if (itemView instanceof FrameLayout) { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 415 | mTileView = (CustomizeTileView) ((FrameLayout) itemView).getChildAt(0); |
Jason Monk | 04fd249 | 2016-02-29 14:29:26 -0500 | [diff] [blame] | 416 | mTileView.setBackground(null); |
Jason Monk | 1aec93f | 2016-03-01 09:39:30 -0500 | [diff] [blame] | 417 | mTileView.getIcon().disableAnimation(); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 418 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 419 | } |
| 420 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 421 | public void startDrag() { |
| 422 | itemView.animate() |
| 423 | .setDuration(DRAG_LENGTH) |
| 424 | .scaleX(DRAG_SCALE) |
| 425 | .scaleY(DRAG_SCALE); |
| 426 | mTileView.findViewById(R.id.tile_label).animate() |
| 427 | .setDuration(DRAG_LENGTH) |
| 428 | .alpha(0); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 429 | mTileView.getAppLabel().animate() |
| 430 | .setDuration(DRAG_LENGTH) |
| 431 | .alpha(0); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 432 | } |
| 433 | |
| 434 | public void stopDrag() { |
| 435 | itemView.animate() |
| 436 | .setDuration(DRAG_LENGTH) |
| 437 | .scaleX(1) |
| 438 | .scaleY(1); |
| 439 | mTileView.findViewById(R.id.tile_label).animate() |
| 440 | .setDuration(DRAG_LENGTH) |
| 441 | .alpha(1); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 442 | mTileView.getAppLabel().animate() |
| 443 | .setDuration(DRAG_LENGTH) |
| 444 | .alpha(.6f); |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 445 | } |
| 446 | } |
| 447 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 448 | private final SpanSizeLookup mSizeLookup = new SpanSizeLookup() { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 449 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 450 | public int getSpanSize(int position) { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 451 | final int type = getItemViewType(position); |
| 452 | return type == TYPE_EDIT || type == TYPE_DIVIDER ? 3 : 1; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 453 | } |
| 454 | }; |
| 455 | |
| 456 | private final ItemDecoration mDecoration = new ItemDecoration() { |
| 457 | // TODO: Move this to resource. |
| 458 | private final ColorDrawable mDrawable = new ColorDrawable(0xff384248); |
| 459 | |
| 460 | @Override |
| 461 | public void onDraw(Canvas c, RecyclerView parent, State state) { |
| 462 | super.onDraw(c, parent, state); |
| 463 | |
| 464 | final int childCount = parent.getChildCount(); |
| 465 | final int width = parent.getWidth(); |
| 466 | final int bottom = parent.getBottom(); |
| 467 | for (int i = 0; i < childCount; i++) { |
| 468 | final View child = parent.getChildAt(i); |
| 469 | final ViewHolder holder = parent.getChildViewHolder(child); |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 470 | if (holder.getAdapterPosition() < mEditIndex) { |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 471 | continue; |
| 472 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 473 | |
| 474 | final RecyclerView.LayoutParams params = (RecyclerView.LayoutParams) child |
| 475 | .getLayoutParams(); |
| 476 | final int top = child.getTop() + params.topMargin + |
| 477 | Math.round(ViewCompat.getTranslationY(child)); |
| 478 | // Draw full width, in case there aren't tiles all the way across. |
| 479 | mDrawable.setBounds(0, top, width, bottom); |
| 480 | mDrawable.draw(c); |
| 481 | break; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 482 | } |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 483 | } |
| 484 | }; |
| 485 | |
| 486 | private final ItemTouchHelper.Callback mCallbacks = new ItemTouchHelper.Callback() { |
| 487 | |
| 488 | @Override |
| 489 | public boolean isLongPressDragEnabled() { |
| 490 | return true; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 491 | } |
| 492 | |
| 493 | @Override |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 494 | public boolean isItemViewSwipeEnabled() { |
| 495 | return false; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 496 | } |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 497 | |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 498 | @Override |
| 499 | public void onSelectedChanged(ViewHolder viewHolder, int actionState) { |
| 500 | super.onSelectedChanged(viewHolder, actionState); |
| 501 | if (mCurrentDrag != null) { |
| 502 | mCurrentDrag.stopDrag(); |
Jason Monk | 24dbd51 | 2016-03-16 14:56:42 -0400 | [diff] [blame] | 503 | mCurrentDrag = null; |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 504 | } |
| 505 | if (viewHolder != null) { |
| 506 | mCurrentDrag = (Holder) viewHolder; |
| 507 | mCurrentDrag.startDrag(); |
| 508 | } |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 509 | mHandler.post(new Runnable() { |
| 510 | @Override |
| 511 | public void run() { |
Jason Monk | 0b349ad | 2016-04-04 11:18:03 -0400 | [diff] [blame] | 512 | notifyItemChanged(mEditIndex); |
Jason Monk | 1c2fea8 | 2016-03-11 11:33:36 -0500 | [diff] [blame] | 513 | } |
| 514 | }); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 515 | } |
| 516 | |
| 517 | @Override |
| 518 | public int getMovementFlags(RecyclerView recyclerView, ViewHolder viewHolder) { |
| 519 | if (viewHolder.getItemViewType() == TYPE_EDIT) { |
| 520 | return makeMovementFlags(0, 0); |
| 521 | } |
| 522 | int dragFlags = ItemTouchHelper.UP | ItemTouchHelper.DOWN | ItemTouchHelper.RIGHT |
| 523 | | ItemTouchHelper.LEFT; |
| 524 | return makeMovementFlags(dragFlags, 0); |
| 525 | } |
| 526 | |
| 527 | @Override |
| 528 | public boolean onMove(RecyclerView recyclerView, ViewHolder viewHolder, ViewHolder target) { |
| 529 | int from = viewHolder.getAdapterPosition(); |
| 530 | int to = target.getAdapterPosition(); |
Jason Monk | 39c98e6 | 2016-03-16 09:18:35 -0400 | [diff] [blame] | 531 | return move(from, to, target.itemView); |
Jason Monk | 62b63a0 | 2016-02-02 15:15:31 -0500 | [diff] [blame] | 532 | } |
| 533 | |
| 534 | @Override |
| 535 | public void onSwiped(ViewHolder viewHolder, int direction) { |
| 536 | } |
| 537 | }; |
Jason Monk | 5db8a41 | 2015-10-21 15:16:23 -0700 | [diff] [blame] | 538 | } |