Search in sources :

Example 1 with UploadsProviderAccessor

use of com.trovebox.android.common.provider.UploadsProviderAccessor in project mobile-android by photo.

the class AbstractUploaderService method handleIntent.

private void handleIntent(Intent intent) {
    if (!GuiUtils.checkLoggedInAndOnline(true)) {
        return;
    }
    UploadsProviderAccessor uploads = new UploadsProviderAccessor(this);
    List<PhotoUpload> pendingUploads;
    synchronized (mIdsToSkip) {
        mIdsToSkip.clear();
        pendingUploads = uploads.getPendingUploads();
    }
    boolean hasSuccessfulUploads = false;
    NotificationCompat.Builder successNotification = getStandardSuccessNotification();
    NotificationCompat.Builder errorNotification = getStandardErrorNotification();
    int uploadedCount = 0;
    int skippedCount = 0;
    int successNotificationId = requestCounter++;
    int errorNotificationId = requestCounter++;
    ArrayList<Photo> uploadedPhotos = new ArrayList<Photo>();
    List<PhotoUploadDetails> uploadDetails = new ArrayList<PhotoUploadDetails>();
    ArrayList<PhotoUpload> errorUploads = new ArrayList<PhotoUpload>();
    List<PhotoUploadDetails> errorDetails = new ArrayList<PhotoUploadDetails>();
    for (final PhotoUpload photoUpload : pendingUploads) {
        if (!GuiUtils.checkLoggedInAndOnline(true)) {
            return;
        }
        Log.i(TAG, "Starting upload to Trovebox: " + photoUpload.getPhotoUri());
        if (isUploadRemoved(photoUpload)) {
            CommonUtils.info(TAG, "Upload skipped, because it was canceled externally");
            continue;
        }
        String filePath = ImageUtils.getRealPathFromURI(this, photoUpload.getPhotoUri());
        if (filePath == null || !(new File(filePath).exists())) {
            uploads.delete(photoUpload.getId());
            // TODO: Maybe set error, and set as "do not try again"
            CommonUtils.info(TAG, "Upload canceled, because file does not exist anymore.");
            UploaderServiceUtils.sendPhotoUploadRemovedBroadcast(photoUpload);
            continue;
        }
        boolean wifiOnlyUpload = CommonConfigurationUtils.isWiFiOnlyUploadActive();
        if (wifiOnlyUpload && !CommonUtils.isWiFiActive()) {
            CommonUtils.info(TAG, "Upload canceled because WiFi is not active anymore");
            break;
        }
        File file = new File(filePath);
        stopErrorNotification(file);
        try {
            boolean skipped = false;
            Photo photo = null;
            if (mCheckPhotoExistingOnServer) {
                String hash = SHA1Utils.computeSha1ForFile(filePath);
                PhotosResponse photos = mApi.getPhotos(hash);
                if (!photos.isSuccess()) {
                    uploads.setError(photoUpload.getId(), photos.getAlertMessage());
                    photoUpload.setError(photos.getAlertMessage());
                    addErrorDetailsAndUpdateErrorNotification(errorNotification, errorNotificationId, errorUploads, errorDetails, photoUpload, file);
                    if (!isUploadRemoved(photoUpload)) {
                        UploaderServiceUtils.sendPhotoUploadUpdatedBroadcast(photoUpload);
                    }
                    continue;
                }
                if (photos.getPhotos().size() > 0) {
                    CommonUtils.debug(TAG, "The photo " + filePath + " with hash " + hash + " already found on the server. Skip uploading");
                    skipped = true;
                    photo = photos.getPhotos().get(0);
                }
            }
            if (isUploadRemoved(photoUpload)) {
                CommonUtils.info(TAG, "Upload skipped, because it was canceled externally");
                continue;
            }
            if (photo == null) {
                long start = System.currentTimeMillis();
                final Notification notification = CommonUtils.isIceCreamSandwichOrHigher() ? null : showUploadNotification(file);
                final NotificationCompat.Builder builder = CommonUtils.isIceCreamSandwichOrHigher() ? getStandardUploadNotification(file) : null;
                UploadMetaData metaData = photoUpload.getMetaData();
                if (!isUploadRemoved(photoUpload)) {
                    UploaderServiceUtils.sendPhotoUploadUpdatedBroadcast(photoUpload, 0);
                }
                UploadResponse uploadResponse = mApi.uploadPhoto(file, metaData, photoUpload.getToken(), photoUpload.getHost(), new ProgressListener() {

                    private int mLastProgress = -1;

                    private boolean mCancelled;

                    @Override
                    public void transferred(long transferedBytes, long totalBytes) {
                        int newProgress = (int) (transferedBytes * 100 / totalBytes);
                        if (mLastProgress < newProgress) {
                            mLastProgress = newProgress;
                            if (mCancelled || isUploadRemoved(photoUpload)) {
                                if (!mCancelled) {
                                    CommonUtils.info(TAG, "Upload interrupted, because it was canceled externally");
                                    mCancelled = true;
                                // stopUploadNotification();
                                }
                            }
                            // else
                            {
                                if (!mCancelled) {
                                    UploaderServiceUtils.sendPhotoUploadUpdatedBroadcast(photoUpload, mLastProgress);
                                }
                                if (builder != null) {
                                    updateUploadNotification(builder, mLastProgress, 100);
                                } else {
                                    updateUploadNotification(notification, mLastProgress, 100);
                                }
                            }
                        }
                    }

                    @Override
                    public boolean isCancelled() {
                        // is detached.
                        return false;
                    }
                });
                // }
                if (uploadResponse.isSuccess()) {
                    Log.i(TAG, "Upload to Trovebox completed for: " + photoUpload.getPhotoUri());
                    photo = uploadResponse.getPhoto();
                    photo.setHost(photoUpload.getHost());
                    photo.setToken(photoUpload.getToken());
                    TrackerUtils.trackDataLoadTiming(System.currentTimeMillis() - start, "photoUpload", TAG);
                } else {
                    uploads.setError(photoUpload.getId(), uploadResponse.getAlertMessage());
                    photoUpload.setError(uploadResponse.getAlertMessage());
                    addErrorDetailsAndUpdateErrorNotification(errorNotification, errorNotificationId, errorUploads, errorDetails, photoUpload, file);
                    stopUploadNotification();
                    if (!isUploadRemoved(photoUpload)) {
                        UploaderServiceUtils.sendPhotoUploadUpdatedBroadcast(photoUpload);
                    }
                    continue;
                }
            }
            adjustRemainingUploadingLimit(-1);
            hasSuccessfulUploads = true;
            long uploaded = uploads.setUploaded(photoUpload.getId());
            photoUpload.setUploaded(uploaded);
            if (!isUploadRemoved(photoUpload)) {
                UploaderServiceUtils.sendPhotoUploadUpdatedBroadcast(photoUpload);
            }
            if (skipped) {
                skippedCount++;
            } else {
                uploadedCount++;
            }
            uploadedPhotos.add(photo);
            uploadDetails.add(new PhotoUploadDetails(photoUpload, skipped, file));
            updateSuccessNotification(successNotification, uploadedCount, skippedCount, uploadedPhotos, uploadDetails, successNotificationId);
            shareIfRequested(photoUpload, photo, true);
            if (!skipped) {
                TrackerUtils.trackServiceEvent("photo_upload", TAG);
                if (!isUploadRemoved(photoUpload)) {
                    UploaderServiceUtils.sendPhotoUploadedBroadcast();
                }
                mUploadCounters.increment(photoUpload.getToken(), photoUpload.getUserName(), photoUpload.getHost());
            } else {
                TrackerUtils.trackServiceEvent("photo_upload_skip", TAG);
            }
        } catch (Exception e) {
            CommonUtils.error(TAG, e);
            uploads.setError(photoUpload.getId(), e.getClass().getSimpleName() + ": " + e.getLocalizedMessage());
            photoUpload.setError(e.getClass().getSimpleName() + ": " + e.getLocalizedMessage());
            addErrorDetailsAndUpdateErrorNotification(errorNotification, errorNotificationId, errorUploads, errorDetails, photoUpload, file);
            if (!isUploadRemoved(photoUpload)) {
                UploaderServiceUtils.sendPhotoUploadUpdatedBroadcast(photoUpload);
            }
        }
        stopUploadNotification();
    }
    mUploadCounters.notifyServer(mApi);
    // update limit information only in case we had successful uploads
    if (hasSuccessfulUploads) {
        runAfterSuccessfullUploads();
    }
}
Also used : ArrayList(java.util.ArrayList) Photo(com.trovebox.android.common.model.Photo) UploadMetaData(com.trovebox.android.common.net.UploadMetaData) PhotoUpload(com.trovebox.android.common.provider.PhotoUpload) Notification(android.app.Notification) UploadResponse(com.trovebox.android.common.net.UploadResponse) ProgressListener(com.trovebox.android.common.net.HttpEntityWithProgress.ProgressListener) NotificationCompat(android.support.v4.app.NotificationCompat) PhotosResponse(com.trovebox.android.common.net.PhotosResponse) UploadsProviderAccessor(com.trovebox.android.common.provider.UploadsProviderAccessor) File(java.io.File)

