2 * Copyright (C) 2017 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
16 package com.android.settings.connecteddevice.usb;
18 import android.content.Context;
19 import android.os.Bundle;
20 import android.support.annotation.VisibleForTesting;
21 import android.support.v14.preference.PreferenceFragment;
23 import com.android.settings.R;
24 import com.android.settings.SettingsActivity;
25 import com.android.settings.connecteddevice.DevicePreferenceCallback;
26 import com.android.settings.dashboard.DashboardFragment;
27 import com.android.settings.widget.GearPreference;
30 * Controller to maintain connected usb device
32 public class ConnectedUsbDeviceUpdater {
33 private PreferenceFragment mFragment;
34 private UsbBackend mUsbBackend;
35 private DevicePreferenceCallback mDevicePreferenceCallback;
37 GearPreference mUsbPreference;
39 UsbConnectionBroadcastReceiver mUsbReceiver;
42 UsbConnectionBroadcastReceiver.UsbConnectionListener mUsbConnectionListener =
43 (connected, newMode) -> {
45 mUsbPreference.setSummary(
46 UsbModePreferenceController.getSummary(mUsbBackend.getCurrentMode()));
47 mDevicePreferenceCallback.onDeviceAdded(mUsbPreference);
49 mDevicePreferenceCallback.onDeviceRemoved(mUsbPreference);
53 public ConnectedUsbDeviceUpdater(DashboardFragment fragment,
54 DevicePreferenceCallback devicePreferenceCallback) {
55 this(fragment, devicePreferenceCallback, new UsbBackend(fragment.getContext()));
59 ConnectedUsbDeviceUpdater(DashboardFragment fragment,
60 DevicePreferenceCallback devicePreferenceCallback, UsbBackend usbBackend) {
62 mDevicePreferenceCallback = devicePreferenceCallback;
63 mUsbBackend = usbBackend;
64 mUsbReceiver = new UsbConnectionBroadcastReceiver(fragment.getContext(),
65 mUsbConnectionListener, mUsbBackend);
68 public void registerCallback() {
69 // This method could handle multiple register
70 mUsbReceiver.register();
73 public void unregisterCallback() {
74 mUsbReceiver.unregister();
77 public void initUsbPreference(Context context) {
78 mUsbPreference = new GearPreference(context, null /* AttributeSet */);
79 mUsbPreference.setTitle(R.string.usb_pref);
80 mUsbPreference.setIcon(R.drawable.ic_usb);
81 mUsbPreference.setSelectable(false);
82 mUsbPreference.setOnGearClickListener((GearPreference p) -> {
83 // New version - uses a separate screen.
84 final Bundle args = new Bundle();
85 final SettingsActivity activity = (SettingsActivity) mFragment.getContext();
86 activity.startPreferencePanel(mFragment,
87 UsbDetailsFragment.class.getName(), args,
88 R.string.device_details_title, null /* titleText */, null /* resultTo */, 0);
94 private void forceUpdate() {
95 // Register so we can get the connection state from sticky intent.
96 //TODO(b/70336520): Use an API to get data instead of sticky intent
97 mUsbReceiver.register();