GvrActivityHelper.cs
3.07 KB
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
//-----------------------------------------------------------------------
// <copyright file="GvrActivityHelper.cs" company="Google Inc.">
// Copyright 2017 Google Inc. All rights reserved.
//
// 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.
// </copyright>
//-----------------------------------------------------------------------
using UnityEngine;
/// <summary>
/// Simple static class to abstract out several JNI calls that need to be shared between different
/// classes.
/// </summary>
public static class GvrActivityHelper
{
/// @cond
#if UNITY_IOS
/// <summary>The name of the GVR DLL.</summary>
public const string GVR_DLL_NAME = "__Internal";
#else
/// <summary>The name of the GVR DLL.</summary>
public const string GVR_DLL_NAME = "gvr";
/// <summary>The name of the GVR Shim DLL.</summary>
public const string GVR_SHIM_DLL_NAME = "gvrshim";
#endif // UNITY_IOS
/// @endcond
/// <summary>Package name for the Android Unity player class.</summary>
public const string PACKAGE_UNITY_PLAYER = "com.unity3d.player.UnityPlayer";
#if UNITY_ANDROID && !UNITY_EDITOR
/// <summary>
/// Returns the Android Activity used by the Unity device player.
/// </summary>
/// <remarks>
/// The caller is responsible for memory-managing the returned `AndroidJavaObject`.
/// </remarks>
/// <returns>An Android GVR activity.</returns>
public static AndroidJavaObject GetActivity()
{
AndroidJavaClass jc = new AndroidJavaClass(PACKAGE_UNITY_PLAYER);
if (jc == null)
{
Debug.LogErrorFormat("Failed to get class {0}", PACKAGE_UNITY_PLAYER);
return null;
}
AndroidJavaObject activity = jc.GetStatic<AndroidJavaObject>("currentActivity");
if (activity == null)
{
Debug.LogError("Failed to obtain current Android activity.");
return null;
}
return activity;
}
/// <summary>Returns the application context of the current Android Activity.</summary>
/// <param name="activity">The Android activity from which to get the context.</param>
/// <returns>The application context from the associated GVR activity.</returns>
public static AndroidJavaObject GetApplicationContext(AndroidJavaObject activity)
{
AndroidJavaObject context = activity.Call<AndroidJavaObject>("getApplicationContext");
if (context == null)
{
Debug.LogError("Failed to get application context from Activity.");
return null;
}
return context;
}
#endif // UNITY_ANDROID && !UNITY_EDITOR
}