2 * Copyright (C) 2015 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.
17 package com.android.settings.deviceinfo;
19 import android.annotation.Nullable;
20 import android.app.Activity;
21 import android.app.ActivityManager;
22 import android.app.AlertDialog;
23 import android.content.DialogInterface;
24 import android.os.Bundle;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.View.OnClickListener;
28 import android.widget.Checkable;
29 import android.widget.LinearLayout;
30 import android.widget.TextView;
32 import com.android.settings.R;
35 * UI for the USB chooser dialog.
38 public class UsbModeChooserActivity extends Activity {
40 public static final int[] DEFAULT_MODES = {
41 UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_NONE,
42 UsbBackend.MODE_POWER_SOURCE | UsbBackend.MODE_DATA_NONE,
43 UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MTP,
44 UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_PTP,
45 UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MIDI
48 private UsbBackend mBackend;
49 private AlertDialog mDialog;
50 private LayoutInflater mLayoutInflater;
53 protected void onCreate(@Nullable Bundle savedInstanceState) {
55 super.onCreate(savedInstanceState);
57 mLayoutInflater = LayoutInflater.from(this);
59 mDialog = new AlertDialog.Builder(this)
60 .setTitle(R.string.usb_use)
61 .setView(R.layout.usb_dialog_container)
62 .setOnDismissListener(new DialogInterface.OnDismissListener() {
64 public void onDismiss(DialogInterface dialog) {
68 .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
70 public void onClick(DialogInterface dialog, int which) {
76 LinearLayout container = (LinearLayout) mDialog.findViewById(R.id.container);
78 mBackend = new UsbBackend(this);
79 int current = mBackend.getCurrentMode();
80 for (int i = 0; i < DEFAULT_MODES.length; i++) {
81 if (mBackend.isModeSupported(DEFAULT_MODES[i])) {
82 inflateOption(DEFAULT_MODES[i], current == DEFAULT_MODES[i], container);
87 private void inflateOption(final int mode, boolean selected, LinearLayout container) {
88 View v = mLayoutInflater.inflate(R.layout.radio_with_summary, container, false);
90 ((TextView) v.findViewById(android.R.id.title)).setText(getTitle(mode));
91 ((TextView) v.findViewById(android.R.id.summary)).setText(getSummary(mode));
93 v.setOnClickListener(new OnClickListener() {
95 public void onClick(View v) {
96 if (!ActivityManager.isUserAMonkey()) {
97 mBackend.setMode(mode);
103 ((Checkable) v).setChecked(selected);
104 container.addView(v);
107 private static int getSummary(int mode) {
109 case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_NONE:
110 return R.string.usb_use_charging_only_desc;
111 case UsbBackend.MODE_POWER_SOURCE | UsbBackend.MODE_DATA_NONE:
112 return R.string.usb_use_power_only_desc;
113 case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MTP:
114 return R.string.usb_use_file_transfers_desc;
115 case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_PTP:
116 return R.string.usb_use_photo_transfers_desc;
117 case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MIDI:
118 return R.string.usb_use_MIDI_desc;
123 private static int getTitle(int mode) {
125 case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_NONE:
126 return R.string.usb_use_charging_only;
127 case UsbBackend.MODE_POWER_SOURCE | UsbBackend.MODE_DATA_NONE:
128 return R.string.usb_use_power_only;
129 case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MTP:
130 return R.string.usb_use_file_transfers;
131 case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_PTP:
132 return R.string.usb_use_photo_transfers;
133 case UsbBackend.MODE_POWER_SINK | UsbBackend.MODE_DATA_MIDI:
134 return R.string.usb_use_MIDI;