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