Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | package com.android.car.settings.quicksettings; |
| 17 | |
| 18 | import android.annotation.Nullable; |
| 19 | import android.content.Context; |
| 20 | import android.graphics.drawable.Drawable; |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 21 | import android.support.v7.widget.GridLayoutManager; |
| 22 | import android.support.v7.widget.RecyclerView; |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 23 | import android.text.TextUtils; |
| 24 | import android.view.LayoutInflater; |
| 25 | import android.view.View; |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 26 | import android.view.View.OnClickListener; |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 27 | import android.view.ViewGroup; |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 28 | import android.widget.ImageView; |
| 29 | import android.widget.SeekBar; |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 30 | import android.widget.TextView; |
| 31 | |
| 32 | import com.android.car.settings.R; |
| 33 | |
| 34 | import java.util.ArrayList; |
| 35 | import java.util.List; |
| 36 | |
| 37 | /** |
| 38 | * Controls the content in quick setting grid view. |
| 39 | */ |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 40 | public class QuickSettingGridAdapter |
| 41 | extends RecyclerView.Adapter<RecyclerView.ViewHolder> implements StateChangedListener { |
| 42 | private static final int COLUMN_COUNT = 4; |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 43 | private static final int SEEKBAR_VIEWTYPE = 0; |
| 44 | private static final int TILE_VIEWTYPE = 1; |
| 45 | private final Context mContext; |
| 46 | private final LayoutInflater mInflater; |
| 47 | private final List<Tile> mTiles = new ArrayList<>(); |
| 48 | private final List<SeekbarTile> mSeekbarTiles = new ArrayList<>(); |
| 49 | private final QsSpanSizeLookup mQsSpanSizeLookup = new QsSpanSizeLookup(); |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 50 | |
| 51 | public QuickSettingGridAdapter(Context context) { |
| 52 | mContext = context; |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 53 | mInflater = LayoutInflater.from(context); |
| 54 | } |
| 55 | |
| 56 | GridLayoutManager getGridLayoutManager() { |
| 57 | GridLayoutManager gridLayoutManager = new GridLayoutManager(mContext, |
| 58 | mContext.getResources().getInteger(R.integer.quick_setting_column_count)); |
| 59 | gridLayoutManager.setSpanSizeLookup(mQsSpanSizeLookup); |
| 60 | return gridLayoutManager; |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | /** |
| 64 | * Represents an UI tile in the quick setting grid. |
| 65 | */ |
| 66 | interface Tile extends View.OnClickListener { |
| 67 | |
| 68 | /** |
| 69 | * A state to indicate how we want to render icon, this is independent of what to show |
| 70 | * in text. |
| 71 | */ |
| 72 | enum State {OFF, ON} |
| 73 | |
| 74 | /** |
| 75 | * Called when activity owning this tile's onStop() gets called. |
| 76 | */ |
| 77 | void stop(); |
| 78 | |
| 79 | Drawable getIcon(); |
| 80 | |
| 81 | @Nullable |
| 82 | String getText(); |
| 83 | |
| 84 | State getState(); |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 85 | |
| 86 | /** |
| 87 | * Returns {@code true} if this tile should be displayed. |
| 88 | */ |
| 89 | boolean isAvailable(); |
| 90 | |
| 91 | /** |
| 92 | * Returns a listener for launching a setting fragment for advanced configuration for this |
| 93 | * tile, A.K.A deep dive, if available. {@code null} if deep dive is not available. |
| 94 | */ |
| 95 | @Nullable |
| 96 | OnClickListener getDeepDiveListener(); |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 97 | } |
| 98 | |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 99 | interface SeekbarTile extends SeekBar.OnSeekBarChangeListener { |
| 100 | /** |
| 101 | * Called when activity owning this tile's onStop() gets called. |
| 102 | */ |
| 103 | void stop(); |
| 104 | |
| 105 | int getMax(); |
| 106 | |
| 107 | int getCurrent(); |
| 108 | } |
| 109 | |
| 110 | QuickSettingGridAdapter addSeekbarTile(SeekbarTile seekbarTile) { |
| 111 | mSeekbarTiles.add(seekbarTile); |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 112 | return this; |
| 113 | } |
| 114 | |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 115 | QuickSettingGridAdapter addTile(Tile tile) { |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 116 | if (tile.isAvailable()) { |
| 117 | mTiles.add(tile); |
| 118 | } |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 119 | return this; |
| 120 | } |
| 121 | |
| 122 | void stop() { |
| 123 | for (SeekbarTile tile : mSeekbarTiles) { |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 124 | tile.stop(); |
| 125 | } |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 126 | for (Tile tile : mTiles) { |
| 127 | tile.stop(); |
| 128 | } |
| 129 | mTiles.clear(); |
| 130 | mSeekbarTiles.clear(); |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Override |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 134 | public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
| 135 | switch (viewType) { |
| 136 | case SEEKBAR_VIEWTYPE: |
| 137 | return new BrightnessViewHolder(mInflater.inflate( |
| 138 | R.layout.brightness_tile, parent, /* attachToRoot= */ false)); |
| 139 | case TILE_VIEWTYPE: |
| 140 | return new TileViewHolder(mInflater.inflate( |
| 141 | R.layout.tile, parent, /* attachToRoot= */ false)); |
| 142 | default: |
| 143 | throw new RuntimeException("unknown viewType: " + viewType); |
| 144 | } |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 148 | public void onBindViewHolder(RecyclerView.ViewHolder holder, int position) { |
| 149 | switch (holder.getItemViewType()) { |
| 150 | case SEEKBAR_VIEWTYPE: |
| 151 | SeekbarTile seekbarTile = mSeekbarTiles.get(position); |
| 152 | SeekBar seekbar = ((BrightnessViewHolder) holder).mSeekBar; |
| 153 | seekbar.setMax(seekbarTile.getMax()); |
| 154 | seekbar.setProgress(seekbarTile.getCurrent()); |
| 155 | seekbar.setOnSeekBarChangeListener(seekbarTile); |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 156 | break; |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 157 | case TILE_VIEWTYPE: |
| 158 | Tile tile = mTiles.get(position - mSeekbarTiles.size()); |
| 159 | TileViewHolder vh = (TileViewHolder) holder; |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 160 | OnClickListener deepDiveListener = tile.getDeepDiveListener(); |
| 161 | if (deepDiveListener != null) { |
| 162 | vh.mDeepDiveIcon.setVisibility(View.VISIBLE); |
| 163 | vh.mTextContainer.setOnClickListener(deepDiveListener); |
| 164 | vh.mIconContainer.setOnClickListener(tile); |
| 165 | vh.itemView.setOnClickListener(null); |
| 166 | vh.itemView.setClickable(false); |
| 167 | } else { |
| 168 | vh.itemView.setOnClickListener(tile); |
| 169 | vh.itemView.setClickable(true); |
| 170 | vh.mIconContainer.setOnClickListener(null); |
| 171 | vh.mIconContainer.setClickable(false); |
| 172 | vh.mDeepDiveIcon.setVisibility(View.GONE); |
| 173 | vh.mTextContainer.setClickable(false); |
| 174 | vh.mTextContainer.setOnClickListener(null); |
| 175 | } |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 176 | vh.mIcon.setImageDrawable(tile.getIcon()); |
| 177 | switch (tile.getState()) { |
| 178 | case ON: |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 179 | vh.mIcon.setEnabled(true); |
| 180 | vh.mIconBackground.setEnabled(true); |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 181 | break; |
| 182 | case OFF: |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 183 | vh.mIcon.setEnabled(false); |
| 184 | vh.mIconBackground.setEnabled(false); |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 185 | break; |
| 186 | default: |
| 187 | } |
| 188 | String textString = tile.getText(); |
| 189 | if (!TextUtils.isEmpty(textString)) { |
| 190 | vh.mText.setText(textString); |
| 191 | } |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 192 | break; |
| 193 | default: |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 194 | } |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 195 | } |
| 196 | |
| 197 | private class BrightnessViewHolder extends RecyclerView.ViewHolder { |
| 198 | private final SeekBar mSeekBar; |
| 199 | |
| 200 | BrightnessViewHolder(View view) { |
| 201 | super(view); |
| 202 | mSeekBar = (SeekBar) view.findViewById(R.id.seekbar); |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 203 | } |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | private class TileViewHolder extends RecyclerView.ViewHolder { |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 207 | private final View mIconContainer; |
| 208 | private final View mTextContainer; |
| 209 | private final View mIconBackground; |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 210 | private final ImageView mIcon; |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 211 | private final ImageView mDeepDiveIcon; |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 212 | private final TextView mText; |
| 213 | |
| 214 | TileViewHolder(View view) { |
| 215 | super(view); |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 216 | mIconContainer = view.findViewById(R.id.icon_container); |
| 217 | mTextContainer = view.findViewById(R.id.text_container); |
| 218 | mIconBackground = view.findViewById(R.id.icon_background); |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 219 | mIcon = (ImageView) view.findViewById(R.id.tile_icon); |
Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 220 | mDeepDiveIcon = (ImageView) view.findViewById(R.id.deep_dive_icon); |
Lujiang Xue | 73f4573 | 2018-04-05 09:42:19 -0700 | [diff] [blame] | 221 | mText = (TextView) view.findViewById(R.id.tile_text); |
| 222 | } |
| 223 | } |
| 224 | |
| 225 | class QsSpanSizeLookup extends GridLayoutManager.SpanSizeLookup { |
| 226 | |
| 227 | /** |
| 228 | * Each line item takes a full row, and each tile takes only 1 span. |
| 229 | */ |
| 230 | @Override |
| 231 | public int getSpanSize(int position) { |
| 232 | return position < mSeekbarTiles.size() ? COLUMN_COUNT : 1; |
| 233 | } |
| 234 | |
| 235 | @Override |
| 236 | public int getSpanIndex(int position, int spanCount) { |
| 237 | return position < mSeekbarTiles.size() |
| 238 | ? 1 : (position - mSeekbarTiles.size()) % COLUMN_COUNT; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | @Override |
| 243 | public int getItemViewType(int position) { |
| 244 | return position < mSeekbarTiles.size() ? SEEKBAR_VIEWTYPE : TILE_VIEWTYPE; |
| 245 | } |
| 246 | |
| 247 | @Override |
| 248 | public int getItemCount() { |
| 249 | return mTiles.size() + mSeekbarTiles.size(); |
Lujiang Xue | 0daa919 | 2017-12-06 11:48:39 -0800 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void onStateChanged() { |
| 254 | notifyDataSetChanged(); |
| 255 | } |
| 256 | } |