-
Notifications
You must be signed in to change notification settings - Fork 26
/
Display.m
278 lines (247 loc) · 9.19 KB
/
Display.m
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
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
/*
* Copyright (c) 2016, 2023, Gluon
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
* ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
* WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL GLUON BE LIABLE FOR ANY
* DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
* (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
* LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
* ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
* SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include "Display.h"
JNIEnv *env;
JNIEXPORT jint JNICALL
JNI_OnLoad_Display(JavaVM *vm, void *reserved)
{
#ifdef JNI_VERSION_1_8
//min. returned JNI_VERSION required by JDK8 for builtin libraries
if ((*vm)->GetEnv(vm, (void **)&env, JNI_VERSION_1_8) != JNI_OK) {
return JNI_VERSION_1_4;
}
return JNI_VERSION_1_8;
#else
return JNI_VERSION_1_4;
#endif
}
static int DisplayInited = 0;
// Display
jclass mat_jDisplayServiceClass;
jmethodID mat_jDisplayService_notifyDisplay = 0;
Display *_display;
bool iPhoneX;
JNIEXPORT void JNICALL Java_com_gluonhq_attach_display_impl_IOSDisplayService_initDisplay
(JNIEnv *env, jclass jClass)
{
if (DisplayInited)
{
return;
}
DisplayInited = 1;
mat_jDisplayServiceClass = (*env)->NewGlobalRef(env, (*env)->FindClass(env, "com/gluonhq/attach/display/impl/IOSDisplayService"));
mat_jDisplayService_notifyDisplay = (*env)->GetStaticMethodID(env, mat_jDisplayServiceClass, "notifyDisplay", "(Ljava/lang/String;)V");
_display = [[Display alloc] init];
[_display isIPhoneX];
}
JNIEXPORT jboolean JNICALL Java_com_gluonhq_attach_display_impl_IOSDisplayService_isIphone
(JNIEnv *env, jclass jClass)
{
NSString *deviceModel = (NSString*)[UIDevice currentDevice].model;
if ([[deviceModel substringWithRange:NSMakeRange(0, 4)] isEqualToString:@"iPad"]) {
return JNI_FALSE;
} else {
return JNI_TRUE;
}
}
JNIEXPORT jdoubleArray JNICALL Java_com_gluonhq_attach_display_impl_IOSDisplayService_screenSize
(JNIEnv *env, jclass jClass)
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
CGFloat screenScale = [[UIScreen mainScreen] scale];
CGSize screenSize = CGSizeMake(screenBounds.size.width * screenScale, screenBounds.size.height * screenScale);
jdoubleArray output = (*env)->NewDoubleArray(env, 2);
if (output == NULL)
{
return NULL;
}
jdouble res[] = {screenSize.width, screenSize.height};
(*env)->SetDoubleArrayRegion(env, output, 0, 2, res);
return output;
}
JNIEXPORT jdoubleArray JNICALL Java_com_gluonhq_attach_display_impl_IOSDisplayService_screenBounds
(JNIEnv *env, jclass jClass)
{
CGRect screenBounds = [[UIScreen mainScreen] bounds];
jdoubleArray output = (*env)->NewDoubleArray(env, 2);
if (output == NULL)
{
return NULL;
}
jdouble res[] = {screenBounds.size.width, screenBounds.size.height};
printf("%f %f\n", res[0], res[1]);
(*env)->SetDoubleArrayRegion(env, output, 0, 2, res);
return output;
}
JNIEXPORT jfloat JNICALL Java_com_gluonhq_attach_display_impl_IOSDisplayService_screenScale
(JNIEnv *env, jclass jClass)
{
return [UIScreen mainScreen].scale;
}
JNIEXPORT jboolean JNICALL Java_com_gluonhq_attach_display_impl_IOSDisplayService_isNotchFound
(JNIEnv *env, jclass jClass)
{
if (iPhoneX)
{
return JNI_TRUE;
} else {
return JNI_FALSE;
}
}
JNIEXPORT void JNICALL Java_com_gluonhq_attach_display_impl_IOSDisplayService_startObserver
(JNIEnv *env, jclass jClass)
{
dispatch_async(dispatch_get_main_queue(), ^{
[_display startObserver];
});
return;
}
JNIEXPORT void JNICALL Java_com_gluonhq_attach_display_impl_IOSDisplayService_stopObserver
(JNIEnv *env, jclass jClass)
{
[_display stopObserver];
return;
}
void sendNotch() {
NSString *notch = [_display getNotch];
if (debugAttach) {
AttachLog(@"Notch is %@", notch);
}
const char *notchChars = [notch UTF8String];
jstring arg = (*env)->NewStringUTF(env, notchChars);
(*env)->CallStaticVoidMethod(env, mat_jDisplayServiceClass, mat_jDisplayService_notifyDisplay, arg);
(*env)->DeleteLocalRef(env, arg);
}
@implementation Display
NSString * GetDeviceModel(void)
{
static dispatch_once_t token;
static NSString *modelString = nil;
dispatch_once(&token, ^{
#if TARGET_OS_SIMULATOR
modelString = NSProcessInfo.processInfo.environment[@"SIMULATOR_MODEL_IDENTIFIER"];
#else
struct utsname systemInfo;
uname(&systemInfo);
modelString = [NSString stringWithCString:systemInfo.machine encoding:NSUTF8StringEncoding];
#endif
});
if (debugAttach) {
AttachLog(@"Device name: %@", modelString);
}
return modelString;
}
- (void) isIPhoneX
{
iPhoneX = NO;
NSArray<NSString *> *modelsWithNotch = @[
@"iPhone10,3", @"iPhone10,6", // iPhone X Global, X GSM
@"iPhone11,2", @"iPhone11,4", @"iPhone11,6", // iPhone XS, XS Max, XS Max Global
@"iPhone11,8", // iPhone XR
@"iPhone12,1", @"iPhone12,3", @"iPhone12,5", // iPhone 11, 11 Pro, 11 Pro Max
@"iPhone13,1", @"iPhone13,2", @"iPhone13,3", @"iPhone13,4", // iPhone 12 Mini, 12, 12 Pro, 12 Pro Max
@"iPhone14,2", @"iPhone14,3", @"iPhone14,4", @"iPhone14,5", // iPhone 13 Pro, 13 Pro Max, 13 Mini, 13
@"iPhone14,7", @"iPhone14,8", @"iPhone15,2", @"iPhone15,3", // iPhone 14, 14 Plus, 14 Pro, 14 Pro Max
@"iPhone15,4", @"iPhone15,5", @"iPhone16,1", @"iPhone16,2", // iPhone 15, 15 Plus, 15 Pro, 15 Pro Max
];
if ([modelsWithNotch containsObject:GetDeviceModel()]) {
if (debugAttach) {
AttachLog(@"This device has a notch according to the model list");
}
iPhoneX = YES;
} else {
// try safeAreaInsets
if (@available(iOS 11.0, *)) {
UIWindow *window = UIApplication.sharedApplication.keyWindow;
if (!window) {
AttachLog(@"key window was nil");
return;
}
CGFloat topPadding = window.safeAreaInsets.top;
if (debugAttach) {
AttachLog(@"topPadding is %.3f", topPadding);
}
if (topPadding > 24) {
if (debugAttach) {
AttachLog(@"This device has a notch according to the top padding");
}
iPhoneX = YES;
}
} else {
// check height
if ([[UIDevice currentDevice].model hasPrefix:@"iPhone"] &&
([[UIScreen mainScreen] nativeBounds].size.height == 1792 || // XR
[[UIScreen mainScreen] nativeBounds].size.height == 2436 || // X, XS
[[UIScreen mainScreen] nativeBounds].size.height == 2688)) // XS MAX
{
if (debugAttach) {
AttachLog(@"This device has a notch according to the screen bounds");
}
iPhoneX = YES;
}
}
}
}
- (void) startObserver
{
if (iPhoneX)
{
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(OrientationDidChange:) name:UIDeviceOrientationDidChangeNotification object:nil];
sendNotch();
}
}
- (void) stopObserver
{
if (iPhoneX)
{
[[NSNotificationCenter defaultCenter] removeObserver:self name:UIDeviceOrientationDidChangeNotification object:nil];
}
}
- (NSString*) getNotch
{
if (! iPhoneX) {
return @"UNKNOWN";
}
UIDeviceOrientation orientation = [[UIDevice currentDevice] orientation];
NSMutableString *value;
if (orientation == UIDeviceOrientationPortraitUpsideDown)
value = [NSMutableString stringWithString: @"BOTTOM"];
else if (orientation == UIDeviceOrientationPortrait)
value = [NSMutableString stringWithString: @"TOP"];
else if (orientation == UIDeviceOrientationLandscapeLeft) // home button on the right side, notch to the left.
value = [NSMutableString stringWithString: @"LEFT"];
else if (orientation == UIDeviceOrientationLandscapeRight) // home button on the left side, notch to the right.
value = [NSMutableString stringWithString: @"RIGHT"];
else
value = [NSMutableString stringWithString: @"UNKNOWN"];
return value;
}
-(void)OrientationDidChange:(NSNotification*)notification
{
sendNotch();
}
@end