1 /*
2  * Copyright (C) 2021 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.car.settings.qc;
18 
19 import android.content.ContentResolver;
20 import android.net.Uri;
21 import android.util.ArrayMap;
22 
23 import androidx.annotation.VisibleForTesting;
24 
25 import java.util.Map;
26 
27 /**
28  * Registry of valid Quick Control Uris provided by CarSettings.
29  */
30 public class SettingsQCRegistry {
31     public static final String AUTHORITY = "com.android.car.settings.qc";
32 
33     // Start Uris
34     public static final Uri BLUETOOTH_SWITCH_URI = new Uri.Builder()
35             .scheme(ContentResolver.SCHEME_CONTENT)
36             .authority(AUTHORITY)
37             .appendPath("bluetooth_switch")
38             .build();
39 
40     public static final Uri PAIRED_BLUETOOTH_DEVICES_URI = new Uri.Builder()
41             .scheme(ContentResolver.SCHEME_CONTENT)
42             .authority(AUTHORITY)
43             .appendPath("paired_bluetooth_devices")
44             .build();
45 
46     public static final Uri WIFI_TILE_URI = new Uri.Builder()
47             .scheme(ContentResolver.SCHEME_CONTENT)
48             .authority(AUTHORITY)
49             .appendPath("wifi_tile")
50             .build();
51 
52     public static final Uri HOTSPOT_TILE_URI = new Uri.Builder()
53             .scheme(ContentResolver.SCHEME_CONTENT)
54             .authority(AUTHORITY)
55             .appendPath("hotspot_tile")
56             .build();
57 
58     public static final Uri MOBILE_DATA_TILE_URI = new Uri.Builder()
59             .scheme(ContentResolver.SCHEME_CONTENT)
60             .authority(AUTHORITY)
61             .appendPath("mobile_data_tile")
62             .build();
63 
64     public static final Uri WIFI_ROW_URI = new Uri.Builder()
65             .scheme(ContentResolver.SCHEME_CONTENT)
66             .authority(AUTHORITY)
67             .appendPath("wifi_row")
68             .build();
69 
70     public static final Uri HOTSPOT_ROW_URI = new Uri.Builder()
71             .scheme(ContentResolver.SCHEME_CONTENT)
72             .authority(AUTHORITY)
73             .appendPath("hotspot_row")
74             .build();
75 
76     public static final Uri MOBILE_DATA_ROW_URI = new Uri.Builder()
77             .scheme(ContentResolver.SCHEME_CONTENT)
78             .authority(AUTHORITY)
79             .appendPath("mobile_data_row")
80             .build();
81 
82     public static final Uri BRIGHTNESS_SLIDER_URI = new Uri.Builder()
83             .scheme(ContentResolver.SCHEME_CONTENT)
84             .authority(AUTHORITY)
85             .appendPath("brightness_slider")
86             .build();
87 
88     public static final Uri BRIGHTNESS_SLIDER_WITH_ICON_URI = new Uri.Builder()
89             .scheme(ContentResolver.SCHEME_CONTENT)
90             .authority(AUTHORITY)
91             .appendPath("brightness_slider_with_icon")
92             .build();
93 
94     public static final Uri ADAPTIVE_BRIGHTNESS_SWITCH_URI = new Uri.Builder()
95             .scheme(ContentResolver.SCHEME_CONTENT)
96             .authority(AUTHORITY)
97             .appendPath("adaptive_brightness_switch")
98             .build();
99 
100     public static final Uri MEDIA_VOLUME_SLIDER_URI = new Uri.Builder()
101             .scheme(ContentResolver.SCHEME_CONTENT)
102             .authority(AUTHORITY)
103             .appendPath("media_volume_slider")
104             .build();
105 
106     public static final Uri CALL_VOLUME_SLIDER_URI = new Uri.Builder()
107             .scheme(ContentResolver.SCHEME_CONTENT)
108             .authority(AUTHORITY)
109             .appendPath("call_volume_slider")
110             .build();
111 
112     public static final Uri NAVIGATION_VOLUME_SLIDER_URI = new Uri.Builder()
113             .scheme(ContentResolver.SCHEME_CONTENT)
114             .authority(AUTHORITY)
115             .appendPath("navigation_volume_slider")
116             .build();
117     // End Uris
118 
119     @VisibleForTesting
120     static final Map<Uri, Class<? extends SettingsQCItem>> sUriToQC = createUriToQCMap();
121 
createUriToQCMap()122     private static Map<Uri, Class<? extends SettingsQCItem>> createUriToQCMap() {
123         Map<Uri, Class<? extends SettingsQCItem>> map = new ArrayMap<>();
124 
125         map.put(BLUETOOTH_SWITCH_URI, BluetoothSwitch.class);
126         map.put(PAIRED_BLUETOOTH_DEVICES_URI, PairedBluetoothDevices.class);
127         map.put(WIFI_TILE_URI, WifiTile.class);
128         map.put(HOTSPOT_TILE_URI, HotspotTile.class);
129         map.put(MOBILE_DATA_TILE_URI, MobileDataTile.class);
130         map.put(WIFI_ROW_URI, WifiRow.class);
131         map.put(HOTSPOT_ROW_URI, HotspotRow.class);
132         map.put(MOBILE_DATA_ROW_URI, MobileDataRow.class);
133         map.put(BRIGHTNESS_SLIDER_URI, BrightnessSlider.class);
134         map.put(BRIGHTNESS_SLIDER_WITH_ICON_URI, BrightnessSliderWithIcon.class);
135         map.put(ADAPTIVE_BRIGHTNESS_SWITCH_URI, AdaptiveBrightnessSwitch.class);
136         map.put(MEDIA_VOLUME_SLIDER_URI, MediaVolumeSlider.class);
137         map.put(CALL_VOLUME_SLIDER_URI, CallVolumeSlider.class);
138         map.put(NAVIGATION_VOLUME_SLIDER_URI, NavigationVolumeSlider.class);
139 
140         return map;
141     }
142 
143     /**
144      * Returns the relevant {@link SettingsQCItem} class that corresponds to the provided uri.
145      */
getQCClassByUri(Uri uri)146     public static Class<? extends SettingsQCItem> getQCClassByUri(Uri uri) {
147         return sUriToQC.get(removeParameterFromUri(uri));
148     }
149 
150     /**
151      * Returns a uri without its parameters (or null if the provided uri is null).
152      */
removeParameterFromUri(Uri uri)153     public static Uri removeParameterFromUri(Uri uri) {
154         return uri != null ? uri.buildUpon().clearQuery().build() : null;
155     }
156 
157     /**
158      * Returns {@code true} if the provided uri is a valid QCItem Uri handled by
159      * {@link SettingsQCRegistry}.
160      */
isValidUri(Uri uri)161     public static boolean isValidUri(Uri uri) {
162         return sUriToQC.containsKey(removeParameterFromUri(uri));
163     }
164 
165     /**
166      * Returns {@code true} if the provided action is a valid intent action handled by
167      * {@link SettingsQCRegistry}.
168      */
isValidAction(String action)169     public static boolean isValidAction(String action) {
170         return isValidUri(Uri.parse(action));
171     }
172 }
173