|
| 1 | +package com.google.appinventor.components.runtime; |
| 2 | + |
| 3 | +import android.app.Activity; |
| 4 | +import android.content.Context; |
| 5 | +import android.content.Intent; |
| 6 | +import android.net.Uri; |
| 7 | +import android.webkit.MimeTypeMap; |
| 8 | +import android.widget.Toast; |
| 9 | +import com.google.appinventor.components.annotations.DesignerComponent; |
| 10 | +import com.google.appinventor.components.annotations.SimpleFunction; |
| 11 | +import com.google.appinventor.components.common.ComponentCategory; |
| 12 | +import com.google.appinventor.components.common.YaVersion; |
| 13 | + |
| 14 | +import java.io.File; |
| 15 | + |
| 16 | + |
| 17 | +/** |
| 18 | + * Component for sharing files and/or messages through Android's built-in sharing |
| 19 | + * functionality. |
| 20 | + * |
| 21 | + * @author victsou@gmail.com (Victor Silva) - Picked up on @cfromknecht's work |
| 22 | + * and fixed file support. |
| 23 | + */ |
| 24 | +@DesignerComponent(version = YaVersion.SHARING_COMPONENT_VERSION, |
| 25 | + description ="Sharing is a non-visible component that enables sharing files and/or " + |
| 26 | + "messages between your app and other apps installed on a device. The component " + |
| 27 | + "will display a list of the installed apps that can handle the information provided, " + |
| 28 | + " and will allow the user to choose one to share the content with, for instance a " + |
| 29 | + "mail app, a social network app, a texting app, and so on.", |
| 30 | + category = ComponentCategory.SOCIAL, |
| 31 | + nonVisible = true, iconName = "images/sharing.png") |
| 32 | +public class Sharing extends AndroidNonvisibleComponent { |
| 33 | + |
| 34 | + public Sharing(ComponentContainer container) { |
| 35 | + super(container.$form()); |
| 36 | + } |
| 37 | + |
| 38 | + /** |
| 39 | + * Shares a message using Android' built-in sharing. |
| 40 | + */ |
| 41 | + @SimpleFunction(description = "Shares a message through any capable " + |
| 42 | + "application installed on the phone by displaying a list of the available apps and " + |
| 43 | + "allowing the user to choose one from the list. The selected app will open with the " + |
| 44 | + "message inserted on it.") |
| 45 | + public void ShareMessage(String message) { |
| 46 | + Intent shareIntent = new Intent(Intent.ACTION_SEND); |
| 47 | + shareIntent.putExtra(Intent.EXTRA_TEXT, message); |
| 48 | + shareIntent.setType("text/plain"); |
| 49 | + |
| 50 | + Context cont = this.form.$context(); |
| 51 | + cont.startActivity(Intent.createChooser(shareIntent, "Send using...")); |
| 52 | + } |
| 53 | + |
| 54 | + /** |
| 55 | + * Shares a file using Android' built-in sharing. |
| 56 | + */ |
| 57 | + @SimpleFunction(description = "Shares a file through any capable application " |
| 58 | + + "installed on the phone by displaying a list of the available apps and allowing the " + |
| 59 | + "user to choose one from the list. The selected app will open with the file inserted on it.") |
| 60 | + public void ShareFile(String file) { |
| 61 | + ShareFileWithMessage(file, ""); |
| 62 | + } |
| 63 | + |
| 64 | + /** |
| 65 | + * Shares a file along with a message using Android' built-in sharing. |
| 66 | + */ |
| 67 | + @SimpleFunction(description = "Shares both a file and a message through any capable application " |
| 68 | + + "installed on the phone by displaying a list of available apps and allowing the user to " + |
| 69 | + " choose one from the list. The selected app will open with the file and message inserted on it.") |
| 70 | + public void ShareFileWithMessage(String file, String message) { |
| 71 | + Activity act = (Activity) this.form.$context(); |
| 72 | + |
| 73 | + Uri uri = Uri.parse(file); |
| 74 | + File f = new File(uri.getPath()); |
| 75 | + |
| 76 | + if (f.exists()) { |
| 77 | + String ext = file.substring(file.lastIndexOf(".")+1).toLowerCase(); |
| 78 | + MimeTypeMap mime = MimeTypeMap.getSingleton(); |
| 79 | + String type = mime.getMimeTypeFromExtension(ext); |
| 80 | + |
| 81 | + Intent shareIntent = new Intent(Intent.ACTION_SEND); |
| 82 | + shareIntent.putExtra(Intent.EXTRA_STREAM, uri); |
| 83 | + shareIntent.setType(type); |
| 84 | + |
| 85 | + if (message.length() > 0) { |
| 86 | + shareIntent.putExtra(Intent.EXTRA_TEXT, message); |
| 87 | + } |
| 88 | + |
| 89 | + act.startActivity(shareIntent); |
| 90 | + } |
| 91 | + else { |
| 92 | + Toast.makeText(act, file + " not found", Toast.LENGTH_SHORT).show(); |
| 93 | + } |
| 94 | + } |
| 95 | +} |
0 commit comments