217 lines
6.5 KiB
Java
217 lines
6.5 KiB
Java
package com.qp51.dev;
|
||
|
||
import java.io.File;
|
||
|
||
import com.qp51.dev.wxapi.WXAPI;
|
||
import com.taurus.sdk.SDKManager;
|
||
import com.unity3d.player.UnityPlayer;
|
||
|
||
import android.annotation.SuppressLint;
|
||
import android.app.Activity;
|
||
import android.content.ClipData;
|
||
import android.content.ClipboardManager;
|
||
import android.content.Context;
|
||
import android.content.Intent;
|
||
import android.content.IntentFilter;
|
||
import android.content.res.Configuration;
|
||
import android.graphics.BitmapFactory;
|
||
import android.graphics.PixelFormat;
|
||
import android.net.Uri;
|
||
import android.os.Build;
|
||
import android.os.Bundle;
|
||
import android.os.Process;
|
||
import android.support.v4.content.FileProvider;
|
||
import android.view.KeyEvent;
|
||
import android.view.MotionEvent;
|
||
import android.view.View;
|
||
import android.view.Window;
|
||
|
||
public class UnityPlayerActivity extends Activity {
|
||
protected UnityPlayer mUnityPlayer; // don't change the name of this
|
||
// variable; referenced from native code
|
||
BatteryReceiver receiver;
|
||
// Setup activity layout
|
||
@SuppressLint("InlinedApi")
|
||
@Override
|
||
protected void onCreate(Bundle savedInstanceState) {
|
||
requestWindowFeature(Window.FEATURE_NO_TITLE);
|
||
super.onCreate(savedInstanceState);
|
||
|
||
getWindow().setFormat(PixelFormat.RGBX_8888); // <--- This makes xperia
|
||
IntentFilter intentFilter = new IntentFilter(Intent.ACTION_BATTERY_CHANGED);
|
||
receiver = new BatteryReceiver();
|
||
registerReceiver(receiver, intentFilter);
|
||
mUnityPlayer = new UnityPlayer(this);
|
||
|
||
|
||
|
||
setContentView(mUnityPlayer);
|
||
mUnityPlayer.requestFocus();
|
||
SDKManager manager = SDKManager.me();
|
||
manager.addAPI(new WXAPI(getApplicationContext()));
|
||
manager.setAppIcon(BitmapFactory.decodeResource(getResources(), R.drawable.app_icon));
|
||
|
||
// mUnityPlayer.setSystemUiVisibility(visibility);
|
||
int cur_ver = Build.VERSION.SDK_INT;
|
||
if(cur_ver>=19) {
|
||
mUnityPlayer.setSystemUiVisibility(
|
||
View.SYSTEM_UI_FLAG_FULLSCREEN
|
||
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
||
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|
||
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
||
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
||
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
|
||
final View paramBundle = getWindow().getDecorView();
|
||
paramBundle.setOnSystemUiVisibilityChangeListener(new View.OnSystemUiVisibilityChangeListener() {
|
||
@SuppressLint("InlinedApi")
|
||
public void onSystemUiVisibilityChange(int paramAnonymousInt) {
|
||
if ((paramAnonymousInt & View.SYSTEM_UI_FLAG_FULLSCREEN) == 0) {
|
||
mUnityPlayer.setSystemUiVisibility(
|
||
View.SYSTEM_UI_FLAG_FULLSCREEN
|
||
| View.SYSTEM_UI_FLAG_LAYOUT_STABLE
|
||
| View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY
|
||
| View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION
|
||
| View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN
|
||
| View.SYSTEM_UI_FLAG_HIDE_NAVIGATION);
|
||
}
|
||
}
|
||
});
|
||
}
|
||
|
||
}
|
||
|
||
// Quit Unity
|
||
@Override
|
||
protected void onDestroy() {
|
||
mUnityPlayer.quit();
|
||
super.onDestroy();
|
||
unregisterReceiver(receiver);
|
||
}
|
||
|
||
// Pause Unity
|
||
@Override
|
||
protected void onPause() {
|
||
super.onPause();
|
||
mUnityPlayer.pause();
|
||
}
|
||
|
||
@SuppressLint("InlinedApi")
|
||
// Resume Unity
|
||
@Override
|
||
protected void onResume() {
|
||
super.onResume();
|
||
mUnityPlayer.resume();
|
||
}
|
||
|
||
// This ensures the layout will be correct.
|
||
@Override
|
||
public void onConfigurationChanged(Configuration newConfig) {
|
||
super.onConfigurationChanged(newConfig);
|
||
mUnityPlayer.configurationChanged(newConfig);
|
||
}
|
||
|
||
// Notify Unity of the focus change.
|
||
@Override
|
||
public void onWindowFocusChanged(boolean hasFocus) {
|
||
super.onWindowFocusChanged(hasFocus);
|
||
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);
|
||
}
|
||
|
||
|
||
int __getVersion() {
|
||
return Build.VERSION.SDK_INT;
|
||
}
|
||
|
||
|
||
private void __install() {
|
||
File file = new File(_apkPath);
|
||
Intent intent = new Intent();
|
||
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
|
||
intent.setAction(Intent.ACTION_VIEW);
|
||
if(Build.VERSION.SDK_INT>=Build.VERSION_CODES.N){
|
||
Uri uriForFile = FileProvider.getUriForFile(this, this.getApplicationContext().getPackageName() + ".provider", file);
|
||
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
|
||
intent.setDataAndType(uriForFile, this.getContentResolver().getType(uriForFile));
|
||
}else{
|
||
intent.setDataAndType(Uri.fromFile(file),"application/vnd.android.package-archive");
|
||
}
|
||
startActivity(intent);
|
||
Process.killProcess(Process.myPid());
|
||
}
|
||
|
||
private String _apkPath;
|
||
void __installApk(final String apkPath) {
|
||
_apkPath = apkPath;
|
||
__install();
|
||
}
|
||
|
||
float __getBattery() {
|
||
return this.receiver.battery;
|
||
}
|
||
|
||
static String _roomid = "";
|
||
String __getRoomID() {
|
||
String tem = _roomid;
|
||
_roomid = "";
|
||
return tem;
|
||
}
|
||
|
||
public static void __setRoomID(String id)
|
||
{
|
||
_roomid = id;
|
||
}
|
||
|
||
void __copyTextToClipboard(final String text) {
|
||
// 从API11<31>???始android推荐使用android.content.ClipboardManager
|
||
// 为了兼容低版本我们这里使用旧版的android.text.ClipboardManager,虽然提示deprecated,但不影响使用<E4BDBF>??
|
||
ClipboardManager cm = (ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
|
||
// 将文本内容放到系统剪贴板里<E69DBF>??
|
||
cm.setPrimaryClip(ClipData.newPlainText("text", text));
|
||
}
|
||
|
||
public void __auth(int id,String jsonstr) {
|
||
SDKManager manager = SDKManager.me();
|
||
if (!manager.isAppInstalled(id) || !manager.isAppSupportAPI(id)) {
|
||
return;
|
||
}
|
||
SDKManager.me().auth(id, jsonstr);
|
||
}
|
||
|
||
public void __shareLink(final int id,final String jsonstr,final String callback) {
|
||
SDKManager manager = SDKManager.me();
|
||
if (!manager.isAppInstalled(id) || !manager.isAppSupportAPI(id)) {
|
||
return;
|
||
}
|
||
manager.share(id, jsonstr,callback);
|
||
}
|
||
|
||
// 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 */public boolean onGenericMotionEvent(MotionEvent event) {
|
||
return mUnityPlayer.injectEvent(event);
|
||
}
|
||
}
|