This repository has been archived by the owner on Mar 2, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 243
/
GoogleUnityActivity.java
279 lines (236 loc) · 9 KB
/
GoogleUnityActivity.java
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
/*
* Copyright 2016 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.
*/
package com.google.unity;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.NativeActivity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.res.Configuration;
import android.graphics.PixelFormat;
import android.hardware.display.DisplayManager;
import android.net.Uri;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.KeyEvent;
import android.view.LayoutInflater;
import android.view.MotionEvent;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import com.unity3d.player.UnityPlayer;
/**
* Custom Unity Activity that passes through Android lifecycle events from Unity appropriately.
*/
public class GoogleUnityActivity
extends Activity
implements ActivityCompat.OnRequestPermissionsResultCallback {
/**
* Callbacks for common Android lifecycle events.
*/
public interface AndroidLifecycleListener {
public void onPause();
public void onResume();
public void onActivityResult(int requestCode, int resultCode, Intent data);
public void onRequestPermissionsResult(
int requestCode, String[] permissions, int[] grantResults);
public void onDisplayChanged();
}
// don't change the name of this variable; referenced from native code
protected UnityPlayer mUnityPlayer;
protected AndroidLifecycleListener mAndroidLifecycleListener;
protected boolean mIsUnityQuit = false;
// Setup activity layout
@Override
protected void onCreate(Bundle savedInstanceState) {
requestWindowFeature(Window.FEATURE_NO_TITLE);
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
getWindow().takeSurface(null);
setTheme(android.R.style.Theme_NoTitleBar_Fullscreen);
getWindow().setFormat(PixelFormat.RGB_565);
DisplayManager displayManager = (DisplayManager) getSystemService(DISPLAY_SERVICE);
if (displayManager != null) {
displayManager.registerDisplayListener(new DisplayManager.DisplayListener() {
@Override
public void onDisplayAdded(int displayId) {}
@Override
public void onDisplayChanged(int displayId) {
synchronized (this) {
if (mAndroidLifecycleListener != null) {
mAndroidLifecycleListener.onDisplayChanged();
}
}
}
@Override
public void onDisplayRemoved(int displayId) {}
}, null);
}
mUnityPlayer = new UnityPlayer(this);
if (mUnityPlayer.getSettings().getBoolean("hide_status_bar", true)) {
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
}
((ViewGroup) findViewById(android.R.id.content)).addView(mUnityPlayer.getView(), 0);
mUnityPlayer.requestFocus();
}
public void showAndroidViewLayer(final int layoutResId) {
final Activity self = this;
runOnUiThread(new Runnable() {
@Override
public void run() {
ViewGroup androidViewContainer =
(ViewGroup) findViewById(R.id.android_view_container);
androidViewContainer.removeAllViews();
// Make it possible for the developer to specify their own layout.
LayoutInflater.from(self).inflate(layoutResId, androidViewContainer);
}
});
}
public View getAndroidViewLayer() {
return findViewById(R.id.android_view_container);
}
public void launchIntent(String packageName, String className, String[] args, int requestcode) {
Intent intent = new Intent();
intent.setClassName(packageName, className);
if (args != null) {
for (int i = 0; i < args.length; i++) {
String[] keyVal = args[i].split(":");
if (keyVal.length == 2) {
intent.putExtra(keyVal[0], keyVal[1]);
}
}
}
startActivityForResult(intent, requestcode);
}
public boolean checkAndroidPermission(String permission) {
int permissionCheck = ContextCompat.checkSelfPermission(this, permission);
return permissionCheck == PackageManager.PERMISSION_GRANTED;
}
public void requestAndroidPermissions(String[] permissions, int requestCode) {
ActivityCompat.requestPermissions(this, permissions, requestCode);
}
public boolean shouldShowRequestAndroidPermissionRationale(String permission) {
return ActivityCompat.shouldShowRequestPermissionRationale(this, permission);
}
public void launchApplicationDetailsSettings() {
Intent intent = new Intent(Settings.ACTION_APPLICATION_DETAILS_SETTINGS);
Uri uri = Uri.fromParts("package", getPackageName(), null);
intent.setData(uri);
startActivity(intent);
}
// CHECKSTYLE:OFF
public void LaunchIntent(String packageName, String className, String[] args, int requestcode) {
// CHECKSTYLE:ON
launchIntent(packageName, className, args, requestcode);
}
public void attachLifecycleListener(AndroidLifecycleListener listener) {
mAndroidLifecycleListener = listener;
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (mAndroidLifecycleListener != null) {
mAndroidLifecycleListener.onActivityResult(requestCode, resultCode, data);
}
}
@Override
public void onRequestPermissionsResult(
int requestCode, String[] permissions, int[] grantResults) {
if (mAndroidLifecycleListener != null) {
mAndroidLifecycleListener.onRequestPermissionsResult(
requestCode, permissions, grantResults);
}
}
// Quit Unity
@Override
protected void onDestroy() {
mUnityPlayer.quit();
mIsUnityQuit = true;
super.onDestroy();
}
// Pause Unity
@Override
protected void onPause() {
super.onPause();
if (mAndroidLifecycleListener != null) {
mAndroidLifecycleListener.onPause();
}
if (!mIsUnityQuit) {
mUnityPlayer.pause();
}
}
// Resume Unity
@Override
protected void onResume() {
super.onResume();
if (mAndroidLifecycleListener != null) {
mAndroidLifecycleListener.onResume();
}
if (!mIsUnityQuit) {
mUnityPlayer.resume();
}
}
public void logAndroidErrorMessage(String message) {
Log.e(this.getPackageName(), message);
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
super.onConfigurationChanged(newConfig);
if (!mIsUnityQuit) {
mUnityPlayer.configurationChanged(newConfig);
}
}
// Notify Unity of the focus change.
@Override
public void onWindowFocusChanged(boolean hasFocus) {
super.onWindowFocusChanged(hasFocus);
if (!mIsUnityQuit) {
mUnityPlayer.windowFocusChanged(hasFocus);
}
}
// For some reason the multiple keyevent type is not supported by the ndk.
// Force event injection by overriding dispatchKeyEvent().
@Override
public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getAction() == KeyEvent.ACTION_MULTIPLE) {
return mUnityPlayer.injectEvent(event);
}
return super.dispatchKeyEvent(event);
}
// Pass any events not handled by (unfocused) views straight to UnityPlayer
@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
return mUnityPlayer.injectEvent(event);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
return mUnityPlayer.injectEvent(event);
}
/* API12 */
@Override
public boolean onGenericMotionEvent(MotionEvent event) {
return mUnityPlayer.injectEvent(event);
}
}