-
Notifications
You must be signed in to change notification settings - Fork 79
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #351 from lmagyar/pr-fix-set-last-modified
Fix setLastModified() exception on SAF
- Loading branch information
Showing
3 changed files
with
116 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
108 changes: 108 additions & 0 deletions
108
primitiveFTPd/src/org/primftpd/filesystem/StorageManagerUtil.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,108 @@ | ||
package org.primftpd.filesystem; | ||
|
||
import android.annotation.SuppressLint; | ||
import android.annotation.TargetApi; | ||
import android.content.Context; | ||
import android.net.Uri; | ||
import android.os.Build; | ||
import android.os.storage.StorageManager; | ||
import android.provider.DocumentsContract; | ||
|
||
import androidx.annotation.Nullable; | ||
import androidx.documentfile.provider.DocumentFile; | ||
|
||
import java.io.File; | ||
import java.io.FileNotFoundException; | ||
import java.lang.reflect.Array; | ||
import java.lang.reflect.Method; | ||
|
||
public final class StorageManagerUtil { | ||
private static final String PRIMARY_VOLUME_NAME = "primary"; | ||
|
||
public static String getFullDocIdPathFromTreeUri(@Nullable final Uri treeUri, Context context) { | ||
if (treeUri == null) { | ||
return null; | ||
} | ||
String volumePath = getVolumePath(getVolumeIdFromTreeUri(treeUri), context); | ||
if (volumePath == null) { | ||
return File.separator; | ||
} | ||
if (volumePath.endsWith(File.separator)) { | ||
volumePath = volumePath.substring(0, volumePath.length() - 1); | ||
} | ||
|
||
String documentPath = getDocumentPathFromTreeUri(treeUri); | ||
if (documentPath.endsWith(File.separator)) { | ||
documentPath = documentPath.substring(0, documentPath.length() - 1); | ||
} | ||
|
||
if (documentPath.length() > 0) { | ||
if (documentPath.startsWith(File.separator)) { | ||
return volumePath + documentPath; | ||
} else { | ||
return volumePath + File.separator + documentPath; | ||
} | ||
} else { | ||
return volumePath; | ||
} | ||
} | ||
|
||
@SuppressLint("ObsoleteSdkInt") | ||
private static String getVolumePath(final String volumeId, Context context) { | ||
if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { | ||
return null; | ||
} | ||
try { | ||
StorageManager mStorageManager = (StorageManager) context.getSystemService(Context.STORAGE_SERVICE); | ||
Class<?> storageVolumeClass = Class.forName("android.os.storage.StorageVolume"); | ||
Method getVolumeList = mStorageManager.getClass().getMethod("getVolumeList"); | ||
Method getUuid = storageVolumeClass.getMethod("getUuid"); | ||
Method getPath = storageVolumeClass.getMethod("getPath"); | ||
Method isPrimary = storageVolumeClass.getMethod("isPrimary"); | ||
Object result = getVolumeList.invoke(mStorageManager); | ||
|
||
final int length = Array.getLength(result); | ||
for (int i = 0; i < length; i++) { | ||
Object storageVolumeElement = Array.get(result, i); | ||
String uuid = (String) getUuid.invoke(storageVolumeElement); | ||
Boolean primary = (Boolean) isPrimary.invoke(storageVolumeElement); | ||
|
||
// primary volume? | ||
if (primary && PRIMARY_VOLUME_NAME.equals(volumeId)) { | ||
return (String) getPath.invoke(storageVolumeElement); | ||
} | ||
|
||
// other volumes? | ||
if (uuid != null && uuid.equals(volumeId)) { | ||
return (String) getPath.invoke(storageVolumeElement); | ||
} | ||
} | ||
// not found. | ||
return null; | ||
} catch (Exception ex) { | ||
return null; | ||
} | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||
public static String getVolumeIdFromTreeUri(final Uri treeUri) { | ||
final String docId = DocumentsContract.getTreeDocumentId(treeUri); | ||
final String[] split = docId.split(":"); | ||
if (split.length > 0) { | ||
return split[0]; | ||
} else { | ||
return null; | ||
} | ||
} | ||
|
||
@TargetApi(Build.VERSION_CODES.LOLLIPOP) | ||
private static String getDocumentPathFromTreeUri(final Uri treeUri) { | ||
final String docId = DocumentsContract.getDocumentId(treeUri); | ||
final String[] split = docId.split(":"); | ||
if ((split.length >= 2) && (split[1] != null)) { | ||
return split[1]; | ||
} else { | ||
return File.separator; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters