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