Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -294,6 +294,11 @@
<intent>
<action android:name="android.media.action.IMAGE_CAPTURE" />
</intent>
<!-- Vendor file managers launched by FileUtils.openFolder. Keep in sync with
FileUtils.VENDOR_FILE_MANAGERS: one <package> line per registry entry. -->
<package android:name="com.oneplus.filemanager" />
<package android:name="com.coloros.filemanager" />
<package android:name="com.oplus.filemanager" />
</queries>

</manifest>
43 changes: 37 additions & 6 deletions app/src/main/java/com/nutomic/syncthingandroid/util/FileUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@

import android.annotation.SuppressLint;
import android.content.ActivityNotFoundException;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.net.Uri;
import android.os.Build;
import android.os.Environment;
import android.os.storage.StorageManager;
import android.provider.DocumentsContract;
Expand All @@ -25,15 +24,13 @@
import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.FileWriter;
import java.io.IOException;
import java.io.OutputStream;
import java.lang.reflect.Array;
import java.lang.reflect.Method;
import java.nio.charset.StandardCharsets;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Locale;
Expand Down Expand Up @@ -1138,10 +1135,44 @@ public static void openFolder(final Context context, String folderPath) {
context.startActivity(intent);
} catch (ActivityNotFoundException | SecurityException e2) {
Log.e(TAG, "openFolder: No compatible file manager app not found or has insufficient permissions (stage #2)", e2);
// No compatible file manager app found.
suggestFileManagerApp(context);
// Stage #3: Try launching a known OS/vendor-specific file manager directly.
if (!tryVendorFileManagers(context, folderPath)) {
// No compatible file manager app found.
suggestFileManagerApp(context);
Comment thread
dbhavsar76 marked this conversation as resolved.
}
}
}
}

/**
* Known OS/vendor file managers to try as a last resort. They don't register the generic
* intent-filters used by the earlier stages, so we launch their browse activity directly.
* Each entry is {package, browse activity, folder-path extra key}. Add more rows to extend.
* Note: each package also needs a {@code <package>} line in the manifest {@code <queries>}
* block to be visible/launchable on Android 11+.
*/
private static final String[][] VENDOR_FILE_MANAGERS = {
// OnePlus / Oppo / Realme "My Files" (reverse-engineered; verify on a real device).
{"com.oneplus.filemanager", "com.oplus.filebrowser.FileBrowserActivity", "CurrentDir"},
{"com.coloros.filemanager", "com.oplus.filebrowser.FileBrowserActivity", "CurrentDir"},
{"com.oplus.filemanager", "com.oplus.filebrowser.FileBrowserActivity", "CurrentDir"},
};

private static boolean tryVendorFileManagers(final Context context, final String folderPath) {
for (String[] fm : VENDOR_FILE_MANAGERS) {
try {
Intent intent = new Intent(Intent.ACTION_VIEW);
intent.setComponent(new ComponentName(fm[0], fm[1]));
intent.putExtra(fm[2], folderPath);
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
context.startActivity(intent);
Log.i(TAG, "openFolder: Launched vendor file manager '" + fm[0] + "'");
return true;
} catch (Exception e) {
// Not installed or not launchable; try the next one.
}
}
return false;
}

private static void suggestFileManagerApp(final Context context) {
Expand Down