twitchapon-anim

Basic Twitchapon Receiver/Visuals
git clone git://bsandro.tech/twitchapon-anim
Log | Files | Refs | README | LICENSE

impl_android.go (2542B)


      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 `getResources().getDisplayMetrics().density`;
     23 static float deviceScale(uintptr_t java_vm, uintptr_t jni_env, uintptr_t ctx) {
     24   JavaVM* vm = (JavaVM*)java_vm;
     25   JNIEnv* env = (JNIEnv*)jni_env;
     26   jobject context = (jobject)ctx;
     27 
     28   const jclass android_content_ContextWrapper =
     29       (*env)->FindClass(env, "android/content/ContextWrapper");
     30   const jclass android_content_res_Resources =
     31       (*env)->FindClass(env, "android/content/res/Resources");
     32   const jclass android_util_DisplayMetrics =
     33       (*env)->FindClass(env, "android/util/DisplayMetrics");
     34 
     35   const jobject resources =
     36       (*env)->CallObjectMethod(
     37           env, context,
     38           (*env)->GetMethodID(env, android_content_ContextWrapper, "getResources", "()Landroid/content/res/Resources;"));
     39   const jobject displayMetrics =
     40       (*env)->CallObjectMethod(
     41           env, resources,
     42           (*env)->GetMethodID(env, android_content_res_Resources, "getDisplayMetrics", "()Landroid/util/DisplayMetrics;"));
     43   const float density =
     44       (*env)->GetFloatField(
     45           env, displayMetrics,
     46           (*env)->GetFieldID(env, android_util_DisplayMetrics, "density", "F"));
     47 
     48   (*env)->DeleteLocalRef(env, android_content_ContextWrapper);
     49   (*env)->DeleteLocalRef(env, android_content_res_Resources);
     50   (*env)->DeleteLocalRef(env, android_util_DisplayMetrics);
     51   (*env)->DeleteLocalRef(env, resources);
     52   (*env)->DeleteLocalRef(env, displayMetrics);
     53 
     54   return density;
     55 }
     56 
     57 */
     58 import "C"
     59 
     60 import (
     61 	"fmt"
     62 
     63 	"golang.org/x/mobile/app"
     64 )
     65 
     66 func impl(x, y int) float64 {
     67 	s := 0.0
     68 	if err := app.RunOnJVM(func(vm, env, ctx uintptr) error {
     69 		// TODO: This might be crash when this is called from init(). How can we detect this?
     70 		s = float64(C.deviceScale(C.uintptr_t(vm), C.uintptr_t(env), C.uintptr_t(ctx)))
     71 		return nil
     72 	}); err != nil {
     73 		panic(fmt.Sprintf("devicescale: error %v", err))
     74 	}
     75 	return s
     76 }