1 /* 2 * Copyright (C) 2021 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.car.qc; 18 19 import android.app.ActivityOptions; 20 import android.content.Context; 21 import android.content.Intent; 22 import android.content.res.TypedArray; 23 import android.os.UserHandle; 24 import android.util.AttributeSet; 25 import android.util.Log; 26 import android.widget.Button; 27 28 import com.android.car.ui.utils.CarUxRestrictionsUtil; 29 import com.android.systemui.R; 30 31 import java.net.URISyntaxException; 32 33 /** 34 * Footer button for quick control panels. 35 * 36 * Allows for an intent action to be specified via the {@link R.styleable.QCFooterButton_intent} 37 * attribute and for enabled state to be set according to driving mode via the 38 * {@link R.styleable.QCFooterButton_disableWhileDriving} attribute. 39 */ 40 public class QCFooterButton extends Button { 41 private static final String TAG = "QCFooterButton"; 42 private final boolean mDisableWhileDriving; 43 private final CarUxRestrictionsUtil.OnUxRestrictionsChangedListener mListener = 44 carUxRestrictions -> setEnabled(!carUxRestrictions.isRequiresDistractionOptimization()); 45 QCFooterButton(Context context)46 public QCFooterButton(Context context) { 47 this(context, /* attrs= */ null); 48 } 49 QCFooterButton(Context context, AttributeSet attrs)50 public QCFooterButton(Context context, AttributeSet attrs) { 51 this(context, attrs, /* defStyleAttr= */ 0); 52 } 53 QCFooterButton(Context context, AttributeSet attrs, int defStyleAttr)54 public QCFooterButton(Context context, AttributeSet attrs, int defStyleAttr) { 55 this(context, attrs, defStyleAttr, /* defStyleRes= */ 0); 56 } 57 QCFooterButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes)58 public QCFooterButton(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { 59 super(context, attrs, defStyleAttr, defStyleRes); 60 if (attrs == null) { 61 mDisableWhileDriving = false; 62 return; 63 } 64 65 TypedArray typedArray = context.obtainStyledAttributes(attrs, 66 R.styleable.QCFooterButton); 67 String intentString = typedArray.getString(R.styleable.QCFooterButton_intent); 68 if (intentString != null) { 69 Intent intent; 70 try { 71 intent = Intent.parseUri(intentString, Intent.URI_INTENT_SCHEME); 72 } catch (URISyntaxException e) { 73 throw new RuntimeException("Failed to attach intent", e); 74 } 75 Intent finalIntent = intent; 76 setOnClickListener(v -> { 77 mContext.sendBroadcastAsUser(new Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS), 78 UserHandle.CURRENT); 79 try { 80 ActivityOptions options = ActivityOptions.makeBasic(); 81 options.setLaunchDisplayId(mContext.getDisplayId()); 82 mContext.startActivityAsUser(finalIntent, options.toBundle(), 83 UserHandle.CURRENT); 84 } catch (Exception e) { 85 Log.e(TAG, "Failed to launch intent", e); 86 } 87 }); 88 } 89 90 mDisableWhileDriving = typedArray.getBoolean( 91 R.styleable.QCFooterButton_disableWhileDriving, /* defValue= */ false); 92 } 93 94 @Override onAttachedToWindow()95 protected void onAttachedToWindow() { 96 super.onAttachedToWindow(); 97 if (mDisableWhileDriving) { 98 CarUxRestrictionsUtil.getInstance(mContext).register(mListener); 99 } 100 } 101 102 @Override onDetachedFromWindow()103 protected void onDetachedFromWindow() { 104 super.onDetachedFromWindow(); 105 if (mDisableWhileDriving) { 106 CarUxRestrictionsUtil.getInstance(mContext).unregister(mListener); 107 } 108 } 109 } 110