FreeRDP
All Data Structures Namespaces Files Functions Variables Typedefs Enumerations Enumerator Modules Pages
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
11package com.freerdp.freerdpcore.application;
12
13import android.content.BroadcastReceiver;
14import android.content.Context;
15import android.content.Intent;
16import android.net.ConnectivityManager;
17import android.net.NetworkInfo;
18import android.util.Log;
19
20import androidx.annotation.NonNull;
21
22public class NetworkStateReceiver extends BroadcastReceiver
23{
24
25 public static boolean isConnectedTo3G(Context context)
26 {
27 ConnectivityManager connectivity =
28 (ConnectivityManager)context.getSystemService(Context.CONNECTIVITY_SERVICE);
29 NetworkInfo info = connectivity.getActiveNetworkInfo();
30
31 // no connection or background data disabled
32 if (info == null || !info.isConnected())
33 return false;
34
35 return (info.getType() != ConnectivityManager.TYPE_WIFI &&
36 info.getType() != ConnectivityManager.TYPE_WIMAX);
37 }
38
39 @Override public void onReceive(@NonNull Context context, @NonNull Intent intent)
40 {
41 String action = intent.getAction();
42 if (!action.equals("android.net.conn.CONNECTIVITY_CHANGE"))
43 {
44 return;
45 }
46
47 // check if we are connected via 3g or wlan
48 if (intent.getExtras() != null)
49 {
50 NetworkInfo info =
51 (NetworkInfo)intent.getExtras().get(ConnectivityManager.EXTRA_NETWORK_INFO);
52
53 // are we connected at all?
54 if (info != null)
55 {
56 if (info.isConnected())
57 {
58 // see if we are connected through 3G or WiFi
59 Log.d("app", "Connected via type " + info.getTypeName());
60 GlobalApp.ConnectedTo3G = (info.getType() != ConnectivityManager.TYPE_WIFI &&
61 info.getType() != ConnectivityManager.TYPE_WIMAX);
62 }
63
64 Log.v("NetworkState", info.toString());
65 }
66 }
67 }
68}