Search in sources :

Example 1 with Downloaded

use of im.actor.core.modules.file.entity.Downloaded in project actor-platform by actorapp.

the class DownloadManager method startDownload.

public void startDownload(FileReference fileReference) {
    if (LOG) {
        Log.d(TAG, "Starting download #" + fileReference.getFileId());
    }
    Downloaded downloaded1 = downloaded.getValue(fileReference.getFileId());
    if (downloaded1 != null) {
        // Already downloaded
        return;
    }
    QueueItem queueItem = findItem(fileReference.getFileId());
    if (queueItem == null) {
        if (LOG) {
            Log.d(TAG, "- Adding to queue");
        }
        queueItem = new QueueItem(fileReference);
        queueItem.isStopped = false;
        queue.add(0, queueItem);
    } else {
        if (LOG) {
            Log.d(TAG, "- Promoting in queue");
        }
        if (queueItem.isStopped) {
            queueItem.isStopped = false;
            for (final FileCallback callback : getSubscribers(fileReference.getFileId())) {
                im.actor.runtime.Runtime.dispatch(() -> callback.onDownloading(0));
            }
        }
        promote(fileReference.getFileId());
    }
    checkQueue();
}
Also used : FileCallback(im.actor.core.viewmodel.FileCallback) Downloaded(im.actor.core.modules.file.entity.Downloaded)

Example 2 with Downloaded

use of im.actor.core.modules.file.entity.Downloaded in project actor-platform by actorapp.

the class DownloadManager method bindDownload.

public void bindDownload(final FileReference fileReference, boolean autoStart, final FileCallback callback) {
    if (LOG) {
        Log.d(TAG, "Binding file #" + fileReference.getFileId());
    }
    Downloaded downloaded1 = downloaded.getValue(fileReference.getFileId());
    if (downloaded1 != null) {
        FileSystemReference reference = Storage.fileFromDescriptor(downloaded1.getDescriptor());
        boolean isExist = reference.isExist();
        int fileSize = reference.getSize();
        if (isExist && fileSize == downloaded1.getFileSize()) {
            if (LOG) {
                Log.d(TAG, "- Downloaded");
            }
            final FileSystemReference fileSystemReference = Storage.fileFromDescriptor(downloaded1.getDescriptor());
            im.actor.runtime.Runtime.dispatch(() -> callback.onDownloaded(fileSystemReference));
            return;
        } else {
            if (LOG) {
                Log.d(TAG, "- File is corrupted");
                if (!isExist) {
                    Log.d(TAG, "- File not found");
                }
                if (fileSize != downloaded1.getFileSize()) {
                    Log.d(TAG, "- Incorrect file size. Expected: " + downloaded1.getFileSize() + ", got: " + fileSize);
                }
            }
            downloaded.removeItem(downloaded1.getFileId());
        }
    }
    QueueItem queueItem = findItem(fileReference.getFileId());
    if (queueItem == null) {
        if (LOG) {
            Log.d(TAG, "- Adding to queue");
        }
        queueItem = new QueueItem(fileReference);
        if (autoStart) {
            queueItem.isStopped = false;
            im.actor.runtime.Runtime.dispatch(() -> callback.onDownloading(0));
        } else {
            queueItem.isStopped = true;
            im.actor.runtime.Runtime.dispatch(() -> callback.onNotDownloaded());
        }
        queue.add(0, queueItem);
    } else {
        Log.d(TAG, "- Promoting in queue");
        promote(fileReference.getFileId());
        if (queueItem.isStopped) {
            im.actor.runtime.Runtime.dispatch(() -> callback.onNotDownloaded());
        } else {
            if (queueItem.isStarted) {
                final float progress = queueItem.progress;
                im.actor.runtime.Runtime.dispatch(() -> callback.onDownloading(progress));
            } else {
                im.actor.runtime.Runtime.dispatch(() -> callback.onDownloading(0));
            }
        }
    }
    getSubscribers(fileReference.getFileId()).add(callback);
    checkQueue();
}
Also used : FileSystemReference(im.actor.runtime.files.FileSystemReference) Downloaded(im.actor.core.modules.file.entity.Downloaded)

Example 3 with Downloaded

use of im.actor.core.modules.file.entity.Downloaded in project actor-platform by actorapp.

the class UploadManager method onUploadTaskComplete.

