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