Search in sources :

Example 1 with UploadMetaData

use of com.trovebox.android.common.net.UploadMetaData in project mobile-android by photo.

the class TroveboxApiTest method testPhotoUpload.

public void testPhotoUpload() throws Exception {
    File file = createTestFileForUpload();
    UploadMetaData settings = new UploadMetaData();
    settings.setTitle("Android");
    settings.setDescription("Nice picture of an android");
    settings.setTags("test");
    Map<String, String> albums = new HashMap<String, String>();
    albums.put("1", "test");
    albums.put("2", "test2");
    settings.setAlbums(albums);
    settings.setPrivate(false);
    try {
        UploadResponse resp = mApi.uploadPhoto(file, settings, null);
        assertTrue(resp.isSuccess());
        assertNotNull(resp.getPhoto());
        try {
            assertTrue(resp.getPhoto().getTags().size() >= 1);
            // assertEquals("test", resp.getPhoto().getTags().get(0));
            assertEquals("Android", resp.getPhoto().getTitle());
            assertEquals("Nice picture of an android", resp.getPhoto().getDescription());
            assertFalse(resp.getPhoto().isPrivate());
        } finally {
            // remove uploaded photo
            mApi.deletePhoto(resp.getPhoto().getId());
        }
    } catch (Exception e) {
        fail("Exception should not happen: " + e.getClass().getSimpleName() + " - " + e.getMessage());
    }
    file.delete();
}
Also used : UploadResponse(com.trovebox.android.common.net.UploadResponse) HashMap(java.util.HashMap) UploadMetaData(com.trovebox.android.common.net.UploadMetaData) File(java.io.File) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Example 2 with UploadMetaData

use of com.trovebox.android.common.net.UploadMetaData in project mobile-android by photo.

the class UploadMetaDataTest method getTestData.

public static UploadMetaData getTestData() {
    UploadMetaData umd = new UploadMetaData();
    umd.setTitle(sTitle);
    umd.setDescription(sDescription);
    umd.setTags(sTags);
    umd.setPermission(sPermission);
    umd.setAlbums(sAlbums);
    return umd;
}
Also used : UploadMetaData(com.trovebox.android.common.net.UploadMetaData)

Example 3 with UploadMetaData

use of com.trovebox.android.common.net.UploadMetaData 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 4 with UploadMetaData

use of com.trovebox.android.common.net.UploadMetaData in project mobile-android by photo.

the class UploadsProviderAccessor method extractPhotoUpload.

private PhotoUpload extractPhotoUpload(Cursor cursor) {
    try {
        long id = cursor.getInt(UploadsProvider.ID_COLUMN);
        Uri photoUri = Uri.parse(cursor.getString(UploadsProvider.URI_COLUMN));
        UploadMetaData metaData = new UploadMetaData();
        String metaJson = cursor.getString(UploadsProvider.METADATA_JSON_COLUMN);
        if (metaJson != null) {
            JSONObject jsonMeta = new JSONObject(metaJson);
            if (jsonMeta.has(JSON_TAGS)) {
                metaData.setTags(jsonMeta.optString(JSON_TAGS));
            }
            if (jsonMeta.has(JSON_ALBUMS)) {
                JSONObject jsonAlbums = jsonMeta.optJSONObject(JSON_ALBUMS);
                if (jsonAlbums != null) {
                    Iterator<?> albumIdsIterator = jsonAlbums.keys();
                    Map<String, String> albums = new HashMap<String, String>();
                    while (albumIdsIterator.hasNext()) {
                        String albumId = (String) albumIdsIterator.next();
                        albums.put(albumId, jsonAlbums.optString(albumId));
                    }
                    metaData.setAlbums(albums);
                }
            }
            if (jsonMeta.has(JSON_TITLE)) {
                metaData.setTitle(jsonMeta.optString(JSON_TITLE));
            }
            if (jsonMeta.has(JSON_DESCRIPTION)) {
                metaData.setDescription(jsonMeta.optString(JSON_DESCRIPTION));
            }
            if (jsonMeta.has(JSON_PERMISSION)) {
                metaData.setPermission(jsonMeta.optInt(JSON_PERMISSION));
            }
        // TODO get other meta data like title, description,
        // location?
        }
        PhotoUpload pendingUpload = new PhotoUpload(id, photoUri, metaData);
        pendingUpload.setError(cursor.getString(UploadsProvider.ERROR_COLUMN));
        pendingUpload.setHost(cursor.getString(UploadsProvider.HOST_COLUMN));
        pendingUpload.setToken(cursor.getString(UploadsProvider.TOKEN_COLUMN));
        pendingUpload.setUserName(cursor.getString(UploadsProvider.USER_NAME_COLUMN));
        pendingUpload.setUploaded(cursor.getLong(UploadsProvider.UPLOADED_COLUMN));
        pendingUpload.setIsAutoUpload(cursor.getInt(UploadsProvider.IS_AUTOUPLOAD_COLUMN) != 0);
        pendingUpload.setShareOnFacebook(cursor.getInt(UploadsProvider.SHARE_ON_FACEBOOK_COLUMN) != 0);
        pendingUpload.setShareOnTwitter(cursor.getInt(UploadsProvider.SHARE_ON_TWITTER_COLUMN) != 0);
        return pendingUpload;
    } catch (Exception e) {
        GuiUtils.noAlertError(TAG, "Could not get pending upload", e);
        return null;
    }
}
Also used : JSONObject(org.json.JSONObject) HashMap(java.util.HashMap) UploadMetaData(com.trovebox.android.common.net.UploadMetaData) Uri(android.net.Uri) JSONException(org.json.JSONException)

