-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathShareIntent.java
115 lines (101 loc) · 4.62 KB
/
ShareIntent.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
package cl.json.social;
import android.content.ActivityNotFoundException;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Bundle;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.bridge.ReadableMap;
import java.io.UnsupportedEncodingException;
import java.net.URLEncoder;
import cl.json.ShareFile;
/**
* Created by disenodosbbcl on 23-07-16.
*/
public abstract class ShareIntent {
protected final ReactApplicationContext reactContext;
protected Intent intent;
protected String chooserTitle = "Share";
public ShareIntent(ReactApplicationContext reactContext) {
this.reactContext = reactContext;
this.setIntent(new Intent(android.content.Intent.ACTION_VIEW)); // was ACTION_SEND
this.getIntent().setType("application/epub+zip"); // was text/plain
}
public void open(ReadableMap options) throws ActivityNotFoundException {
if (ShareIntent.hasValidKey("subject", options) ) {
this.getIntent().putExtra(Intent.EXTRA_SUBJECT, options.getString("subject"));
}
if (ShareIntent.hasValidKey("message", options) && ShareIntent.hasValidKey("url", options)) {
ShareFile fileShare = getFileShare(options);
if(fileShare.isFile()) {
Uri uriFile = fileShare.getURI();
this.getIntent().setType(fileShare.getType());
this.getIntent().putExtra(Intent.EXTRA_STREAM, uriFile);
this.getIntent().putExtra(Intent.EXTRA_TEXT, options.getString("message"));
this.getIntent().addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
this.getIntent().putExtra(Intent.EXTRA_TEXT, options.getString("message") + " " + options.getString("url"));
}
} else if (ShareIntent.hasValidKey("url", options)) {
ShareFile fileShare = getFileShare(options);
if(fileShare.isFile()) {
Uri uriFile = fileShare.getURI();
this.getIntent().setType(fileShare.getType());
this.getIntent().putExtra(Intent.EXTRA_STREAM, uriFile);
this.getIntent().addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION);
} else {
this.getIntent().putExtra(Intent.EXTRA_TEXT, options.getString("url"));
}
} else if (ShareIntent.hasValidKey("message", options) ) {
this.getIntent().putExtra(Intent.EXTRA_TEXT, options.getString("message"));
}
}
protected ShareFile getFileShare(ReadableMap options) {
if (ShareIntent.hasValidKey("type", options)) {
return new ShareFile(options.getString("url"), options.getString("type"), this.reactContext);
} else {
return new ShareFile(options.getString("url"), this.reactContext);
}
}
protected static String urlEncode(String param) {
try {
return URLEncoder.encode( param , "UTF-8" );
} catch (UnsupportedEncodingException e) {
throw new RuntimeException("URLEncoder.encode() failed for " + param);
}
}
protected void openIntentChooser() throws ActivityNotFoundException {
System.out.println(this.getIntent());
System.out.println(this.getIntent().getExtras());
// Intent chooser = Intent.createChooser(this.getIntent(), this.chooserTitle);
// chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
// this.reactContext.startActivity(chooser);
Intent chooser = new Intent();
chooser.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
chooser.setAction(android.content.Intent.ACTION_VIEW);
chooser.setDataAndType((Uri) this.getIntent().getParcelableExtra("android.intent.extra.STREAM"), "application/epub+zip");
this.reactContext.startActivity(chooser);
}
protected boolean isPackageInstalled(String packagename, Context context) {
PackageManager pm = context.getPackageManager();
try {
pm.getPackageInfo(packagename, PackageManager.GET_ACTIVITIES);
return true;
} catch (PackageManager.NameNotFoundException e) {
return false;
}
}
protected Intent getIntent(){
return this.intent;
}
protected void setIntent(Intent intent) {
this.intent = intent;
}
public static boolean hasValidKey(String key, ReadableMap options) {
return options.hasKey(key) && !options.isNull(key);
}
protected abstract String getPackage();
protected abstract String getDefaultWebLink();
protected abstract String getPlayStoreLink();
}