UsbDevicePreference.java revision b6af5c511a7f78e6bec870a5a0f660dabda45665
1/*
2 * Copyright (C) 2016 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 */
16package com.google.android.car.kitchensink.setting.usb;
17
18import android.app.AlertDialog;
19import android.content.Context;
20import android.content.DialogInterface;
21import android.preference.Preference;
22
23import com.google.android.car.kitchensink.R;
24
25/**
26 * Setting preferabce used for USB devices.
27 */
28public final class UsbDevicePreference extends Preference
29        implements Preference.OnPreferenceClickListener {
30
31    /**
32     * Callbacks to handle preference changes.
33     */
34    public interface UsbDevicePreferenceCallback {
35        /** Preference deleted */
36        void onUsbDevicePreferenceDelete(Preference preference, UsbDeviceSettings settings);
37    }
38
39    private static final String TAG = UsbDevicePreference.class.getSimpleName();
40
41    private final UsbDeviceSettings mUsbDeviceSettings;
42    private final UsbDevicePreferenceCallback mCallback;
43
44    public UsbDevicePreference(Context context, UsbDeviceSettings usbDeviceSettings,
45            UsbDevicePreferenceCallback callback) {
46        super(context);
47        mCallback = callback;
48        mUsbDeviceSettings = usbDeviceSettings;
49        setTitle(usbDeviceSettings.getDeviceName());
50        if (usbDeviceSettings.getHandler() != null) {
51            setSummary(usbDeviceSettings.getHandler().flattenToShortString());
52        }
53        setOnPreferenceClickListener(this);
54    }
55
56    @Override
57    public boolean onPreferenceClick(final Preference preference) {
58        new AlertDialog.Builder(getContext())
59                .setTitle(R.string.usb_pref_delete_title)
60                .setMessage(String.format(
61                        getContext().getResources().getString(R.string.usb_pref_delete_message),
62                        mUsbDeviceSettings.getDeviceName()))
63                .setIcon(android.R.drawable.ic_dialog_alert)
64                .setPositiveButton(R.string.usb_pref_delete_yes,
65                        new DialogInterface.OnClickListener() {
66                            public void onClick(DialogInterface dialog, int whichButton) {
67                                mCallback.onUsbDevicePreferenceDelete(
68                                        preference, mUsbDeviceSettings);
69                            }})
70                .setNegativeButton(R.string.usb_pref_delete_cancel, null)
71                .show();
72        return true;
73    }
74}
75