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.tv.settings.library.network;
18 
19 import android.content.Context;
20 import android.content.pm.PackageManager;
21 import android.util.ArraySet;
22 import android.util.Log;
23 
24 import com.android.tv.settings.library.State;
25 import com.android.tv.settings.library.data.Module;
26 
27 /** Provide network listener for all network states. */
28 public class NetworkModule implements Module, ConnectivityListener.Listener,
29         ConnectivityListener.WifiNetworkListener {
30     private static final String TAG = "NetworkModule";
31     private static final boolean DEBUG = false;
32     private ConnectivityListener mConnectivityListener;
33     private boolean mIsWifiHardwarePresent;
34     private static NetworkModule instance;
35     private final Context mContext;
36     ArraySet<State> states = new ArraySet<>();
37 
getInstance(Context context)38     public static NetworkModule getInstance(Context context) {
39         if (instance == null) {
40             instance = new NetworkModule(context.getApplicationContext());
41         }
42         return instance;
43     }
44 
NetworkModule(Context context)45     private NetworkModule(Context context) {
46         mContext = context;
47     }
48 
49     @Override
addState(State state)50     public void addState(State state) {
51         if (states.size() == 0) {
52             if (DEBUG) {
53                 Log.d(TAG, "Network module create");
54             }
55             create();
56         }
57         if (DEBUG) {
58             Log.d(TAG, "Network module add state " + state.getStateIdentifier());
59         }
60 
61         states.add(state);
62     }
63 
64     @Override
removeState(State state)65     public void removeState(State state) {
66         if (DEBUG) {
67             Log.d(TAG, "Network module remove state " + state.getStateIdentifier());
68         }
69         states.remove(state);
70         if (states.size() == 0) {
71             if (DEBUG) {
72                 Log.d(TAG, "Network module destroy");
73             }
74             destroy();
75         }
76     }
77 
78     @Override
create()79     public void create() {
80         mIsWifiHardwarePresent = mContext.getPackageManager()
81                 .hasSystemFeature(PackageManager.FEATURE_WIFI);
82         mConnectivityListener = new ConnectivityListener(mContext, this, null);
83         mConnectivityListener.setWifiListener(this);
84         mConnectivityListener.start();
85     }
86 
87     @Override
destroy()88     public void destroy() {
89         mConnectivityListener.destroy();
90     }
91 
92     @Override
onConnectivityChange()93     public void onConnectivityChange() {
94         states.stream()
95                 .filter(state -> state instanceof ConnectivityListener.Listener)
96                 .forEach(state -> ((ConnectivityListener.Listener) state).onConnectivityChange());
97     }
98 
getConnectivityListener()99     ConnectivityListener getConnectivityListener() {
100         return mConnectivityListener;
101     }
102 
isWifiHardwarePresent()103     boolean isWifiHardwarePresent() {
104         return mIsWifiHardwarePresent;
105     }
106 
107     @Override
onWifiListChanged()108     public void onWifiListChanged() {
109         states.stream()
110                 .filter(state -> state instanceof ConnectivityListener.WifiNetworkListener)
111                 .forEach(
112                         state -> ((ConnectivityListener.WifiNetworkListener) state)
113                                 .onWifiListChanged());
114     }
115 }
116