forked from react-native-share/react-native-share
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
29 changed files
with
1,325 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,115 @@ | ||
package cl.json; | ||
|
||
import android.content.CursorLoader; | ||
import android.content.Intent; | ||
import android.database.Cursor; | ||
import android.media.MediaScannerConnection; | ||
import android.net.Uri; | ||
import android.os.Environment; | ||
import android.provider.MediaStore; | ||
import android.util.Base64; | ||
import android.webkit.MimeTypeMap; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
|
||
import java.io.File; | ||
import java.io.FileOutputStream; | ||
import java.io.IOException; | ||
import java.io.StringReader; | ||
import java.net.URI; | ||
|
||
/** | ||
* Created by disenodosbbcl on 22-07-16. | ||
*/ | ||
public class ShareFile { | ||
|
||
private final ReactApplicationContext reactContext; | ||
private String url; | ||
private Uri uri; | ||
private String type = "*/*"; | ||
private String extension = ""; | ||
|
||
public ShareFile(String url, ReactApplicationContext reactContext){ | ||
this.url = url; | ||
this.uri = Uri.parse(this.url); | ||
this.reactContext = reactContext; | ||
} | ||
/** | ||
* Obtain mime type from URL | ||
* @param {@link String} url | ||
* @return {@link String} mime type | ||
*/ | ||
private String getMimeType(String url) { | ||
String type = "*/*"; | ||
String extension = MimeTypeMap.getFileExtensionFromUrl(url); | ||
if (extension != null) { | ||
type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension); | ||
} | ||
return type; | ||
} | ||
/** | ||
* Return an if the url is a file (local or base64)l | ||
* @return {@link boolean} | ||
*/ | ||
public boolean isFile() { | ||
return this.isBase64File() || this.isLocalFile(); | ||
} | ||
public boolean isBase64File() { | ||
if(uri.getScheme().equals("data")) { | ||
this.type = this.uri.getSchemeSpecificPart().substring(0, this.uri.getSchemeSpecificPart().indexOf(";")); | ||
return true; | ||
} | ||
return false; | ||
} | ||
public boolean isLocalFile() { | ||
if(uri.getScheme().equals("content") || uri.getScheme().equals("file")) { | ||
String realPath = this.getRealPathFromURI(uri); | ||
this.type = this.getMimeType(realPath); | ||
|
||
return true; | ||
} | ||
return false; | ||
} | ||
public String getType() { | ||
return this.type; | ||
} | ||
private String getRealPathFromURI(Uri contentUri) { | ||
String[] proj = { MediaStore.Images.Media.DATA }; | ||
CursorLoader loader = new CursorLoader(this.reactContext, contentUri, proj, null, null, null); | ||
Cursor cursor = loader.loadInBackground(); | ||
int column_index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA); | ||
cursor.moveToFirst(); | ||
String result = cursor.getString(column_index); | ||
cursor.close(); | ||
return result; | ||
} | ||
public Uri getURI() { | ||
|
||
final MimeTypeMap mime = MimeTypeMap.getSingleton(); | ||
this.extension = mime.getExtensionFromMimeType(this.type); | ||
if(this.isBase64File()) { | ||
String encodedImg = this.uri.getSchemeSpecificPart().substring(this.uri.getSchemeSpecificPart().indexOf(";base64,") + 8); | ||
try { | ||
File dir = new File(Environment.getExternalStorageDirectory(), Environment.DIRECTORY_DOWNLOADS ); | ||
if (!dir.exists()) { | ||
dir.mkdirs(); | ||
} | ||
File file = new File(dir, System.currentTimeMillis() + "." + this.extension); | ||
final FileOutputStream fos = new FileOutputStream(file); | ||
fos.write(Base64.decode(encodedImg, Base64.DEFAULT)); | ||
fos.flush(); | ||
fos.close(); | ||
return Uri.fromFile(file); | ||
|
||
} catch (IOException e) { | ||
e.printStackTrace(); | ||
} | ||
} else if(this.isLocalFile()) { | ||
Uri uri = Uri.parse(this.url); | ||
|
||
return uri; | ||
} | ||
|
||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,39 @@ | ||
package cl.json.social; | ||
|
||
import android.content.ActivityNotFoundException; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
/** | ||
* Created by disenodosbbcl on 23-07-16. | ||
*/ | ||
public class EmailShare extends SingleShareIntent { | ||
|
||
private static final String PACKAGE = "com.google.android.gm"; | ||
|
||
public EmailShare(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
@Override | ||
public void open(ReadableMap options) throws ActivityNotFoundException { | ||
super.open(options); | ||
// extra params here | ||
this.openIntentChooser(); | ||
} | ||
@Override | ||
protected String getPackage() { | ||
return PACKAGE; | ||
} | ||
|
||
@Override | ||
protected String getDefaultWebLink() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected String getPlayStoreLink() { | ||
return null; | ||
} | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
package cl.json.social; | ||
|
||
import android.content.ActivityNotFoundException; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
/** | ||
* Created by disenodosbbcl on 23-07-16. | ||
*/ | ||
public class FacebookShare extends SingleShareIntent { | ||
|
||
private static final String PACKAGE = "com.facebook.katana"; | ||
private static final String DEFAULT_WEB_LINK = "https://www.facebook.com/sharer/sharer.php?u={url}"; | ||
|
||
public FacebookShare(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
|
||
} | ||
@Override | ||
public void open(ReadableMap options) throws ActivityNotFoundException { | ||
super.open(options); | ||
// MORE DATA | ||
this.openIntentChooser(); | ||
} | ||
@Override | ||
protected String getPackage() { | ||
return PACKAGE; | ||
} | ||
|
||
@Override | ||
protected String getDefaultWebLink() { | ||
return DEFAULT_WEB_LINK; | ||
} | ||
|
||
@Override | ||
protected String getPlayStoreLink() { | ||
return null; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package cl.json.social; | ||
|
||
import android.content.ActivityNotFoundException; | ||
|
||
import com.facebook.react.bridge.ReactApplicationContext; | ||
import com.facebook.react.bridge.ReadableMap; | ||
|
||
/** | ||
* Created by disenodosbbcl on 23-07-16. | ||
*/ | ||
public class GenericShare extends ShareIntent { | ||
public GenericShare(ReactApplicationContext reactContext) { | ||
super(reactContext); | ||
} | ||
|
||
@Override | ||
public void open(ReadableMap options) throws ActivityNotFoundException { | ||
super.open(options); | ||
// extra params here | ||
this.openIntentChooser(); | ||
} | ||
|
||
@Override | ||
protected String getPackage() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected String getDefaultWebLink() { | ||
return null; | ||
} | ||
|
||
@Override | ||
protected String getPlayStoreLink() { | ||
return null; | ||
} | ||
} |
Oops, something went wrong.