aboutsummaryrefslogtreecommitdiffstats
path: root/OpenKeychain/src/main/java/org/sufficientlysecure/keychain/util/UsbConnectionDispatcher.java
blob: 7a8e65ae43d4519364f0e03863b3e0bbd4e5db8d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
/*
 * Copyright (C) 2016 Nikita Mikhailov <nikita.s.mikhailov@gmail.com>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

package org.sufficientlysecure.keychain.util;

import android.app.Activity;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.hardware.usb.UsbDevice;
import android.hardware.usb.UsbManager;

import org.sufficientlysecure.keychain.Constants;
import org.sufficientlysecure.keychain.ui.UsbEventReceiverActivity;

public class UsbConnectionDispatcher {
    private Activity mActivity;

    private OnDiscoveredUsbDeviceListener mListener;
    private UsbManager mUsbManager;

    /**
     * Receives broadcast when a supported USB device get permission.
     */
    private final BroadcastReceiver mUsbReceiver = new BroadcastReceiver() {
        @Override
        public void onReceive(Context context, Intent intent) {
            String action = intent.getAction();

            switch (action) {
                case UsbEventReceiverActivity.ACTION_USB_PERMISSION: {
                    UsbDevice usbDevice = intent.getParcelableExtra(UsbManager.EXTRA_DEVICE);
                    boolean permission = intent.getBooleanExtra(UsbManager.EXTRA_PERMISSION_GRANTED,
                            false);
                    if (permission) {
                        Log.d(Constants.TAG, "Got permission for " + usbDevice.getDeviceName());
                        mListener.usbDeviceDiscovered(usbDevice);
                    }
                    break;
                }
            }
        }
    };

    public UsbConnectionDispatcher(final Activity activity, final OnDiscoveredUsbDeviceListener listener) {
        this.mActivity = activity;
        this.mListener = listener;
        this.mUsbManager = (UsbManager) activity.getSystemService(Context.USB_SERVICE);
    }

    public void onStart() {
        final IntentFilter intentFilter = new IntentFilter();
        intentFilter.addAction(UsbEventReceiverActivity.ACTION_USB_PERMISSION);

        mActivity.registerReceiver(mUsbReceiver, intentFilter);
    }

    public void onStop() {
        mActivity.unregisterReceiver(mUsbReceiver);
    }

    /**
     * Rescans devices and triggers {@link OnDiscoveredUsbDeviceListener}
     */
    public void rescanDevices() {
        // Note: we don't check devices VID/PID because
        // we check for permission instead.
        // We should have permission only for matching devices
        for (UsbDevice device : mUsbManager.getDeviceList().values()) {
            if (mUsbManager.hasPermission(device)) {
                if (mListener != null) {
                    mListener.usbDeviceDiscovered(device);
                }
                break;
            }
        }
    }

    public interface OnDiscoveredUsbDeviceListener {
        void usbDeviceDiscovered(UsbDevice usbDevice);
    }
}