1 /*
2  * Copyright (C) 2022 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.stylus;
18 
19 import android.app.settings.SettingsEnums;
20 import android.content.Context;
21 import android.hardware.input.InputManager;
22 import android.view.InputDevice;
23 
24 import androidx.annotation.Nullable;
25 import androidx.annotation.VisibleForTesting;
26 
27 import com.android.settings.R;
28 import com.android.settings.dashboard.DashboardFragment;
29 import com.android.settingslib.core.AbstractPreferenceController;
30 import com.android.settingslib.core.lifecycle.Lifecycle;
31 
32 import java.util.ArrayList;
33 import java.util.List;
34 
35 /** Controls the USI stylus details and provides updates to individual controllers. */
36 public class StylusUsiDetailsFragment extends DashboardFragment {
37     private static final String TAG = StylusUsiDetailsFragment.class.getSimpleName();
38     private static final String KEY_DEVICE_INPUT_ID = "device_input_id";
39 
40     @VisibleForTesting
41     @Nullable
42     InputDevice mInputDevice;
43 
44     @Override
onAttach(Context context)45     public void onAttach(Context context) {
46         super.onAttach(context);
47         int inputDeviceId = getArguments().getInt(KEY_DEVICE_INPUT_ID);
48         InputManager im = context.getSystemService(InputManager.class);
49         mInputDevice = im.getInputDevice(inputDeviceId);
50 
51         super.onAttach(context);
52         if (mInputDevice == null) {
53             finish();
54         }
55     }
56 
57     @Override
getMetricsCategory()58     public int getMetricsCategory() {
59         return SettingsEnums.USI_DEVICE_DETAILS;
60     }
61 
62     @Override
getLogTag()63     protected String getLogTag() {
64         return TAG;
65     }
66 
67     @Override
getPreferenceScreenResId()68     protected int getPreferenceScreenResId() {
69         return R.xml.stylus_usi_details_fragment;
70     }
71 
72     @Override
createPreferenceControllers(Context context)73     protected List<AbstractPreferenceController> createPreferenceControllers(Context context) {
74         ArrayList<AbstractPreferenceController> controllers = new ArrayList<>();
75         if (mInputDevice != null) {
76             Lifecycle lifecycle = getSettingsLifecycle();
77             controllers.add(new StylusUsiHeaderController(context, mInputDevice));
78             controllers.add(new StylusDevicesController(context, mInputDevice, null, lifecycle));
79         }
80         return controllers;
81     }
82 }
83