OSDN Git Service

Merge "Zen Condition text and primary click changes"
[android-x86/packages-apps-Settings.git] / src / com / android / settings / development / SelectUsbConfigPreferenceController.java
1 /*
2  * Copyright (C) 2017 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.settings.development;
18
19 import android.content.BroadcastReceiver;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.content.IntentFilter;
23 import android.content.pm.PackageManager;
24 import android.hardware.usb.UsbManager;
25 import android.os.Bundle;
26 import android.support.annotation.VisibleForTesting;
27 import android.support.v7.preference.ListPreference;
28 import android.support.v7.preference.Preference;
29 import android.support.v7.preference.PreferenceScreen;
30
31 import com.android.settings.R;
32 import com.android.settings.Utils;
33 import com.android.settings.core.PreferenceControllerMixin;
34 import com.android.settings.connecteddevice.usb.UsbBackend;
35 import com.android.settingslib.core.lifecycle.Lifecycle;
36 import com.android.settingslib.core.lifecycle.LifecycleObserver;
37 import com.android.settingslib.core.lifecycle.events.OnCreate;
38 import com.android.settingslib.core.lifecycle.events.OnDestroy;
39 import com.android.settingslib.development.DeveloperOptionsPreferenceController;
40
41 public class SelectUsbConfigPreferenceController extends
42         DeveloperOptionsPreferenceController implements
43         Preference.OnPreferenceChangeListener, LifecycleObserver, OnCreate, OnDestroy,
44         PreferenceControllerMixin {
45
46     private static final String USB_CONFIGURATION_KEY = "select_usb_configuration";
47
48     private final String[] mListValues;
49     private final String[] mListSummaries;
50     private final UsbManager mUsbManager;
51     @VisibleForTesting
52     UsbBackend.UsbManagerPassThrough mUsbManagerPassThrough;
53     private BroadcastReceiver mUsbReceiver;
54     private ListPreference mPreference;
55
56     public SelectUsbConfigPreferenceController(Context context, Lifecycle lifecycle) {
57         super(context);
58
59         mListValues = context.getResources().getStringArray(R.array.usb_configuration_values);
60         mListSummaries = context.getResources().getStringArray(R.array.usb_configuration_titles);
61         mUsbManager = (UsbManager) context.getSystemService(Context.USB_SERVICE);
62         mUsbManagerPassThrough = new UsbBackend.UsbManagerPassThrough(mUsbManager);
63         mUsbReceiver = new BroadcastReceiver() {
64             @Override
65             public void onReceive(Context context, Intent intent) {
66                 if (mPreference != null) {
67                     updateUsbConfigurationValues();
68                 }
69             }
70         };
71         if (lifecycle != null) {
72             lifecycle.addObserver(this);
73         }
74     }
75
76     @Override
77     public void onCreate(Bundle savedInstanceState) {
78         IntentFilter filter = new IntentFilter();
79         filter.addAction(UsbManager.ACTION_USB_STATE);
80         mContext.registerReceiver(mUsbReceiver, filter);
81     }
82
83     @Override
84     public String getPreferenceKey() {
85         return USB_CONFIGURATION_KEY;
86     }
87
88     @Override
89     public void displayPreference(PreferenceScreen screen) {
90         super.displayPreference(screen);
91
92         mPreference = (ListPreference) screen.findPreference(getPreferenceKey());
93     }
94
95     @Override
96     public boolean onPreferenceChange(Preference preference, Object newValue) {
97         if (Utils.isMonkeyRunning()) {
98             return false;
99         }
100
101         writeUsbConfigurationOption(mUsbManagerPassThrough
102                 .usbFunctionsFromString(newValue.toString()));
103         updateUsbConfigurationValues();
104         return true;
105     }
106
107     @Override
108     public void updateState(Preference preference) {
109         updateUsbConfigurationValues();
110     }
111
112     @Override
113     public void onDestroy() {
114         mContext.unregisterReceiver(mUsbReceiver);
115     }
116
117     @Override
118     public boolean isAvailable() {
119         final PackageManager packageManager = mContext.getPackageManager();
120
121         return packageManager.hasSystemFeature(PackageManager.FEATURE_USB_HOST)
122                 || packageManager.hasSystemFeature(PackageManager.FEATURE_USB_ACCESSORY);
123     }
124
125     @Override
126     protected void onDeveloperOptionsSwitchEnabled() {
127         mPreference.setEnabled(true);
128     }
129
130     @Override
131     protected void onDeveloperOptionsSwitchDisabled() {
132         mPreference.setEnabled(false);
133     }
134
135     @VisibleForTesting
136     void setCurrentFunctions(long functions) {
137         mUsbManager.setCurrentFunctions(functions);
138     }
139
140     private void updateUsbConfigurationValues() {
141         long functions = mUsbManagerPassThrough.getCurrentFunctions();
142         int index = 0;
143         for (int i = 0; i < mListValues.length; i++) {
144             if (functions == mUsbManagerPassThrough.usbFunctionsFromString(mListValues[i])) {
145                 index = i;
146                 break;
147             }
148         }
149         mPreference.setValue(mListValues[index]);
150         mPreference.setSummary(mListSummaries[index]);
151     }
152
153     private void writeUsbConfigurationOption(long newValue) {
154         setCurrentFunctions(newValue);
155     }
156 }