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 HOTSPOT_ROW_WITH_ACTION_URI = new Uri.Builder()
77             .scheme(ContentResolver.SCHEME_CONTENT)
78             .authority(AUTHORITY)
79             .appendPath("hotspot_row_with_action")
80             .build();
81 
82     public static final Uri MOBILE_DATA_ROW_URI = new Uri.Builder()
83             .scheme(ContentResolver.SCHEME_CONTENT)
84             .authority(AUTHORITY)
85             .appendPath("mobile_data_row")
86             .build();
87 
88     public static final Uri BRIGHTNESS_SLIDER_URI = new Uri.Builder()
89             .scheme(ContentResolver.SCHEME_CONTENT)
90             .authority(AUTHORITY)
91             .appendPath("brightness_slider")
92             .build();
93 
94     public static final Uri BRIGHTNESS_SLIDER_WITH_ICON_URI = new Uri.Builder()
95             .scheme(ContentResolver.SCHEME_CONTENT)
96             .authority(AUTHORITY)
97             .appendPath("brightness_slider_with_icon")
98             .build();
99 
100     public static final Uri ADAPTIVE_BRIGHTNESS_SWITCH_URI = new Uri.Builder()
101             .scheme(ContentResolver.SCHEME_CONTENT)
102             .authority(AUTHORITY)
103             .appendPath("adaptive_brightness_switch")
104             .build();
105 
106     public static final Uri THEME_TOGGLE_URI = new Uri.Builder()
107             .scheme(ContentResolver.SCHEME_CONTENT)
108             .authority(AUTHORITY)
109             .appendPath("theme_toggle")
110             .build();
111 
112     public static final Uri MEDIA_AUDIO_SELECTOR_URI = new Uri.Builder()
113             .scheme(ContentResolver.SCHEME_CONTENT)
114             .authority(AUTHORITY)
115             .appendPath("media_audio_selector")
116             .build();
117 
118     public static final Uri MEDIA_VOLUME_SLIDER_URI = new Uri.Builder()
119             .scheme(ContentResolver.SCHEME_CONTENT)
120             .authority(AUTHORITY)
121             .appendPath("media_volume_slider")
122             .build();
123 
124     public static final Uri MEDIA_VOLUME_SLIDER_WITHOUT_ICON_URI = new Uri.Builder()
125             .scheme(ContentResolver.SCHEME_CONTENT)
126             .authority(AUTHORITY)
127             .appendPath("media_volume_slider_without_icon")
128             .build();
129 
130     public static final Uri CALL_VOLUME_SLIDER_URI = new Uri.Builder()
131             .scheme(ContentResolver.SCHEME_CONTENT)
132             .authority(AUTHORITY)
133             .appendPath("call_volume_slider")
134             .build();
135 
136     public static final Uri NAVIGATION_VOLUME_SLIDER_URI = new Uri.Builder()
137             .scheme(ContentResolver.SCHEME_CONTENT)
138             .authority(AUTHORITY)
139             .appendPath("navigation_volume_slider")
140             .build();
141     // End Uris
142 
143     @VisibleForTesting
144     static final Map<Uri, Class<? extends SettingsQCItem>> sUriToQC = createUriToQCMap();
145 
createUriToQCMap()146     private static Map<Uri, Class<? extends SettingsQCItem>> createUriToQCMap() {
147         Map<Uri, Class<? extends SettingsQCItem>> map = new ArrayMap<>();
148 
149         map.put(BLUETOOTH_SWITCH_URI, BluetoothSwitch.class);
150         map.put(PAIRED_BLUETOOTH_DEVICES_URI, PairedBluetoothDevices.class);
151         map.put(WIFI_TILE_URI, WifiTile.class);
152         map.put(HOTSPOT_TILE_URI, HotspotTile.class);
153         map.put(MOBILE_DATA_TILE_URI, MobileDataTile.class);
154         map.put(WIFI_ROW_URI, WifiRow.class);
155         map.put(HOTSPOT_ROW_URI, HotspotRow.class);
156         map.put(HOTSPOT_ROW_WITH_ACTION_URI, HotspotRowWithAction.class);
157         map.put(MOBILE_DATA_ROW_URI, MobileDataRow.class);
158         map.put(BRIGHTNESS_SLIDER_URI, BrightnessSlider.class);
159         map.put(BRIGHTNESS_SLIDER_WITH_ICON_URI, BrightnessSliderWithIcon.class);
160         map.put(ADAPTIVE_BRIGHTNESS_SWITCH_URI, AdaptiveBrightnessSwitch.class);
161         map.put(THEME_TOGGLE_URI, ThemeToggle.class);
162         map.put(MEDIA_AUDIO_SELECTOR_URI, MediaAudioSelectorRow.class);
163         map.put(MEDIA_VOLUME_SLIDER_URI, MediaVolumeSlider.class);
164         map.put(MEDIA_VOLUME_SLIDER_WITHOUT_ICON_URI, MediaVolumeSliderWithoutIcon.class);
165         map.put(CALL_VOLUME_SLIDER_URI, CallVolumeSlider.class);
166         map.put(NAVIGATION_VOLUME_SLIDER_URI, NavigationVolumeSlider.class);
167 
168         return map;
169     }
170 
171     /**
172      * Returns the relevant {@link SettingsQCItem} class that corresponds to the provided uri.
173      */
getQCClassByUri(Uri uri)174     public static Class<? extends SettingsQCItem> getQCClassByUri(Uri uri) {
175         return sUriToQC.get(removeParameterFromUri(uri));
176     }
177 
178     /**
179      * Returns a uri without its parameters (or null if the provided uri is null).
180      */
removeParameterFromUri(Uri uri)181     public static Uri removeParameterFromUri(Uri uri) {
182         return uri != null ? uri.buildUpon().clearQuery().build() : null;
183     }
184 
185     /**
186      * Returns {@code true} if the provided uri is a valid QCItem Uri handled by
187      * {@link SettingsQCRegistry}.
188      */
isValidUri(Uri uri)189     public static boolean isValidUri(Uri uri) {
190         return sUriToQC.containsKey(removeParameterFromUri(uri));
191     }
192 
193     /**
194      * Returns {@code true} if the provided action is a valid intent action handled by
195      * {@link SettingsQCRegistry}.
196      */
isValidAction(String action)197     public static boolean isValidAction(String action) {
198         return isValidUri(Uri.parse(action));
199     }
200 }
201