summaryrefslogtreecommitdiffstats
path: root/hal/android/AndroidHal.cpp
blob: 3a7e1bf121e66963bb2fede57ac115f83fdb017f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "Hal.h"
#include "HalImpl.h"
#include "WindowIdentifier.h"
#include "AndroidBridge.h"
#include "mozilla/dom/network/Constants.h"
#include "mozilla/java/GeckoAppShellWrappers.h"
#include "mozilla/java/GeckoRuntimeWrappers.h"
#include "mozilla/widget/ScreenManager.h"
#include "nsPIDOMWindow.h"

using namespace mozilla::dom;
using namespace mozilla::hal;

namespace mozilla::hal_impl {

void Vibrate(const nsTArray<uint32_t>& pattern, WindowIdentifier&&) {
  // Ignore the WindowIdentifier parameter; it's here only because hal::Vibrate,
  // hal_sandbox::Vibrate, and hal_impl::Vibrate all must have the same
  // signature.

  // Strangely enough, the Android Java API seems to treat vibrate([0]) as a
  // nop.  But we want to treat vibrate([0]) like CancelVibrate!  (Note that we
  // also need to treat vibrate([]) as a call to CancelVibrate.)
  bool allZero = true;
  for (uint32_t i = 0; i < pattern.Length(); i++) {
    if (pattern[i] != 0) {
      allZero = false;
      break;
    }
  }

  if (allZero) {
    hal_impl::CancelVibrate(WindowIdentifier());
    return;
  }

  AndroidBridge* b = AndroidBridge::Bridge();
  if (!b) {
    return;
  }

  b->Vibrate(pattern);
}

void CancelVibrate(WindowIdentifier&&) {
  // Ignore WindowIdentifier parameter.

  java::GeckoAppShell::CancelVibrate();
}

void EnableBatteryNotifications() {
  java::GeckoAppShell::EnableBatteryNotifications();
}

void DisableBatteryNotifications() {
  java::GeckoAppShell::DisableBatteryNotifications();
}

void GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo) {
  AndroidBridge::Bridge()->GetCurrentBatteryInformation(aBatteryInfo);
}

void EnableNetworkNotifications() {
  java::GeckoAppShell::EnableNetworkNotifications();
}

void DisableNetworkNotifications() {
  java::GeckoAppShell::DisableNetworkNotifications();
}

void GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo) {
  AndroidBridge::Bridge()->GetCurrentNetworkInformation(aNetworkInfo);
}

static bool IsSupportedScreenOrientation(hal::ScreenOrientation aOrientation) {
  // The Android backend only supports these orientations.
  static constexpr ScreenOrientation kSupportedOrientations[] = {
      ScreenOrientation::PortraitPrimary,
      ScreenOrientation::PortraitSecondary,
      ScreenOrientation::PortraitPrimary | ScreenOrientation::PortraitSecondary,
      ScreenOrientation::LandscapePrimary,
      ScreenOrientation::LandscapeSecondary,
      ScreenOrientation::LandscapePrimary |
          ScreenOrientation::LandscapeSecondary,
      ScreenOrientation::PortraitPrimary |
          ScreenOrientation::PortraitSecondary |
          ScreenOrientation::LandscapePrimary |
          ScreenOrientation::LandscapeSecondary,
      ScreenOrientation::Default,
  };
  for (auto supportedOrientation : kSupportedOrientations) {
    if (aOrientation == supportedOrientation) {
      return true;
    }
  }
  return false;
}

RefPtr<GenericNonExclusivePromise> LockScreenOrientation(
    const hal::ScreenOrientation& aOrientation) {
  if (!IsSupportedScreenOrientation(aOrientation)) {
    NS_WARNING("Unsupported screen orientation type");
    return GenericNonExclusivePromise::CreateAndReject(
        NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__);
  }

  java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
  if (!runtime) {
    return GenericNonExclusivePromise::CreateAndReject(NS_ERROR_DOM_ABORT_ERR,
                                                       __func__);
  }

  hal::ScreenOrientation orientation = [&aOrientation]() {
    if (aOrientation == hal::ScreenOrientation::Default) {
      // GeckoView only supports single monitor, so get primary screen data for
      // natural orientation.
      RefPtr<widget::Screen> screen =
          widget::ScreenManager::GetSingleton().GetPrimaryScreen();
      return screen->GetDefaultOrientationType();
    }
    return aOrientation;
  }();

  auto result = runtime->LockScreenOrientation(uint32_t(orientation));
  auto geckoResult = java::GeckoResult::LocalRef(std::move(result));
  return GenericNonExclusivePromise::FromGeckoResult(geckoResult)
      ->Then(
          GetCurrentSerialEventTarget(), __func__,
          [](const GenericNonExclusivePromise::ResolveOrRejectValue& aValue) {
            if (aValue.IsResolve()) {
              if (aValue.ResolveValue()) {
                return GenericNonExclusivePromise::CreateAndResolve(true,
                                                                    __func__);
              }
              // Delegated orientation controller returns failure for
              // lock.
              return GenericNonExclusivePromise::CreateAndReject(
                  NS_ERROR_DOM_ABORT_ERR, __func__);
            }
            // Browser side doesn't implement orientation delegate.
            return GenericNonExclusivePromise::CreateAndReject(
                NS_ERROR_DOM_NOT_SUPPORTED_ERR, __func__);
          });
}

void UnlockScreenOrientation() {
  java::GeckoRuntime::LocalRef runtime = java::GeckoRuntime::GetInstance();
  if (runtime) {
    runtime->UnlockScreenOrientation();
  }
}

}  // namespace mozilla::hal_impl