|
| 1 | +package com.trophit; |
| 2 | + |
| 3 | +import com.unity3d.player.UnityPlayerActivity; |
| 4 | +import android.app.Activity; |
| 5 | +import android.content.Intent; |
| 6 | +import android.os.Bundle; |
| 7 | +import android.util.Log; |
| 8 | +import android.view.Window; |
| 9 | + |
| 10 | + |
| 11 | +public class DeeplinkActivity extends Activity { |
| 12 | + |
| 13 | + @Override |
| 14 | + protected void onCreate(Bundle savedInstanceState) { |
| 15 | + super.onCreate(savedInstanceState); |
| 16 | + Intent intent = getIntent(); |
| 17 | + Log.d(getClass().getName(), "onCreate " + intent.getAction() + " " + intent.getDataString()); |
| 18 | + requestWindowFeature(Window.FEATURE_NO_TITLE); |
| 19 | + |
| 20 | + Class<?> cls = getMainActivityClass(); |
| 21 | + if (cls != null) { |
| 22 | + Intent newIntent = new Intent(this, cls); |
| 23 | + this.startActivity(newIntent); |
| 24 | + onDeeplink(intent); |
| 25 | + } |
| 26 | + finish(); |
| 27 | + } |
| 28 | + |
| 29 | + protected void onDeeplink(Intent intent) { |
| 30 | + if (Intent.ACTION_VIEW.equals(intent.getAction())) { |
| 31 | + String deeplink = intent.getDataString(); |
| 32 | + Log.d(getClass().getName(), "onDeeplink " + deeplink); |
| 33 | + if (deeplink != null) |
| 34 | + com.unity3d.player.UnityPlayer.UnitySendMessage(gameObject, deeplinkMethod, deeplink); |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + private Class<?> getMainActivityClass() { |
| 39 | + String packageName = this.getPackageName(); |
| 40 | + Intent launchIntent = this.getPackageManager().getLaunchIntentForPackage(packageName); |
| 41 | + try { |
| 42 | + return Class.forName(launchIntent.getComponent().getClassName()); |
| 43 | + } catch (Exception e) { |
| 44 | + Log.e(getClass().getName(), "getMainActivityClass: Unable to find Main Activity Class"); |
| 45 | + return null; |
| 46 | + } |
| 47 | + } |
| 48 | + |
| 49 | + protected static final String gameObject = "UnityDeeplinks"; |
| 50 | + protected static final String deeplinkMethod = "onDeeplink"; |
| 51 | +} |
0 commit comments