Skip to content

Commit 224023e

Browse files
victorssilvajisqyv
authored andcommitted
Implements Sharing component
Work started by cfromknecht@d890e10 Change-Id: I117c697e0de5a924ea937630eecc00a133adaf44
1 parent 8438f65 commit 224023e

6 files changed

Lines changed: 129 additions & 0 deletions

File tree

‎appinventor/appengine/src/com/google/appinventor/client/Images.java‎

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -434,4 +434,10 @@ public interface Images extends Resources {
434434
*/
435435
@Source("com/google/appinventor/images/procedures.png")
436436
ImageResource procedures();
437+
438+
/**
439+
* Designer palette item: Sharing Component
440+
*/
441+
@Source("com/google/appinventor/images/sharing.png")
442+
ImageResource sharingComponent();
437443
}

‎appinventor/appengine/src/com/google/appinventor/client/editor/simple/palette/SimpleComponentDescriptor.java‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -109,6 +109,7 @@ private static void initBundledImages() {
109109
bundledImages.put("images/twitter.png", images.twitterComponent());
110110
bundledImages.put("images/voting.png", images.voting());
111111
bundledImages.put("images/web.png", images.web());
112+
bundledImages.put("images/sharing.png", images.sharingComponent());
112113
imagesInitialized = true;
113114
}
114115

3.56 KB
Loading

‎appinventor/components/src/com/google/appinventor/components/common/YaVersion.java‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,7 @@ private YaVersion() {
243243
// - CAMERA_COMPONENT_VERSION was incremented to 2.
244244
// For YOUNG_ANDROID_VERSION 86:
245245
// - VIDEOPLAYER_COMPONENT_VERSION was incremented to 5.
246+
// - The Sharing Component was added
246247

247248
public static final int YOUNG_ANDROID_VERSION = 86;
248249

@@ -569,6 +570,8 @@ private YaVersion() {
569570

570571
public static final int PLAYER_COMPONENT_VERSION = 5;
571572

573+
public static final int SHARING_COMPONENT_VERSION = 1;
574+
572575
// For SOUND_COMPONENT_VERSION 2:
573576
// - The Sound.SoundError event was added.
574577
// For SOUND_COMPONENT_VERSION 3:
Lines changed: 95 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,95 @@
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+
}

‎appinventor/docs/reference/components/social.html‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -458,6 +458,11 @@ <h2>Table of Contents</h2>
458458
PhoneNumberPicker
459459
</a>
460460
</li>
461+
<li>
462+
<a href="#Sharing">
463+
Sharing
464+
</a>
465+
</li>
461466
<li>
462467
<a href="#Texting">
463468
Texting
@@ -747,6 +752,25 @@ <h3>Methods</h3>
747752
<dd>Opens the picker, as though the user clicked on it.</dd>
748753
</dl>
749754

755+
<h2 id="Sharing">Sharing</h2>
756+
757+
<p>Sharing is a non-visible component that enables sharing files and/or messages between your app and other apps installed on a device. The component will display a list of the installed apps that can handle the information provided, and will allow the user to choose one to share the content with, for instance a mail app, a social network app, a texting app, and so on.</p>
758+
759+
<h3>Properties</h3>
760+
none
761+
762+
<h3>Events</h3>
763+
none
764+
765+
<h3>Methods</h3>
766+
<dl>
767+
<dt><code>ShareFile(text file)</code></dt>
768+
<dd>Shares a file through any capable application installed on the phone by displaying a list of the available apps and allowing the user to choose one from the list. The selected app will open with the file inserted on it.</dd>
769+
<dt><code>ShareFileWithMessage(text file, text message)</code></dt>
770+
<dd>Shares both a file and a message through any capable application installed on the phone by displaying a list of available apps and allowing the user to choose one from the list. The selected app will open with the file and message inserted on it.</dd>
771+
<dt><code>ShareMessage(text message)</code></dt>
772+
<dd>Shares a message through any capable application installed on the phone by displaying a list of the available apps and allowing the user to choose one from the list. The selected app will open with the message inserted on it.</dd>
773+
</dl>
750774

751775
<h2 id="Texting">
752776
Texting

0 commit comments

Comments
 (0)