Home | History | Annotate | Download | only in verifierusbcompanion
      1 /*
      2  * Copyright (C) 2016 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.verifierusbcompanion;
     18 
     19 import android.app.Activity;
     20 import android.os.Bundle;
     21 import android.support.annotation.NonNull;
     22 import android.support.annotation.Nullable;
     23 import android.view.View;
     24 import android.view.WindowManager;
     25 import android.widget.Button;
     26 import android.widget.TextView;
     27 
     28 /**
     29  * UI of this app
     30  */
     31 public class Main extends Activity implements TestCompanion.TestObserver {
     32     private TextView mStatusMessage;
     33     private Button mDeviceTestButton;
     34     private Button mAccessoryTestButton;
     35     private Button mAbortButton;
     36     private TestCompanion mCurrentTest;
     37 
     38     @Override
     39     protected void onCreate(@Nullable Bundle savedInstanceState) {
     40         super.onCreate(savedInstanceState);
     41 
     42         getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
     43 
     44         setContentView(R.layout.main);
     45 
     46         mStatusMessage = (TextView) findViewById(R.id.status_message);
     47         mDeviceTestButton = (Button) findViewById(R.id.deviceTest);
     48         mAccessoryTestButton = (Button) findViewById(R.id.accessoryTest);
     49         mAbortButton = (Button) findViewById(R.id.abort);
     50 
     51         mStatusMessage.setText(getString(R.string.status_no_test));
     52 
     53         mDeviceTestButton.setOnClickListener(view -> runDeviceTest());
     54         mAccessoryTestButton.setOnClickListener(view -> runAccessoryTest());
     55         mAbortButton.setOnClickListener(view -> abortCurrentTest());
     56     }
     57 
     58     /**
     59      * Abort the current test companion.
     60      */
     61     private void abortCurrentTest() {
     62         mCurrentTest.requestAbort();
     63     }
     64 
     65     /**
     66      * Run the {@link DeviceTestCompanion}
     67      */
     68     private void runDeviceTest() {
     69         runTestCompanion(new DeviceTestCompanion(this, this));
     70     }
     71 
     72     /**
     73      * Run the {@link AccessoryTestCompanion}
     74      */
     75     private void runAccessoryTest() {
     76         runTestCompanion(new AccessoryTestCompanion(this, this));
     77     }
     78 
     79     /**
     80      * Run a test.
     81      * @param test The test to run
     82      */
     83     private void runTestCompanion(@NonNull TestCompanion test) {
     84         mAbortButton.setVisibility(View.VISIBLE);
     85         mDeviceTestButton.setVisibility(View.GONE);
     86         mAccessoryTestButton.setVisibility(View.GONE);
     87 
     88         mCurrentTest = test;
     89         test.start();
     90     }
     91 
     92     /**
     93      * Reset the UI after a test
     94      */
     95     private void resetUI() {
     96         mAbortButton.setVisibility(View.GONE);
     97         mDeviceTestButton.setVisibility(View.VISIBLE);
     98         mAccessoryTestButton.setVisibility(View.VISIBLE);
     99     }
    100 
    101     @Override
    102     public void onStatusUpdate(@NonNull CharSequence status) {
    103         mStatusMessage.setText(status);
    104     }
    105 
    106     @Override
    107     public void onSuccess() {
    108         mStatusMessage.setText(getString(R.string.status_finished));
    109         resetUI();
    110     }
    111 
    112     @Override
    113     public void onFail(@NonNull CharSequence error) {
    114         mStatusMessage.setText(error);
    115         resetUI();
    116     }
    117 
    118     @Override
    119     public void onAbort() {
    120         mStatusMessage.setText(getString(R.string.status_abort));
    121         resetUI();
    122     }
    123 }
    124