Implement file/folder opening along with link opening

This commit is contained in:
artdeell 2023-01-27 21:41:55 +03:00
parent 33d7d854ae
commit db7881792f
3 changed files with 46 additions and 3 deletions

View File

@ -20,6 +20,7 @@ import android.os.*;
import android.provider.DocumentsContract; import android.provider.DocumentsContract;
import android.util.*; import android.util.*;
import android.view.*; import android.view.*;
import android.webkit.MimeTypeMap;
import android.widget.*; import android.widget.*;
import androidx.annotation.NonNull; import androidx.annotation.NonNull;
@ -508,12 +509,37 @@ public class MainActivity extends BaseActivity implements ControlButtonMenuListe
b.show(); 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) { public static void openLink(String link) {
Context ctx = touchpad.getContext(); // no more better way to obtain a context statically Context ctx = touchpad.getContext(); // no more better way to obtain a context statically
((Activity)ctx).runOnUiThread(() -> { ((Activity)ctx).runOnUiThread(() -> {
try { try {
Intent intent = new Intent(Intent.ACTION_VIEW); Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setDataAndType(Uri.parse(link.replace("file://", "content://")), "*/*"); setUri(ctx, link, intent);
ctx.startActivity(intent); ctx.startActivity(intent);
} catch (Throwable th) { } catch (Throwable th) {
Tools.showError(ctx, th); Tools.showError(ctx, th);

View File

@ -1,16 +1,21 @@
package net.kdt.pojavlaunch.scoped; package net.kdt.pojavlaunch.scoped;
import android.annotation.TargetApi;
import android.content.res.AssetFileDescriptor; import android.content.res.AssetFileDescriptor;
import android.database.Cursor; import android.database.Cursor;
import android.database.MatrixCursor; import android.database.MatrixCursor;
import android.graphics.Point; import android.graphics.Point;
import android.os.CancellationSignal; import android.os.CancellationSignal;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.provider.DocumentsContract.Document; import android.provider.DocumentsContract.Document;
import android.provider.DocumentsContract.Root; import android.provider.DocumentsContract.Root;
import android.provider.DocumentsProvider; import android.provider.DocumentsProvider;
import android.util.Log;
import android.webkit.MimeTypeMap; import android.webkit.MimeTypeMap;
import androidx.annotation.Nullable;
import net.kdt.pojavlaunch.R; import net.kdt.pojavlaunch.R;
import net.kdt.pojavlaunch.Tools; import net.kdt.pojavlaunch.Tools;
@ -19,8 +24,10 @@ import org.apache.commons.io.FileUtils;
import java.io.File; import java.io.File;
import java.io.FileNotFoundException; import java.io.FileNotFoundException;
import java.io.IOException; import java.io.IOException;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.LinkedList; import java.util.LinkedList;
import java.util.List;
/** /**
* A document provider for the Storage Access Framework which exposes the files in the * A document provider for the Storage Access Framework which exposes the files in the
@ -183,6 +190,7 @@ public class FolderProvider extends DocumentsProvider {
@Override @Override
public String getDocumentType(String documentId) throws FileNotFoundException { public String getDocumentType(String documentId) throws FileNotFoundException {
Log.i("FolderPRovider", "getDocumentType("+documentId+")");
File file = getFileForDocId(documentId); File file = getFileForDocId(documentId);
return getMimeType(file); return getMimeType(file);
} }
@ -300,4 +308,11 @@ public class FolderProvider extends DocumentsProvider {
row.add(Document.COLUMN_ICON, R.mipmap.ic_launcher); row.add(Document.COLUMN_ICON, R.mipmap.ic_launcher);
} }
@Override
@TargetApi(26)
public DocumentsContract.Path findDocumentPath(@Nullable String parentDocumentId, String childDocumentId) throws FileNotFoundException {
File source = BASE_DIR;
if(parentDocumentId != null) source = getFileForDocId(parentDocumentId);
return new DocumentsContract.Path(getDocIdForFile(source), Collections.singletonList(childDocumentId));
}
} }

View File

@ -93,7 +93,8 @@ public class AsyncMinecraftDownloader {
} }
try { try {
assets = downloadIndex(verInfo, new File(Tools.ASSETS_PATH, "indexes/" + verInfo.assets + ".json")); if(verInfo.assets != null)
assets = downloadIndex(verInfo, new File(Tools.ASSETS_PATH, "indexes/" + verInfo.assets + ".json"));
} catch (IOException e) { } catch (IOException e) {
Log.e("AsyncMcDownloader", e.toString(), e); Log.e("AsyncMcDownloader", e.toString(), e);
throw new DownloaderException(e); throw new DownloaderException(e);
@ -199,7 +200,8 @@ public class AsyncMinecraftDownloader {
try { try {
downloadAssets(assets, verInfo.assets, assets.mapToResources ? new File(Tools.OBSOLETE_RESOURCES_PATH) : new File(Tools.ASSETS_PATH)); if(assets != null)
downloadAssets(assets, verInfo.assets, assets.mapToResources ? new File(Tools.OBSOLETE_RESOURCES_PATH) : new File(Tools.ASSETS_PATH));
} catch (Exception e) { } catch (Exception e) {
Log.e("AsyncMcDownloader", e.toString(), e); Log.e("AsyncMcDownloader", e.toString(), e);
ProgressKeeper.submitProgress(ProgressLayout.DOWNLOAD_MINECRAFT, -1, -1); ProgressKeeper.submitProgress(ProgressLayout.DOWNLOAD_MINECRAFT, -1, -1);