FileDocCategorySizeDatePackage
SharedStorageAgent.javaAPI DocAndroid 5.1 API3789Thu Mar 12 22:22:42 GMT 2015com.android.sharedstoragebackup

SharedStorageAgent

public class SharedStorageAgent extends android.app.backup.FullBackupAgent

Fields Summary
static final String
TAG
static final boolean
DEBUG
android.os.storage.StorageVolume[]
mVolumes
Constructors Summary
Methods Summary
public voidonCreate()


    
       
        StorageManager mgr = (StorageManager) getSystemService(Context.STORAGE_SERVICE);
        if (mgr != null) {
            mVolumes = mgr.getVolumeList();
        } else {
            Slog.e(TAG, "Unable to access Storage Manager");
        }
    
public voidonFullBackup(android.app.backup.FullBackupDataOutput output)
Full backup of the shared-storage filesystem

        // If there are shared-storage volumes available, run the inherited directory-
        // hierarchy backup process on them.  By convention in the Storage Manager, the
        // "primary" shared storage volume is first in the list.
        if (mVolumes != null) {
            if (DEBUG) Slog.i(TAG, "Backing up " + mVolumes.length + " shared volumes");
            // Ignore all apps' getExternalFilesDir() content; it is backed up as part of
            // each app-specific payload.
            HashSet<String> externalFilesDirFilter = new HashSet<String>();
            final File externalAndroidRoot = new File(Environment.getExternalStorageDirectory(),
                    Environment.DIRECTORY_ANDROID);
            externalFilesDirFilter.add(externalAndroidRoot.getCanonicalPath());

            for (int i = 0; i < mVolumes.length; i++) {
                StorageVolume v = mVolumes[i];
                // Express the contents of volume N this way in the tar stream:
                //     shared/N/path/to/file
                // The restore will then extract to the given volume
                String domain = FullBackup.SHARED_PREFIX + i;
                fullBackupFileTree(null, domain, v.getPath(), externalFilesDirFilter, output);
            }
        }
    
public voidonRestoreFile(android.os.ParcelFileDescriptor data, long size, int type, java.lang.String domain, java.lang.String relpath, long mode, long mtime)
Full restore of one file to shared storage

        if (DEBUG) Slog.d(TAG, "Shared restore: [ " + domain + " : " + relpath + "]");

        File outFile = null;

        // The file path must be in the semantic form [number]/path/to/file...
        int slash = relpath.indexOf('/");
        if (slash > 0) {
            try {
                int i = Integer.parseInt(relpath.substring(0, slash));
                if (i <= mVolumes.length) {
                    outFile = new File(mVolumes[i].getPath(), relpath.substring(slash + 1));
                    if (DEBUG) Slog.i(TAG, " => " + outFile.getAbsolutePath());
                } else {
                    Slog.w(TAG, "Cannot restore data for unavailable volume " + i);
                }
            } catch (NumberFormatException e) {
                if (DEBUG) Slog.w(TAG, "Bad volume number token: " + relpath.substring(0, slash));
            }
        } else {
            if (DEBUG) Slog.i(TAG, "Can't find volume-number token");
        }
        if (outFile == null) {
            Slog.e(TAG, "Skipping data with malformed path " + relpath);
        }

        FullBackup.restoreFile(data, size, type, -1, mtime, outFile);