2016-06-18 17:55:24 +02:00
|
|
|
// Copyright 2016 Hajime Hoshi
|
|
|
|
//
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
//
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
//
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
// limitations under the License.
|
|
|
|
|
2018-01-02 21:22:56 +01:00
|
|
|
package devicescale
|
2016-06-18 17:55:24 +02:00
|
|
|
|
2016-07-02 22:37:56 +02:00
|
|
|
/*
|
|
|
|
|
|
|
|
#include <jni.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
|
|
|
|
// Basically same as `getResources().getDisplayMetrics().density`;
|
|
|
|
static float deviceScale(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx) {
|
|
|
|
JavaVM* vm = (JavaVM*)java_vm;
|
|
|
|
JNIEnv* env = (JNIEnv*)jni_env;
|
|
|
|
jobject context = (jobject)ctx;
|
|
|
|
|
|
|
|
const jclass android_content_ContextWrapper =
|
|
|
|
(*env)->FindClass(env, "android/content/ContextWrapper");
|
|
|
|
const jclass android_content_res_Resources =
|
|
|
|
(*env)->FindClass(env, "android/content/res/Resources");
|
|
|
|
const jclass android_util_DisplayMetrics =
|
|
|
|
(*env)->FindClass(env, "android/util/DisplayMetrics");
|
|
|
|
|
|
|
|
const jobject resources =
|
|
|
|
(*env)->CallObjectMethod(
|
|
|
|
env, context,
|
|
|
|
(*env)->GetMethodID(env, android_content_ContextWrapper, "getResources", "()Landroid/content/res/Resources;"));
|
|
|
|
const jobject displayMetrics =
|
|
|
|
(*env)->CallObjectMethod(
|
|
|
|
env, resources,
|
|
|
|
(*env)->GetMethodID(env, android_content_res_Resources, "getDisplayMetrics", "()Landroid/util/DisplayMetrics;"));
|
|
|
|
const float density =
|
|
|
|
(*env)->GetFloatField(
|
|
|
|
env, displayMetrics,
|
|
|
|
(*env)->GetFieldID(env, android_util_DisplayMetrics, "density", "F"));
|
2017-11-12 17:30:08 +01:00
|
|
|
|
|
|
|
(*env)->DeleteLocalRef(env, android_content_ContextWrapper);
|
|
|
|
(*env)->DeleteLocalRef(env, android_content_res_Resources);
|
|
|
|
(*env)->DeleteLocalRef(env, android_util_DisplayMetrics);
|
|
|
|
(*env)->DeleteLocalRef(env, resources);
|
|
|
|
(*env)->DeleteLocalRef(env, displayMetrics);
|
|
|
|
|
2016-07-02 22:37:56 +02:00
|
|
|
return density;
|
|
|
|
}
|
|
|
|
|
|
|
|
*/
|
|
|
|
import "C"
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2018-08-06 16:29:31 +02:00
|
|
|
"golang.org/x/mobile/app"
|
2016-07-02 22:37:56 +02:00
|
|
|
)
|
|
|
|
|
2018-10-06 10:29:40 +02:00
|
|
|
func impl(x, y int) float64 {
|
2018-01-02 21:22:56 +01:00
|
|
|
s := 0.0
|
2018-08-06 16:29:31 +02:00
|
|
|
if err := app.RunOnJVM(func(vm, env, ctx uintptr) error {
|
|
|
|
// TODO: This might be crash when this is called from init(). How can we detect this?
|
2018-01-02 21:22:56 +01:00
|
|
|
s = float64(C.deviceScale(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx)))
|
2016-07-02 22:37:56 +02:00
|
|
|
return nil
|
|
|
|
}); err != nil {
|
2018-01-02 21:22:56 +01:00
|
|
|
panic(fmt.Sprintf("devicescale: error %v", err))
|
2016-07-02 22:37:56 +02:00
|
|
|
}
|
2018-01-02 21:22:56 +01:00
|
|
|
return s
|
2016-06-18 17:55:24 +02:00
|
|
|
}
|