AI2 Component  (Version nb184)
Sharing.java
Go to the documentation of this file.
1 // -*- mode: java; c-basic-offset: 2; -*-
2 // Copyright 2009-2011 Google, All Rights reserved
3 // Copyright 2011-2018 MIT, All rights reserved
4 // Released under the Apache License, Version 2.0
5 // http://www.apache.org/licenses/LICENSE-2.0
6 
7 package com.google.appinventor.components.runtime;
8 
9 import android.content.Intent;
10 
11 import android.net.Uri;
12 
13 import android.webkit.MimeTypeMap;
14 
19 
22 
25 
26 import java.io.File;
27 
46 @DesignerComponent(version = YaVersion.SHARING_COMPONENT_VERSION,
47  description ="Sharing is a non-visible component that enables sharing files and/or " +
48  "messages between your app and other apps installed on a device. The component " +
49  "will display a list of the installed apps that can handle the information provided, " +
50  "and will allow the user to choose one to share the content with, for instance a " +
51  "mail app, a social network app, a texting app, and so on.<br>" +
52  "The file path can be taken directly from other components such as the Camera or the " +
53  "ImagePicker, but can also be specified directly to read from storage. Be aware that " +
54  "different devices treat storage differently, so a few things to try if, " +
55  "for instance, you have a file called arrow.gif in the folder " +
56  "<code>Appinventor/assets</code>, would be: <ul>" +
57  "<li><code>\"file:///sdcard/Appinventor/assets/arrow.gif\"</code></li> or " +
58  "<li><code>\"/storage/Appinventor/assets/arrow.gif\"</code></li></ul>",
59  category = ComponentCategory.SOCIAL,
60  nonVisible = true, iconName = "images/sharing.png")
61 @SimpleObject
62 @UsesPermissions(permissionNames = "android.permission.READ_EXTERNAL_STORAGE")
63 public class Sharing extends AndroidNonvisibleComponent {
64 
65  public Sharing(ComponentContainer container) {
66  super(container.$form());
67  }
68 
73  @SimpleFunction(description = "Shares a message through any capable " +
74  "application installed on the phone by displaying a list of the available apps and " +
75  "allowing the user to choose one from the list. The selected app will open with the " +
76  "message inserted on it.")
77  public void ShareMessage(String message) {
78  Intent shareIntent = new Intent(Intent.ACTION_SEND);
79  shareIntent.putExtra(Intent.EXTRA_TEXT, message);
80  shareIntent.setType("text/plain");
81 
82  // We cannot use Intent.createChooser(shareIntent, "Send using...") because it creates an
83  // oversized pop up sharing window.
84  this.form.startActivity(shareIntent);
85  }
86 
91  @SimpleFunction(description = "Shares a file through any capable application "
92  + "installed on the phone by displaying a list of the available apps and allowing the " +
93  "user to choose one from the list. The selected app will open with the file inserted on it.")
94  public void ShareFile(String file) {
95  ShareFileWithMessage(file, "");
96  }
97 
102  @SimpleFunction(description = "Shares both a file and a message through any capable application "
103  + "installed on the phone by displaying a list of available apps and allowing the user to " +
104  " choose one from the list. The selected app will open with the file and message inserted on it.")
105  public void ShareFileWithMessage(String file, String message) {
106  if (!file.startsWith("file://"))
107  file = "file://" + file;
108 
109  Uri uri = Uri.parse(file);
110  File imageFile = new File(uri.getPath());
111  if (imageFile.isFile()) {
112  String fileExtension = file.substring(file.lastIndexOf(".")+1).toLowerCase();
113  MimeTypeMap mime = MimeTypeMap.getSingleton();
114  String type = mime.getMimeTypeFromExtension(fileExtension);
115  if (type == null) {
116  // Fix for #1701: We don't know what it is, but it's at least a sequence of bytes (we hope)
117  type = "application/octet-stream";
118  }
119 
120  Uri shareableUri = NougatUtil.getPackageUri(form, imageFile);
121  Intent shareIntent = new Intent(Intent.ACTION_SEND);
122  shareIntent.putExtra(Intent.EXTRA_STREAM, shareableUri);
123  shareIntent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
124  shareIntent.setType(type);
125  if (message.length() > 0) {
126  shareIntent.putExtra(Intent.EXTRA_TEXT, message);
127  }
128  // We cannot use Intent.createChooser(shareIntent, "Send using...") because it creates an
129  // oversized pop up sharing window.
130  this.form.startActivity(shareIntent);
131  }
132  else {
133  String eventName = "ShareFile";
134  if (message.equals(""))
135  eventName = "ShareFileWithMessage";
136  form.dispatchErrorOccurredEvent(Sharing.this, eventName,
138  }
139  }
140 }
com.google.appinventor.components.annotations.SimpleFunction
Definition: SimpleFunction.java:23
com.google.appinventor.components.runtime.util.ErrorMessages
Definition: ErrorMessages.java:17
com.google.appinventor.components.runtime.util
-*- mode: java; c-basic-offset: 2; -*-
Definition: AccountChooser.java:7
com.google.appinventor.components.common.YaVersion
Definition: YaVersion.java:14
com.google.appinventor.components.runtime.util.ErrorMessages.ERROR_FILE_NOT_FOUND_FOR_SHARING
static final int ERROR_FILE_NOT_FOUND_FOR_SHARING
Definition: ErrorMessages.java:173
com.google.appinventor.components
com.google.appinventor.components.annotations.DesignerComponent
Definition: DesignerComponent.java:22
com.google.appinventor.components.runtime.util.NougatUtil.getPackageUri
static Uri getPackageUri(Form form, File apk)
Definition: NougatUtil.java:23
com.google.appinventor.components.runtime.File
Definition: File.java:53
com.google.appinventor.components.annotations.UsesPermissions
Definition: UsesPermissions.java:21
com.google.appinventor.components.runtime.Sharing
Definition: Sharing.java:63
com.google.appinventor.components.runtime.AndroidNonvisibleComponent
Definition: AndroidNonvisibleComponent.java:17
com.google.appinventor.components.runtime.Sharing.Sharing
Sharing(ComponentContainer container)
Definition: Sharing.java:65
com.google.appinventor.components.runtime.ComponentContainer
Definition: ComponentContainer.java:16
com.google.appinventor.components.runtime
Copyright 2009-2011 Google, All Rights reserved.
Definition: AccelerometerSensor.java:8
com.google.appinventor.components.common
Definition: ComponentCategory.java:7
com.google.appinventor.components.common.ComponentCategory
Definition: ComponentCategory.java:48
com.google.appinventor.components.runtime.util.NougatUtil
Definition: NougatUtil.java:16
com.google.appinventor.components.annotations.SimpleObject
Definition: SimpleObject.java:23
com.google
com
com.google.appinventor.components.runtime.ComponentContainer.$form
Form $form()
com.google.appinventor.components.annotations
com.google.appinventor