UsbModeChooserActivity.java revision 9824c5da1efa9750fb19d477d29875a08289ecf5
1/*
2 * Copyright (C) 2015 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
17package com.android.settings.deviceinfo;
18
19import android.annotation.Nullable;
20import android.app.Activity;
21import android.app.ActivityManager;
22import android.app.AlertDialog;
23import android.content.DialogInterface;
24import android.os.Bundle;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.View.OnClickListener;
28import android.widget.Checkable;
29import android.widget.LinearLayout;
30import android.widget.TextView;
31
32import com.android.settings.R;
33
34/**
35 * UI for the USB chooser dialog.
36 *
37 */
38public class UsbModeChooserActivity extends Activity {
39
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
46    };
47
48    private UsbBackend mBackend;
49    private AlertDialog mDialog;
50    private LayoutInflater mLayoutInflater;
51
52    @Override
53    protected void onCreate(@Nullable Bundle savedInstanceState) {
54
55        super.onCreate(savedInstanceState);
56
57        mLayoutInflater = LayoutInflater.from(this);
58
59        mDialog = new AlertDialog.Builder(this)
60                .setTitle(R.string.usb_use)
61                .setView(R.layout.usb_dialog_container)
62                .setOnDismissListener(new DialogInterface.OnDismissListener() {
63                    @Override
64                    public void onDismiss(DialogInterface dialog) {
65                        finish();
66                    }
67                })
68                .setNegativeButton(R.string.cancel, new DialogInterface.OnClickListener() {
69                    @Override
70                    public void onClick(DialogInterface dialog, int which) {
71                        finish();
72                    }
73                }).create();
74        mDialog.show();
75
76        LinearLayout container = (LinearLayout) mDialog.findViewById(R.id.container);
77
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);
83            }
84        }
85    }
86
87    private void inflateOption(final int mode, boolean selected, LinearLayout container) {
88        View v = mLayoutInflater.inflate(R.layout.radio_with_summary, container, false);
89
90        ((TextView) v.findViewById(android.R.id.title)).setText(getTitle(mode));
91        ((TextView) v.findViewById(android.R.id.summary)).setText(getSummary(mode));
92
93        v.setOnClickListener(new OnClickListener() {
94            @Override
95            public void onClick(View v) {
96                if (!ActivityManager.isUserAMonkey()) {
97                    mBackend.setMode(mode);
98                }
99                mDialog.dismiss();
100                finish();
101            }
102        });
103        ((Checkable) v).setChecked(selected);
104        container.addView(v);
105    }
106
107    private static int getSummary(int mode) {
108        switch (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;
119        }
120        return 0;
121    }
122
123    private static int getTitle(int mode) {
124        switch (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;
135        }
136        return 0;
137    }
138}
139