]>
Commit | Line | Data |
---|---|---|
8f092a22 | 1 | /* |
41b59a34 | 2 | * Copyright (C) 2012-2015 Tobias Brunner |
1b671669 | 3 | * HSR Hochschule fuer Technik Rapperswil |
8f092a22 TB |
4 | * |
5 | * This program is free software; you can redistribute it and/or modify it | |
6 | * under the terms of the GNU General Public License as published by the | |
7 | * Free Software Foundation; either version 2 of the License, or (at your | |
8 | * option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>. | |
9 | * | |
10 | * This program is distributed in the hope that it will be useful, but | |
11 | * WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY | |
12 | * or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License | |
13 | * for more details. | |
14 | */ | |
15 | ||
16 | package org.strongswan.android.logic; | |
17 | ||
ef3d1a1b TB |
18 | import android.content.BroadcastReceiver; |
19 | import android.content.Context; | |
20 | import android.content.Intent; | |
21 | import android.content.IntentFilter; | |
22 | import android.net.ConnectivityManager; | |
23 | import android.net.NetworkInfo; | |
24 | ||
94375d46 TB |
25 | import java.util.LinkedList; |
26 | ||
27 | public class NetworkManager extends BroadcastReceiver implements Runnable | |
8f092a22 | 28 | { |
ef3d1a1b | 29 | private final Context mContext; |
94375d46 TB |
30 | private volatile boolean mRegistered; |
31 | private Thread mEventNotifier; | |
32 | private LinkedList<Boolean> mEvents = new LinkedList<>(); | |
ef3d1a1b TB |
33 | |
34 | public NetworkManager(Context context) | |
35 | { | |
36 | mContext = context; | |
37 | } | |
38 | ||
39 | public void Register() | |
40 | { | |
94375d46 TB |
41 | mEvents.clear(); |
42 | mRegistered = true; | |
43 | mEventNotifier = new Thread(this); | |
44 | mEventNotifier.start(); | |
ef3d1a1b TB |
45 | mContext.registerReceiver(this, new IntentFilter(ConnectivityManager.CONNECTIVITY_ACTION)); |
46 | } | |
47 | ||
48 | public void Unregister() | |
49 | { | |
50 | mContext.unregisterReceiver(this); | |
94375d46 TB |
51 | mRegistered = false; |
52 | synchronized (this) | |
53 | { | |
54 | notifyAll(); | |
55 | } | |
56 | try | |
57 | { | |
58 | mEventNotifier.join(); | |
59 | mEventNotifier = null; | |
60 | } | |
61 | catch (InterruptedException e) | |
62 | { | |
63 | e.printStackTrace(); | |
64 | } | |
ef3d1a1b TB |
65 | } |
66 | ||
41b59a34 TB |
67 | public boolean isConnected() |
68 | { | |
69 | ConnectivityManager cm = (ConnectivityManager)mContext.getSystemService(Context.CONNECTIVITY_SERVICE); | |
70 | NetworkInfo info = null; | |
71 | if (cm != null) | |
72 | { | |
73 | info = cm.getActiveNetworkInfo(); | |
74 | } | |
75 | return info != null && info.isConnected(); | |
76 | } | |
77 | ||
ef3d1a1b TB |
78 | @Override |
79 | public void onReceive(Context context, Intent intent) | |
8f092a22 | 80 | { |
94375d46 TB |
81 | synchronized (this) |
82 | { | |
83 | mEvents.addLast(isConnected()); | |
84 | notifyAll(); | |
85 | } | |
86 | } | |
87 | ||
88 | @Override | |
89 | public void run() | |
90 | { | |
91 | while (mRegistered) | |
92 | { | |
93 | boolean connected; | |
94 | ||
95 | synchronized (this) | |
96 | { | |
97 | try | |
98 | { | |
99 | while (mRegistered && mEvents.isEmpty()) | |
100 | { | |
101 | wait(); | |
102 | } | |
103 | } | |
104 | catch (InterruptedException ex) | |
105 | { | |
106 | break; | |
107 | } | |
108 | if (!mRegistered) | |
109 | { | |
110 | break; | |
111 | } | |
112 | connected = mEvents.removeFirst(); | |
113 | } | |
114 | /* call the native parts without holding the lock */ | |
115 | networkChanged(!connected); | |
116 | } | |
8f092a22 TB |
117 | } |
118 | ||
ef3d1a1b TB |
119 | /** |
120 | * Notify the native parts about a network change | |
121 | * | |
122 | * @param disconnected true if no connection is available at the moment | |
123 | */ | |
124 | public native void networkChanged(boolean disconnected); | |
8f092a22 | 125 | } |