Search in sources :

Example 6 with SyncedFolderProvider

use of com.owncloud.android.datamodel.SyncedFolderProvider in project android by nextcloud.

the class FilesSyncJob method onRunJob.

@NonNull
@Override
protected Result onRunJob(Params params) {
    final Context context = MainApp.getAppContext();
    final ContentResolver contentResolver = context.getContentResolver();
    FileUploader.UploadRequester requester = new FileUploader.UploadRequester();
    PowerManager.WakeLock wakeLock = null;
    if (android.os.Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) {
        PowerManager powerManager = (PowerManager) context.getSystemService(Context.POWER_SERVICE);
        wakeLock = powerManager.newWakeLock(PowerManager.PARTIAL_WAKE_LOCK, TAG);
        wakeLock.acquire();
    }
    PersistableBundleCompat bundle = params.getExtras();
    final boolean skipCustom = bundle.getBoolean(SKIP_CUSTOM, false);
    final boolean overridePowerSaving = bundle.getBoolean(OVERRIDE_POWER_SAVING, false);
    // If we are in power save mode, better to postpone upload
    if (PowerUtils.isPowerSaveMode(context) && !overridePowerSaving) {
        wakeLock.release();
        return Result.SUCCESS;
    }
    Resources resources = MainApp.getAppContext().getResources();
    boolean lightVersion = resources.getBoolean(R.bool.syncedFolder_light);
    FilesSyncHelper.restartJobsIfNeeded();
    FilesSyncHelper.insertAllDBEntries(skipCustom);
    // Create all the providers we'll need
    final FilesystemDataProvider filesystemDataProvider = new FilesystemDataProvider(contentResolver);
    SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver);
    for (SyncedFolder syncedFolder : syncedFolderProvider.getSyncedFolders()) {
        if ((syncedFolder.isEnabled()) && (!skipCustom || MediaFolderType.CUSTOM != syncedFolder.getType())) {
            for (String path : filesystemDataProvider.getFilesForUpload(syncedFolder.getLocalPath(), Long.toString(syncedFolder.getId()))) {
                File file = new File(path);
                Long lastModificationTime = file.lastModified();
                final Locale currentLocale = context.getResources().getConfiguration().locale;
                if (MediaFolderType.IMAGE == syncedFolder.getType()) {
                    String mimeTypeString = FileStorageUtils.getMimeTypeFromName(file.getAbsolutePath());
                    if ("image/jpeg".equalsIgnoreCase(mimeTypeString) || "image/tiff".equalsIgnoreCase(mimeTypeString)) {
                        try {
                            ExifInterface exifInterface = new ExifInterface(file.getAbsolutePath());
                            String exifDate = exifInterface.getAttribute(ExifInterface.TAG_DATETIME);
                            if (!TextUtils.isEmpty(exifDate)) {
                                ParsePosition pos = new ParsePosition(0);
                                SimpleDateFormat sFormatter = new SimpleDateFormat("yyyy:MM:dd HH:mm:ss", currentLocale);
                                sFormatter.setTimeZone(TimeZone.getTimeZone(TimeZone.getDefault().getID()));
                                Date dateTime = sFormatter.parse(exifDate, pos);
                                lastModificationTime = dateTime.getTime();
                            }
                        } catch (IOException e) {
                            Log_OC.d(TAG, "Failed to get the proper time " + e.getLocalizedMessage());
                        }
                    }
                }
                String mimeType = MimeTypeUtil.getBestMimeTypeByFilename(file.getAbsolutePath());
                Account account = AccountUtils.getOwnCloudAccountByName(context, syncedFolder.getAccount());
                String remotePath;
                boolean subfolderByDate;
                Integer uploadAction;
                boolean needsCharging;
                boolean needsWifi;
                if (lightVersion) {
                    ArbitraryDataProvider arbitraryDataProvider = new ArbitraryDataProvider(context.getContentResolver());
                    needsCharging = resources.getBoolean(R.bool.syncedFolder_light_on_charging);
                    needsWifi = account == null || arbitraryDataProvider.getBooleanValue(account.name, Preferences.SYNCED_FOLDER_LIGHT_UPLOAD_ON_WIFI);
                    String uploadActionString = resources.getString(R.string.syncedFolder_light_upload_behaviour);
                    uploadAction = getUploadAction(uploadActionString);
                    subfolderByDate = resources.getBoolean(R.bool.syncedFolder_light_use_subfolders);
                    remotePath = resources.getString(R.string.syncedFolder_remote_folder);
                } else {
                    needsCharging = syncedFolder.getChargingOnly();
                    needsWifi = syncedFolder.getWifiOnly();
                    uploadAction = syncedFolder.getUploadAction();
                    subfolderByDate = syncedFolder.getSubfolderByDate();
                    remotePath = syncedFolder.getRemotePath();
                }
                if (!subfolderByDate) {
                    String adaptedPath = file.getAbsolutePath().replace(syncedFolder.getLocalPath(), "").replace("/" + file.getName(), "");
                    remotePath += adaptedPath;
                }
                requester.uploadFileWithOverwrite(context, account, file.getAbsolutePath(), FileStorageUtils.getInstantUploadFilePath(currentLocale, remotePath, file.getName(), lastModificationTime, subfolderByDate), uploadAction, mimeType, // create parent folder if not existent
                true, UploadFileOperation.CREATED_AS_INSTANT_PICTURE, needsWifi, needsCharging, true);
                filesystemDataProvider.updateFilesystemFileAsSentForUpload(path, Long.toString(syncedFolder.getId()));
            }
        }
    }
    if (wakeLock != null) {
        wakeLock.release();
    }
    return Result.SUCCESS;
}
Also used : Locale(java.util.Locale) FilesystemDataProvider(com.owncloud.android.datamodel.FilesystemDataProvider) Account(android.accounts.Account) PersistableBundleCompat(com.evernote.android.job.util.support.PersistableBundleCompat) FileUploader(com.owncloud.android.files.services.FileUploader) ContentResolver(android.content.ContentResolver) PowerManager(android.os.PowerManager) SyncedFolder(com.owncloud.android.datamodel.SyncedFolder) ParsePosition(java.text.ParsePosition) Context(android.content.Context) ExifInterface(android.support.media.ExifInterface) SyncedFolderProvider(com.owncloud.android.datamodel.SyncedFolderProvider) ArbitraryDataProvider(com.owncloud.android.datamodel.ArbitraryDataProvider) IOException(java.io.IOException) Date(java.util.Date) Resources(android.content.res.Resources) File(java.io.File) SimpleDateFormat(java.text.SimpleDateFormat) NonNull(android.support.annotation.NonNull)

