Merge pull request #3283 from kevlahnota/newmaster2

[Android] support receiving text intent
This commit is contained in:
Anthony Calosa
2023-06-17 16:22:03 +08:00
committed by GitHub
2 changed files with 49 additions and 6 deletions

View File

@@ -24,11 +24,18 @@
<activity <activity
android:name=".Main" android:name=".Main"
android:label="@string/app_name" android:label="@string/app_name"
android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout"> android:configChanges="keyboard|keyboardHidden|navigation|orientation|screenSize|screenLayout"
android:launchMode="singleInstance"
android:theme="@android:style/Theme.Black.NoTitleBar">
<intent-filter> <intent-filter>
<action android:name="android.intent.action.MAIN" /> <action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" /> <category android:name="android.intent.category.LAUNCHER" />
</intent-filter> </intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity> </activity>
<activity android:name=".Exiter" android:theme="@android:style/Theme.NoDisplay"/> <activity android:name=".Exiter" android:theme="@android:style/Theme.NoDisplay"/>
<meta-data android:name="io.sentry.dsn" android:value="https://a0b8dbad9b8a49cfa51bf65d462e8dae:b3f27d7461224cb8836eb5c6050c666c@sentry.cardforge.org/2?buffer.enabled=false" /> <meta-data android:name="io.sentry.dsn" android:value="https://a0b8dbad9b8a49cfa51bf65d462e8dae:b3f27d7461224cb8836eb5c6050c666c@sentry.cardforge.org/2?buffer.enabled=false" />

View File

