package io.keybase.ossifrage.modules;

import android.content.Intent;
import android.net.Uri;
import android.support.v4.content.FileProvider;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReactContextBaseJavaModule;
import com.facebook.react.bridge.ReactMethod;

import java.io.File;

import io.keybase.ossifrage.R;


public class ShareFiles extends ReactContextBaseJavaModule {

    private final ReactApplicationContext reactContext;

    public ShareFiles(ReactApplicationContext reactContext) {
        super(reactContext);
        this.reactContext = reactContext;
    }

    @Override
    public String getName() {
        return "ShareFiles";
    }

    @ReactMethod
    public void share(String uriPath, String mimeType, Promise promise) {
        File file = new File(uriPath);
        Uri fileUri = FileProvider.getUriForFile(reactContext, "io.keybase.ossifrage.fileprovider", file);

        Intent intent = new Intent(Intent.ACTION_SEND)
                .putExtra(Intent.EXTRA_STREAM, fileUri)
                .setType(mimeType);
        Intent chooser = Intent.createChooser(intent, reactContext.getResources().getText(R.string.send_to));
        if (intent.resolveActivity(reactContext.getPackageManager()) != null) {
            reactContext.startActivity(chooser);
            promise.resolve(true);
        } else {
            promise.reject(new Exception("Invalid chooser"));
        }
    }
}

@@ -359,20 +359,144 @@
 java.io.
-File
+BufferedReader;%0Aimport java.io.File;%0Aimport java.io.FileNotFoundException;%0Aimport java.io.FileReader;%0Aimport java.io.IOException
 ;%0A%0Aimpor
@@ -640,16 +640,176 @@
 Context;
+%0A    private static final int MAX_TEXT_FILE_SIZE = 100 * 1024; // 100 kiB%0A    private static final String LINE_SEPARATOR = System.getProperty(%22line.separator%22);
 %0A%0A    pu
@@ -1164,179 +1164,1299 @@
     
-Uri fileUri = FileProvider.getUriForFile(reactContext, %22io.keybase.ossifrage.fileprovider%22, file);%0A%0A        Intent intent = new Intent(Intent.ACTION_SEND)%0A                
+Intent intent = new Intent(Intent.ACTION_SEND).setType(mimeType);%0A        if (mimeType.startsWith(%22text/%22)) %7B%0A            try %7B%0A                BufferedReader br = new BufferedReader(new FileReader(file));%0A                StringBuilder textBuilder = new StringBuilder();%0A                String text;%0A                boolean isFirst = true;%0A                while (textBuilder.length() %3C MAX_TEXT_FILE_SIZE && (text = br.readLine()) != null) %7B%0A                    if (isFirst) %7B%0A                        isFirst = false;%0A                    %7D else %7B%0A                        textBuilder.append(LINE_SEPARATOR);%0A                    %7D%0A                    textBuilder.append(text);%0A                %7D%0A                intent.putExtra(Intent.EXTRA_TEXT, textBuilder.toString());%0A            %7D catch (FileNotFoundException ex) %7B%0A                // Create our own exceptions for the promise so we don't leak anything.%0A                promise.reject(new Exception(%22File not found%22));%0A                return;%0A            %7D catch (IOException ex) %7B%0A                promise.reject(new Exception(%22Error reading the file%22));%0A                return;%0A            %7D%0A        %7D else %7B%0A            Uri fileUri = FileProvider.getUriForFile(reactContext, %22io.keybase.ossifrage.fileprovider%22, file);%0A            intent
 .put
@@ -2490,16 +2490,17 @@
 fileUri)
+;
 %0A       
@@ -2504,35 +2504,9 @@
     
-        .setType(mimeType);
+%7D
 %0A   

Fix text file sharing (#11498)