• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package com.android.systemui.statusbar.car;
2 
3 import android.content.Context;
4 import android.content.Intent;
5 import android.content.res.TypedArray;
6 import android.os.UserHandle;
7 import android.util.AttributeSet;
8 import android.util.Log;
9 import android.widget.ImageView;
10 
11 import com.android.systemui.R;
12 
13 import java.net.URISyntaxException;
14 
15 /**
16  * CarNavigationButton is an image button that allows for a bit more configuration at the
17  * xml file level. This allows for more control via overlays instead of having to update
18  * code.
19  */
20 public class CarNavigationButton extends com.android.keyguard.AlphaOptimizedImageButton {
21 
22     private static final String TAG = "CarNavigationButton";
23     private Context mContext;
24     private String mIntent;
25     private String mLongIntent;
26     private boolean mBroadcastIntent;
27     private boolean mSelected = false;
28     private float mSelectedAlpha = 1f;
29     private float mUnselectedAlpha = 1f;
30     private int mSelectedIconResourceId;
31     private int mIconResourceId;
32 
33 
CarNavigationButton(Context context, AttributeSet attrs)34     public CarNavigationButton(Context context, AttributeSet attrs) {
35         super(context, attrs);
36         mContext = context;
37         TypedArray typedArray = context.obtainStyledAttributes(
38                 attrs, R.styleable.CarNavigationButton);
39         mIntent = typedArray.getString(R.styleable.CarNavigationButton_intent);
40         mLongIntent = typedArray.getString(R.styleable.CarNavigationButton_longIntent);
41         mBroadcastIntent = typedArray.getBoolean(R.styleable.CarNavigationButton_broadcast, false);
42         mSelectedAlpha = typedArray.getFloat(
43                 R.styleable.CarNavigationButton_selectedAlpha, mSelectedAlpha);
44         mUnselectedAlpha = typedArray.getFloat(
45                 R.styleable.CarNavigationButton_unselectedAlpha, mUnselectedAlpha);
46         mIconResourceId = typedArray.getResourceId(
47                 com.android.internal.R.styleable.ImageView_src, 0);
48         mSelectedIconResourceId = typedArray.getResourceId(
49                 R.styleable.CarNavigationButton_selectedIcon, mIconResourceId);
50     }
51 
52 
53     /**
54      * After the standard inflate this then adds the xml defined intents to click and long click
55      * actions if defined.
56      */
57     @Override
onFinishInflate()58     public void onFinishInflate() {
59         super.onFinishInflate();
60         setScaleType(ImageView.ScaleType.CENTER);
61         setAlpha(mUnselectedAlpha);
62         try {
63             if (mIntent != null) {
64                 final Intent intent = Intent.parseUri(mIntent, Intent.URI_INTENT_SCHEME);
65                 setOnClickListener(v -> {
66                     try {
67                         if (mBroadcastIntent) {
68                             mContext.sendBroadcastAsUser(intent, UserHandle.CURRENT);
69                             return;
70                         }
71                         mContext.startActivityAsUser(intent, UserHandle.CURRENT);
72                     } catch (Exception e) {
73                         Log.e(TAG, "Failed to launch intent", e);
74                     }
75                 });
76             }
77         } catch (URISyntaxException e) {
78             throw new RuntimeException("Failed to attach intent", e);
79         }
80 
81         try {
82             if (mLongIntent != null) {
83                 final Intent intent = Intent.parseUri(mLongIntent, Intent.URI_INTENT_SCHEME);
84                 setOnLongClickListener(v -> {
85                     try {
86                         mContext.startActivityAsUser(intent, UserHandle.CURRENT);
87                     } catch (Exception e) {
88                         Log.e(TAG, "Failed to launch intent", e);
89                     }
90                     // consume event either way
91                     return true;
92                 });
93             }
94         } catch (URISyntaxException e) {
95             throw new RuntimeException("Failed to attach long press intent", e);
96         }
97     }
98 
99     /**
100      * @param selected true if should indicate if this is a selected state, false otherwise
101      */
setSelected(boolean selected)102     public void setSelected(boolean selected) {
103         super.setSelected(selected);
104         mSelected = selected;
105         setAlpha(mSelected ? mSelectedAlpha : mUnselectedAlpha);
106         setImageResource(mSelected ? mSelectedIconResourceId : mIconResourceId);
107     }
108 }
109