1 // Copyright 2015 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.
5 #include "chrome/browser/ui/android/bluetooth_chooser_android.h"
7 #include "base/android/jni_android.h"
8 #include "base/android/jni_string.h"
9 #include "base/strings/utf_string_conversions.h"
10 #include "chrome/browser/ssl/security_state_model.h"
11 #include "chrome/browser/ui/android/window_android_helper.h"
12 #include "content/public/browser/android/content_view_core.h"
13 #include "jni/BluetoothChooserDialog_jni.h"
14 #include "ui/android/window_android.h"
16 using base::android::AttachCurrentThread
;
17 using base::android::ConvertUTF8ToJavaString
;
18 using base::android::ConvertUTF16ToJavaString
;
19 using base::android::ScopedJavaLocalRef
;
21 BluetoothChooserAndroid::BluetoothChooserAndroid(
22 content::WebContents
* web_contents
,
23 const EventHandler
& event_handler
,
25 : event_handler_(event_handler
) {
26 base::android::ScopedJavaLocalRef
<jobject
> window_android
=
27 content::ContentViewCore::FromWebContents(
28 web_contents
)->GetWindowAndroid()->GetJavaObject();
30 SecurityStateModel
* security_model
=
31 SecurityStateModel::FromWebContents(web_contents
);
32 DCHECK(security_model
);
34 // Create (and show) the BluetoothChooser dialog.
35 JNIEnv
* env
= AttachCurrentThread();
36 ScopedJavaLocalRef
<jstring
> origin_string
=
37 ConvertUTF8ToJavaString(env
, origin
.spec());
38 java_dialog_
.Reset(Java_BluetoothChooserDialog_create(
39 env
, window_android
.obj(), origin_string
.obj(),
40 security_model
->security_info().security_level
,
41 reinterpret_cast<intptr_t>(this)));
44 BluetoothChooserAndroid::~BluetoothChooserAndroid() {
45 Java_BluetoothChooserDialog_closeDialog(AttachCurrentThread(),
49 void BluetoothChooserAndroid::SetAdapterPresence(AdapterPresence presence
) {
50 if (presence
!= AdapterPresence::POWERED_ON
) {
51 Java_BluetoothChooserDialog_notifyAdapterTurnedOff(AttachCurrentThread(),
56 void BluetoothChooserAndroid::ShowDiscoveryState(DiscoveryState state
) {
57 NOTIMPLEMENTED(); // Currently we don't show a 'still searching' state.
60 void BluetoothChooserAndroid::AddDevice(const std::string
& device_id
,
61 const base::string16
& device_name
) {
62 JNIEnv
* env
= AttachCurrentThread();
63 ScopedJavaLocalRef
<jstring
> java_device_id
=
64 ConvertUTF8ToJavaString(env
, device_id
);
65 ScopedJavaLocalRef
<jstring
> java_device_name
=
66 ConvertUTF16ToJavaString(env
, device_name
);
67 Java_BluetoothChooserDialog_addDevice(
68 env
, java_dialog_
.obj(), java_device_id
.obj(), java_device_name
.obj());
71 void BluetoothChooserAndroid::RemoveDevice(const std::string
& device_id
) {
72 JNIEnv
* env
= AttachCurrentThread();
73 ScopedJavaLocalRef
<jstring
> java_device_id
=
74 ConvertUTF16ToJavaString(env
, base::UTF8ToUTF16(device_id
));
75 Java_BluetoothChooserDialog_removeDevice(env
, java_dialog_
.obj(),
76 java_device_id
.obj());
79 void BluetoothChooserAndroid::OnDeviceSelected(JNIEnv
* env
,
82 std::string id
= base::android::ConvertJavaStringToUTF8(env
, device_id
);
84 event_handler_
.Run(Event::CANCELLED
, "");
86 event_handler_
.Run(Event::SELECTED
, id
);
90 void BluetoothChooserAndroid::RestartSearch(JNIEnv
* env
, jobject obj
) {
91 event_handler_
.Run(Event::RESCAN
, "");
94 void BluetoothChooserAndroid::ShowBluetoothOverviewLink(JNIEnv
* env
,
96 event_handler_
.Run(Event::SHOW_OVERVIEW_HELP
, "");
99 void BluetoothChooserAndroid::ShowBluetoothPairingLink(JNIEnv
* env
,
101 event_handler_
.Run(Event::SHOW_PAIRING_HELP
, "");
104 void BluetoothChooserAndroid::ShowBluetoothAdapterOffLink(JNIEnv
* env
,
106 event_handler_
.Run(Event::SHOW_ADAPTER_OFF_HELP
, "");
110 bool BluetoothChooserAndroid::Register(JNIEnv
* env
) {
111 return RegisterNativesImpl(env
);