|
| 1 | +package de.skycoder42.androidutils; |
| 2 | + |
| 3 | +import java.util.Map; |
| 4 | +import android.util.Log; |
| 5 | +import android.content.Context; |
| 6 | +import android.content.Intent; |
| 7 | +import android.app.Activity; |
| 8 | +import org.qtproject.qt5.android.QtNative; |
| 9 | +import androidnative.SystemDispatcher; |
| 10 | + |
| 11 | +public class FileChooser { |
| 12 | + public static final int GET_CONTENT_ACTION = 0x1091c657; |
| 13 | + public static final int OPEN_DOCUMENT_ACTION = 0x83895290; |
| 14 | + public static final int CREATE_DOCUMENT_ACTION = 0x7e97d25e; |
| 15 | + |
| 16 | + public static final String GET_CONTENT_MESSAGE = "AndroidUtils.FileChooser.getContent"; |
| 17 | + public static final String OPEN_DOCUMENT_MESSAGE = "AndroidUtils.FileChooser.openDocument"; |
| 18 | + public static final String CREATE_DOCUMENT_MESSAGE = "AndroidUtils.FileChooser.createDocument"; |
| 19 | + |
| 20 | + static { |
| 21 | + SystemDispatcher.addListener(new SystemDispatcher.Listener() { |
| 22 | + public void onDispatched(String type , Map message) { |
| 23 | + if (type.equals(GET_CONTENT_MESSAGE)) { |
| 24 | + getContent((String)message.get("title"), |
| 25 | + (String)message.get("mime"), |
| 26 | + (Boolean)message.get("openable"), |
| 27 | + (Boolean)message.get("localOnly"), |
| 28 | + (Boolean)message.get("allowMultiple")); |
| 29 | + } else if (type.equals(OPEN_DOCUMENT_MESSAGE)) { |
| 30 | + openDocument((String)message.get("title"), |
| 31 | + (String)message.get("mime"), |
| 32 | + (String)message.get("url"), |
| 33 | + (Boolean)message.get("openable"), |
| 34 | + (Boolean)message.get("allowMultiple")); |
| 35 | + } else if (type.equals(CREATE_DOCUMENT_MESSAGE)) { |
| 36 | + createDocument((String)message.get("title"), |
| 37 | + (String)message.get("mime"), |
| 38 | + (String)message.get("url"), |
| 39 | + (String)message.get("name"), |
| 40 | + (Boolean)message.get("openable")); |
| 41 | + } else if (type.equals(SystemDispatcher.ACTIVITY_RESULT_MESSAGE)) { |
| 42 | + onActivityResult(message); |
| 43 | + } |
| 44 | + } |
| 45 | + }); |
| 46 | + } |
| 47 | + |
| 48 | + static private void getContent(final String title, String mime, boolean openable, boolean localOnly, boolean allowMultiple) { |
| 49 | + final Activity activity = QtNative.activity(); |
| 50 | + |
| 51 | + final Intent intent = new Intent(Intent.ACTION_GET_CONTENT); |
| 52 | + intent.setType(mime); |
| 53 | + if(openable) |
| 54 | + intent.addCategory(Intent.CATEGORY_OPENABLE); |
| 55 | + if(localOnly) |
| 56 | + intent.putExtra(Intent.EXTRA_LOCAL_ONLY, true); |
| 57 | + if(allowMultiple) |
| 58 | + intent.putExtra(Intent.EXTRA_ALLOW_MULTIPLE, true); |
| 59 | + |
| 60 | + Runnable runnable = new Runnable () { |
| 61 | + public void run() { |
| 62 | + activity.startActivityForResult( |
| 63 | + Intent.createChooser(intent, title), |
| 64 | + GET_CONTENT_ACTION); |
| 65 | + }; |
| 66 | + }; |
| 67 | + activity.runOnUiThread(runnable); |
| 68 | + } |
| 69 | + |
| 70 | + static private void openDocument(final String title, String mime, String url, boolean openable, boolean allowMultiple) { |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + static private void createDocument(final String title, String mime, String url, String name, boolean openable) { |
| 75 | + |
| 76 | + } |
| 77 | + |
| 78 | + static private void onActivityResult(Map message) { |
| 79 | + Log.d("AndroidUtils", message.toString()); |
| 80 | + } |
| 81 | +} |
0 commit comments