Example 5 with UploadMetaData

use of com.trovebox.android.common.net.UploadMetaData in project mobile-android by photo.

the class TroveboxApiTest method testPhotoUploadToken.

public void testPhotoUploadToken() throws Exception {
    File file = createTestFileForUpload();
    UploadMetaData settings = new UploadMetaData();
    settings.setTags("uploadedBy: Test");
    settings.setPrivate(true);
    try {
        UploadResponse resp = mApi.uploadPhoto(file, settings, "8f890593c5", "http://test.trovebox.com", null);
        assertTrue(resp.isSuccess());
        assertNotNull(resp.getPhoto());
        try {
            assertTrue(resp.getPhoto().getTags().size() >= 1);
            assertTrue(resp.getPhoto().isPrivate());
        } finally {
        // remove uploaded photo
        // mApi.deletePhoto(resp.getPhoto().getId());
        }
    } catch (Exception e) {
        CommonUtils.error(TroveboxApiTest.class.getSimpleName(), e);
        fail("Exception should not happen: " + e.getClass().getSimpleName() + " - " + e.getMessage());
    }
    file.delete();
}
Also used : UploadResponse(com.trovebox.android.common.net.UploadResponse) UploadMetaData(com.trovebox.android.common.net.UploadMetaData) File(java.io.File) ClientProtocolException(org.apache.http.client.ClientProtocolException) JSONException(org.json.JSONException) IOException(java.io.IOException)

Aggregations

UploadMetaData (com.trovebox.android.common.net.UploadMetaData)9 File (java.io.File)6 UploadResponse (com.trovebox.android.common.net.UploadResponse)5 JSONException (org.json.JSONException)5 IOException (java.io.IOException)4 ClientProtocolException (org.apache.http.client.ClientProtocolException)4 Photo (com.trovebox.android.common.model.Photo)3 PhotosResponse (com.trovebox.android.common.net.PhotosResponse)2 UploadsProviderAccessor (com.trovebox.android.common.provider.UploadsProviderAccessor)2 ArrayList (java.util.ArrayList)2 HashMap (java.util.HashMap)2 Notification (android.app.Notification)1 Intent (android.content.Intent)1 Uri (android.net.Uri)1 Parcel (android.os.Parcel)1 NotificationCompat (android.support.v4.app.NotificationCompat)1 Token (com.trovebox.android.common.model.Token)1 ProgressListener (com.trovebox.android.common.net.HttpEntityWithProgress.ProgressListener)1 PhotoResponse (com.trovebox.android.common.net.PhotoResponse)1 TokenResponse (com.trovebox.android.common.net.TokenResponse)1