1 /*
2  * Copyright (C) 2013 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 package android.support.v7.media;
17 
18 import android.os.Bundle;
19 
20 /**
21  * Describes the kinds of routes that the media router would like to discover
22  * and whether to perform active scanning.
23  * <p>
24  * This object is immutable once created.
25  * </p>
26  */
27 public final class MediaRouteDiscoveryRequest {
28     private static final String KEY_SELECTOR = "selector";
29     private static final String KEY_ACTIVE_SCAN = "activeScan";
30 
31     private final Bundle mBundle;
32     private MediaRouteSelector mSelector;
33 
34     /**
35      * Creates a media route discovery request.
36      *
37      * @param selector The route selector that specifies the kinds of routes to discover.
38      * @param activeScan True if active scanning should be performed.
39      */
MediaRouteDiscoveryRequest(MediaRouteSelector selector, boolean activeScan)40     public MediaRouteDiscoveryRequest(MediaRouteSelector selector, boolean activeScan) {
41         if (selector == null) {
42             throw new IllegalArgumentException("selector must not be null");
43         }
44 
45         mBundle = new Bundle();
46         mSelector = selector;
47         mBundle.putBundle(KEY_SELECTOR, selector.asBundle());
48         mBundle.putBoolean(KEY_ACTIVE_SCAN, activeScan);
49     }
50 
MediaRouteDiscoveryRequest(Bundle bundle)51     private MediaRouteDiscoveryRequest(Bundle bundle) {
52         mBundle = bundle;
53     }
54 
55     /**
56      * Gets the route selector that specifies the kinds of routes to discover.
57      */
getSelector()58     public MediaRouteSelector getSelector() {
59         ensureSelector();
60         return mSelector;
61     }
62 
ensureSelector()63     private void ensureSelector() {
64         if (mSelector == null) {
65             mSelector = MediaRouteSelector.fromBundle(mBundle.getBundle(KEY_SELECTOR));
66             if (mSelector == null) {
67                 mSelector = MediaRouteSelector.EMPTY;
68             }
69         }
70     }
71 
72     /**
73      * Returns true if active scanning should be performed.
74      *
75      * @see MediaRouter#CALLBACK_FLAG_PERFORM_ACTIVE_SCAN
76      */
isActiveScan()77     public boolean isActiveScan() {
78         return mBundle.getBoolean(KEY_ACTIVE_SCAN);
79     }
80 
81     /**
82      * Returns true if the discovery request has all of the required fields.
83      */
isValid()84     public boolean isValid() {
85         ensureSelector();
86         return mSelector.isValid();
87     }
88 
89     @Override
equals(Object o)90     public boolean equals(Object o) {
91         if (o instanceof MediaRouteDiscoveryRequest) {
92             MediaRouteDiscoveryRequest other = (MediaRouteDiscoveryRequest)o;
93             return getSelector().equals(other.getSelector())
94                     && isActiveScan() == other.isActiveScan();
95         }
96         return false;
97     }
98 
99     @Override
hashCode()100     public int hashCode() {
101         return getSelector().hashCode() ^ (isActiveScan() ? 1 : 0);
102     }
103 
104     @Override
toString()105     public String toString() {
106         StringBuilder result = new StringBuilder();
107         result.append("DiscoveryRequest{ selector=").append(getSelector());
108         result.append(", activeScan=").append(isActiveScan());
109         result.append(", isValid=").append(isValid());
110         result.append(" }");
111         return result.toString();
112     }
113 
114     /**
115      * Converts this object to a bundle for serialization.
116      *
117      * @return The contents of the object represented as a bundle.
118      */
asBundle()119     public Bundle asBundle() {
120         return mBundle;
121     }
122 
123     /**
124      * Creates an instance from a bundle.
125      *
126      * @param bundle The bundle, or null if none.
127      * @return The new instance, or null if the bundle was null.
128      */
fromBundle(Bundle bundle)129     public static MediaRouteDiscoveryRequest fromBundle(Bundle bundle) {
130         return bundle != null ? new MediaRouteDiscoveryRequest(bundle) : null;
131     }
132 }
133