• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 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.connecteddevice.virtual;
18 
19 import android.app.Activity;
20 import android.app.Dialog;
21 import android.app.settings.SettingsEnums;
22 import android.companion.CompanionDeviceManager;
23 import android.content.Context;
24 import android.content.DialogInterface;
25 import android.os.Bundle;
26 
27 import androidx.annotation.NonNull;
28 import androidx.annotation.Nullable;
29 import androidx.annotation.VisibleForTesting;
30 import androidx.appcompat.app.AlertDialog;
31 
32 import com.android.settings.R;
33 import com.android.settings.core.instrumentation.InstrumentedDialogFragment;
34 
35 /**
36  * Implements an AlertDialog for confirming that a user wishes to unpair or "forget" a paired
37  * device.
38  */
39 public class ForgetDeviceDialogFragment extends InstrumentedDialogFragment {
40 
41     public static final String TAG = ForgetDeviceDialogFragment.class.getSimpleName();
42 
43     private static final String DEVICE_ARG = "virtual_device_arg";
44 
45     @VisibleForTesting
46     CompanionDeviceManager mCompanionDeviceManager;
47     @VisibleForTesting
48     VirtualDeviceWrapper mDevice;
49 
newInstance(VirtualDeviceWrapper device)50     static ForgetDeviceDialogFragment newInstance(VirtualDeviceWrapper device) {
51         Bundle args = new Bundle(1);
52         args.putParcelable(DEVICE_ARG, device);
53         ForgetDeviceDialogFragment dialog = new ForgetDeviceDialogFragment();
54         dialog.setArguments(args);
55         return dialog;
56     }
57 
58     @Override
getMetricsCategory()59     public int getMetricsCategory() {
60         return SettingsEnums.DIALOG_VIRTUAL_DEVICE_FORGET;
61     }
62 
63     @Override
onAttach(@onNull Context context)64     public void onAttach(@NonNull Context context) {
65         super.onAttach(context);
66         mCompanionDeviceManager = context.getSystemService(CompanionDeviceManager.class);
67         mDevice = getArguments().getParcelable(DEVICE_ARG, VirtualDeviceWrapper.class);
68     }
69 
70     @Override
71     @NonNull
onCreateDialog(@ullable Bundle inState)72     public Dialog onCreateDialog(@Nullable Bundle inState) {
73         Context context = getContext();
74         CharSequence deviceName = mDevice.getDeviceName(context);
75 
76         AlertDialog dialog = new AlertDialog.Builder(context)
77                 .setPositiveButton(R.string.virtual_device_forget_dialog_confirm_button,
78                         this::onForgetButtonClick)
79                 .setNegativeButton(android.R.string.cancel, null)
80                 .create();
81         dialog.setTitle(
82                 context.getString(R.string.virtual_device_forget_dialog_title, deviceName));
83         dialog.setMessage(
84                 context.getString(R.string.virtual_device_forget_dialog_body, deviceName));
85         return dialog;
86     }
87 
onForgetButtonClick(DialogInterface dialog, int which)88     private void onForgetButtonClick(DialogInterface dialog, int which) {
89         mCompanionDeviceManager.disassociate(mDevice.getAssociationInfo().getId());
90         Activity activity = getActivity();
91         if (activity != null) {
92             activity.finish();
93         }
94     }
95 }
96