ee7aed022906797b6d2b2a04a4107a5dfe8724fd
[rocksndiamonds.git] / rocksndiamonds.java
1
2 package org.artsoft.rocksndiamonds;
3
4 import org.libsdl.app.SDLActivity;
5
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;
13
14 public class rocksndiamonds extends SDLActivity {
15     private static final String TAG = "RND";
16     private String[] args;
17
18     @Override
19     protected String[] getArguments() {
20         return args;
21     }
22
23     @Override
24     protected void onCreate(Bundle savedInstanceState) {
25         Log.d(TAG, "onCreate");
26
27         // init program arguments
28         args = new String[0];
29
30         // prevent SDL from sending "drop file" event on app start; use program arguments instead
31         Intent intent = getIntent();
32         handleIntent(intent, true);
33         intent.setData(null);
34
35         super.onCreate(savedInstanceState);
36     }
37
38     @Override
39     protected void onNewIntent(Intent intent) {
40         Log.d(TAG, "onNewIntent");
41
42         // handle file opened with "open with" when app is already running
43         handleIntent(intent, false);
44     }
45
46     private void handleIntent(Intent intent, boolean onCreate) {
47         Log.d(TAG, "handleIntent");
48
49         Uri uri = intent.getData();
50         if (uri == null) {
51             Log.d(TAG, "handleIntent: uri == null");
52             return;
53         }
54
55         if (onCreate) {
56             Log.d(TAG, "handleIntent: starting app with file as argument");
57
58             // app not running yet -- starting app with "--drop-file" argument
59             setProgramArgs(uri);
60         } else {
61             Log.d(TAG, "handleIntent: sending drop event to running app");
62
63             // app already running -- sending file as a "drop file" event
64             sendUriAsDroppedFile(uri);
65         }
66     }
67
68     public void sendUriAsDroppedFile(Uri uri) {
69         SDLActivity.onNativeDropFile(getFileDescriptorStringFromUri(uri));
70     }
71
72     private int getFileDescriptorFromUri(Uri uri) {
73         int fd = -1;
74
75         try {
76             ParcelFileDescriptor pfd = getContentResolver().openFileDescriptor(uri, "r");
77             if (pfd == null) {
78                 throw new RuntimeException("pfd is null");
79             }
80
81             fd = pfd.dup().detachFd();
82             pfd.close();
83         } catch (Exception e) {
84             Log.e(TAG, "Failed to convert URI " + uri.toString() + " to file descriptor", e);
85         }
86
87         return fd;
88     }
89
90     private String getFileDescriptorStringFromUri(Uri uri) {
91         return "fd:" + getFileDescriptorFromUri(uri);
92     }
93
94     private void setProgramArgs(Uri uri) {
95         Log.d(TAG, "setProgramArgs");
96
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)));
104
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 };
117             }
118         }
119     }
120 }