Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit eb3885d6 authored by Mike Lockwood's avatar Mike Lockwood Committed by Android (Google) Code Review
Browse files

Merge "USB accessory support library"

parents c93f67c2 27555315
Loading
Loading
Loading
Loading
+20 −0
Original line number Diff line number Diff line
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2011 The Android Open Source Project

     Licensed under the Apache License, Version 2.0 (the "License");
     you may not use this file except in compliance with the License.
     You may obtain a copy of the License at
  
          http://www.apache.org/licenses/LICENSE-2.0
  
     Unless required by applicable law or agreed to in writing, software
     distributed under the License is distributed on an "AS IS" BASIS,
     WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     See the License for the specific language governing permissions and
     limitations under the License.
-->

<permissions>
    <library name="com.google.android.usb"
            file="/system/framework/com.google.android.usb.jar" />
</permissions>

libs/usb/Android.mk

0 → 100644
+27 −0
Original line number Diff line number Diff line
#
# Copyright (C) 2008 The Android Open Source Project
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
#      http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#

LOCAL_PATH := $(call my-dir)

include $(CLEAR_VARS)

LOCAL_SRC_FILES := $(call all-java-files-under,src)

LOCAL_MODULE_TAGS := optional

LOCAL_MODULE:= com.google.android.usb

include $(BUILD_JAVA_LIBRARY)
+96 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.google.android.usb;

/**
 * A class representing a USB accessory.
 */
public final class UsbAccessory {

    private final String mManufacturer;
    private final String mModel;
    private final String mType;
    private final String mVersion;

    /* package */ UsbAccessory(android.hardware.UsbAccessory accessory) {
        mManufacturer = accessory.getManufacturer();
        mModel = accessory.getModel();
        mType = accessory.getType();
        mVersion = accessory.getVersion();
    }

    /**
     * Returns the manufacturer of the accessory.
     *
     * @return the accessory manufacturer
     */
    public String getManufacturer() {
        return mManufacturer;
    }

    /**
     * Returns the model name of the accessory.
     *
     * @return the accessory model
     */
    public String getModel() {
        return mModel;
    }

    /**
     * Returns the type of the accessory.
     *
     * @return the accessory type
     */
    public String getType() {
        return mType;
    }

    /**
     * Returns the version of the accessory.
     *
     * @return the accessory version
     */
    public String getVersion() {
        return mVersion;
    }

    private static boolean compare(String s1, String s2) {
        if (s1 == null) return (s2 == null);
        return s1.equals(s2);
    }

    @Override
    public boolean equals(Object obj) {
        if (obj instanceof UsbAccessory) {
            UsbAccessory accessory = (UsbAccessory)obj;
            return (compare(mManufacturer, accessory.getManufacturer()) &&
                    compare(mModel, accessory.getModel()) &&
                    compare(mType, accessory.getType()) &&
                    compare(mVersion, accessory.getVersion()));
        }
        return false;
    }

    @Override
    public String toString() {
        return "UsbAccessory[mManufacturer=" + mManufacturer +
                            ", mModel=" + mModel +
                            ", mType=" + mType +
                            ", mVersion=" + mVersion + "]";
    }
}
+127 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2011 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */


package com.google.android.usb;

import android.content.Context;
import android.content.Intent;
import android.hardware.IUsbManager;
import android.os.IBinder;
import android.os.ParcelFileDescriptor;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

/**
 * This class allows you to access the state of USB, both in host and device mode.
 *
 * <p>You can obtain an instance of this class by calling {@link #getInstance}
 *
 */
public class UsbManager {
    private static final String TAG = "UsbManager";

   /**
     * Broadcast Action:  A broadcast for USB accessory attached event.
     *
     * This intent is sent when a USB accessory is attached.
     * Call {@link #getAccessory(android.content.Intent)} to retrieve the
     * {@link com.google.android.usb.UsbAccessory} for the attached accessory.
     */
    public static final String ACTION_USB_ACCESSORY_ATTACHED =
            "android.hardware.action.USB_ACCESSORY_ATTACHED";

   /**
     * Broadcast Action:  A broadcast for USB accessory detached event.
     *
     * This intent is sent when a USB accessory is detached.
     * Call {@link #getAccessory(android.content.Intent)} to retrieve the
     * {@link com.google.android.usb.UsbAccessory} for the attached accessory that was detached.
     */
    public static final String ACTION_USB_ACCESSORY_DETACHED =
            "android.hardware.action.USB_ACCESSORY_DETACHED";

    private final IUsbManager mService;

    private UsbManager(IUsbManager service) {
        mService = service;
    }

    /**
     * Returns a new instance of this class.
     *
     * @return UsbManager instance.
     */
    public static UsbManager getInstance() {
        IBinder b = ServiceManager.getService(Context.USB_SERVICE);
        return new UsbManager(IUsbManager.Stub.asInterface(b));
    }

    /**
     * Returns the {@link com.google.android.usb.UsbAccessory} for
     * a {@link #ACTION_USB_ACCESSORY_ATTACHED} or {@link #ACTION_USB_ACCESSORY_ATTACHED}
     * broadcast Intent
     *
     * @return UsbAccessory for the broadcast.
     */
    public static UsbAccessory getAccessory(Intent intent) {
        android.hardware.UsbAccessory accessory =
            intent.getParcelableExtra(android.hardware.UsbManager.EXTRA_ACCESSORY);
        if (accessory == null) {
            return null;
        } else {
            return new UsbAccessory(accessory);
        }
    }

    /**
     * Returns a list of currently attached USB accessories.
     * (in the current implementation there can be at most one)
     *
     * @return list of USB accessories, or null if none are attached.
     */
    public UsbAccessory[] getAccessoryList() {
        try {
            android.hardware.UsbAccessory accessory = mService.getCurrentAccessory();
            if (accessory == null) {
                return null;
            } else {
                return new UsbAccessory[] { new UsbAccessory(accessory) };
            }
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in getAccessoryList" , e);
            return null;
        }
    }

    /**
     * Opens a file descriptor for reading and writing data to the USB accessory.
     *
     * @param accessory the USB accessory to open
     * @return file descriptor, or null if the accessor could not be opened.
     */
    public ParcelFileDescriptor openAccessory(UsbAccessory accessory) {
        try {
            return mService.openAccessory(new android.hardware.UsbAccessory(
                    accessory.getManufacturer(),accessory.getModel(),
                    accessory.getType(), accessory.getVersion()));
        } catch (RemoteException e) {
            Log.e(TAG, "RemoteException in openAccessory" , e);
            return null;
        }
    }
}
+15 −0
Original line number Diff line number Diff line
LOCAL_PATH:= $(call my-dir)
include $(CLEAR_VARS)

LOCAL_MODULE_TAGS := tests

LOCAL_SRC_FILES := $(call all-subdir-java-files)

LOCAL_PACKAGE_NAME := AccessoryChatGB

LOCAL_JAVA_LIBRARIES := com.google.android.usb

# Force an old SDK version to make sure we aren't using newer UsbManager APIs
LOCAL_SDK_VERSION := 8

include $(BUILD_PACKAGE)
Loading