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