impl_android.go (3727B)
1 // Copyright 2016 Hajime Hoshi 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 package devicescale 16 17 /* 18 19 #include <jni.h> 20 #include <stdlib.h> 21 22 // Basically same as: 23 // 24 // WindowService windowService = context.getSystemService(Context.WINDOW_SERVICE); 25 // Display display = windowManager.getDefaultDisplay(); 26 // DisplayMetrics displayMetrics = new DisplayMetrics(); 27 // display.getRealMetrics(displayMetrics); 28 // this.deviceScale = displayMetrics.density; 29 // 30 static float deviceScale(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx) { 31 JavaVM* vm = (JavaVM*)java_vm; 32 JNIEnv* env = (JNIEnv*)jni_env; 33 jobject context = (jobject)ctx; 34 35 const char* kWindowService = "window"; 36 37 const jclass android_content_Context = 38 (*env)->FindClass(env, "android/content/Context"); 39 const jclass android_view_WindowManager = 40 (*env)->FindClass(env, "android/view/WindowManager"); 41 const jclass android_view_Display = 42 (*env)->FindClass(env, "android/view/Display"); 43 const jclass android_util_DisplayMetrics = 44 (*env)->FindClass(env, "android/util/DisplayMetrics"); 45 46 const jobject android_context_Context_WINDOW_SERVICE = 47 (*env)->GetStaticObjectField( 48 env, android_content_Context, 49 (*env)->GetStaticFieldID(env, android_content_Context, "WINDOW_SERVICE", "Ljava/lang/String;")); 50 51 const jobject windowManager = 52 (*env)->CallObjectMethod( 53 env, context, 54 (*env)->GetMethodID(env, android_content_Context, "getSystemService", "(Ljava/lang/String;)Ljava/lang/Object;"), 55 android_context_Context_WINDOW_SERVICE); 56 const jobject display = 57 (*env)->CallObjectMethod( 58 env, windowManager, 59 (*env)->GetMethodID(env, android_view_WindowManager, "getDefaultDisplay", "()Landroid/view/Display;")); 60 const jobject displayMetrics = 61 (*env)->NewObject( 62 env, android_util_DisplayMetrics, 63 (*env)->GetMethodID(env, android_util_DisplayMetrics, "<init>", "()V")); 64 (*env)->CallVoidMethod( 65 env, display, 66 (*env)->GetMethodID(env, android_view_Display, "getRealMetrics", "(Landroid/util/DisplayMetrics;)V"), 67 displayMetrics); 68 const float density = 69 (*env)->GetFloatField( 70 env, displayMetrics, 71 (*env)->GetFieldID(env, android_util_DisplayMetrics, "density", "F")); 72 73 (*env)->DeleteLocalRef(env, android_content_Context); 74 (*env)->DeleteLocalRef(env, android_view_WindowManager); 75 (*env)->DeleteLocalRef(env, android_view_Display); 76 (*env)->DeleteLocalRef(env, android_util_DisplayMetrics); 77 78 (*env)->DeleteLocalRef(env, android_context_Context_WINDOW_SERVICE); 79 (*env)->DeleteLocalRef(env, windowManager); 80 (*env)->DeleteLocalRef(env, display); 81 (*env)->DeleteLocalRef(env, displayMetrics); 82 83 return density; 84 } 85 86 */ 87 import "C" 88 89 import ( 90 "fmt" 91 92 "golang.org/x/mobile/app" 93 ) 94 95 func impl(x, y int) float64 { 96 s := 0.0 97 if err := app.RunOnJVM(func(vm, env, ctx uintptr) error { 98 // TODO: This might be crash when this is called from init(). How can we detect this? 99 s = float64(C.deviceScale(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx))) 100 return nil 101 }); err != nil { 102 panic(fmt.Sprintf("devicescale: error %v", err)) 103 } 104 return s 105 }