package com.SalmanDev.SharingTools; import android.content.Context; import android.content.Intent; import android.util.Log; import android.net.Uri; import android.webkit.MimeTypeMap; import com.google.appinventor.components.annotations.*; import com.google.appinventor.components.runtime.*; import com.google.appinventor.components.annotations.DesignerComponent; import com.google.appinventor.components.annotations.SimpleObject; import com.google.appinventor.components.annotations.SimpleFunction; import com.google.appinventor.components.annotations.SimpleEvent; import com.google.appinventor.components.annotations.SimpleProperty; import com.google.appinventor.components.common.ComponentCategory; import com.google.appinventor.components.common.PropertyTypeConstants; import com.google.appinventor.components.runtime.AndroidNonvisibleComponent; import com.google.appinventor.components.runtime.AndroidViewComponent; import com.google.appinventor.components.runtime.ComponentContainer; import com.google.appinventor.components.runtime.EventDispatcher; import com.google.appinventor.components.runtime.util.NougatUtil; import com.google.appinventor.components.runtime.util.YailList; import java.util.ArrayList; import java.util.List; import java.io.File; @DesignerComponent(version = 1, description = "Created by Salman Developer", category = ComponentCategory.EXTENSION, nonVisible = true, iconName = "https://img.icons8.com/metro/16/000000/share.png") @SimpleObject(external = true) public class SharingTools extends AndroidNonvisibleComponent { private ComponentContainer container; private Context context; private String dialogText; public SharingTools(ComponentContainer container) { super(container.$form()); this.container = container; context = (Context) container.$context(); } @DesignerProperty(editorType = PropertyTypeConstants.PROPERTY_TYPE_TEXTAREA, defaultValue = "") @SimpleProperty public void DialogText(String text) { dialogText = text; } @SimpleProperty(category = PropertyCategory.APPEARANCE) public String DialogText() { return dialogText; } @SimpleFunction(description = "") public void ShareMessage(String message) { try { Intent sharingIntent = new Intent(Intent.ACTION_SEND); sharingIntent.setType("text/plain"); sharingIntent.putExtra(Intent.EXTRA_TEXT, message); context.startActivity(Intent.createChooser(sharingIntent, dialogText)); } catch (Exception e){ Error(e.getMessage()); } } @SimpleFunction(description = "") public void ShareFile(String message, String filePath) { try { if (!filePath.startsWith("file://")) { filePath = "file://" + filePath; } Uri uri = Uri.parse(filePath); File allFile = new File(uri.getPath()); if (allFile.isFile()) { String fileExtension = filePath.substring(filePath.lastIndexOf(".")+1).toLowerCase(); MimeTypeMap mime = MimeTypeMap.getSingleton(); String type = mime.getMimeTypeFromExtension(fileExtension); if (type == null) { type = "application/octet-stream"; } Uri shareableUri = NougatUtil.getPackageUri(form, allFile); Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND); intent.putExtra(Intent.EXTRA_TEXT, message); intent.setType("text/plain"); intent.putExtra(Intent.EXTRA_STREAM, shareableUri); intent.setType(type); context.startActivity(Intent.createChooser(intent, dialogText)); } } catch (Exception e){ Error(e.getMessage()); } } @SimpleFunction(description = "") public void ShareMultipleFiles(String message, YailList listFilePath) { ArrayList fileUris = new ArrayList(); try { for(Object item : listFilePath.toArray()) { String fileItem = item.toString(); if (!fileItem.startsWith("file://")) { fileItem = "file://" + fileItem; } Uri uri = Uri.parse(fileItem); File allFile = new File(uri.getPath()); if (allFile.isFile()) { Uri shareableUri = NougatUtil.getPackageUri(form, allFile); fileUris.add(shareableUri); } } Intent intent = new Intent(); intent.setAction(Intent.ACTION_SEND_MULTIPLE); intent.putExtra(Intent.EXTRA_TEXT, message); intent.setType("text/plain"); intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, fileUris); intent.setType("*/*"); context.startActivity(Intent.createChooser(intent, dialogText)); } catch (Exception e){ Error(e.getMessage()); } } @SimpleEvent(description = "Event is fired when error happens.") public void Error(String error){ EventDispatcher.dispatchEvent(this, "Error", error); } }