Skip to content

Commit

Permalink
[Fix] Use document path for non-primary storage volume in navigation.
Browse files Browse the repository at this point in the history
  • Loading branch information
zhanghai committed Dec 11, 2019
1 parent 20e8a73 commit c81f380
Showing 1 changed file with 33 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ private static List<NavigationItem> getRootItems() {
List<StorageVolume> storageVolumes = StorageManagerCompat.getStorageVolumes(storageManager);
for (StorageVolume storageVolume : storageVolumes) {
if (StorageVolumeCompat.isPrimary(storageVolume)) {
rootItems.add(new StorageVolumeRootItem(storageVolume));
rootItems.add(new PrimaryStorageVolumeRootItem(storageVolume));
}
}
List<Uri> treeUris = new ArrayList<>(DocumentTreesLiveData.getInstance().getValue());
Expand All @@ -128,7 +128,7 @@ private static List<NavigationItem> getRootItems() {
}
Uri treeUri = getStorageVolumeTreeUri(storageVolume);
if (treeUris.remove(treeUri)) {
rootItems.add(new DocumentTreeStorageVolumeRootItem(storageVolume, treeUri));
rootItems.add(new DocumentTreeStorageVolumeRootItem(treeUri, storageVolume));
}
}
for (Uri treeUri : treeUris) {
Expand Down Expand Up @@ -273,13 +273,14 @@ private static abstract class LocalRootItem extends RootItem {
private final long mFreeSpace;
private final long mTotalSpace;

public LocalRootItem(@NonNull String path, @DrawableRes int iconRes) {
super(Paths.get(path));
public LocalRootItem(@NonNull Path path, @NonNull String localPath,
@DrawableRes int iconRes) {
super(path);

mIconRes = iconRes;
long totalSpace = JavaFile.getTotalSpace(path);
long totalSpace = JavaFile.getTotalSpace(localPath);
if (totalSpace != 0) {
mFreeSpace = JavaFile.getFreeSpace(path);
mFreeSpace = JavaFile.getFreeSpace(localPath);
mTotalSpace = totalSpace;
} else {
// Root directory may not be an actual partition on legacy Android versions (can be
Expand All @@ -292,6 +293,10 @@ public LocalRootItem(@NonNull String path, @DrawableRes int iconRes) {
}
}

public LocalRootItem(@NonNull String path, @DrawableRes int iconRes) {
this(Paths.get(path), path, iconRes);
}

@DrawableRes
@Override
public int getIconRes() {
Expand Down Expand Up @@ -321,22 +326,17 @@ public int getTitleRes() {
}
}

private static class StorageVolumeRootItem extends LocalRootItem {
private static class PrimaryStorageVolumeRootItem extends LocalRootItem {

@NonNull
private final StorageVolume mStorageVolume;

public StorageVolumeRootItem(@NonNull StorageVolume storageVolume) {
public PrimaryStorageVolumeRootItem(@NonNull StorageVolume storageVolume) {
super(StorageVolumeCompat.getPath(storageVolume), R.drawable.sd_card_icon_white_24dp);

mStorageVolume = storageVolume;
}

@NonNull
protected StorageVolume getStorageVolume() {
return mStorageVolume;
}

@NonNull
@Override
public String getTitle(@NonNull Context context) {
Expand All @@ -349,21 +349,36 @@ public int getTitleRes() {
}
}

private static class DocumentTreeStorageVolumeRootItem extends StorageVolumeRootItem {
private static class DocumentTreeStorageVolumeRootItem extends LocalRootItem {

@NonNull
private final Uri mTreeUri;
@NonNull
private final StorageVolume mStorageVolume;

public DocumentTreeStorageVolumeRootItem(@NonNull StorageVolume storageVolume,
@NonNull Uri treeUri) {
super(storageVolume);
public DocumentTreeStorageVolumeRootItem(@NonNull Uri treeUri,
@NonNull StorageVolume storageVolume) {
super(DocumentFileSystemProvider.getRootPathForTreeUri(treeUri),
StorageVolumeCompat.getPath(storageVolume), R.drawable.sd_card_icon_white_24dp);

mTreeUri = treeUri;
mStorageVolume = storageVolume;
}

@NonNull
@Override
public String getTitle(@NonNull Context context) {
return StorageVolumeCompat.getDescription(mStorageVolume, context);
}

@Override
public int getTitleRes() {
throw new AssertionError();
}

@Override
public boolean onLongClick(@NonNull Listener listener) {
listener.onRemoveDocumentTree(mTreeUri, getStorageVolume());
listener.onRemoveDocumentTree(mTreeUri, mStorageVolume);
return true;
}
}
Expand Down

0 comments on commit c81f380

Please sign in to comment.