Home | History | Annotate | Download | only in gceservice
      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.NetworkInfo;
     21 import android.util.Log;
     22 
     23 import java.util.ArrayList;
     24 
     25 public class ConnectivityChecker extends JobBase {
     26     private static final String LOG_TAG = "GceConnChecker";
     27 
     28     private final Context mContext;
     29     private final GceFuture<Boolean> mConnected = new GceFuture<Boolean>("Connectivity");
     30 
     31 
     32     public ConnectivityChecker(Context context) {
     33         super(LOG_TAG);
     34         mContext = context;
     35     }
     36 
     37 
     38     @Override
     39     public int execute() {
     40         ConnectivityManager connManager = (ConnectivityManager) mContext.getSystemService(Context.CONNECTIVITY_SERVICE);
     41         if (mConnected.isDone()) return 0;
     42 
     43         NetworkInfo[] networks = connManager.getAllNetworkInfo();
     44         ArrayList<String> connected = new ArrayList<String>();
     45         ArrayList<String> disconnected = new ArrayList<String>();
     46 
     47         for (NetworkInfo network : networks) {
     48             if (network.isConnected()) {
     49                 connected.add(network.getTypeName());
     50                 mConnected.set(true);
     51                 break;
     52             } else {
     53                 disconnected.add(network.getTypeName());
     54             }
     55         }
     56 
     57         Log.i(LOG_TAG, "Connectivity status: connected:" + connected + ", disconnected:" + disconnected);
     58 
     59         return 0;
     60     }
     61 
     62 
     63     @Override
     64     public void onDependencyFailed(Exception e) {
     65         mConnected.set(e);
     66     }
     67 
     68 
     69     public GceFuture<Boolean> getConnected() {
     70         return mConnected;
     71     }
     72 }
     73