2 package org.artsoft.rocksndiamonds;
4 import org.libsdl.app.SDLActivity;
6 import android.content.Intent;
7 import android.database.Cursor;
8 import android.net.Uri;
9 import android.os.Bundle;
10 import android.os.ParcelFileDescriptor;
11 import android.provider.OpenableColumns;
12 import android.util.Log;
14 public class rocksndiamonds extends SDLActivity {
15 private static final String TAG = "RND";
16 private String[] args;
19 protected String[] getArguments() {
24 protected void onCreate(Bundle savedInstanceState) {
25 Log.d(TAG, "onCreate");
27 // init program arguments
30 // prevent SDL from sending "drop file" event on app start; use program arguments instead
31 Intent intent = getIntent();
32 handleIntent(intent, true);
35 super.onCreate(savedInstanceState);
39 protected void onNewIntent(Intent intent) {
40 Log.d(TAG, "onNewIntent");
42 // handle file opened with "open with" when app is already running
43 handleIntent(intent, false);
46 private void handleIntent(Intent intent, boolean onCreate) {
47 Log.d(TAG, "handleIntent");
49 Uri uri = intent.getData();
51 Log.d(TAG, "handleIntent: uri == null");
56 Log.d(TAG, "handleIntent: starting app with file as argument");
58 // app not running yet -- starting app with "--drop-file" argument
61 Log.d(TAG, "handleIntent: sending drop event to running app");
63 // app already running -- sending file as a "drop file" event
64 sendUriAsDroppedFile(uri);
68 public void sendUriAsDroppedFile(Uri uri) {
69 SDLActivity.onNativeDropFile(getFileDescriptorStringFromUri(uri));
72 private int getFileDescriptorFromUri(Uri uri) {
76 ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "r");
78 throw new RuntimeException("pfd is null");
81 fd = pfd.dup().detachFd();
83 } catch (Exception e) {
84 Log.e(TAG, "Failed to convert URI " + uri.toString() + " to file descriptor", e);
90 private String getFileDescriptorStringFromUri(Uri uri) {
91 return "fd:" + getFileDescriptorFromUri(uri);
94 private void setProgramArgs(Uri uri) {
95 Log.d(TAG, "setProgramArgs");
97 // log some file details
98 Cursor returnCursor = getContentResolver().query(uri, null, null, null, null);
99 int nameIndex = returnCursor.getColumnIndex(OpenableColumns.DISPLAY_NAME);
100 int sizeIndex = returnCursor.getColumnIndex(OpenableColumns.SIZE);
101 returnCursor.moveToFirst();
102 Log.e(TAG, "setProgramArgs: file name: " + returnCursor.getString(nameIndex));
103 Log.e(TAG, "setProgramArgs: file size: " + Long.toString(returnCursor.getLong(sizeIndex)));
105 String scheme = uri.getScheme();
106 if (scheme != null) {
107 if (scheme.equals("content")) {
108 // convert URI to file descriptor
109 String fd = getFileDescriptorStringFromUri(uri);
110 Log.e(TAG, "setProgramArgs: setting argument to file descriptor: " + fd);
111 args = new String[]{ "--drop-file", fd };
112 } else if (scheme.equals("file")) {
113 // directly use regular file
114 String path = uri.getPath();
115 Log.e(TAG, "setProgramArgs: setting argument to file path: " + path);
116 args = new String[]{ "--drop-file", path };