public void onUploadTaskComplete(long rid, FileReference fileReference, FileSystemReference reference) {
    if (LOG) {
        Log.d(TAG, "Upload #" + rid + " complete");
    }
    QueueItem queueItem = findItem(rid);
    if (queueItem == null) {
        return;
    }
    if (!queueItem.isStarted) {
        return;
    }
    queue.remove(queueItem);
    queueItem.taskRef.send(PoisonPill.INSTANCE);
    // Saving reference to uploaded file
    context().getFilesModule().getDownloadedEngine().addOrUpdateItem(new Downloaded(fileReference.getFileId(), fileReference.getFileSize(), reference.getDescriptor()));
    ArrayList<UploadFileCallback> clist = callbacks.get(rid);
    if (clist != null) {
        for (final UploadFileCallback callback : clist) {
            im.actor.runtime.Runtime.dispatch(() -> callback.onUploaded());
        }
    }
    queueItem.requestActor.send(new UploadCompleted(rid, fileReference));
    checkQueue();
}
Also used : UploadFileCallback(im.actor.core.viewmodel.UploadFileCallback) Downloaded(im.actor.core.modules.file.entity.Downloaded)

Example 4 with Downloaded

use of im.actor.core.modules.file.entity.Downloaded in project actor-platform by actorapp.

the class DownloadManager method onDownloaded.

public void onDownloaded(final long fileId, final FileSystemReference reference) {
    if (LOG) {
        Log.d(TAG, "onDownloaded file #" + fileId);
    }
    QueueItem queueItem = findItem(fileId);
    if (queueItem == null) {
        return;
    }
    if (!queueItem.isStarted) {
        return;
    }
    downloaded.addOrUpdateItem(new Downloaded(queueItem.fileReference.getFileId(), queueItem.fileReference.getFileSize(), reference.getDescriptor()));
    queue.remove(queueItem);
    queueItem.taskRef.send(PoisonPill.INSTANCE);
    for (final WeakCallbackHolder weakReference : globalCallbacks) {
        final FileEventCallback callback = weakReference.getCallbackWeakReference().get();
        if (callback != null) {
            im.actor.runtime.Runtime.dispatch(() -> callback.onDownloaded(fileId));
        }
    }
    for (final FileCallback fileCallback : getSubscribers(fileId)) {
        im.actor.runtime.Runtime.dispatch(() -> fileCallback.onDownloaded(reference));
    }
    checkQueue();
}
Also used : FileCallback(im.actor.core.viewmodel.FileCallback) Downloaded(im.actor.core.modules.file.entity.Downloaded) FileEventCallback(im.actor.core.viewmodel.FileEventCallback)

Example 5 with Downloaded

use of im.actor.core.modules.file.entity.Downloaded in project actor-platform by actorapp.

the class DownloadManager method requestState.

// Tasks
public void requestState(long fileId, final FileCallback callback) {
    if (LOG) {
        Log.d(TAG, "Requesting state file #" + fileId);
    }
    Downloaded downloaded1 = downloaded.getValue(fileId);
    if (downloaded1 != null) {
        FileSystemReference reference = Storage.fileFromDescriptor(downloaded1.getDescriptor());
        boolean isExist = reference.isExist();
        int fileSize = reference.getSize();
        if (isExist && fileSize == downloaded1.getFileSize()) {
            if (LOG) {
                Log.d(TAG, "- Downloaded");
            }
            final FileSystemReference fileSystemReference = Storage.fileFromDescriptor(downloaded1.getDescriptor());
            im.actor.runtime.Runtime.dispatch(() -> callback.onDownloaded(fileSystemReference));
            return;
        } else {
            if (LOG) {
                Log.d(TAG, "- File is corrupted");
                if (!isExist) {
                    Log.d(TAG, "- File not found");
                }
                if (fileSize != downloaded1.getFileSize()) {
                    Log.d(TAG, "- Incorrect file size. Expected: " + downloaded1.getFileSize() + ", got: " + fileSize);
                }
            }
            downloaded.removeItem(downloaded1.getFileId());
        }
    }
    final QueueItem queueItem = findItem(fileId);
    if (queueItem == null) {
        im.actor.runtime.Runtime.dispatch(() -> callback.onNotDownloaded());
    } else {
        if (queueItem.isStarted) {
            final float progress = queueItem.progress;
            im.actor.runtime.Runtime.dispatch(() -> callback.onDownloading(progress));
        } else if (queueItem.isStopped) {
            im.actor.runtime.Runtime.dispatch(() -> callback.onNotDownloaded());
        } else {
            im.actor.runtime.Runtime.dispatch(() -> callback.onDownloading(0));
        }
    }
}
Also used : FileSystemReference(im.actor.runtime.files.FileSystemReference) Downloaded(im.actor.core.modules.file.entity.Downloaded)

Aggregations

Downloaded (im.actor.core.modules.file.entity.Downloaded)5 FileCallback (im.actor.core.viewmodel.FileCallback)2 FileSystemReference (im.actor.runtime.files.FileSystemReference)2 FileEventCallback (im.actor.core.viewmodel.FileEventCallback)1 UploadFileCallback (im.actor.core.viewmodel.UploadFileCallback)1