/*
ApicAday - Everyday.. is different, your mood, your life.
Copyright (c) 2010
Oliver Selinger <oliver.selinger@autburst.com>,
Michael Greifeneder <michael.greifeneder@autburst.com>
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 <http://www.gnu.org/licenses/>.
*/
package com.autburst.picture;
import java.io.File;
import java.util.Arrays;
import java.util.List;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Resources;
import android.os.Message;
import android.util.Log;
import com.autburst.picture.server.PictureServer;
public class Uploader implements Runnable {
private static final String TAG = Uploader.class.getSimpleName();
public static final int NOTE_ID = 32482;
//private Handler handler;
private SharedPreferences preferences;
private String albumName;
private NotificationManager mgr;
private Context ctx;
public Uploader (Context ctx, NotificationManager mgr, String albumName, SharedPreferences preferences) {
this.ctx = ctx;
this.mgr = mgr;
this.preferences = preferences;
this.albumName = albumName;
Log.d(TAG, "Uploader created!");
}
public void run() {
Resources res = ctx.getResources();
Notification note = new Notification(R.drawable.launch, res.getString(R.string.transfer_pictures), System.currentTimeMillis());
PendingIntent pi = PendingIntent.getActivity(ctx, 0, new Intent(ctx, FinishedUploadActivity.class), 0);
Log.d(TAG, "start synchronizing...");
note.setLatestEventInfo(ctx, res.getString(R.string.connecting_to_server), "", pi);
mgr.notify(albumName, NOTE_ID, note);
final PictureServer server = new PictureServer();
float frameRate = preferences.getFloat(albumName + ".frameRate", Utilities.MEDIUM_FRAME_RATE);
try {
//check if unique id already exist
String id = preferences.getString(albumName + ".id", null);
if(id == null) {
//request id
String newId = server.getUniqueId();
Editor editor = preferences.edit();
editor.putString(albumName + ".id", newId);
editor.commit();
id = newId;
} else
Log.d(TAG, id);
File albumDir = Utilities.getAlbumDirectory(albumName);
//get files of server and of mobile device
String[] filesOnMobileDevice = albumDir.list();
String[] filesOnServer = server.getImageList(id);
Arrays.sort(filesOnMobileDevice);
Arrays.sort(filesOnServer);
List<String> filesOnServerAsList = Arrays.asList(filesOnServer);
List<String> filesOnMobileDeviceAsList = Arrays.asList(filesOnMobileDevice);
for (int i = 0; i < filesOnServer.length; i++) {
Log.d(TAG, filesOnServer[i]);
}
Log.d(TAG, "############");
for (int i = 0; i < filesOnMobileDevice.length; i++) {
Log.d(TAG, filesOnMobileDevice[i]);
}
//compare lists
boolean arraysEqual = Arrays.equals(filesOnMobileDevice, filesOnServer);
if(!arraysEqual) {
//upload new ones
int amount = 0;
for (int i = 0; i < filesOnMobileDevice.length; i++) {
String fileOnMobilDevice = filesOnMobileDevice[i];
if(!filesOnServerAsList.contains(fileOnMobilDevice)) {
amount ++;
}
}
int count = 1;
for (int i = 0; i < filesOnMobileDevice.length; i++) {
String fileOnMobilDevice = filesOnMobileDevice[i];
if(!filesOnServerAsList.contains(fileOnMobilDevice)) {
//upload
note.setLatestEventInfo(ctx,
String.format(res.getString(R.string.transfer_picture),count, amount),
fileOnMobilDevice, pi);
mgr.notify(albumName, NOTE_ID, note);
server.postImages(id, new File(albumDir, fileOnMobilDevice));
Log.d(TAG, "Upload Image: " + albumDir.getAbsolutePath() + "/" + fileOnMobilDevice);
count ++;
}
}
//delete images which were deleted on mobile device
for (int i = 0; i < filesOnServer.length; i++) {
if(!filesOnMobileDeviceAsList.contains(filesOnServer[i])) {
//image does not exist anymore on mobile device -> delete on server
Log.d(TAG, "Delete Image: " + filesOnServer[i]);
server.deleteImage(id, filesOnServer[i]);
}
}
}
String savedVideoId = preferences.getString(albumName + ".videoId", null);
Log.d(TAG, "savedVideoId: " + savedVideoId + " arraysEqual: " + arraysEqual);
//create video and get video URL only if no URL is available or arrays were not equal
// if(savedVideoId == null)
// savedVideoId = retrieveAndSetVideoUrl(server, id, albumName, frameRate, getFormat(albumName));
// else if(!arraysEqual)
savedVideoId = retrieveAndSetVideoUrl(server, id, albumName, frameRate, getFormat(albumName));
//return URL
Message finishUploadMessage = Message.obtain();
finishUploadMessage.what = Utilities.MSG_FINISHED_UPLOADING;
finishUploadMessage.obj = savedVideoId;
//handler.sendMessage(finishUploadMessage);
note.setLatestEventInfo(ctx, res.getString(R.string.transfer_finished), "", pi);
mgr.notify(albumName, NOTE_ID, note);
Intent i = new Intent(FinishedUploadActivity.UPLOAD_FINISHED);
i.putExtra(UploadService.ALBUM_NAME, albumName);
ctx.sendBroadcast(i);
} catch (Exception e) {
Log.e(TAG, "Uploader error!", e);
//send error msg
Message errorMsg = Message.obtain();
errorMsg.what = Utilities.MSG_ERROR_UPLOADING;
errorMsg.obj = e.getMessage();
PendingIntent exPi = PendingIntent.getActivity(ctx, 0, new Intent(ctx, GalleryActivity.class),0);
note.setLatestEventInfo(ctx, res.getString(R.string.transfer_error), e.getMessage(), exPi);
mgr.notify(albumName, NOTE_ID, note);
//TODO: send intent!
//handler.sendMessage(errorMsg);
}
}
private String getFormat(String albumName) {
boolean isPortrait = preferences.getBoolean(albumName + ".portrait", true);
if(isPortrait)
return Utilities.PORTRAIT;
else
return Utilities.LANDSCAPE;
}
private String retrieveAndSetVideoUrl(PictureServer server, String id, String albumName, float frameRate, String format) throws Exception {
String newVideoId = server.getVideoUrl(id, albumName, frameRate, format);
Editor editor = preferences.edit();
editor.putString(albumName + ".videoId", newVideoId);
editor.commit();
Log.d(TAG, "Video Id: " + newVideoId);
return newVideoId;
}
}