Example 2 with UploadsProviderAccessor

use of com.trovebox.android.common.provider.UploadsProviderAccessor in project mobile-android by photo.

the class NewPhotoObserver method onEvent.

@Override
public void onEvent(int event, String fileName) {
    if (event == FileObserver.CREATE && !fileName.equals(".probe")) {
        File file = new File(mPath + "/" + fileName);
        CommonUtils.debug(TAG, "File created [" + file.getAbsolutePath() + "]");
        // fix for the issue #309
        String type = getMimeType(file);
        if (type != null && type.toLowerCase().startsWith("image/")) {
            TrackerUtils.trackBackgroundEvent("autoupload_observer", CommonUtils.format("Processed for Mime-Type: %1$s", type));
            if (!Preferences.isAutoUploadActive(mContext) || !GuiUtils.checkLoggedIn(true) || Preferences.isLimitedAccountAccessType()) {
                return;
            }
            if (checkLimits()) {
                TrackerUtils.trackLimitEvent("auto_upload_limit_check", "success");
                CommonUtils.debug(TAG, "Adding new autoupload to queue for file: " + fileName);
                UploadsProviderAccessor uploads = new UploadsProviderAccessor(mContext);
                UploadMetaData metaData = new UploadMetaData();
                metaData.setTags(Preferences.getAutoUploadTag(mContext));
                uploads.addPendingAutoUpload(Uri.fromFile(file), metaData);
                mContext.startService(new Intent(mContext, UploaderService.class));
            } else {
                TrackerUtils.trackLimitEvent("autoupload_observer", "fail");
                showAutouploadIgnoredNotification(file);
            }
        } else {
            TrackerUtils.trackBackgroundEvent("autoupload_observer", CommonUtils.format("Skipped for Mime-Type: %1$s", type == null ? "null" : type));
        }
    }
}
Also used : UploadMetaData(com.trovebox.android.common.net.UploadMetaData) Intent(android.content.Intent) UploadsProviderAccessor(com.trovebox.android.common.provider.UploadsProviderAccessor) File(java.io.File)

