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/. */
5 
6 #include "Hal.h"
7 #include "HalImpl.h"
8 #include "WindowIdentifier.h"
9 #include "AndroidBridge.h"
10 #include "mozilla/dom/network/Constants.h"
11 #include "mozilla/dom/ScreenOrientation.h"
12 #include "nsIScreenManager.h"
13 #include "nsServiceManagerUtils.h"
14 
15 using namespace mozilla::dom;
16 using namespace mozilla::hal;
17 
18 namespace java = mozilla::java;
19 
20 namespace mozilla {
21 namespace hal_impl {
22 
23 void
Vibrate(const nsTArray<uint32_t> & pattern,const WindowIdentifier &)24 Vibrate(const nsTArray<uint32_t> &pattern, const WindowIdentifier &)
25 {
26   // Ignore the WindowIdentifier parameter; it's here only because hal::Vibrate,
27   // hal_sandbox::Vibrate, and hal_impl::Vibrate all must have the same
28   // signature.
29 
30   // Strangely enough, the Android Java API seems to treat vibrate([0]) as a
31   // nop.  But we want to treat vibrate([0]) like CancelVibrate!  (Note that we
32   // also need to treat vibrate([]) as a call to CancelVibrate.)
33   bool allZero = true;
34   for (uint32_t i = 0; i < pattern.Length(); i++) {
35     if (pattern[i] != 0) {
36       allZero = false;
37       break;
38     }
39   }
40 
41   if (allZero) {
42     hal_impl::CancelVibrate(WindowIdentifier());
43     return;
44   }
45 
46   AndroidBridge* b = AndroidBridge::Bridge();
47   if (!b) {
48     return;
49   }
50 
51   b->Vibrate(pattern);
52 }
53 
54 void
CancelVibrate(const WindowIdentifier &)55 CancelVibrate(const WindowIdentifier &)
56 {
57   // Ignore WindowIdentifier parameter.
58 
59   java::GeckoAppShell::CancelVibrate();
60 }
61 
62 void
EnableBatteryNotifications()63 EnableBatteryNotifications()
64 {
65   java::GeckoAppShell::EnableBatteryNotifications();
66 }
67 
68 void
DisableBatteryNotifications()69 DisableBatteryNotifications()
70 {
71   java::GeckoAppShell::DisableBatteryNotifications();
72 }
73 
74 void
GetCurrentBatteryInformation(hal::BatteryInformation * aBatteryInfo)75 GetCurrentBatteryInformation(hal::BatteryInformation* aBatteryInfo)
76 {
77   AndroidBridge::Bridge()->GetCurrentBatteryInformation(aBatteryInfo);
78 }
79 
80 void
EnableNetworkNotifications()81 EnableNetworkNotifications()
82 {
83   java::GeckoAppShell::EnableNetworkNotifications();
84 }
85 
86 void
DisableNetworkNotifications()87 DisableNetworkNotifications()
88 {
89   java::GeckoAppShell::DisableNetworkNotifications();
90 }
91 
92 void
GetCurrentNetworkInformation(hal::NetworkInformation * aNetworkInfo)93 GetCurrentNetworkInformation(hal::NetworkInformation* aNetworkInfo)
94 {
95   AndroidBridge::Bridge()->GetCurrentNetworkInformation(aNetworkInfo);
96 }
97 
98 void
EnableScreenConfigurationNotifications()99 EnableScreenConfigurationNotifications()
100 {
101   java::GeckoAppShell::EnableScreenOrientationNotifications();
102 }
103 
104 void
DisableScreenConfigurationNotifications()105 DisableScreenConfigurationNotifications()
106 {
107   java::GeckoAppShell::DisableScreenOrientationNotifications();
108 }
109 
110 void
GetCurrentScreenConfiguration(ScreenConfiguration * aScreenConfiguration)111 GetCurrentScreenConfiguration(ScreenConfiguration* aScreenConfiguration)
112 {
113   AndroidBridge* bridge = AndroidBridge::Bridge();
114   if (!bridge) {
115     return;
116   }
117 
118   nsresult rv;
119   nsCOMPtr<nsIScreenManager> screenMgr =
120     do_GetService("@mozilla.org/gfx/screenmanager;1", &rv);
121   if (NS_FAILED(rv)) {
122     NS_ERROR("Can't find nsIScreenManager!");
123     return;
124   }
125 
126   nsIntRect rect;
127   int32_t colorDepth, pixelDepth;
128   int16_t angle;
129   ScreenOrientationInternal orientation;
130   nsCOMPtr<nsIScreen> screen;
131 
132   screenMgr->GetPrimaryScreen(getter_AddRefs(screen));
133   screen->GetRect(&rect.x, &rect.y, &rect.width, &rect.height);
134   screen->GetColorDepth(&colorDepth);
135   screen->GetPixelDepth(&pixelDepth);
136   orientation = static_cast<ScreenOrientationInternal>(bridge->GetScreenOrientation());
137   angle = bridge->GetScreenAngle();
138 
139   *aScreenConfiguration =
140     hal::ScreenConfiguration(rect, orientation, angle, colorDepth, pixelDepth);
141 }
142 
143 bool
LockScreenOrientation(const ScreenOrientationInternal & aOrientation)144 LockScreenOrientation(const ScreenOrientationInternal& aOrientation)
145 {
146   // Force the default orientation to be portrait-primary.
147   ScreenOrientationInternal orientation =
148     aOrientation == eScreenOrientation_Default ? eScreenOrientation_PortraitPrimary
149                                                : aOrientation;
150 
151   switch (orientation) {
152     // The Android backend only supports these orientations.
153     case eScreenOrientation_PortraitPrimary:
154     case eScreenOrientation_PortraitSecondary:
155     case eScreenOrientation_PortraitPrimary | eScreenOrientation_PortraitSecondary:
156     case eScreenOrientation_LandscapePrimary:
157     case eScreenOrientation_LandscapeSecondary:
158     case eScreenOrientation_LandscapePrimary | eScreenOrientation_LandscapeSecondary:
159     case eScreenOrientation_Default:
160       java::GeckoAppShell::LockScreenOrientation(orientation);
161       return true;
162     default:
163       return false;
164   }
165 }
166 
167 void
UnlockScreenOrientation()168 UnlockScreenOrientation()
169 {
170   java::GeckoAppShell::UnlockScreenOrientation();
171 }
172 
173 } // hal_impl
174 } // mozilla
175 
176