-
Notifications
You must be signed in to change notification settings - Fork 2
/
InteropLib.cs
68 lines (57 loc) · 2.07 KB
/
InteropLib.cs
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
using System.Collections;
using System.Collections.Generic;
using UnityEngine;
public class AppInfo
{
public AppInfo() { }
public string label;
public string name;
public Texture2D icon;
public AppInfo(AndroidJavaObject jniObj)
{
name = jniObj.Get<string>("name");
label = jniObj.Get<string>("label");
byte[] pngBytes = jniObj.Get<byte[]>("icon");
if (pngBytes != null)
{
icon = new Texture2D(1, 1);
icon.LoadImage(pngBytes, true);
}
}
}
public static class InteropLib
{
public static List<AppInfo> GetAllInstalledApps()
{
List<AppInfo> result;
#if UNITY_EDITOR
result = new List<AppInfo>();
for (int i = 0; i < 20; ++i)
result.Add(new AppInfo { name = "Test name", label = "Test label" });
#else
AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject provider = new AndroidJavaObject("com.unity3d.richardf.UnityBasedLauncher.AppInfoProvider", currentActivity);
int appCount = provider.Call<int>("getAppCount");
Debug.LogFormat("Retrieving {0} apps", appCount);
result = new List<AppInfo>(appCount);
for (int i = 0; i < appCount; ++i)
{
AndroidJavaObject app = provider.Call<AndroidJavaObject>("getApp", i);
result.Add(new AppInfo(app));
}
#endif
return result;
}
public static void LaunchApp(string appName)
{
#if UNITY_EDITOR
Debug.LogFormat("Launching app {0}", appName);
#else
AndroidJavaClass unityPlayerClass = new AndroidJavaClass("com.unity3d.player.UnityPlayer");
AndroidJavaObject currentActivity = unityPlayerClass.GetStatic<AndroidJavaObject>("currentActivity");
AndroidJavaObject launcher = new AndroidJavaObject("com.unity3d.richardf.UnityBasedLauncher.AppLauncher", currentActivity);
launcher.Call("launchApp", appName);
#endif
}
}