1 /*
2  * Copyright 2023 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.example.androidx.mediarouting.activities.systemrouting.source;
18 
19 import com.example.androidx.mediarouting.activities.systemrouting.SystemRouteItem;
20 import com.example.androidx.mediarouting.activities.systemrouting.SystemRoutesSourceItem;
21 
22 import org.jspecify.annotations.NonNull;
23 
24 import java.util.List;
25 
26 /**
27  * Abstracts different route sources.
28  */
29 public abstract class SystemRoutesSource {
30 
31     protected @NonNull Runnable mOnRoutesChangedListener = () -> {};
32 
33     /** Sets a {@link Runnable} to invoke whenever routes change. */
setOnRoutesChangedListener(@onNull Runnable onRoutesChangedListener)34     public void setOnRoutesChangedListener(@NonNull Runnable onRoutesChangedListener) {
35         mOnRoutesChangedListener = onRoutesChangedListener;
36     }
37 
38     /**
39      * Starts the source. The source may use this opportunity to subscribe to changes that
40      * happen in the abstractions at a lower level.
41      */
start()42     public void start() {
43         // Empty on purpose.
44     }
45 
46     /**
47      * Stops the source. The source releases resources if applicable.
48      */
stop()49     public void stop() {
50         // Empty on purpose.
51     }
52 
53     /** Returns a string that uniquely identifies this source. */
getSourceId()54     public final @NonNull String getSourceId() {
55         return getClass().getSimpleName();
56     }
57 
58     /**
59      * Gets a source item containing source type.
60      */
getSourceItem()61     public abstract @NonNull SystemRoutesSourceItem getSourceItem();
62 
63     /**
64      * Fetches a list of {@link SystemRouteItem} discovered by this source.
65      */
fetchSourceRouteItems()66     public abstract @NonNull List<SystemRouteItem> fetchSourceRouteItems();
67 
68     /**
69      * Selects the route that corresponds to the given item.
70      *
71      * @param item An item with {@link SystemRouteItem#mSelectionSupportState} {@link
72      *     SystemRouteItem.SelectionSupportState#SELECTABLE}.
73      * @return Whether the selection was successful.
74      */
select(@onNull SystemRouteItem item)75     public abstract boolean select(@NonNull SystemRouteItem item);
76 }
77