// bytran -|- spectral calculations for portable devices using the HITRAN database
//
// Developed by Dzianis Pliutau (Denis Plutov) and Kostya Roslyakov
// www.bytran.org | contact@bytran.org
// Copyright (c) 2016 Dzianis Pliutau
// Distributed under the MIT license
//
// Share results methods are based on the tutorial and code Copyrighted (c) 2014 Nicolas Froment
// http://blog.lasconic.com/share-on-ios-and-android-using-qml/
//
// The MIT License (MIT)
// Copyright (c) 2016 Dzianis Pliutau
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy,
// modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software
// is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR
// IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.



// Java code to initiate the share intent under Android
package org.bytran.bytran;

import org.qtproject.qt5.android.QtNative;

import java.lang.String;
import android.content.Intent;
import java.io.File;
import android.net.Uri;
import android.util.Log;
import java.util.ArrayList;
import java.util.List;

public class ShareIntent
{ 
    // Java class to share text results and the image
    // Share results methods are based on the tutorial and code Copyrighted (c) 2014 Nicolas Froment - http://blog.lasconic.com/share-on-ios-and-android-using-qml/
    // Other resources were used as well:
    // http://stackoverflow.com/questions/27094100/how-to-use-qandroidjniobject-to-call-java-code-from-inside-qt
    // https://www.calligra.org/blogs/sharing-with-qt-on-android/
    // http://www.kdab.com/qt-android-episode-5/
    // http://stackoverflow.com/questions/2264622/android-multiple-email-attachment-using-intent-question
    // https://amin-ahmadi.com/2015/12/07/how-to-mix-c-and-java-code-in-qt-for-android/
    public static void share(String imagePath, String textPath, String configPath)
    {
        if (QtNative.activity() == null) { return; }

        Intent share = new Intent();
        if ((imagePath.length() > 0) && (textPath.length() > 0))
        {
            // Creating an action to send multiple items
            share.setAction(Intent.ACTION_SEND_MULTIPLE);
            share.setType("text/plain");

            // Appending files to share in the uri format
            ArrayList<Uri> uris = new ArrayList<Uri>();

            // Appending the image to the Uri
            File fileIn = new File(imagePath);
            fileIn.setReadable(true, false);
            Uri u = Uri.fromFile(fileIn);
            uris.add(u);

            // Appending text to the Uri
            fileIn = new File(textPath);
            fileIn.setReadable(true, false);
            u = Uri.fromFile(fileIn);
            uris.add(u);

            // Appending the configuration file to the Uri
            fileIn = new File(configPath);
            fileIn.setReadable(true, false);
            u = Uri.fromFile(fileIn);
            uris.add(u);

            // Sharing the data
            share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
        }
        else
        {
            // Appending files to share in the uri format
            ArrayList<Uri> uris = new ArrayList<Uri>();

            // Creating a share intent for an image separately
            if (imagePath.length() > 0)
            {
                // Creating an action to send a single item
                share.setAction(Intent.ACTION_SEND);
                share.setType("image/png");
                File fileIn = new File(imagePath);
                fileIn.setReadable(true, false);
                Uri u = Uri.fromFile(fileIn);
                share.putExtra(Intent.EXTRA_STREAM, u);
            }

            // Creating a share intent for a text file separately
            if (textPath.length() > 0)
            {
                // Creating an action to send multiple items
                share.setAction(Intent.ACTION_SEND_MULTIPLE);
                share.setType("text/plain");

                // Appending text to the Uri
                File fileIn = new File(textPath);
                fileIn.setReadable(true, false);
                Uri u = Uri.fromFile(fileIn);
                uris.add(u);

                // Appending the configuration file to the Uri
                fileIn = new File(configPath);
                fileIn.setReadable(true, false);
                u = Uri.fromFile(fileIn);
                uris.add(u);

                // Sharing the data
                share.putParcelableArrayListExtra(Intent.EXTRA_STREAM, uris);
            }
        }

        // Initiating the activity
        QtNative.activity().startActivity(Intent.createChooser(share, "Share bytran results.."));
    }
}
