Skip to content

Commit 6a23ce9

Browse files
authored
[Android standalone] fix association of .tic and .png to TIC-80 app (#2837)
* [Android standalone] fix association of .tic and .png to TIC-80 app Solves #2811 * [Android standalone] Fix loading directly from files The android app was not configured to open directly form apps. This now should work. Code written by Gemini. * [Android standalone] .tic file association case insensitive
1 parent bae9661 commit 6a23ce9

File tree

2 files changed

+100
-5
lines changed

2 files changed

+100
-5
lines changed

build/android/app/src/main/AndroidManifest.xml

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -89,11 +89,31 @@
8989
</intent-filter>
9090

9191
<!-- Drop file event -->
92+
<!-- Handle .tic files -->
9293
<intent-filter>
9394
<action android:name="android.intent.action.VIEW" />
9495
<category android:name="android.intent.category.DEFAULT" />
96+
<data android:scheme="content" />
97+
<data android:scheme="file" />
98+
<data android:host="*" />
99+
<!-- case insensitive options is only available from Android 12, so we have to list all -->
95100
<data android:mimeType="*/*" android:pathPattern=".*\\.tic" />
96-
<data android:mimeType="*/*" android:pathPattern=".*\\.png" />
101+
<data android:mimeType="*/*" android:pathPattern=".*\\.Tic" />
102+
<data android:mimeType="*/*" android:pathPattern=".*\\.TIc" />
103+
<data android:mimeType="*/*" android:pathPattern=".*\\.TIC" />
104+
<data android:mimeType="*/*" android:pathPattern=".*\\.tIc" />
105+
<data android:mimeType="*/*" android:pathPattern=".*\\.tIC" />
106+
<data android:mimeType="*/*" android:pathPattern=".*\\.tiC" />
107+
<data android:mimeType="*/*" android:pathPattern=".*\\.TiC" />
108+
</intent-filter>
109+
110+
<!-- Handle .png files -->
111+
<intent-filter>
112+
<action android:name="android.intent.action.VIEW" />
113+
<category android:name="android.intent.category.DEFAULT" />
114+
<data android:scheme="content" />
115+
<data android:scheme="file" />
116+
<data android:mimeType="image/png" />
97117
</intent-filter>
98118

99119
</activity>
Lines changed: 79 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,86 @@
11
package com.nesbox.tic;
22

3+
import android.content.Intent;
4+
import android.database.Cursor;
5+
import android.net.Uri;
6+
import android.os.Bundle;
7+
import android.provider.OpenableColumns;
8+
import android.util.Log;
9+
import java.io.File;
10+
import java.io.FileOutputStream;
11+
import java.io.InputStream;
12+
313
import org.libsdl.app.SDLActivity;
414

515
public class TIC extends SDLActivity
616
{
7-
@Override
8-
protected String[] getLibraries() {
9-
return new String[] { "tic80" };
10-
}
17+
private static String sGameToLaunch = null;
18+
19+
@Override
20+
protected void onCreate(Bundle savedInstanceState) {
21+
// Check for a file intent before starting the SDLActivity's native code.
22+
Intent intent = getIntent();
23+
Uri uri = intent.getData();
24+
25+
if (uri != null && Intent.ACTION_VIEW.equals(intent.getAction())) {
26+
sGameToLaunch = copyUriToCache(uri);
27+
}
28+
29+
super.onCreate(savedInstanceState);
30+
}
31+
32+
private String copyUriToCache(Uri uri) {
33+
String fileName = "cart.tic"; // A fallback filename.
34+
// Get the original filename from the URI's metadata.
35+
try (Cursor cursor = getContentResolver().query(uri, new String[]{OpenableColumns.DISPLAY_NAME}, null, null, null)) {
36+
if (cursor != null && cursor.moveToFirst()) {
37+
int nameIndex = cursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
38+
if (nameIndex > -1) {
39+
fileName = cursor.getString(nameIndex);
40+
}
41+
}
42+
}
43+
44+
try (InputStream inputStream = getContentResolver().openInputStream(uri)) {
45+
if (inputStream == null) {
46+
Log.e("TIC", "Could not open input stream for URI: " + uri);
47+
return null;
48+
}
49+
50+
// Create a file in the app's cache directory using the original filename.
51+
File tempFile = new File(getCacheDir(), fileName);
52+
53+
// Copy the content from the URI to the temporary file.
54+
try (FileOutputStream outputStream = new FileOutputStream(tempFile)) {
55+
byte[] buffer = new byte[4096];
56+
int bytesRead;
57+
while ((bytesRead = inputStream.read(buffer)) != -1) {
58+
outputStream.write(buffer, 0, bytesRead);
59+
}
60+
}
61+
62+
String filePath = tempFile.getAbsolutePath();
63+
Log.i("TIC", "Copied content URI to cache file: " + filePath);
64+
return filePath;
65+
66+
} catch (Exception e) {
67+
Log.e("TIC", "Error copying URI to cache", e);
68+
return null;
69+
}
70+
}
71+
72+
@Override
73+
protected String[] getArguments() {
74+
if (sGameToLaunch != null) {
75+
String[] args = { sGameToLaunch };
76+
sGameToLaunch = null; // Clear it after use so it's not reused.
77+
return args;
78+
}
79+
return new String[0];
80+
}
81+
82+
@Override
83+
protected String[] getLibraries() {
84+
return new String[] { "tic80" };
85+
}
1186
}

0 commit comments

Comments
 (0)