@@ -3,6 +3,7 @@ package forge.app;
import java.io.File; import java.io.File;
import java.io.InputStream; import java.io.InputStream;
import java.io.OutputStream; import java.io.OutputStream;
import java.text.Normalizer;
import java.util.ArrayList; import java.util.ArrayList;
import android.graphics.Point; import android.graphics.Point;
@@ -62,11 +63,44 @@ import org.apache.commons.lang3.tuple.Pair;
public class Main extends AndroidApplication { public class Main extends AndroidApplication {
AndroidAdapter Gadapter; AndroidAdapter Gadapter;
ArrayList<String> gamepads; ArrayList<String> gamepads;
AndroidClipboard androidClipboard;
boolean hasLaunched;
private AndroidClipboard getAndroidClipboard() {
if (androidClipboard == null)
androidClipboard = new AndroidClipboard();
return androidClipboard;
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
getAndroidClipboard().setContents(intent.getStringExtra(Intent.EXTRA_TEXT));
}
}
}
@Override @Override
protected void onCreate(Bundle savedInstanceState) { protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);
gamepads = getGameControllers(); gamepads = getGameControllers();
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
if ("text/plain".equals(type)) {
getAndroidClipboard().setContents(intent.getStringExtra(Intent.EXTRA_TEXT));
}
}
if (hasLaunched)
return;
hasLaunched = true;
//init Sentry //init Sentry
//SentryAndroid.init(this); //SentryAndroid.init(this);
@@ -211,7 +245,7 @@ public class Main extends AndroidApplication {
String message = getDeviceName()+"\n"+"Android "+AndroidRelease+"\n"+"RAM "+ totalRAM+"MB" +"\n"+"LibGDX "+ Version.VERSION+"\n"+"Can't access external storage"; String message = getDeviceName()+"\n"+"Android "+AndroidRelease+"\n"+"RAM "+ totalRAM+"MB" +"\n"+"LibGDX "+ Version.VERSION+"\n"+"Can't access external storage";
Sentry.addBreadcrumb(new Breadcrumb(message)); Sentry.addBreadcrumb(new Breadcrumb(message));
Main.this.setRequestedOrientation(Main.this.getResources().getConfiguration().orientation); Main.this.setRequestedOrientation(Main.this.getResources().getConfiguration().orientation);
initialize(Forge.getApp(new AndroidClipboard(), adapter, "", false, true, totalRAM, isTabletDevice, AndroidAPI, AndroidRelease, getDeviceName())); initialize(Forge.getApp(getAndroidClipboard(), adapter, "", false, true, totalRAM, isTabletDevice, AndroidAPI, AndroidRelease, getDeviceName()));
displayMessage(adapter, true, message); displayMessage(adapter, true, message);
return; return;
} }
@@ -224,7 +258,7 @@ public class Main extends AndroidApplication {
String message = getDeviceName()+"\n"+"Android "+AndroidRelease+"\n"+"RAM "+ totalRAM+"MB" +"\n"+"LibGDX "+ Version.VERSION+"\n"+"Can't access external storage\nPath: " + assetsDir; String message = getDeviceName()+"\n"+"Android "+AndroidRelease+"\n"+"RAM "+ totalRAM+"MB" +"\n"+"LibGDX "+ Version.VERSION+"\n"+"Can't access external storage\nPath: " + assetsDir;
Sentry.addBreadcrumb(new Breadcrumb(message)); Sentry.addBreadcrumb(new Breadcrumb(message));
Main.this.setRequestedOrientation(Main.this.getResources().getConfiguration().orientation); Main.this.setRequestedOrientation(Main.this.getResources().getConfiguration().orientation);
initialize(Forge.getApp(new AndroidClipboard(), adapter, "", false, true, totalRAM, isTabletDevice, AndroidAPI, AndroidRelease, getDeviceName())); initialize(Forge.getApp(getAndroidClipboard(), adapter, "", false, true, totalRAM, isTabletDevice, AndroidAPI, AndroidRelease, getDeviceName()));
displayMessage(adapter, true, message); displayMessage(adapter, true, message);
return; return;
} }
@@ -250,18 +284,19 @@ public class Main extends AndroidApplication {
isPortrait = true; isPortrait = true;
Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT); Main.this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_PORTRAIT);
} }
initialize(Forge.getApp(new AndroidClipboard(), adapter, assetsDir, propertyConfig, isPortrait, totalRAM, isTabletDevice, AndroidAPI, AndroidRelease, getDeviceName())); initialize(Forge.getApp(getAndroidClipboard(), adapter, assetsDir, propertyConfig, isPortrait, totalRAM, isTabletDevice, AndroidAPI, AndroidRelease, getDeviceName()));
} else { } else {
isPortrait = true; isPortrait = true;
//fake init for permission instruction //fake init for permission instruction
Main.this.setRequestedOrientation(Main.this.getResources().getConfiguration().orientation); Main.this.setRequestedOrientation(Main.this.getResources().getConfiguration().orientation);
initialize(Forge.getApp(new AndroidClipboard(), adapter, "", false, isPortrait, totalRAM, isTabletDevice, AndroidAPI, AndroidRelease, getDeviceName())); initialize(Forge.getApp(getAndroidClipboard(), adapter, "", false, isPortrait, totalRAM, isTabletDevice, AndroidAPI, AndroidRelease, getDeviceName()));
displayMessage(adapter, false, ""); displayMessage(adapter, false, "");
} }
} }
@Override @Override
protected void onDestroy() { protected void onDestroy() {
hasLaunched = false;
try { try {
final Forge forge = (Forge) Gdx.app.getApplicationListener(); final Forge forge = (Forge) Gdx.app.getApplicationListener();
if (forge != null) if (forge != null)
@@ -315,7 +350,8 @@ public class Main extends AndroidApplication {
public String getContents() { public String getContents() {
if (cm.getPrimaryClip().getItemCount() > 0) { if (cm.getPrimaryClip().getItemCount() > 0) {
try { try {
return cm.getPrimaryClip().getItemAt(0).coerceToText(getContext()).toString(); String text = cm.getPrimaryClip().getItemAt(0).coerceToText(getContext()).toString();
return Normalizer.normalize(text, Normalizer.Form.NFD);
} }
catch (Exception ex) { catch (Exception ex) {
ex.printStackTrace(); ex.printStackTrace();