fix share text/data to Forge app

- add additional launcher activity to prevent GLES Context loss (when sharing data via intent)
- fix passing data from another app to Forge via share text / share file / share deck
This commit is contained in:
Anthony Calosa
2023-06-25 13:54:32 +08:00
parent 8468f5a21e
commit 198180eec5
4 changed files with 101 additions and 34 deletions

View File

@@ -1,10 +0,0 @@
package forge.app;
import android.app.Activity;
import android.os.Bundle;
public class Exiter extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
}
}

View File

@@ -0,0 +1,91 @@
package forge.app;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import forge.gui.GuiBase;
import java.io.BufferedReader;
import java.io.InputStream;
import java.io.InputStreamReader;
public class Launcher extends Activity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
Intent main = new Intent(Launcher.this, Main.class);
main.setFlags(Intent.FLAG_ACTIVITY_REORDER_TO_FRONT);
startActivity(main);
Intent intent = getIntent();
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
final Handler handler = new Handler();
handler.postDelayed(() -> {
if ("text/plain".equals(type)) {
Uri textUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (textUri != null) {
try {
InputStream in = getContentResolver().openInputStream(textUri);
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
for (String line; (line = r.readLine()) != null; ) {
total.append(line).append('\n');
}
GuiBase.getInterface().copyToClipboard(total.toString());
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
GuiBase.getInterface().copyToClipboard(intent.getStringExtra(Intent.EXTRA_TEXT));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}, 1500);
}
finish();
}
@Override
protected void onNewIntent(Intent intent) {
super.onNewIntent(intent);
setIntent(intent);
String action = intent.getAction();
String type = intent.getType();
if (Intent.ACTION_SEND.equals(action) && type != null) {
final Handler handler = new Handler();
handler.postDelayed(() -> {
if ("text/plain".equals(type)) {
Uri textUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (textUri != null) {
try {
InputStream in = getContentResolver().openInputStream(textUri);
BufferedReader r = new BufferedReader(new InputStreamReader(in));
StringBuilder total = new StringBuilder();
for (String line; (line = r.readLine()) != null; ) {
total.append(line).append('\n');
}
GuiBase.getInterface().copyToClipboard(total.toString());
} catch (Exception e) {
e.printStackTrace();
}
} else {
try {
GuiBase.getInterface().copyToClipboard(intent.getStringExtra(Intent.EXTRA_TEXT));
} catch (Exception e) {
e.printStackTrace();
}
}
}
}, 1500);
}
}
}

View File

@@ -71,33 +71,12 @@ public class Main extends AndroidApplication {
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
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
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;