Example 7 with SyncedFolderProvider

use of com.owncloud.android.datamodel.SyncedFolderProvider in project android by nextcloud.

the class MainApp method updateAutoUploadEntries.

private static void updateAutoUploadEntries() {
    // updates entries to reflect their true paths
    Context context = getAppContext();
    if (!PreferenceManager.getAutoUploadPathsUpdate(context)) {
        SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(MainApp.getAppContext().getContentResolver());
        syncedFolderProvider.updateAutoUploadPaths(mContext);
    }
}
Also used : Context(android.content.Context) SyncedFolderProvider(com.owncloud.android.datamodel.SyncedFolderProvider)

Example 8 with SyncedFolderProvider

use of com.owncloud.android.datamodel.SyncedFolderProvider in project android by nextcloud.

the class MainApp method splitOutAutoUploadEntries.

private static void splitOutAutoUploadEntries() {
    Context context = getAppContext();
    if (!PreferenceManager.getAutoUploadSplitEntries(context)) {
        // magic to split out existing synced folders in two when needed
        // otherwise, we migrate them to their proper type (image or video)
        Log_OC.i(TAG, "Migrate synced_folders records for image/video split");
        ContentResolver contentResolver = context.getContentResolver();
        SyncedFolderProvider syncedFolderProvider = new SyncedFolderProvider(contentResolver);
        final List<MediaFolder> imageMediaFolders = MediaProvider.getImageFolders(contentResolver, 1, null);
        final List<MediaFolder> videoMediaFolders = MediaProvider.getVideoFolders(contentResolver, 1, null);
        ArrayList<Long> idsToDelete = new ArrayList<>();
        List<SyncedFolder> syncedFolders = syncedFolderProvider.getSyncedFolders();
        long primaryKey;
        SyncedFolder newSyncedFolder;
        for (SyncedFolder syncedFolder : syncedFolders) {
            idsToDelete.add(syncedFolder.getId());
            Log_OC.i(TAG, "Migration check for synced_folders record: " + syncedFolder.getId() + " - " + syncedFolder.getLocalPath());
            for (int i = 0; i < imageMediaFolders.size(); i++) {
                if (imageMediaFolders.get(i).absolutePath.equals(syncedFolder.getLocalPath())) {
                    newSyncedFolder = (SyncedFolder) syncedFolder.clone();
                    newSyncedFolder.setType(MediaFolderType.IMAGE);
                    primaryKey = syncedFolderProvider.storeSyncedFolder(newSyncedFolder);
                    Log_OC.i(TAG, "Migrated image synced_folders record: " + primaryKey + " - " + newSyncedFolder.getLocalPath());
                    break;
                }
            }
            for (int j = 0; j < videoMediaFolders.size(); j++) {
                if (videoMediaFolders.get(j).absolutePath.equals(syncedFolder.getLocalPath())) {
                    newSyncedFolder = (SyncedFolder) syncedFolder.clone();
                    newSyncedFolder.setType(MediaFolderType.VIDEO);
                    primaryKey = syncedFolderProvider.storeSyncedFolder(newSyncedFolder);
                    Log_OC.i(TAG, "Migrated video synced_folders record: " + primaryKey + " - " + newSyncedFolder.getLocalPath());
                    break;
                }
            }
        }
        for (long id : idsToDelete) {
            Log_OC.i(TAG, "Removing legacy synced_folders record: " + id);
            syncedFolderProvider.deleteSyncedFolder(id);
        }
        PreferenceManager.setAutoUploadSplitEntries(context, true);
    }
}
Also used : Context(android.content.Context) SyncedFolder(com.owncloud.android.datamodel.SyncedFolder) ArrayList(java.util.ArrayList) SyncedFolderProvider(com.owncloud.android.datamodel.SyncedFolderProvider) MediaFolder(com.owncloud.android.datamodel.MediaFolder) StoragePoint(com.owncloud.android.datastorage.StoragePoint) SuppressLint(android.annotation.SuppressLint) ContentResolver(android.content.ContentResolver)

Aggregations

SyncedFolderProvider (com.owncloud.android.datamodel.SyncedFolderProvider)8 Context (android.content.Context)6 SyncedFolder (com.owncloud.android.datamodel.SyncedFolder)6 ContentResolver (android.content.ContentResolver)3 ArrayList (java.util.ArrayList)3 Account (android.accounts.Account)2 NonNull (android.support.annotation.NonNull)2 PersistableBundleCompat (com.evernote.android.job.util.support.PersistableBundleCompat)2 ArbitraryDataProvider (com.owncloud.android.datamodel.ArbitraryDataProvider)2 FilesystemDataProvider (com.owncloud.android.datamodel.FilesystemDataProvider)2 File (java.io.File)2 AccountManager (android.accounts.AccountManager)1 SuppressLint (android.annotation.SuppressLint)1 Resources (android.content.res.Resources)1 PowerManager (android.os.PowerManager)1 BottomNavigationView (android.support.design.widget.BottomNavigationView)1 ExifInterface (android.support.media.ExifInterface)1 Pair (android.support.v4.util.Pair)1 GridLayoutManager (android.support.v7.widget.GridLayoutManager)1 FileDataStorageManager (com.owncloud.android.datamodel.FileDataStorageManager)1