public class

WorkDatabasePathHelper

extends java.lang.Object

 java.lang.Object

↳androidx.work.impl.WorkDatabasePathHelper

Overview

Keeps track of WorkDatabase paths.

Summary

Methods
public static java.io.FilegetDatabasePath(Context context)

public static java.io.FilegetDefaultDatabasePath(Context context)

public static java.lang.StringgetWorkDatabaseName()

public static voidmigrateDatabase(Context context)

Migrates WorkDatabase to the no-backup directory.

public static java.util.Map<java.io.File, java.io.File>migrationPaths(Context context)

Returns a java.util.Map of all paths which need to be migrated to the no-backup directory.

from java.lang.Objectclone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait

Methods

public static java.lang.String getWorkDatabaseName()

Returns:

The name of the database.

public static void migrateDatabase(Context context)

Migrates WorkDatabase to the no-backup directory.

Parameters:

context: The application context.

public static java.util.Map<java.io.File, java.io.File> migrationPaths(Context context)

Returns a java.util.Map of all paths which need to be migrated to the no-backup directory.

Parameters:

context: The application

Returns:

a java.util.Map of paths to be migrated from source -> destination

public static java.io.File getDefaultDatabasePath(Context context)

Parameters:

context: The application

Returns:

The database path before migration to the no-backup directory.

public static java.io.File getDatabasePath(Context context)

Parameters:

context: The application

Returns:

The the migrated database path.

Source

/*
 * Copyright 2019 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 androidx.work.impl;

import android.content.Context;
import android.os.Build;

import androidx.annotation.NonNull;
import androidx.annotation.RequiresApi;
import androidx.annotation.RestrictTo;
import androidx.annotation.VisibleForTesting;
import androidx.work.Logger;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

/**
 * Keeps track of {@link WorkDatabase} paths.
 *
 * @hide
 */
@RestrictTo(RestrictTo.Scope.LIBRARY_GROUP)
public class WorkDatabasePathHelper {
    private WorkDatabasePathHelper() {
    }

    private static final String TAG = Logger.tagWithPrefix("WrkDbPathHelper");

    private static final String WORK_DATABASE_NAME = "androidx.work.workdb";

    // Supporting files for a SQLite database
    private static final String[] DATABASE_EXTRA_FILES = new String[]{"-journal", "-shm", "-wal"};

    /**
     * @return The name of the database.
     */
    @NonNull
    public static String getWorkDatabaseName() {
        return WORK_DATABASE_NAME;
    }

    /**
     * Migrates {@link WorkDatabase} to the no-backup directory.
     *
     * @param context The application context.
     */
    public static void migrateDatabase(@NonNull Context context) {
        File defaultDatabasePath = getDefaultDatabasePath(context);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && defaultDatabasePath.exists()) {
            Logger.get().debug(TAG, "Migrating WorkDatabase to the no-backup directory");
            Map<File, File> paths = migrationPaths(context);
            for (File source : paths.keySet()) {
                File destination = paths.get(source);
                if (source.exists() && destination != null) {
                    if (destination.exists()) {
                        String message = String.format("Over-writing contents of %s", destination);
                        Logger.get().warning(TAG, message);
                    }
                    boolean renamed = source.renameTo(destination);
                    String message;
                    if (renamed) {
                        message = String.format("Migrated %s to %s", source, destination);
                    } else {
                        message = String.format("Renaming %s to %s failed", source, destination);
                    }
                    Logger.get().debug(TAG, message);
                }
            }
        }
    }

    /**
     * Returns a {@link Map} of all paths which need to be migrated to the no-backup directory.
     *
     * @param context The application {@link Context}
     * @return a {@link Map} of paths to be migrated from source -> destination
     */
    @NonNull
    @VisibleForTesting
    public static Map<File, File> migrationPaths(@NonNull Context context) {
        Map<File, File> paths = new HashMap<>();
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
            File databasePath = getDefaultDatabasePath(context);
            File migratedPath = getDatabasePath(context);
            paths.put(databasePath, migratedPath);
            for (String extra : DATABASE_EXTRA_FILES) {
                File source = new File(databasePath.getPath() + extra);
                File destination = new File(migratedPath.getPath() + extra);
                paths.put(source, destination);
            }
        }
        return paths;
    }

    /**
     * @param context The application {@link Context}
     * @return The database path before migration to the no-backup directory.
     */
    @NonNull
    @VisibleForTesting
    public static File getDefaultDatabasePath(@NonNull Context context) {
        return context.getDatabasePath(WORK_DATABASE_NAME);
    }

    /**
     * @param context The application {@link Context}
     * @return The the migrated database path.
     */
    @NonNull
    @VisibleForTesting
    public static File getDatabasePath(@NonNull Context context) {
        if (Build.VERSION.SDK_INT < Build.VERSION_CODES.M) {
            // No notion of a backup directory exists.
            return getDefaultDatabasePath(context);
        } else {
            return getNoBackupPath(context, WORK_DATABASE_NAME);
        }
    }

    /**
     * Return the path for a {@link File} path in the {@link Context#getNoBackupFilesDir()}
     * identified by the {@link String} fragment.
     *
     * @param context  The application {@link Context}
     * @param filePath The {@link String} file path
     * @return the {@link File}
     */
    @RequiresApi(23)
    private static File getNoBackupPath(@NonNull Context context, @NonNull String filePath) {
        return new File(context.getNoBackupFilesDir(), filePath);
    }
}