Home | History | Annotate | Download | only in accessory
      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.verifier.usb.accessory;
     18 
     19 import android.app.Activity;
     20 import android.hardware.usb.UsbAccessory;
     21 import android.hardware.usb.UsbManager;
     22 import android.os.Bundle;
     23 import androidx.annotation.NonNull;
     24 import androidx.annotation.Nullable;
     25 
     26 import java.util.ArrayList;
     27 
     28 /**
     29  * Utility to receive callbacks when an USB accessory is attached.
     30  */
     31 public class AccessoryAttachmentHandler extends Activity {
     32     private static final ArrayList<AccessoryAttachmentObserver> sObservers = new ArrayList<>();
     33 
     34     /**
     35      * Register an observer to be called when an USB accessory connects.
     36      *
     37      * @param observer The observer that should be called when an USB accessory connects.
     38      */
     39     static void addObserver(@NonNull AccessoryAttachmentObserver observer) {
     40         synchronized (sObservers) {
     41             sObservers.add(observer);
     42         }
     43     }
     44 
     45     /**
     46      * Remove an observer that was added in {@link #addObserver}.
     47      *
     48      * @param observer The observer to remove
     49      */
     50     static void removeObserver(@NonNull AccessoryAttachmentObserver observer) {
     51         synchronized (sObservers) {
     52             sObservers.remove(observer);
     53         }
     54     }
     55 
     56     @Override
     57     protected void onCreate(@Nullable Bundle savedInstanceState) {
     58         super.onCreate(savedInstanceState);
     59 
     60         UsbAccessory accessory = getIntent().getParcelableExtra(UsbManager.EXTRA_ACCESSORY);
     61 
     62         synchronized (sObservers) {
     63             ArrayList<AccessoryAttachmentObserver> observers =
     64                     (ArrayList<AccessoryAttachmentObserver>) sObservers.clone();
     65 
     66             for (AccessoryAttachmentObserver observer : observers) {
     67                 observer.onAttached(accessory);
     68             }
     69         }
     70 
     71         finish();
     72     }
     73 
     74     /**
     75      * Callback when an accessory is attached
     76      */
     77     interface AccessoryAttachmentObserver {
     78         void onAttached(UsbAccessory accessory);
     79     }
     80 }
     81