blob: c955d0aea18f7762fd544f53346923dea5781cfa [file] [log] [blame]
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.providers.media;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.UserHandle;
import android.os.storage.StorageVolume;
import android.provider.MediaStore;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import com.android.modules.utils.build.SdkLevel;
import java.io.File;
import java.util.Objects;
/**
* MediaVolume is a MediaProvider-internal representation of a storage volume.
*
* Before MediaVolume, volumes inside MediaProvider were represented by their name;
* but now that MediaProvider handles volumes on behalf on multiple users, the name of a volume
* might no longer be unique. So MediaVolume holds both a name and a user. The user may be
* null on volumes without an owner (eg public volumes).
*
* In addition to that, we keep the path and ID of the volume cached in here as well
* for easy access.
*/
public final class MediaVolume implements Parcelable {
/**
* Name of the volume.
*/
private final @NonNull String mName;
/**
* User to which the volume belongs to; might be null in case of public volumes.
*/
private final @Nullable UserHandle mUser;
/**
* Path on which the volume is mounted.
*/
private final @Nullable File mPath;
/**
* Unique ID of the volume; eg "external;0"
*/
private final @Nullable String mId;
/**
* Whether the volume is a stub volume (a volume managed from outside Android).
*/
private final boolean mStub;
public @NonNull String getName() {
return mName;
}
public @Nullable UserHandle getUser() {
return mUser;
}
public @Nullable File getPath() {
return mPath;
}
public @Nullable String getId() {
return mId;
}
public boolean isStub() {
return mStub;
}
private MediaVolume (@NonNull String name, UserHandle user, File path, String id,
boolean stub) {
this.mName = name;
this.mUser = user;
this.mPath = path;
this.mId = id;
this.mStub = stub;
}
private MediaVolume (Parcel in) {
this.mName = in.readString();
this.mUser = in.readParcelable(null);
this.mPath = new File(in.readString());
this.mId = in.readString();
this.mStub = in.readInt() != 0;
}
@Override
public boolean equals(Object obj) {
if (this == obj) return true;
if (obj == null || getClass() != obj.getClass()) return false;
MediaVolume that = (MediaVolume) obj;
// We consciously don't compare the path, because:
// 1. On unmount events, the returned path for StorageVolumes is
// 'null', and different from a mounted volume.
// 2. A volume with a certain ID should never be mounted in two different paths, anyway
return Objects.equals(mName, that.mName) &&
Objects.equals(mUser, that.mUser) &&
Objects.equals(mId, that.mId) &&
(mStub == that.mStub);
}
@Override
public int hashCode() {
return Objects.hash(mName, mUser, mId, mStub);
}
public boolean isVisibleToUser(UserHandle user) {
return mUser == null || user.equals(mUser);
}
@NonNull
public static MediaVolume fromStorageVolume(StorageVolume storageVolume) {
String name = storageVolume.getMediaStoreVolumeName();
UserHandle user = storageVolume.getOwner();
File path = storageVolume.getDirectory();
String id = storageVolume.getId();
boolean stub = SdkLevel.isAtLeastT() ? storageVolume.isStub() : false;
return new MediaVolume(name, user, path, id, stub);
}
public static MediaVolume fromInternal() {
String name = MediaStore.VOLUME_INTERNAL;
return new MediaVolume(name, null, null, null, false);
}
@Override
public int describeContents() {
return 0;
}
@Override
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mName);
dest.writeParcelable(mUser, flags);
dest.writeString(mPath.toString());
dest.writeString(mId);
dest.writeInt(mStub ? 1 : 0);
}
@Override
public String toString() {
return "MediaVolume name: [" + mName + "] id: [" + mId + "] user: [" + mUser + "] path: ["
+ mPath + "] stub: [" + mStub + "]";
}
public static final @android.annotation.NonNull Creator<MediaVolume> CREATOR
= new Creator<MediaVolume>() {
@Override
public MediaVolume createFromParcel(Parcel in) {
return new MediaVolume(in);
}
@Override
public MediaVolume[] newArray(int size) {
return new MediaVolume[size];
}
};
}