OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/android/network_change_notifier_android.h" | 5 #include "net/android/network_change_notifier_android.h" |
6 | 6 |
7 #include "base/logging.h" | |
8 #include "base/android/jni_android.h" | |
9 #include "jni/network_change_notifier_jni.h" | |
10 | |
11 namespace net { | 7 namespace net { |
12 namespace android { | 8 namespace android { |
13 | 9 |
14 NetworkChangeNotifier::NetworkChangeNotifier() { | 10 NetworkChangeNotifier::NetworkChangeNotifier() : is_connected_(false) { |
15 JNIEnv* env = base::android::AttachCurrentThread(); | |
16 CreateJavaObject(env); | |
17 } | 11 } |
18 | 12 |
19 NetworkChangeNotifier::~NetworkChangeNotifier() { | 13 NetworkChangeNotifier::~NetworkChangeNotifier() { |
20 JNIEnv* env = base::android::AttachCurrentThread(); | |
21 Java_NetworkChangeNotifier_unregisterReceiver( | |
22 env, java_network_change_notifier_.obj()); | |
23 } | |
24 | |
25 void NetworkChangeNotifier::CreateJavaObject(JNIEnv* env) { | |
26 java_network_change_notifier_.Reset( | |
27 Java_NetworkChangeNotifier_create( | |
28 env, | |
29 base::android::GetApplicationContext(), | |
30 reinterpret_cast<jint>(this))); | |
31 } | |
32 | |
33 void NetworkChangeNotifier::NotifyObservers(JNIEnv* env, jobject obj) { | |
34 NotifyObserversOfConnectionTypeChange(); | |
35 } | 14 } |
36 | 15 |
37 net::NetworkChangeNotifier::ConnectionType | 16 net::NetworkChangeNotifier::ConnectionType |
38 NetworkChangeNotifier::GetCurrentConnectionType() const { | 17 NetworkChangeNotifier::GetCurrentConnectionType() const { |
39 JNIEnv* env = base::android::AttachCurrentThread(); | 18 // TODO(benm): Add support for returning different types of connection here. |
40 // TODO(droger): Return something more detailed than CONNECTION_UNKNOWN. | 19 // See crbug.com/112937 |
41 return Java_NetworkChangeNotifier_isConnected( | 20 return is_connected_ ? |
42 env, java_network_change_notifier_.obj()) ? | 21 net::NetworkChangeNotifier::CONNECTION_UNKNOWN : |
43 net::NetworkChangeNotifier::CONNECTION_UNKNOWN : | 22 net::NetworkChangeNotifier::CONNECTION_NONE; |
44 net::NetworkChangeNotifier::CONNECTION_NONE; | |
45 } | 23 } |
46 | 24 |
47 // static | 25 void NetworkChangeNotifier::setConnected(bool network_up) { |
48 bool NetworkChangeNotifier::Register(JNIEnv* env) { | 26 if (is_connected_ != network_up) { |
49 return RegisterNativesImpl(env); | 27 is_connected_ = network_up; |
| 28 NotifyObserversOfConnectionTypeChange(); |
| 29 } |
50 } | 30 } |
51 | 31 |
52 } // namespace android | 32 } // namespace android |
53 } // namespace net | 33 } // namespace net |
OLD | NEW |