Example usage for com.badlogic.gdx.backends.android AndroidEventListener AndroidEventListener

List of usage examples for com.badlogic.gdx.backends.android AndroidEventListener AndroidEventListener

Introduction

In this page you can find the example usage for com.badlogic.gdx.backends.android AndroidEventListener AndroidEventListener.

Prototype

AndroidEventListener

Source Link

Usage

From source file:net.spookygames.gdx.nativefilechooser.android.AndroidFileChooser.java

License:Open Source License

@Override
public void chooseFile(NativeFileChooserConfiguration configuration, final NativeFileChooserCallback callback) {

    NativeFileChooserUtils.checkNotNull(configuration, "configuration");
    NativeFileChooserUtils.checkNotNull(callback, "callback");

    // Create target Intent for new Activity
    Intent intent = new Intent();
    intent.setAction(Intent.ACTION_GET_CONTENT);

    // This one will ensure we have access to the
    // MediaStore.MediaColumns.DISPLAY_NAME property
    intent.addCategory(Intent.CATEGORY_OPENABLE);

    // Handle MIME type filter and starting path, if any
    Uri data = null;/*from w  w  w . j  a va  2  s.  c om*/
    String type = null;

    if (configuration.directory != null) {
        try {
            data = Uri.parse(configuration.directory.file().toURI().toURL().toString());
        } catch (MalformedURLException ex) {
            app.error(getClass().getSimpleName(), "Invalid starting directory", ex);
        }
    }

    if (configuration.mimeFilter != null)
        type = normalizeMimeType(configuration.mimeFilter);

    if (data == null) {
        if (type != null) {
            intent.setType(type);
        }
    } else {
        if (type == null) {
            intent.setData(data);
        } else {
            intent.setDataAndType(data, type);
        }
    }

    // Warn if name filter was provided (not supported on this platform)
    if (configuration.nameFilter != null)
        app.debug(getClass().getSimpleName(), "nameFilter property is not supported on Android");

    // Register a listener to get a callback
    // It will deregister by itself on first call
    app.addAndroidEventListener(new AndroidEventListener() {
        @Override
        public void onActivityResult(int requestCode, int resultCode, Intent data) {

            // Don't interfere with other activity results
            if (requestCode != IntentCode)
                return;

            switch (resultCode) {
            case Activity.RESULT_CANCELED:
                // Action got cancelled
                callback.onCancellation();
                break;
            case Activity.RESULT_OK:
                try {
                    FileHandle file = null;

                    // Get the Uri of the selected file
                    Uri uri = data.getData();

                    // Try to build file from it
                    file = fileHandleFromUri(uri);

                    // Call success callback
                    callback.onFileChosen(file);
                } catch (IOException ex) {
                    callback.onError(ex);
                }
                break;
            default:
                break;
            }

            // Self deregistration
            app.removeAndroidEventListener(this);
        }
    });

    try {
        app.startActivityForResult(Intent.createChooser(intent, configuration.title), IntentCode);
    } catch (ActivityNotFoundException ex) {
        callback.onError(ex);
    }

}