• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 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.wfd;
18 
19 import android.content.Context;
20 import android.media.MediaRouter;
21 import android.media.MediaRouter.RouteInfo;
22 
23 import androidx.preference.Preference;
24 import androidx.preference.PreferenceScreen;
25 
26 import com.android.settings.R;
27 import com.android.settings.core.BasePreferenceController;
28 import com.android.settingslib.core.lifecycle.LifecycleObserver;
29 import com.android.settingslib.core.lifecycle.events.OnStart;
30 import com.android.settingslib.core.lifecycle.events.OnStop;
31 
32 public class WifiDisplayPreferenceController extends BasePreferenceController implements
33         LifecycleObserver, OnStart, OnStop {
34 
35     private final MediaRouter mRouter;
36     private Preference mPreference;
37     private final MediaRouter.Callback mRouterCallback = new MediaRouter.SimpleCallback() {
38         @Override
39         public void onRouteSelected(MediaRouter router, int type, RouteInfo info) {
40             refreshSummary(mPreference);
41         }
42 
43         @Override
44         public void onRouteUnselected(MediaRouter router, int type, RouteInfo info) {
45             refreshSummary(mPreference);
46         }
47 
48         @Override
49         public void onRouteAdded(MediaRouter router, RouteInfo info) {
50             refreshSummary(mPreference);
51         }
52 
53         @Override
54         public void onRouteRemoved(MediaRouter router, RouteInfo info) {
55             refreshSummary(mPreference);
56         }
57 
58         @Override
59         public void onRouteChanged(MediaRouter router, RouteInfo info) {
60             refreshSummary(mPreference);
61         }
62     };
63 
WifiDisplayPreferenceController(Context context, String key)64     public WifiDisplayPreferenceController(Context context, String key) {
65         super(context, key);
66         mRouter = context.getSystemService(MediaRouter.class);
67         mRouter.setRouterGroupId(MediaRouter.MIRRORING_GROUP_ID);
68     }
69 
70     @Override
getAvailabilityStatus()71     public int getAvailabilityStatus() {
72         return WifiDisplaySettings.isAvailable(mContext) ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
73     }
74 
75     @Override
displayPreference(PreferenceScreen screen)76     public void displayPreference(PreferenceScreen screen) {
77         super.displayPreference(screen);
78         mPreference = screen.findPreference(getPreferenceKey());
79     }
80 
81     @Override
getSummary()82     public CharSequence getSummary() {
83         CharSequence summary = mContext.getString(R.string.disconnected);
84 
85         final int routeCount = mRouter.getRouteCount();
86         for (int i = 0; i < routeCount; i++) {
87             final MediaRouter.RouteInfo route = mRouter.getRouteAt(i);
88             if (route.matchesTypes(MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY)
89                     && route.isSelected() && !route.isConnecting()) {
90                 summary = mContext.getString(R.string.wifi_display_status_connected);
91                 break;
92             }
93         }
94         return summary;
95     }
96 
97     @Override
onStart()98     public void onStart() {
99         mRouter.addCallback(MediaRouter.ROUTE_TYPE_REMOTE_DISPLAY, mRouterCallback);
100     }
101 
102     @Override
onStop()103     public void onStop() {
104         mRouter.removeCallback(mRouterCallback);
105     }
106 }
107