FreeRDP
NetworkStateReceiver.java
1 /*
2  Network State Receiver
3 
4  Copyright 2013 Thincast Technologies GmbH, Author: Martin Fleisz
5 
6  This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
7  If a copy of the MPL was not distributed with this file, You can obtain one at
8  http://mozilla.org/MPL/2.0/.
9 */
10 
11 package com.freerdp.freerdpcore.application;
12 
13 import android.content.BroadcastReceiver;
14 import android.content.Context;
15 import android.content.Intent;
16 import android.net.ConnectivityManager;
17 import android.net.NetworkInfo;
18 import android.util.Log;
19 
20 public class NetworkStateReceiver extends BroadcastReceiver
21 {
22 
23  public static boolean isConnectedTo3G(Context context)
24  {
25  ConnectivityManager connectivity =
26  (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
27  NetworkInfo info = connectivity.getActiveNetworkInfo();
28 
29  // no connection or background data disabled
30  if (info == null || !info.isConnected())
31  return false;
32 
33  return (info.getType() != ConnectivityManager.TYPE_WIFI &&
34  info.getType() != ConnectivityManager.TYPE_WIMAX);
35  }
36 
37  @Override public void onReceive(Context context, Intent intent)
38  {
39 
40  // check if we are connected via 3g or wlan
41  if (intent.getExtras() != null)
42  {
43  NetworkInfo info =
44  (NetworkInfo)intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
45 
46  // are we connected at all?
47  if (info != null && info.isConnected())
48  {
49  // see if we are connected through 3G or WiFi
50  Log.d("app", "Connected via type " + info.getTypeName());
51  GlobalApp.ConnectedTo3G = (info.getType() != ConnectivityManager.TYPE_WIFI &&
52  info.getType() != ConnectivityManager.TYPE_WIMAX);
53  }
54 
55  Log.v("NetworkState", info.toString());
56  }
57  }
58 }