|
19 | 19 | import java.io.InputStream; |
20 | 20 | import java.nio.charset.StandardCharsets; |
21 | 21 | import java.util.ArrayList; |
| 22 | +import java.util.Locale; |
22 | 23 |
|
23 | 24 | @CapacitorPlugin(name = "SharedImport") |
24 | 25 | public class SharedImportPlugin extends Plugin { |
@@ -79,8 +80,7 @@ private JSObject payloadFromIntent(Context context, Intent intent) { |
79 | 80 |
|
80 | 81 | String action = intent.getAction(); |
81 | 82 | if (Intent.ACTION_VIEW.equals(action)) { |
82 | | - Uri data = intent.getData(); |
83 | | - return isManaVaultLink(context, data) ? linkPayload(data.toString(), "android-view") : null; |
| 83 | + return payloadFromViewIntent(context, intent); |
84 | 84 | } |
85 | 85 |
|
86 | 86 | if (!Intent.ACTION_SEND.equals(action) && !Intent.ACTION_SEND_MULTIPLE.equals(action)) return null; |
@@ -111,6 +111,75 @@ private JSObject payloadFromIntent(Context context, Intent intent) { |
111 | 111 | return importPayload(text, "Shared list.txt", intent.getType(), "android-share"); |
112 | 112 | } |
113 | 113 |
|
| 114 | + private JSObject payloadFromViewIntent(Context context, Intent intent) { |
| 115 | + Uri data = intent.getData(); |
| 116 | + if (isManaVaultLink(context, data)) return linkPayload(data.toString(), "android-view"); |
| 117 | + if (!isViewFileUri(data)) return null; |
| 118 | + |
| 119 | + ContentResolver resolver = context.getContentResolver(); |
| 120 | + String intentType = normalizeMimeType(intent.getType()); |
| 121 | + String resolverType = normalizeMimeType(resolverMimeType(resolver, data)); |
| 122 | + boolean supportedType = isSupportedTextFileMimeType(intentType) || isSupportedTextFileMimeType(resolverType); |
| 123 | + |
| 124 | + String fileName = null; |
| 125 | + if (!supportedType && !hasTextFileExtension(data.getLastPathSegment())) { |
| 126 | + fileName = displayName(resolver, data); |
| 127 | + supportedType = hasTextFileExtension(fileName); |
| 128 | + } |
| 129 | + if (!supportedType) return null; |
| 130 | + |
| 131 | + String text = readText(resolver, data); |
| 132 | + if (text == null || text.trim().isEmpty()) return null; |
| 133 | + |
| 134 | + if (fileName == null) fileName = displayName(resolver, data); |
| 135 | + return importPayload(text, fileName, bestMimeType(intentType, resolverType), "android-view-file"); |
| 136 | + } |
| 137 | + |
| 138 | + private boolean isViewFileUri(Uri uri) { |
| 139 | + if (uri == null) return false; |
| 140 | + |
| 141 | + String scheme = uri.getScheme(); |
| 142 | + return "content".equalsIgnoreCase(scheme) || "file".equalsIgnoreCase(scheme); |
| 143 | + } |
| 144 | + |
| 145 | + private String resolverMimeType(ContentResolver resolver, Uri uri) { |
| 146 | + try { |
| 147 | + return resolver.getType(uri); |
| 148 | + } catch (SecurityException e) { |
| 149 | + return null; |
| 150 | + } |
| 151 | + } |
| 152 | + |
| 153 | + private String normalizeMimeType(String mimeType) { |
| 154 | + if (mimeType == null) return null; |
| 155 | + |
| 156 | + String normalized = mimeType.trim().toLowerCase(Locale.ROOT); |
| 157 | + int parameters = normalized.indexOf(';'); |
| 158 | + return parameters >= 0 ? normalized.substring(0, parameters).trim() : normalized; |
| 159 | + } |
| 160 | + |
| 161 | + private boolean isSupportedTextFileMimeType(String mimeType) { |
| 162 | + if (mimeType == null || mimeType.trim().isEmpty()) return false; |
| 163 | + |
| 164 | + return mimeType.startsWith("text/") |
| 165 | + || "application/csv".equals(mimeType) |
| 166 | + || "application/vnd.ms-excel".equals(mimeType); |
| 167 | + } |
| 168 | + |
| 169 | + private boolean hasTextFileExtension(String value) { |
| 170 | + if (value == null) return false; |
| 171 | + |
| 172 | + String lower = value.trim().toLowerCase(Locale.ROOT); |
| 173 | + return lower.endsWith(".txt") || lower.endsWith(".csv"); |
| 174 | + } |
| 175 | + |
| 176 | + private String bestMimeType(String intentType, String resolverType) { |
| 177 | + if (isSupportedTextFileMimeType(intentType)) return intentType; |
| 178 | + if (isSupportedTextFileMimeType(resolverType)) return resolverType; |
| 179 | + if (intentType != null && !intentType.trim().isEmpty()) return intentType; |
| 180 | + return resolverType; |
| 181 | + } |
| 182 | + |
114 | 183 | @SuppressWarnings("deprecation") |
115 | 184 | private Uri firstStreamUri(Intent intent) { |
116 | 185 | Object stream = intent.getParcelableExtra(Intent.EXTRA_STREAM); |
|
0 commit comments