• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 com.android.google.gce.gceservice;
17 
18 import android.content.Context;
19 import android.net.ConnectivityManager;
20 import android.net.Network;
21 import android.net.NetworkCapabilities;
22 import android.net.NetworkInfo;
23 import android.util.Log;
24 
25 import java.util.ArrayList;
26 
27 public class ConnectivityChecker extends JobBase {
28     private static final String LOG_TAG = "GceConnChecker";
29     private static final String MOBILE_NETWORK_CONNECTED_MESSAGE =
30         "VIRTUAL_DEVICE_NETWORK_MOBILE_CONNECTED";
31     private static final String ETHERNET_NETWORK_CONNECTED_MESSAGE =
32         "VIRTUAL_DEVICE_NETWORK_ETHERNET_CONNECTED";
33 
34     private final Context mContext;
35     private final EventReporter mEventReporter;
36     private final GceFuture<Boolean> mConnected = new GceFuture<Boolean>("Connectivity");
37     // TODO(schuffelen): Figure out why this has to be static in order to not report 3 times.
38     private static boolean reportedMobileConnectivity = false;
39     private static boolean reportedEthernetConnectivity = false;
40 
ConnectivityChecker(Context context, EventReporter eventReporter)41     public ConnectivityChecker(Context context, EventReporter eventReporter) {
42         super(LOG_TAG);
43         mContext = context;
44         mEventReporter = eventReporter;
45     }
46 
47 
48     @Override
execute()49     public int execute() {
50         ConnectivityManager connManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
51         if (mConnected.isDone()) {
52             return 0;
53         }
54 
55         Network[] networks = connManager.getAllNetworks();
56         for (Network network : networks) {
57             NetworkInfo info = connManager.getNetworkInfo(network);
58             if (info.isConnected()) {
59                 NetworkCapabilities capabilities = connManager.getNetworkCapabilities(network);
60                 if (capabilities != null) {
61                     if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_CELLULAR)
62                             && !reportedMobileConnectivity) {
63                         mEventReporter.reportMessage(MOBILE_NETWORK_CONNECTED_MESSAGE);
64                         reportedMobileConnectivity = true;
65                     } else if (capabilities.hasTransport(NetworkCapabilities.TRANSPORT_ETHERNET)
66                                    && !reportedEthernetConnectivity) {
67                         mEventReporter.reportMessage(ETHERNET_NETWORK_CONNECTED_MESSAGE);
68                         reportedEthernetConnectivity = true;
69                     }
70                 }
71             }
72         }
73 
74         return 0;
75     }
76 
77 
78     @Override
onDependencyFailed(Exception e)79     public void onDependencyFailed(Exception e) {
80         mConnected.set(e);
81     }
82 
83 
getConnected()84     public GceFuture<Boolean> getConnected() {
85         return mConnected;
86     }
87 }
88