Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit fda65517 authored by Fahim Salam Chowdhury's avatar Fahim Salam Chowdhury 👽
Browse files

Merge branch '1712-Ignore_part_file_sync' into '115-epic'

1712-Ignore_part_file_sync

See merge request !270
parents 20842d8e 0b5839ab
Loading
Loading
Loading
Loading
Loading
+0 −27
Original line number Diff line number Diff line
/*
 * Copyright © CLEUS SAS 2018-2019.
 * Copyright © ECORP SAS 2020.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

package foundation.e.drive.fileFilters;

import java.io.File;
import java.io.FileFilter;
import java.util.Locale;

/**
 * @author Vincent Bourgmayer
 * FileFilter for application Settings
 */
class AppSettingsFileFilter implements FileFilter {
    @Override
    public boolean accept(File pathname) {
        return  pathname.isFile()
            || (pathname.isDirectory()
            && !pathname.getName().toLowerCase(Locale.ROOT).contains("cache"));
    }
}
+0 −56
Original line number Diff line number Diff line
/*
 * Copyright © CLEUS SAS 2018-2019.
 * Copyright © ECORP SAS 2020.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

package foundation.e.drive.fileFilters;

import androidx.annotation.NonNull;

import java.io.File;
import java.io.FileFilter;

import foundation.e.drive.utils.ServiceExceptionHandler;

/**
 * @Author Vincent Bourgmayer
 */
public class CrashlogsFileFilter implements FileFilter {
    private final static long max_timestamp_delta = 864000000; //10 days in ms (240*3600*1000)

    @Override
    public boolean accept(@NonNull File pathname) {
        String fileTimestamp = extractTimestamp(pathname.getName(),
                ServiceExceptionHandler.LOG_FILE_NAME_PREFIX,
                ServiceExceptionHandler.LOG_FILE_EXTENSION);

        long timestamp;
        try {
            timestamp = Long.parseLong(fileTimestamp);
        }catch (NumberFormatException e){
            //Can't parse the extracted timestamp
            //This file has not the expected name. It must be removed
            return true;
        }

        //if current Date - file date >= max deta allowed
        return ((System.currentTimeMillis() - timestamp ) >= max_timestamp_delta);
    }

    /**
     * Extract the timestamp from the name of the file
     * UnitTested!
     * @param fileName Filename
     * @param prefix prefix to ignore
     * @param extension extension to ignore
     * @return the timestamp extracted from the name
     */
    private String extractTimestamp(String fileName, @NonNull String prefix, @NonNull String extension){
        return fileName.substring(prefix.length(), (fileName.length() - extension.length()));
    }

}
 No newline at end of file
+0 −40
Original line number Diff line number Diff line
/*
 * Copyright © CLEUS SAS 2018-2019.
 * Copyright © ECORP SAS 2020.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

package foundation.e.drive.fileFilters;

import androidx.annotation.NonNull;

import java.io.FileFilter;

/**
 * @author Vincent Bourgmayer
 */
public class FileFilterFactory {

    @NonNull
    public static FileFilter getFileFilter(@NonNull String category){
        FileFilter filter;
        switch (category){
            case "Rom settings":
                filter = new SettingsFileFilter();
                break;
            case "Applications":
                filter = new AppSettingsFileFilter();
                break;
            case "media":
                filter = new MediaFileFilter();
                break;
            default:
                filter = new NoCacheFileFilter();
                break;
        }
        return filter;
    }
}
+73 −0
Original line number Diff line number Diff line
/*
 * Copyright MURENA SAS 2024
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 *  along with this program.  If not, see <https://www.gnu.org/licenses/>.
 */

package foundation.e.drive.fileFilters;

import foundation.e.drive.utils.AppConstants
import foundation.e.drive.utils.FileUtils
import java.io.File
import java.io.FileFilter

/**
 * @author Vincent Bourgmayer
 * @author Fahim Salam Chowdhury
 */
object FileFilterFactory {

    fun buildFileFilter(category: String?): FileFilter {
        return when (category) {
            "Rom settings" -> buildSettingsFilter()
            "Applications" -> buildAppSettingsFilter()
            "media" -> BasicFileFilter()
            else -> buildNoCacheFileFilter()
        }
    }

    private fun buildSettingsFilter(): FileFilter {
        return object : BasicFileFilter() {
            override fun accept(pathname: File?): Boolean {
                return super.accept(pathname) && pathname!!.isFile &&
                        (pathname.name.startsWith("settings_") && pathname.name.endsWith(".xml")
                                || pathname.name == AppConstants.APPLICATIONS_LIST_FILE_NAME)
            }
        }
    }

    private fun buildAppSettingsFilter(): FileFilter {
        return object : BasicFileFilter() {
            override fun accept(pathname: File?): Boolean {
                return (super.accept(pathname)
                        && (pathname!!.isFile || (pathname.isDirectory
                        && !pathname.name.lowercase().contains("cache"))))
            }
        }
    }

    private fun buildNoCacheFileFilter(): FileFilter {
        return object : BasicFileFilter() {
            override fun accept(pathname: File?): Boolean {
                return super.accept(pathname) &&
                        !pathname!!.name.lowercase().contains("cache")
            }
        }
    }
}

private open class BasicFileFilter : FileFilter {
    override fun accept(pathname: File?): Boolean {
        return pathname != null && !pathname.isHidden && FileUtils.isNotPartFile(pathname)
    }
}
+0 −29
Original line number Diff line number Diff line
/*
 * Copyright © CLEUS SAS 2018-2019.
 * Copyright © ECORP SAS 2020.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the GNU Public License v3.0
 * which accompanies this distribution, and is available at
 * http://www.gnu.org/licenses/gpl.html
 */

package foundation.e.drive.fileFilters;

import java.io.File;
import java.io.FileFilter;

/**
 * @author Vincent Bourgmayer
 */
class MediaFileFilter implements FileFilter {
    /**
     * Only accept not hidden files:
     * Media should not be synced if they're hidden files
     * @param file File to check
     * @return true if file is accepted
     */
    @Override
    public boolean accept(File file) {
        return !file.isHidden();
    }
}
Loading