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