Avi Drissman | 8ba1bad | 2022-09-13 19:22:36 | [diff] [blame] | 1 | // Copyright 2015 The Chromium Authors |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
Clark DuVall | 18d5be8 | 2020-03-16 23:40:34 | [diff] [blame] | 5 | #include "components/location/android/location_settings_impl.h" |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 6 | |
| 7 | #include "base/android/jni_android.h" |
Clark DuVall | 18d5be8 | 2020-03-16 23:40:34 | [diff] [blame] | 8 | #include "components/location/android/location_settings_dialog_outcome.h" |
| 9 | #include "ui/android/window_android.h" |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 10 | |
Andrew Grieve | ecb885bb | 2024-05-29 18:14:19 | [diff] [blame] | 11 | // Must come after all headers that specialize FromJniType() / ToJniType(). |
| 12 | #include "components/location/android/jni_headers/LocationSettings_jni.h" |
| 13 | |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 14 | using base::android::AttachCurrentThread; |
| 15 | |
qfiard | 6b1ebf7 | 2017-03-02 23:41:18 | [diff] [blame] | 16 | using LocationSettingsDialogOutcomeCallback = |
| 17 | LocationSettings::LocationSettingsDialogOutcomeCallback; |
| 18 | |
Sorin Jianu | 6fbeca0 | 2024-10-09 17:31:52 | [diff] [blame^] | 19 | LocationSettingsImpl::LocationSettingsImpl() = default; |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 20 | |
Sorin Jianu | 6fbeca0 | 2024-10-09 17:31:52 | [diff] [blame^] | 21 | LocationSettingsImpl::~LocationSettingsImpl() = default; |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 22 | |
benwells | e962df5 | 2017-03-03 23:26:58 | [diff] [blame] | 23 | bool LocationSettingsImpl::HasAndroidLocationPermission() { |
| 24 | JNIEnv* env = AttachCurrentThread(); |
| 25 | return Java_LocationSettings_hasAndroidLocationPermission(env); |
| 26 | } |
| 27 | |
Matt Reynolds | a8ed687 | 2023-07-06 19:21:26 | [diff] [blame] | 28 | bool LocationSettingsImpl::HasAndroidFineLocationPermission() { |
| 29 | JNIEnv* env = AttachCurrentThread(); |
| 30 | return Java_LocationSettings_hasAndroidFineLocationPermission(env); |
| 31 | } |
| 32 | |
benwells | e962df5 | 2017-03-03 23:26:58 | [diff] [blame] | 33 | bool LocationSettingsImpl::CanPromptForAndroidLocationPermission( |
Clark DuVall | 18d5be8 | 2020-03-16 23:40:34 | [diff] [blame] | 34 | ui::WindowAndroid* window) { |
| 35 | if (window == nullptr) |
| 36 | return false; |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 37 | JNIEnv* env = AttachCurrentThread(); |
benwells | e962df5 | 2017-03-03 23:26:58 | [diff] [blame] | 38 | return Java_LocationSettings_canPromptForAndroidLocationPermission( |
Clark DuVall | 18d5be8 | 2020-03-16 23:40:34 | [diff] [blame] | 39 | env, window->GetJavaObject()); |
newt | e587c6d | 2015-01-21 20:45:12 | [diff] [blame] | 40 | } |
qfiard | 6b1ebf7 | 2017-03-02 23:41:18 | [diff] [blame] | 41 | |
benwells | e962df5 | 2017-03-03 23:26:58 | [diff] [blame] | 42 | bool LocationSettingsImpl::IsSystemLocationSettingEnabled() { |
| 43 | JNIEnv* env = AttachCurrentThread(); |
| 44 | return Java_LocationSettings_isSystemLocationSettingEnabled(env); |
| 45 | } |
| 46 | |
qfiard | 6b1ebf7 | 2017-03-02 23:41:18 | [diff] [blame] | 47 | bool LocationSettingsImpl::CanPromptToEnableSystemLocationSetting() { |
| 48 | JNIEnv* env = AttachCurrentThread(); |
| 49 | return Java_LocationSettings_canPromptToEnableSystemLocationSetting(env); |
| 50 | } |
| 51 | |
| 52 | void LocationSettingsImpl::PromptToEnableSystemLocationSetting( |
| 53 | const LocationSettingsDialogContext prompt_context, |
Clark DuVall | 18d5be8 | 2020-03-16 23:40:34 | [diff] [blame] | 54 | ui::WindowAndroid* window, |
qfiard | 6b1ebf7 | 2017-03-02 23:41:18 | [diff] [blame] | 55 | LocationSettingsDialogOutcomeCallback callback) { |
Clark DuVall | 18d5be8 | 2020-03-16 23:40:34 | [diff] [blame] | 56 | if (window == nullptr) { |
| 57 | std::move(callback).Run(LocationSettingsDialogOutcome::NO_PROMPT); |
| 58 | return; |
| 59 | } |
qfiard | 6b1ebf7 | 2017-03-02 23:41:18 | [diff] [blame] | 60 | JNIEnv* env = AttachCurrentThread(); |
| 61 | // Transfers the ownership of the callback to the Java callback. The Java |
| 62 | // callback is guaranteed to be called unless the user never replies to the |
| 63 | // dialog, and the callback pointer will be destroyed in |
| 64 | // OnLocationSettingsDialogOutcome. |
| 65 | auto* callback_ptr = |
| 66 | new LocationSettingsDialogOutcomeCallback(std::move(callback)); |
| 67 | Java_LocationSettings_promptToEnableSystemLocationSetting( |
Clark DuVall | 18d5be8 | 2020-03-16 23:40:34 | [diff] [blame] | 68 | env, prompt_context, window->GetJavaObject(), |
qfiard | 6b1ebf7 | 2017-03-02 23:41:18 | [diff] [blame] | 69 | reinterpret_cast<jlong>(callback_ptr)); |
| 70 | } |
| 71 | |
Daniel Bratell | 7aacf95 | 2017-11-21 17:51:25 | [diff] [blame] | 72 | static void JNI_LocationSettings_OnLocationSettingsDialogOutcome( |
qfiard | 6b1ebf7 | 2017-03-02 23:41:18 | [diff] [blame] | 73 | JNIEnv* env, |
qfiard | 6b1ebf7 | 2017-03-02 23:41:18 | [diff] [blame] | 74 | jlong callback_ptr, |
| 75 | int result) { |
| 76 | auto* callback = |
| 77 | reinterpret_cast<LocationSettingsDialogOutcomeCallback*>(callback_ptr); |
| 78 | std::move(*callback).Run(static_cast<LocationSettingsDialogOutcome>(result)); |
| 79 | // Destroy the callback whose ownership was transferred in |
| 80 | // PromptToEnableSystemLocationSetting. |
| 81 | delete callback; |
| 82 | } |