Launch with intent

In Chapter 7, 360-Degree Gallery, we introduced the use of Android intents to associate an app with a specific file type in order to launch our app as a viewer of those files. We'll do the same for OBJ files here.

An intent is a message that any app can send to the Android system that declares its intent to use another app for a certain purpose. The intent object contains a number of members to describe what type of action needs to be done, and, if any, the data on which it needs to be done. For the image gallery, we associated the intent filter with an image mime type. For this project, we'll associate an intent filter with a filename extension.

In your AndroidManifest.xml file, add an intent filter to the activity block. This lets Android know that the app can be used as an OBJ file viewer. We need to specify it as a file scheme and the filename pattern. The wildcard mime type and host are also required by Android. Add the following XML code:

            <intent-filter>
                <action android:name="android.intent.action.VIEW" />
                <category android:name="android.intent.category.DEFAULT" />
                <category android:name="android.intent.category.BROWSABLE" />
                <data android:scheme="file" />
                <data android:mimeType="*/*" />
                <data android:pathPattern=".*\.obj" />
                <data android:host="*" />
            </intent-filter>

To handle the situation, we'll add a new constructor to ModelObject that takes a URI string instead of a resource ID, as we did earlier. Like the other constructor, we need to open an input stream and pass it to parseObj. Here's the constructor, including the worker thread:

    public ModelObject(final String uri) {
        super();
        extentsMin = new Vector3(Float.MAX_VALUE, Float.MAX_VALUE, Float.MAX_VALUE);
        extentsMax = new Vector3(Float.MIN_VALUE, Float.MIN_VALUE, Float.MIN_VALUE);
        SolidColorLightingMaterial.setupProgram();
        enabled = false;
        new Thread(new Runnable() {
            @Override
            public void run() {
                File file = new File(uri.toString());
                FileInputStream fileInputStream;
                try {
                    fileInputStream = new FileInputStream(file);
                } catch (IOException e) {
                    e.printStackTrace();
                    return; // error
                }
                parseObj(fileInputStream);
                createMaterial();
                enabled = true;
                float scalar = normalScalar();
                transform.setLocalScale(scalar, scalar, scalar);
            }
        }).start();
    }

Now in the MainActivity class's setup, we'll check whether the app is launched from an intent and use the intent URI. Otherwise, we'll view the default model, as we did earlier:

    public void setup() {
        ModelObject modelObj;
        Uri intentUri = getIntent().getData();
        if (intentUri != null) {
            Log.d(TAG, "!!!! intent " + intentUri.getPath());
            modelObj = new ModelObject(intentUri.getPath());
        } else {
            // default object
            modelObj = new ModelObject(R.raw.teapot);
        }
        //...        

Now with the project built and installed on the phone, let's try some web integration. Open the web browser and visit a 3D model download site.

Find the Download link for the interesting model to download it into the phone, and then when prompted, use the ModelViewer app to view it!

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset