OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #include "blimp/client/core/settings/android/blimp_settings_android.h" | |
6 | |
7 #include "base/android/jni_string.h" | |
8 #include "base/bind.h" | |
9 #include "blimp/client/core/session/connection_status.h" | |
10 #include "blimp/client/core/settings/blimp_settings_delegate.h" | |
11 #include "blimp/client/public/blimp_client_context.h" | |
12 #include "jni/AboutBlimpPreferences_jni.h" | |
13 | |
14 using base::android::AttachCurrentThread; | |
15 | |
16 namespace blimp { | |
17 namespace client { | |
18 | |
19 // static | |
20 bool BlimpSettingsAndroid::RegisterJni(JNIEnv* env) { | |
21 return RegisterNativesImpl(env); | |
22 } | |
23 | |
24 // static | |
25 BlimpSettingsAndroid* BlimpSettingsAndroid::FromJavaObject( | |
26 JNIEnv* env, | |
27 const base::android::JavaRef<jobject>& jobj) { | |
28 return reinterpret_cast<BlimpSettingsAndroid*>( | |
29 Java_AboutBlimpPreferences_getNativePtr(env, jobj)); | |
30 } | |
31 | |
32 static jlong Init(JNIEnv* env, | |
33 const base::android::JavaParamRef<jobject>& jobj) { | |
34 return reinterpret_cast<intptr_t>(new BlimpSettingsAndroid(env, jobj)); | |
35 } | |
36 | |
37 BlimpSettingsAndroid::BlimpSettingsAndroid( | |
38 JNIEnv* env, | |
39 const base::android::JavaRef<jobject>& jobj) | |
40 : delegate_(nullptr) { | |
41 java_obj_.Reset(jobj); | |
42 } | |
43 | |
44 BlimpSettingsAndroid::~BlimpSettingsAndroid() { | |
45 Java_AboutBlimpPreferences_clearNativePtr( | |
46 base::android::AttachCurrentThread(), java_obj_); | |
47 if (delegate_) { | |
48 delegate_->GetIdentitySource()->RemoveObserver(this); | |
49 delegate_->GetConnectionStatus()->RemoveObserver(this); | |
50 } | |
51 } | |
52 | |
53 void BlimpSettingsAndroid::Destroy( | |
54 JNIEnv* env, | |
55 const base::android::JavaParamRef<jobject>& jobj) { | |
56 delete this; | |
57 } | |
58 | |
59 void BlimpSettingsAndroid::SetDelegate(BlimpSettingsDelegate* delegate) { | |
60 // Set the Delegate, it can only be called for once. | |
61 DCHECK(!delegate_ && delegate); | |
62 delegate_ = delegate; | |
63 | |
64 // Listen to sign in state change. | |
65 delegate_->GetIdentitySource()->AddObserver(this); | |
66 | |
67 // Listen to connection state change. | |
68 ConnectionStatus* conn_status = delegate_->GetConnectionStatus(); | |
69 DCHECK(conn_status); | |
70 conn_status->AddObserver(this); | |
71 | |
72 // Propagate connection info if the client is connected to the engine. | |
73 if (conn_status->is_connected()) { | |
74 OnConnected(); | |
75 } | |
76 } | |
77 | |
78 void BlimpSettingsAndroid::OnSignedOut() const { | |
79 Java_AboutBlimpPreferences_onSignedOut(base::android::AttachCurrentThread(), | |
80 java_obj_); | |
81 } | |
82 | |
83 void BlimpSettingsAndroid::OnSignedIn() const { | |
84 Java_AboutBlimpPreferences_onSignedIn(base::android::AttachCurrentThread(), | |
85 java_obj_); | |
86 } | |
87 | |
88 void BlimpSettingsAndroid::UpdateEngineInfo() const { | |
89 DCHECK(delegate_); | |
90 | |
91 JNIEnv* env = base::android::AttachCurrentThread(); | |
92 std::string engine_id = | |
93 delegate_->GetConnectionStatus()->engine_endpoint().address().ToString(); | |
94 | |
95 base::android::ScopedJavaLocalRef<jstring> jengine_id( | |
96 base::android::ConvertUTF8ToJavaString(env, engine_id)); | |
97 | |
98 Java_AboutBlimpPreferences_setEngineInfo(env, java_obj_, jengine_id); | |
99 } | |
100 | |
101 void BlimpSettingsAndroid::OnActiveAccountLogout() { | |
102 OnSignedOut(); | |
103 } | |
104 | |
105 void BlimpSettingsAndroid::OnActiveAccountLogin() { | |
106 OnSignedIn(); | |
107 } | |
108 | |
109 void BlimpSettingsAndroid::OnConnected() { | |
110 UpdateEngineInfo(); | |
111 } | |
112 | |
113 void BlimpSettingsAndroid::OnDisconnected(int result) { | |
114 UpdateEngineInfo(); | |
115 } | |
116 | |
117 } // namespace client | |
118 } // namespace blimp | |
OLD | NEW |