Example 3 with UploadsProviderAccessor

use of com.trovebox.android.common.provider.UploadsProviderAccessor in project mobile-android by photo.

the class NewPhotoObserver method checkLimits.

/**
     * Check upload limits to determine whether autoupload could be done
     * @return
     */
private boolean checkLimits() {
    AccountLimitUtils2.updateLimitInformationCacheIfNecessary(true);
    if (Preferences.isSelfHosted() || Preferences.isProUser()) {
        return true;
    } else {
        int remaining = Preferences.getRemainingUploadingLimit();
        UploadsProviderAccessor uploads = new UploadsProviderAccessor(TroveboxApplication.getContext());
        int pending = uploads.getPendingUploadsCount();
        boolean result = remaining - pending >= 1;
        return result;
    }
}
Also used : UploadsProviderAccessor(com.trovebox.android.common.provider.UploadsProviderAccessor)

Aggregations

UploadsProviderAccessor (com.trovebox.android.common.provider.UploadsProviderAccessor)3 UploadMetaData (com.trovebox.android.common.net.UploadMetaData)2 File (java.io.File)2 Notification (android.app.Notification)1 Intent (android.content.Intent)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 Photo (com.trovebox.android.common.model.Photo)1 ProgressListener (com.trovebox.android.common.net.HttpEntityWithProgress.ProgressListener)1 PhotosResponse (com.trovebox.android.common.net.PhotosResponse)1 UploadResponse (com.trovebox.android.common.net.UploadResponse)1 PhotoUpload (com.trovebox.android.common.provider.PhotoUpload)1 ArrayList (java.util.ArrayList)1