1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
8 #include "WindowIdentifier.h"
9 #include "AndroidBridge.h"
10 #include "mozilla/dom/network/Constants.h"
11 #include "mozilla/java/GeckoAppShellWrappers.h"
12 #include "mozilla/java/GeckoRuntimeWrappers.h"
13 #include "mozilla/widget/ScreenManager.h"
14 #include "nsPIDOMWindow.h"
16 using namespace mozilla::dom
;
17 using namespace mozilla::hal
;
19 namespace mozilla::hal_impl
{
21 void Vibrate(const nsTArray
<uint32_t>& pattern
, WindowIdentifier
&&) {
22 // Ignore the WindowIdentifier parameter; it's here only because hal::Vibrate,
23 // hal_sandbox::Vibrate, and hal_impl::Vibrate all must have the same
26 // Strangely enough, the Android Java API seems to treat vibrate([0]) as a
27 // nop. But we want to treat vibrate([0]) like CancelVibrate! (Note that we
28 // also need to treat vibrate([]) as a call to CancelVibrate.)
30 for (uint32_t i
= 0; i
< pattern
.Length(); i
++) {
31 if (pattern
[i
] != 0) {
38 hal_impl::CancelVibrate(WindowIdentifier());
42 AndroidBridge
* b
= AndroidBridge::Bridge();
50 void CancelVibrate(WindowIdentifier
&&) {
51 // Ignore WindowIdentifier parameter.
53 java::GeckoAppShell::CancelVibrate();
56 void EnableBatteryNotifications() {
57 java::GeckoAppShell::EnableBatteryNotifications();
60 void DisableBatteryNotifications() {
61 java::GeckoAppShell::DisableBatteryNotifications();
64 void GetCurrentBatteryInformation(hal::BatteryInformation
* aBatteryInfo
) {
65 AndroidBridge::Bridge()->GetCurrentBatteryInformation(aBatteryInfo
);
68 void EnableNetworkNotifications() {
69 java::GeckoAppShell::EnableNetworkNotifications();
72 void DisableNetworkNotifications() {
73 java::GeckoAppShell::DisableNetworkNotifications();
76 void GetCurrentNetworkInformation(hal::NetworkInformation
* aNetworkInfo
) {
77 AndroidBridge::Bridge()->GetCurrentNetworkInformation(aNetworkInfo
);
80 static bool IsSupportedScreenOrientation(hal::ScreenOrientation aOrientation
) {
81 // The Android backend only supports these orientations.
82 static constexpr ScreenOrientation kSupportedOrientations
[] = {
83 ScreenOrientation::PortraitPrimary
,
84 ScreenOrientation::PortraitSecondary
,
85 ScreenOrientation::PortraitPrimary
| ScreenOrientation::PortraitSecondary
,
86 ScreenOrientation::LandscapePrimary
,
87 ScreenOrientation::LandscapeSecondary
,
88 ScreenOrientation::LandscapePrimary
|
89 ScreenOrientation::LandscapeSecondary
,
90 ScreenOrientation::PortraitPrimary
|
91 ScreenOrientation::PortraitSecondary
|
92 ScreenOrientation::LandscapePrimary
|
93 ScreenOrientation::LandscapeSecondary
,
94 ScreenOrientation::Default
,
96 for (auto supportedOrientation
: kSupportedOrientations
) {
97 if (aOrientation
== supportedOrientation
) {
104 RefPtr
<GenericNonExclusivePromise
> LockScreenOrientation(
105 const hal::ScreenOrientation
& aOrientation
) {
106 if (!IsSupportedScreenOrientation(aOrientation
)) {
107 NS_WARNING("Unsupported screen orientation type");
108 return GenericNonExclusivePromise::CreateAndReject(
109 NS_ERROR_DOM_NOT_SUPPORTED_ERR
, __func__
);
112 java::GeckoRuntime::LocalRef runtime
= java::GeckoRuntime::GetInstance();
114 return GenericNonExclusivePromise::CreateAndReject(NS_ERROR_DOM_ABORT_ERR
,
117 auto result
= runtime
->LockScreenOrientation(uint32_t(aOrientation
));
118 auto geckoResult
= java::GeckoResult::LocalRef(std::move(result
));
119 return GenericNonExclusivePromise::FromGeckoResult(geckoResult
)
121 GetCurrentSerialEventTarget(), __func__
,
122 [](const GenericNonExclusivePromise::ResolveOrRejectValue
& aValue
) {
123 if (aValue
.IsResolve()) {
124 if (aValue
.ResolveValue()) {
125 return GenericNonExclusivePromise::CreateAndResolve(true,
128 // Delegated orientation controller returns failure for
130 return GenericNonExclusivePromise::CreateAndReject(
131 NS_ERROR_DOM_ABORT_ERR
, __func__
);
133 // Browser side doesn't implement orientation delegate.
134 return GenericNonExclusivePromise::CreateAndReject(
135 NS_ERROR_DOM_NOT_SUPPORTED_ERR
, __func__
);
139 void UnlockScreenOrientation() {
140 java::GeckoRuntime::LocalRef runtime
= java::GeckoRuntime::GetInstance();
142 runtime
->UnlockScreenOrientation();
146 } // namespace mozilla::hal_impl