Skip to content

Commit

Permalink
refactor: remove unused code & fix warnings
Browse files Browse the repository at this point in the history
  • Loading branch information
amanharwara committed Jul 11, 2023
1 parent 8e35e67 commit 008e3f4
Showing 1 changed file with 20 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,12 @@
import android.content.Intent;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.provider.OpenableColumns;

import androidx.annotation.RequiresApi;

import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.WritableMap;
import com.facebook.react.bridge.WritableNativeMap;

import java.net.URLConnection;
import java.util.ArrayList;
import java.util.Objects;

Expand All @@ -31,7 +27,6 @@ public ReceiveSharingIntentHelper(Application context) {
this.context = context;
}

@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public void sendFileNames(Context context, Intent intent, Promise promise) {
try {
String action = intent.getAction();
Expand All @@ -43,20 +38,21 @@ public void sendFileNames(Context context, Intent intent, Promise promise) {
WritableMap files = getMediaUris(intent, context);
if (files == null) return;
promise.resolve(files);
} else if (type.startsWith("text") && Objects.equals(action, Intent.ACTION_SEND)) {
}
else if (type.startsWith("text") && Objects.equals(action, Intent.ACTION_SEND)) {
String text = null;
String subject = null;
try {
text = intent.getStringExtra(Intent.EXTRA_TEXT);
subject = intent.getStringExtra(Intent.EXTRA_SUBJECT);
} catch (Exception ignored) {
}
} catch (Exception ignored) {}
WritableMap files;
if (text == null) {
WritableMap files = getMediaUris(intent, context);
files = getMediaUris(intent, context);
if (files == null) return;
promise.resolve(files);
} else {
WritableMap files = new WritableNativeMap();
}
else {
files = new WritableNativeMap();
WritableMap file = new WritableNativeMap();
file.putString("contentUri", null);
file.putString("fileName", null);
Expand All @@ -70,10 +66,10 @@ public void sendFileNames(Context context, Intent intent, Promise promise) {
}
file.putString("subject", subject);
files.putMap("0", file);
promise.resolve(files);
}

} else if (Objects.equals(action, Intent.ACTION_VIEW)) {
promise.resolve(files);
}
else if (Objects.equals(action, Intent.ACTION_VIEW)) {
String link = intent.getDataString();
WritableMap files = new WritableNativeMap();
WritableMap file = new WritableNativeMap();
Expand All @@ -85,11 +81,12 @@ public void sendFileNames(Context context, Intent intent, Promise promise) {
file.putString("extension", null);
files.putMap("0", file);
promise.resolve(files);
} else if (Objects.equals(action, "android.intent.action.PROCESS_TEXT")) {
}
else if (Objects.equals(action, Intent.ACTION_PROCESS_TEXT)) {
String text = null;
try {
text = intent.getStringExtra(intent.EXTRA_PROCESS_TEXT);
} catch (Exception e) {
text = intent.getStringExtra(Intent.EXTRA_PROCESS_TEXT);
} catch (Exception ignored) {
}
WritableMap files = new WritableNativeMap();
WritableMap file = new WritableNativeMap();
Expand All @@ -100,19 +97,17 @@ public void sendFileNames(Context context, Intent intent, Promise promise) {
file.putString("text", text);
files.putMap("0", file);
promise.resolve(files);
} else {
}
else {
promise.reject("error", "Invalid file type.");
}
} catch (Exception e) {
promise.reject("error", e.toString());
}
}

;


@SuppressLint("Range")
@RequiresApi(api = Build.VERSION_CODES.KITKAT)
public WritableMap getMediaUris(Intent intent, Context context) {
if (intent == null) return null;

Expand All @@ -125,7 +120,7 @@ public WritableMap getMediaUris(Intent intent, Context context) {
WritableMap files = new WritableNativeMap();
if (Objects.equals(intent.getAction(), Intent.ACTION_SEND)) {
WritableMap file = new WritableNativeMap();
Uri contentUri = (Uri) intent.getParcelableExtra(Intent.EXTRA_STREAM);
Uri contentUri = intent.getParcelableExtra(Intent.EXTRA_STREAM);
if (contentUri == null) return null;
ContentResolver contentResolver = context.getContentResolver();
file.putString("mimeType", contentResolver.getType(contentUri));
Expand All @@ -137,6 +132,7 @@ public WritableMap getMediaUris(Intent intent, Context context) {
file.putString("weblink", null);
file.putString("subject", subject);
files.putMap("0", file);
queryResult.close();
} else if (Objects.equals(intent.getAction(), Intent.ACTION_SEND_MULTIPLE)) {
ArrayList<Uri> contentUris = intent.getParcelableArrayListExtra(Intent.EXTRA_STREAM);
if (contentUris != null) {
Expand All @@ -154,19 +150,14 @@ public WritableMap getMediaUris(Intent intent, Context context) {
file.putString("weblink", null);
file.putString("subject", subject);
files.putMap(Integer.toString(index), file);
queryResult.close();
index++;
}
}
}
return files;
}


private String getMediaType(String url) {
return URLConnection.guessContentTypeFromName(url);
}


public void clearFileNames(Intent intent) {
String type = intent.getType();
if (type == null) return;
Expand All @@ -177,12 +168,4 @@ public void clearFileNames(Intent intent) {
}
}

public String getFileName(String file) {
return file.substring(file.lastIndexOf('/') + 1);
}

public String getExtension(String file) {
return file.substring(file.lastIndexOf('.') + 1);
}

}

0 comments on commit 008e3f4

Please sign in to comment.