mirror of
https://github.com/2009scape/2009Scape-mobile.git
synced 2025-12-12 01:21:09 -07:00
Implement file/folder opening along with link opening
This commit is contained in:
parent
33d7d854ae
commit
db7881792f
3 changed files with 46 additions and 3 deletions
|
|
@ -20,6 +20,7 @@ import android.os.*;
|
|||
import android.provider.DocumentsContract;
|
||||
import android.util.*;
|
||||
import android.view.*;
|
||||
import android.webkit.MimeTypeMap;
|
||||
import android.widget.*;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
|
|
@ -508,12 +509,37 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe
|
|||
b.show();
|
||||
}
|
||||
|
||||
private static void setUri(Context context, String input, Intent intent) {
|
||||
if(input.startsWith("file:")) {
|
||||
int truncLength = 5;
|
||||
if(input.startsWith("file://")) truncLength = 7;
|
||||
input = input.substring(truncLength);
|
||||
Log.i("MainActivity", input);
|
||||
boolean isDirectory = new File(input).isDirectory();
|
||||
if(isDirectory) {
|
||||
intent.setType(DocumentsContract.Document.MIME_TYPE_DIR);
|
||||
}else{
|
||||
String type = null;
|
||||
String extension = MimeTypeMap.getFileExtensionFromUrl(input);
|
||||
if(extension != null) type = MimeTypeMap.getSingleton().getMimeTypeFromExtension(extension);
|
||||
if(type == null) type = "*/*";
|
||||
intent.setType(type);
|
||||
}
|
||||
intent.setFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION | Intent.FLAG_GRANT_WRITE_URI_PERMISSION);
|
||||
intent.setData(DocumentsContract.buildDocumentUri(
|
||||
context.getString(R.string.storageProviderAuthorities), input
|
||||
));
|
||||
return;
|
||||
}
|
||||
intent.setDataAndType(Uri.parse(input), "*/*");
|
||||
}
|
||||
|
||||
public static void openLink(String link) {
|
||||
Context ctx = touchpad.getContext(); // no more better way to obtain a context statically
|
||||
((Activity)ctx).runOnUiThread(() -> {
|
||||
try {
|
||||
Intent intent = new Intent(Intent.ACTION_VIEW);
|
||||
intent.setDataAndType(Uri.parse(link.replace("file://", "content://")), "*/*");
|
||||
setUri(ctx, link, intent);
|
||||
ctx.startActivity(intent);
|
||||
} catch (Throwable th) {
|
||||
Tools.showError(ctx, th);
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue