Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2018 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 | |
| 17 | package com.android.systemui.volume; |
| 18 | |
Lin Guo | a65cf948 | 2018-04-23 15:19:35 -0700 | [diff] [blame] | 19 | import android.animation.Animator; |
| 20 | import android.animation.AnimatorInflater; |
| 21 | import android.animation.AnimatorSet; |
Lin Guo | 30ceb78 | 2018-05-07 15:01:40 -0700 | [diff] [blame] | 22 | import android.annotation.DrawableRes; |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 23 | import android.annotation.Nullable; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 24 | import android.app.Dialog; |
| 25 | import android.app.KeyguardManager; |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 26 | import android.car.Car; |
| 27 | import android.car.CarNotConnectedException; |
| 28 | import android.car.media.CarAudioManager; |
| 29 | import android.car.media.ICarVolumeCallback; |
| 30 | import android.content.ComponentName; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 31 | import android.content.Context; |
| 32 | import android.content.DialogInterface; |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 33 | import android.content.ServiceConnection; |
| 34 | import android.content.res.TypedArray; |
| 35 | import android.content.res.XmlResourceParser; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 36 | import android.graphics.Color; |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 37 | import android.graphics.PixelFormat; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 38 | import android.graphics.drawable.ColorDrawable; |
Lin Guo | a65cf948 | 2018-04-23 15:19:35 -0700 | [diff] [blame] | 39 | import android.graphics.drawable.Drawable; |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 40 | import android.media.AudioAttributes; |
Lin Guo | f37d0eb | 2018-06-07 17:08:30 -0700 | [diff] [blame] | 41 | import android.media.AudioManager; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 42 | import android.os.Debug; |
| 43 | import android.os.Handler; |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 44 | import android.os.IBinder; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 45 | import android.os.Looper; |
| 46 | import android.os.Message; |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 47 | import android.util.AttributeSet; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 48 | import android.util.Log; |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 49 | import android.util.SparseArray; |
| 50 | import android.util.Xml; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 51 | import android.view.ContextThemeWrapper; |
| 52 | import android.view.Gravity; |
| 53 | import android.view.MotionEvent; |
| 54 | import android.view.View; |
| 55 | import android.view.ViewGroup; |
| 56 | import android.view.Window; |
| 57 | import android.view.WindowManager; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 58 | import android.widget.SeekBar; |
| 59 | import android.widget.SeekBar.OnSeekBarChangeListener; |
| 60 | |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 61 | import androidx.car.widget.ListItem; |
| 62 | import androidx.car.widget.ListItemAdapter; |
| 63 | import androidx.car.widget.ListItemAdapter.BackgroundStyle; |
| 64 | import androidx.car.widget.ListItemProvider.ListProvider; |
| 65 | import androidx.car.widget.PagedListView; |
| 66 | import androidx.car.widget.SeekbarListItem; |
| 67 | |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 68 | import com.android.systemui.R; |
| 69 | import com.android.systemui.plugins.VolumeDialog; |
| 70 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 71 | import org.xmlpull.v1.XmlPullParserException; |
| 72 | |
| 73 | import java.io.IOException; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 74 | import java.io.PrintWriter; |
| 75 | import java.util.ArrayList; |
Gus Prevas | ab33679 | 2018-11-14 13:52:20 -0500 | [diff] [blame] | 76 | import java.util.Iterator; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 77 | import java.util.List; |
| 78 | |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 79 | /** |
| 80 | * Car version of the volume dialog. |
| 81 | * |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 82 | * Methods ending in "H" must be called on the (ui) handler. |
| 83 | */ |
| 84 | public class CarVolumeDialogImpl implements VolumeDialog { |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 85 | private static final String TAG = Util.logTag(CarVolumeDialogImpl.class); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 86 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 87 | private static final String XML_TAG_VOLUME_ITEMS = "carVolumeItems"; |
| 88 | private static final String XML_TAG_VOLUME_ITEM = "item"; |
| 89 | private static final int HOVERING_TIMEOUT = 16000; |
| 90 | private static final int NORMAL_TIMEOUT = 3000; |
| 91 | private static final int LISTVIEW_ANIMATION_DURATION_IN_MILLIS = 250; |
| 92 | private static final int DISMISS_DELAY_IN_MILLIS = 50; |
| 93 | private static final int ARROW_FADE_IN_START_DELAY_IN_MILLIS = 100; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 94 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 95 | private final Context mContext; |
| 96 | private final H mHandler = new H(); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 97 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 98 | private Window mWindow; |
| 99 | private CustomDialog mDialog; |
| 100 | private PagedListView mListView; |
| 101 | private ListItemAdapter mPagedListAdapter; |
| 102 | // All the volume items. |
| 103 | private final SparseArray<VolumeItem> mVolumeItems = new SparseArray<>(); |
| 104 | // Available volume items in car audio manager. |
| 105 | private final List<VolumeItem> mAvailableVolumeItems = new ArrayList<>(); |
| 106 | // Volume items in the PagedListView. |
| 107 | private final List<ListItem> mVolumeLineItems = new ArrayList<>(); |
| 108 | private final KeyguardManager mKeyguard; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 109 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 110 | private Car mCar; |
| 111 | private CarAudioManager mCarAudioManager; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 112 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 113 | private boolean mHovering; |
| 114 | private boolean mShowing; |
| 115 | private boolean mExpanded; |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 116 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 117 | public CarVolumeDialogImpl(Context context) { |
| 118 | mContext = new ContextThemeWrapper(context, com.android.systemui.R.style.qs_theme); |
| 119 | mKeyguard = (KeyguardManager) mContext.getSystemService(Context.KEYGUARD_SERVICE); |
| 120 | mCar = Car.createCar(mContext, mServiceConnection); |
| 121 | } |
| 122 | |
| 123 | public void init(int windowType, Callback callback) { |
| 124 | initDialog(); |
| 125 | |
| 126 | mCar.connect(); |
| 127 | } |
| 128 | |
| 129 | @Override |
| 130 | public void destroy() { |
| 131 | mHandler.removeCallbacksAndMessages(null); |
| 132 | |
| 133 | cleanupAudioManager(); |
| 134 | // unregisterVolumeCallback is not being called when disconnect car, so we manually cleanup |
| 135 | // audio manager beforehand. |
| 136 | mCar.disconnect(); |
| 137 | } |
| 138 | |
| 139 | private void initDialog() { |
| 140 | loadAudioUsageItems(); |
| 141 | mVolumeLineItems.clear(); |
| 142 | mDialog = new CustomDialog(mContext); |
| 143 | |
| 144 | mHovering = false; |
| 145 | mShowing = false; |
| 146 | mExpanded = false; |
| 147 | mWindow = mDialog.getWindow(); |
| 148 | mWindow.requestFeature(Window.FEATURE_NO_TITLE); |
| 149 | mWindow.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT)); |
| 150 | mWindow.clearFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND |
| 151 | | WindowManager.LayoutParams.FLAG_LAYOUT_INSET_DECOR); |
| 152 | mWindow.addFlags(WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE |
| 153 | | WindowManager.LayoutParams.FLAG_LAYOUT_IN_SCREEN |
| 154 | | WindowManager.LayoutParams.FLAG_NOT_TOUCH_MODAL |
| 155 | | WindowManager.LayoutParams.FLAG_SHOW_WHEN_LOCKED |
| 156 | | WindowManager.LayoutParams.FLAG_WATCH_OUTSIDE_TOUCH |
| 157 | | WindowManager.LayoutParams.FLAG_HARDWARE_ACCELERATED); |
| 158 | mWindow.setType(WindowManager.LayoutParams.TYPE_VOLUME_OVERLAY); |
| 159 | mWindow.setWindowAnimations(com.android.internal.R.style.Animation_Toast); |
| 160 | final WindowManager.LayoutParams lp = mWindow.getAttributes(); |
| 161 | lp.format = PixelFormat.TRANSLUCENT; |
| 162 | lp.setTitle(VolumeDialogImpl.class.getSimpleName()); |
| 163 | lp.gravity = Gravity.TOP | Gravity.CENTER_HORIZONTAL; |
| 164 | lp.windowAnimations = -1; |
| 165 | mWindow.setAttributes(lp); |
| 166 | mWindow.setLayout(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT); |
| 167 | |
| 168 | mDialog.setCanceledOnTouchOutside(true); |
| 169 | mDialog.setContentView(R.layout.car_volume_dialog); |
| 170 | mDialog.setOnShowListener(dialog -> { |
| 171 | mListView.setTranslationY(-mListView.getHeight()); |
| 172 | mListView.setAlpha(0); |
| 173 | mListView.animate() |
| 174 | .alpha(1) |
| 175 | .translationY(0) |
| 176 | .setDuration(LISTVIEW_ANIMATION_DURATION_IN_MILLIS) |
| 177 | .setInterpolator(new SystemUIInterpolators.LogDecelerateInterpolator()) |
| 178 | .start(); |
| 179 | }); |
| 180 | mListView = (PagedListView) mWindow.findViewById(R.id.volume_list); |
| 181 | mListView.setOnHoverListener((v, event) -> { |
| 182 | int action = event.getActionMasked(); |
| 183 | mHovering = (action == MotionEvent.ACTION_HOVER_ENTER) |
| 184 | || (action == MotionEvent.ACTION_HOVER_MOVE); |
| 185 | rescheduleTimeoutH(); |
| 186 | return true; |
| 187 | }); |
| 188 | |
| 189 | mPagedListAdapter = new ListItemAdapter(mContext, new ListProvider(mVolumeLineItems), |
| 190 | BackgroundStyle.PANEL); |
| 191 | mListView.setAdapter(mPagedListAdapter); |
| 192 | mListView.setMaxPages(PagedListView.UNLIMITED_PAGES); |
| 193 | } |
| 194 | |
| 195 | public void show(int reason) { |
Lin Guo | 1e7b32f | 2018-06-08 15:15:48 -0700 | [diff] [blame] | 196 | mHandler.obtainMessage(H.SHOW, reason).sendToTarget(); |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | public void dismiss(int reason) { |
Lin Guo | 1e7b32f | 2018-06-08 15:15:48 -0700 | [diff] [blame] | 200 | mHandler.obtainMessage(H.DISMISS, reason).sendToTarget(); |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | private void showH(int reason) { |
| 204 | if (D.BUG) { |
| 205 | Log.d(TAG, "showH r=" + Events.DISMISS_REASONS[reason]); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 206 | } |
| 207 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 208 | mHandler.removeMessages(H.SHOW); |
| 209 | mHandler.removeMessages(H.DISMISS); |
| 210 | rescheduleTimeoutH(); |
| 211 | // Refresh the data set before showing. |
| 212 | mPagedListAdapter.notifyDataSetChanged(); |
| 213 | if (mShowing) { |
| 214 | return; |
| 215 | } |
| 216 | mShowing = true; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 217 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 218 | mDialog.show(); |
| 219 | Events.writeEvent(mContext, Events.EVENT_SHOW_DIALOG, reason, mKeyguard.isKeyguardLocked()); |
| 220 | } |
| 221 | |
| 222 | protected void rescheduleTimeoutH() { |
| 223 | mHandler.removeMessages(H.DISMISS); |
| 224 | final int timeout = computeTimeoutH(); |
| 225 | mHandler.sendMessageDelayed(mHandler |
Lin Guo | 1e7b32f | 2018-06-08 15:15:48 -0700 | [diff] [blame] | 226 | .obtainMessage(H.DISMISS, Events.DISMISS_REASON_TIMEOUT), timeout); |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 227 | |
| 228 | if (D.BUG) { |
| 229 | Log.d(TAG, "rescheduleTimeout " + timeout + " " + Debug.getCaller()); |
| 230 | } |
| 231 | } |
| 232 | |
| 233 | private int computeTimeoutH() { |
| 234 | return mHovering ? HOVERING_TIMEOUT : NORMAL_TIMEOUT; |
| 235 | } |
| 236 | |
| 237 | protected void dismissH(int reason) { |
| 238 | if (D.BUG) { |
| 239 | Log.d(TAG, "dismissH r=" + Events.DISMISS_REASONS[reason]); |
| 240 | } |
| 241 | |
| 242 | mHandler.removeMessages(H.DISMISS); |
| 243 | mHandler.removeMessages(H.SHOW); |
| 244 | if (!mShowing) { |
| 245 | return; |
| 246 | } |
| 247 | |
| 248 | mListView.animate().cancel(); |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 249 | |
| 250 | mListView.setTranslationY(0); |
| 251 | mListView.setAlpha(1); |
| 252 | mListView.animate() |
| 253 | .alpha(0) |
| 254 | .translationY(-mListView.getHeight()) |
| 255 | .setDuration(LISTVIEW_ANIMATION_DURATION_IN_MILLIS) |
| 256 | .setInterpolator(new SystemUIInterpolators.LogAccelerateInterpolator()) |
| 257 | .withEndAction(() -> mHandler.postDelayed(() -> { |
| 258 | if (D.BUG) { |
| 259 | Log.d(TAG, "mDialog.dismiss()"); |
| 260 | } |
| 261 | mDialog.dismiss(); |
Lin Guo | 1e7b32f | 2018-06-08 15:15:48 -0700 | [diff] [blame] | 262 | mShowing = false; |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 263 | }, DISMISS_DELAY_IN_MILLIS)) |
| 264 | .start(); |
| 265 | |
| 266 | Events.writeEvent(mContext, Events.EVENT_DISMISS_DIALOG, reason); |
| 267 | } |
| 268 | |
| 269 | public void dump(PrintWriter writer) { |
| 270 | writer.println(VolumeDialogImpl.class.getSimpleName() + " state:"); |
| 271 | writer.print(" mShowing: "); writer.println(mShowing); |
| 272 | } |
| 273 | |
| 274 | private void loadAudioUsageItems() { |
| 275 | try (XmlResourceParser parser = mContext.getResources().getXml(R.xml.car_volume_items)) { |
| 276 | AttributeSet attrs = Xml.asAttributeSet(parser); |
| 277 | int type; |
| 278 | // Traverse to the first start tag |
| 279 | while ((type=parser.next()) != XmlResourceParser.END_DOCUMENT |
| 280 | && type != XmlResourceParser.START_TAG) { |
| 281 | } |
| 282 | |
| 283 | if (!XML_TAG_VOLUME_ITEMS.equals(parser.getName())) { |
| 284 | throw new RuntimeException("Meta-data does not start with carVolumeItems tag"); |
| 285 | } |
| 286 | int outerDepth = parser.getDepth(); |
| 287 | int rank = 0; |
| 288 | while ((type=parser.next()) != XmlResourceParser.END_DOCUMENT |
| 289 | && (type != XmlResourceParser.END_TAG || parser.getDepth() > outerDepth)) { |
| 290 | if (type == XmlResourceParser.END_TAG) { |
| 291 | continue; |
| 292 | } |
| 293 | if (XML_TAG_VOLUME_ITEM.equals(parser.getName())) { |
| 294 | TypedArray item = mContext.getResources().obtainAttributes( |
| 295 | attrs, R.styleable.carVolumeItems_item); |
| 296 | int usage = item.getInt(R.styleable.carVolumeItems_item_usage, -1); |
| 297 | if (usage >= 0) { |
| 298 | VolumeItem volumeItem = new VolumeItem(); |
| 299 | volumeItem.usage = usage; |
| 300 | volumeItem.rank = rank; |
| 301 | volumeItem.icon = item.getResourceId(R.styleable.carVolumeItems_item_icon, 0); |
| 302 | mVolumeItems.put(usage, volumeItem); |
| 303 | rank++; |
| 304 | } |
| 305 | item.recycle(); |
| 306 | } |
| 307 | } |
| 308 | } catch (XmlPullParserException | IOException e) { |
| 309 | Log.e(TAG, "Error parsing volume groups configuration", e); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | private VolumeItem getVolumeItemForUsages(int[] usages) { |
| 314 | int rank = Integer.MAX_VALUE; |
| 315 | VolumeItem result = null; |
| 316 | for (int usage : usages) { |
| 317 | VolumeItem volumeItem = mVolumeItems.get(usage); |
| 318 | if (volumeItem.rank < rank) { |
| 319 | rank = volumeItem.rank; |
| 320 | result = volumeItem; |
| 321 | } |
| 322 | } |
| 323 | return result; |
| 324 | } |
| 325 | |
| 326 | private static int getSeekbarValue(CarAudioManager carAudioManager, int volumeGroupId) { |
| 327 | try { |
| 328 | return carAudioManager.getGroupVolume(volumeGroupId); |
| 329 | } catch (CarNotConnectedException e) { |
| 330 | Log.e(TAG, "Car is not connected!", e); |
| 331 | } |
| 332 | return 0; |
| 333 | } |
| 334 | |
| 335 | private static int getMaxSeekbarValue(CarAudioManager carAudioManager, int volumeGroupId) { |
| 336 | try { |
| 337 | return carAudioManager.getGroupMaxVolume(volumeGroupId); |
| 338 | } catch (CarNotConnectedException e) { |
| 339 | Log.e(TAG, "Car is not connected!", e); |
| 340 | } |
| 341 | return 0; |
| 342 | } |
| 343 | |
| 344 | private SeekbarListItem addSeekbarListItem(VolumeItem volumeItem, int volumeGroupId, |
| 345 | int supplementalIconId, @Nullable View.OnClickListener supplementalIconOnClickListener) { |
| 346 | SeekbarListItem listItem = new SeekbarListItem(mContext); |
| 347 | listItem.setMax(getMaxSeekbarValue(mCarAudioManager, volumeGroupId)); |
Lin Guo | a412b45 | 2018-05-24 17:31:50 -0700 | [diff] [blame] | 348 | int color = mContext.getResources().getColor(R.color.car_volume_dialog_tint); |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 349 | int progress = getSeekbarValue(mCarAudioManager, volumeGroupId); |
| 350 | listItem.setProgress(progress); |
| 351 | listItem.setOnSeekBarChangeListener( |
| 352 | new CarVolumeDialogImpl.VolumeSeekBarChangeListener(volumeGroupId, mCarAudioManager)); |
Lin Guo | a412b45 | 2018-05-24 17:31:50 -0700 | [diff] [blame] | 353 | Drawable primaryIcon = mContext.getResources().getDrawable(volumeItem.icon); |
Brad Stenning | 754f8c9 | 2018-06-19 15:03:46 -0700 | [diff] [blame] | 354 | primaryIcon.mutate().setTint(color); |
Lin Guo | a412b45 | 2018-05-24 17:31:50 -0700 | [diff] [blame] | 355 | listItem.setPrimaryActionIcon(primaryIcon); |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 356 | if (supplementalIconId != 0) { |
| 357 | Drawable supplementalIcon = mContext.getResources().getDrawable(supplementalIconId); |
Brad Stenning | 754f8c9 | 2018-06-19 15:03:46 -0700 | [diff] [blame] | 358 | supplementalIcon.mutate().setTint(color); |
Anthony Chen | df94129 | 2018-09-11 15:35:50 -0700 | [diff] [blame] | 359 | listItem.setSupplementalIcon(supplementalIcon, true); |
| 360 | listItem.setSupplementalIconListener(supplementalIconOnClickListener); |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 361 | } else { |
| 362 | listItem.setSupplementalEmptyIcon(true); |
Anthony Chen | df94129 | 2018-09-11 15:35:50 -0700 | [diff] [blame] | 363 | listItem.setSupplementalIconListener(null); |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | mVolumeLineItems.add(listItem); |
| 367 | volumeItem.listItem = listItem; |
| 368 | volumeItem.progress = progress; |
| 369 | return listItem; |
| 370 | } |
| 371 | |
| 372 | private VolumeItem findVolumeItem(SeekbarListItem targetItem) { |
| 373 | for (int i = 0; i < mVolumeItems.size(); ++i) { |
| 374 | VolumeItem volumeItem = mVolumeItems.valueAt(i); |
| 375 | if (volumeItem.listItem == targetItem) { |
| 376 | return volumeItem; |
| 377 | } |
| 378 | } |
| 379 | return null; |
| 380 | } |
| 381 | |
| 382 | private void cleanupAudioManager() { |
| 383 | try { |
| 384 | mCarAudioManager.unregisterVolumeCallback(mVolumeChangeCallback.asBinder()); |
| 385 | } catch (CarNotConnectedException e) { |
| 386 | Log.e(TAG, "Car is not connected!", e); |
| 387 | } |
| 388 | mVolumeLineItems.clear(); |
| 389 | mCarAudioManager = null; |
| 390 | } |
| 391 | |
| 392 | private final class H extends Handler { |
| 393 | private static final int SHOW = 1; |
| 394 | private static final int DISMISS = 2; |
| 395 | |
| 396 | public H() { |
| 397 | super(Looper.getMainLooper()); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 398 | } |
| 399 | |
| 400 | @Override |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 401 | public void handleMessage(Message msg) { |
| 402 | switch (msg.what) { |
| 403 | case SHOW: |
| 404 | showH(msg.arg1); |
| 405 | break; |
| 406 | case DISMISS: |
| 407 | dismissH(msg.arg1); |
| 408 | break; |
| 409 | default: |
| 410 | } |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | private final class CustomDialog extends Dialog implements DialogInterface { |
| 415 | public CustomDialog(Context context) { |
| 416 | super(context, com.android.systemui.R.style.qs_theme); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 417 | } |
| 418 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 419 | @Override |
| 420 | public boolean dispatchTouchEvent(MotionEvent ev) { |
| 421 | rescheduleTimeoutH(); |
| 422 | return super.dispatchTouchEvent(ev); |
| 423 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 424 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 425 | @Override |
| 426 | protected void onStart() { |
| 427 | super.setCanceledOnTouchOutside(true); |
| 428 | super.onStart(); |
| 429 | } |
| 430 | |
| 431 | @Override |
| 432 | protected void onStop() { |
| 433 | super.onStop(); |
| 434 | } |
| 435 | |
| 436 | @Override |
| 437 | public boolean onTouchEvent(MotionEvent event) { |
| 438 | if (isShowing()) { |
| 439 | if (event.getAction() == MotionEvent.ACTION_OUTSIDE) { |
Lin Guo | 1e7b32f | 2018-06-08 15:15:48 -0700 | [diff] [blame] | 440 | mHandler.obtainMessage( |
| 441 | H.DISMISS, Events.DISMISS_REASON_TOUCH_OUTSIDE).sendToTarget(); |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 442 | return true; |
| 443 | } |
| 444 | } |
| 445 | return false; |
| 446 | } |
| 447 | } |
| 448 | |
| 449 | private final class ExpandIconListener implements View.OnClickListener { |
| 450 | @Override |
| 451 | public void onClick(final View v) { |
| 452 | mExpanded = !mExpanded; |
| 453 | Animator inAnimator; |
| 454 | if (mExpanded) { |
| 455 | for (int groupId = 0; groupId < mAvailableVolumeItems.size(); ++groupId) { |
| 456 | // Adding the items which are not coming from the default item. |
| 457 | VolumeItem volumeItem = mAvailableVolumeItems.get(groupId); |
| 458 | if (volumeItem.defaultItem) { |
| 459 | // Set progress here due to the progress of seekbar may not be updated. |
| 460 | volumeItem.listItem.setProgress(volumeItem.progress); |
| 461 | } else { |
| 462 | addSeekbarListItem(volumeItem, groupId, 0, null); |
| 463 | } |
| 464 | } |
| 465 | inAnimator = AnimatorInflater.loadAnimator( |
| 466 | mContext, R.anim.car_arrow_fade_in_rotate_up); |
| 467 | } else { |
| 468 | // Only keeping the default stream if it is not expended. |
| 469 | Iterator itr = mVolumeLineItems.iterator(); |
| 470 | while (itr.hasNext()) { |
| 471 | SeekbarListItem seekbarListItem = (SeekbarListItem) itr.next(); |
| 472 | VolumeItem volumeItem = findVolumeItem(seekbarListItem); |
| 473 | if (!volumeItem.defaultItem) { |
| 474 | itr.remove(); |
| 475 | } else { |
| 476 | // Set progress here due to the progress of seekbar may not be updated. |
| 477 | seekbarListItem.setProgress(volumeItem.progress); |
| 478 | } |
| 479 | } |
| 480 | inAnimator = AnimatorInflater.loadAnimator( |
| 481 | mContext, R.anim.car_arrow_fade_in_rotate_down); |
| 482 | } |
| 483 | |
| 484 | Animator outAnimator = AnimatorInflater.loadAnimator( |
| 485 | mContext, R.anim.car_arrow_fade_out); |
| 486 | inAnimator.setStartDelay(ARROW_FADE_IN_START_DELAY_IN_MILLIS); |
| 487 | AnimatorSet animators = new AnimatorSet(); |
| 488 | animators.playTogether(outAnimator, inAnimator); |
| 489 | animators.setTarget(v); |
| 490 | animators.start(); |
| 491 | mPagedListAdapter.notifyDataSetChanged(); |
| 492 | } |
| 493 | } |
| 494 | |
| 495 | private final class VolumeSeekBarChangeListener implements OnSeekBarChangeListener { |
| 496 | private final int mVolumeGroupId; |
| 497 | private final CarAudioManager mCarAudioManager; |
| 498 | |
| 499 | private VolumeSeekBarChangeListener(int volumeGroupId, CarAudioManager carAudioManager) { |
| 500 | mVolumeGroupId = volumeGroupId; |
| 501 | mCarAudioManager = carAudioManager; |
| 502 | } |
| 503 | |
| 504 | @Override |
| 505 | public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) { |
| 506 | if (!fromUser) { |
| 507 | // For instance, if this event is originated from AudioService, |
| 508 | // we can ignore it as it has already been handled and doesn't need to be |
| 509 | // sent back down again. |
| 510 | return; |
| 511 | } |
| 512 | try { |
| 513 | if (mCarAudioManager == null) { |
| 514 | Log.w(TAG, "Ignoring volume change event because the car isn't connected"); |
| 515 | return; |
| 516 | } |
| 517 | mAvailableVolumeItems.get(mVolumeGroupId).progress = progress; |
| 518 | mCarAudioManager.setGroupVolume(mVolumeGroupId, progress, 0); |
| 519 | } catch (CarNotConnectedException e) { |
| 520 | Log.e(TAG, "Car is not connected!", e); |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | @Override |
| 525 | public void onStartTrackingTouch(SeekBar seekBar) {} |
| 526 | |
| 527 | @Override |
| 528 | public void onStopTrackingTouch(SeekBar seekBar) {} |
| 529 | } |
| 530 | |
| 531 | private final ICarVolumeCallback mVolumeChangeCallback = new ICarVolumeCallback.Stub() { |
| 532 | @Override |
Hongwei Wang | c8cdc59 | 2018-06-07 12:35:08 -0700 | [diff] [blame] | 533 | public void onGroupVolumeChanged(int groupId, int flags) { |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 534 | VolumeItem volumeItem = mAvailableVolumeItems.get(groupId); |
| 535 | int value = getSeekbarValue(mCarAudioManager, groupId); |
| 536 | // Do not update the progress if it is the same as before. When car audio manager sets its |
| 537 | // group volume caused by the seekbar progress changed, it also triggers this callback. |
| 538 | // Updating the seekbar at the same time could block the continuous seeking. |
| 539 | if (value != volumeItem.progress) { |
| 540 | volumeItem.listItem.setProgress(value); |
| 541 | volumeItem.progress = value; |
Priyank | e5c1db9 | 2018-11-01 17:49:54 -0700 | [diff] [blame] | 542 | } |
| 543 | if ((flags & AudioManager.FLAG_SHOW_UI) != 0) { |
| 544 | show(Events.SHOW_REASON_VOLUME_CHANGED); |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 545 | } |
| 546 | } |
| 547 | |
| 548 | @Override |
Hongwei Wang | c8cdc59 | 2018-06-07 12:35:08 -0700 | [diff] [blame] | 549 | public void onMasterMuteChanged(int flags) { |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 550 | // ignored |
| 551 | } |
| 552 | }; |
| 553 | |
| 554 | private final ServiceConnection mServiceConnection = new ServiceConnection() { |
| 555 | @Override |
| 556 | public void onServiceConnected(ComponentName name, IBinder service) { |
| 557 | try { |
Lin Guo | aea0023 | 2018-04-06 19:18:33 -0700 | [diff] [blame] | 558 | mExpanded = false; |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 559 | mCarAudioManager = (CarAudioManager) mCar.getCarManager(Car.AUDIO_SERVICE); |
| 560 | int volumeGroupCount = mCarAudioManager.getVolumeGroupCount(); |
| 561 | // Populates volume slider items from volume groups to UI. |
| 562 | for (int groupId = 0; groupId < volumeGroupCount; groupId++) { |
| 563 | VolumeItem volumeItem = getVolumeItemForUsages( |
| 564 | mCarAudioManager.getUsagesForVolumeGroupId(groupId)); |
| 565 | mAvailableVolumeItems.add(volumeItem); |
| 566 | // The first one is the default item. |
| 567 | if (groupId == 0) { |
| 568 | volumeItem.defaultItem = true; |
| 569 | addSeekbarListItem(volumeItem, groupId, R.drawable.car_ic_keyboard_arrow_down, |
| 570 | new ExpandIconListener()); |
| 571 | } |
| 572 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 573 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 574 | // If list is already initiated, update its content. |
| 575 | if (mPagedListAdapter != null) { |
| 576 | mPagedListAdapter.notifyDataSetChanged(); |
| 577 | } |
| 578 | mCarAudioManager.registerVolumeCallback(mVolumeChangeCallback.asBinder()); |
| 579 | } catch (CarNotConnectedException e) { |
| 580 | Log.e(TAG, "Car is not connected!", e); |
| 581 | } |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 582 | } |
| 583 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 584 | /** |
| 585 | * This does not get called when service is properly disconnected. |
| 586 | * So we need to also handle cleanups in destroy(). |
| 587 | */ |
| 588 | @Override |
| 589 | public void onServiceDisconnected(ComponentName name) { |
| 590 | cleanupAudioManager(); |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 591 | } |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 592 | }; |
Lujiang Xue | e53f749 | 2018-03-01 09:53:28 -0800 | [diff] [blame] | 593 | |
Lin Guo | d7468d4 | 2018-05-02 14:25:10 -0700 | [diff] [blame] | 594 | /** |
| 595 | * Wrapper class which contains information of each volume group. |
| 596 | */ |
| 597 | private static class VolumeItem { |
| 598 | private @AudioAttributes.AttributeUsage int usage; |
| 599 | private int rank; |
| 600 | private boolean defaultItem = false; |
| 601 | private @DrawableRes int icon; |
| 602 | private SeekbarListItem listItem; |
| 603 | private int progress; |
| 604 | } |
| 605 | } |