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 
17 package com.android.cts.verifier.audio;
18 
19 import android.app.AlertDialog;
20 import android.content.DialogInterface;
21 import android.content.res.Resources;
22 import android.graphics.Color;
23 import android.os.Bundle;
24 import android.util.Log;
25 import android.view.KeyEvent;
26 import android.widget.TextView;
27 
28 import com.android.cts.verifier.audio.peripheralprofile.PeripheralProfile;
29 import com.android.cts.verifier.audio.peripheralprofile.ProfileButtonAttributes;
30 
31 import com.android.cts.verifier.R;  // needed to access resource in CTSVerifier project namespace.
32 
33 public class USBAudioPeripheralButtonsActivity extends USBAudioPeripheralActivity {
34     private static final String TAG = "USBAudioPeripheralButtonsActivity";
35 
36     // State
37     private boolean mHasBtnA;
38     private boolean mHasBtnB;
39     private boolean mHasBtnC;
40 
41     // Widgets
42     private TextView mBtnALabelTxt;
43     private TextView mBtnBLabelTxt;
44     private TextView mBtnCLabelTxt;
45 
46     private TextView mBtnAStatusTxt;
47     private TextView mBtnBStatusTxt;
48     private TextView mBtnCStatusTxt;
49 
USBAudioPeripheralButtonsActivity()50     public USBAudioPeripheralButtonsActivity() {
51         super(false); // Mandated peripheral is NOT required
52     }
53 
showDisableAssistantDialog()54     private void showDisableAssistantDialog() {
55         AlertDialog.Builder builder =
56                 new AlertDialog.Builder(this, android.R.style.Theme_Material_Dialog_Alert);
57         builder.setTitle(getResources().getString(R.string.uapButtonsDisableAssistantTitle));
58         builder.setMessage(getResources().getString(R.string.uapButtonsDisableAssistant));
59         builder.setPositiveButton(android.R.string.yes,
60             new DialogInterface.OnClickListener() {
61                 public void onClick(DialogInterface dialog, int which) {}
62          });
63         builder.setIcon(android.R.drawable.ic_dialog_alert);
64         builder.show();
65     }
66 
67     @Override
onCreate(Bundle savedInstanceState)68     protected void onCreate(Bundle savedInstanceState) {
69         super.onCreate(savedInstanceState);
70         setContentView(R.layout.uap_buttons_panel);
71 
72         connectPeripheralStatusWidgets();
73 
74         mBtnALabelTxt = (TextView)findViewById(R.id.uap_buttonsBtnALabelTx);
75         mBtnBLabelTxt = (TextView)findViewById(R.id.uap_buttonsBtnBLabelTx);
76         mBtnCLabelTxt = (TextView)findViewById(R.id.uap_buttonsBtnCLabelTx);
77 
78         mBtnAStatusTxt = (TextView)findViewById(R.id.uap_buttonsBtnAStatusTx);
79         mBtnBStatusTxt = (TextView)findViewById(R.id.uap_buttonsBtnBStatusTx);
80         mBtnCStatusTxt = (TextView)findViewById(R.id.uap_buttonsBtnCStatusTx);
81 
82         setPassFailButtonClickListeners();
83         setInfoResources(R.string.usbaudio_buttons_test, R.string.usbaudio_buttons_info, -1);
84 
85         showDisableAssistantDialog();
86     }
87 
showButtonsState()88     private void showButtonsState() {
89         int ctrlColor = mIsPeripheralAttached ? Color.WHITE : Color.GRAY;
90         mBtnALabelTxt.setTextColor(ctrlColor);
91         mBtnAStatusTxt.setTextColor(ctrlColor);
92         mBtnBLabelTxt.setTextColor(ctrlColor);
93         mBtnBStatusTxt.setTextColor(ctrlColor);
94         mBtnCLabelTxt.setTextColor(ctrlColor);
95         mBtnCStatusTxt.setTextColor(ctrlColor);
96 
97         mBtnAStatusTxt.setText(getString(
98             mHasBtnA ? R.string.uapButtonsRecognized : R.string.uapButtonsNotRecognized));
99         mBtnBStatusTxt.setText(getString(
100             mHasBtnB ? R.string.uapButtonsRecognized : R.string.uapButtonsNotRecognized));
101         mBtnCStatusTxt.setText(getString(
102             mHasBtnC ? R.string.uapButtonsRecognized : R.string.uapButtonsNotRecognized));
103 
104         calculateMatch();
105     }
106 
calculateMatch()107     private void calculateMatch() {
108         if (mIsPeripheralAttached) {
109             boolean match = mHasBtnA && mHasBtnB && mHasBtnC;
110             Log.i(TAG, "match:" + match);
111             getPassButton().setEnabled(match);
112         } else {
113             getPassButton().setEnabled(false);
114         }
115     }
116 
117     @Override
onKeyDown(int keyCode, KeyEvent event)118     public boolean onKeyDown(int keyCode, KeyEvent event) {
119         // Log.i(TAG, "onKeyDown(" + keyCode + ")");
120         switch (keyCode) {
121         // Function A control event
122         case KeyEvent.KEYCODE_MEDIA_PLAY_PAUSE:
123             mHasBtnA = true;
124             break;
125 
126         // Function B control event
127         case KeyEvent.KEYCODE_VOLUME_UP:
128             mHasBtnB = true;
129             break;
130 
131         // Function C control event
132         case KeyEvent.KEYCODE_VOLUME_DOWN:
133             mHasBtnC = true;
134             break;
135         }
136 
137         showButtonsState();
138         calculateMatch();
139 
140         return super.onKeyDown(keyCode, event);
141     }
142 
143     //
144     // USBAudioPeripheralActivity
145     //
updateConnectStatus()146     public void updateConnectStatus() {
147         mHasBtnA = mHasBtnB = mHasBtnC = false;
148         showButtonsState();
149         calculateMatch();
150     }
151 }
152 
153