Update Progress in notification:
public class NetworkService extends IntentService {
public static final String FILE_PATH = "file_path";
public static final String _FILE_PATH = "_path";
public static final String PERCENT_EXTRA = "percent";
public static final String FILE_NAME_EXTRA = "FileName";
public static final String STATE_EXTRA = "state";
public static final String IS_ALBUM_UPLAODING = "IsAlbumUplaoding";
public static final String XML = "xml";
public static final String TOKEN = "token";
public static final String MESSAGE = "mesasge";
public static final String POST_DATE = "postdate";
public static final String USERID = "userid";
public static final String GROUPID = "groupid";
public static final String PATH = "path";
public static final String FOLDER = "folder";
public static final String XML_RESPONSE = "response";
public static final String UPLOAD_STATE_CHANGED_ACTION = "com.package.UPLOAD_STATE_CHANGED_ACTION";
public static final String UPLOAD_CANCELLED_ACTION = "com.package.UPLOAD_CANCELLED_ACTION";
//
private NotificationManager notificationManager;
private Builder notification;
// private PowerManager.WakeLock wakeLock;
private static final String TAG = "UploadProcess";
private int lastPublishedProgress;
private static final int UPLOAD_NOTIFICATION_ID = 1234; // Something unique
private static final int UPLOAD_NOTIFICATION_ID_DONE = 1235; // Something
// unique
public static String NAMESPACE = "com.package";
private static final String BROADCAST_ACTION_SUFFIX = ".uploadservice.broadcast.status";
public static final String UPLOAD_ID = "id";
public static final String STATUS = "status";
public static final int STATUS_IN_PROGRESS = 1;
public static final int STATUS_COMPLETED = 2;
public static final int STATUS_ERROR = 3;
public static final String PROGRESS = "progress";
public static final String ERROR_EXCEPTION = "errorException";
public static final String SERVER_RESPONSE_CODE = "serverResponseCode";
public static final String SERVER_RESPONSE_MESSAGE = "serverResponseMessage";
private String UpoloadedId = "33";
public static long TOTAL_BYTES = 0;
public static long UPLOADED_BYTES = 0;
public static boolean UPLOADING_FROM;
public static String UPLOADED_BYTES_TAG;
public static String getActionBroadcast() {
return NAMESPACE + BROADCAST_ACTION_SUFFIX;
}
private Manager mManager;
// private NotificationManager nm;
// private static final int NOTIFY_ID_UPLOAD = 1337;
public NetworkService() {
super("huudle_file_upload_service");
}
@Override
public void onCreate() {
super.onCreate();
TOTAL_BYTES = 0;
UPLOADED_BYTES = 0;
mManager = new Manager(getApplicationContext());
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification = new NotificationCompat.Builder(this);
}
@Override
protected void onHandleIntent(Intent intent) {
lastPublishedProgress = 0;
String filePath = intent.getStringExtra(FILE_PATH);
String fileName = intent.getStringExtra(FILE_NAME_EXTRA);
String Path = intent
.getStringExtra(HuudleConstants.AMAZON_FOLDER_NAME);
//
String xml = intent.getStringExtra(XML);
String token = intent.getStringExtra(TOKEN);
String message = intent.getStringExtra(MESSAGE);
String date = intent.getStringExtra(POST_DATE);
String userid = intent.getStringExtra(USERID);
String groupid = intent.getStringExtra(GROUPID);
String path = intent.getStringExtra(PATH);
String folder = intent.getStringExtra(FOLDER);
UPLOADING_FROM = intent.getBooleanExtra(IS_ALBUM_UPLAODING, false);
File fileToUpload = new File(filePath);
if (fileToUpload.exists()) {
TOTAL_BYTES = fileToUpload.length();
Log.e("@@@@", "uploading total bytes=" + TOTAL_BYTES);
mManager.uploadFile(filePath,Path, fileName, UPLOADING_FROM,
onUploadListener);
if (UPLOADING_FROM) {
mManager.CallService(xml, token, message, date, userid,
groupid, path, folder);
createNotification();
}
}
}
private OnUploadListener onUploadListener = new OnUploadListener() {
@Override
public void uploadProgress(UPLOAD_STATE uState, long uploaded) {
broadcastState(uState, uploaded);
}
};
private void broadcastState(UPLOAD_STATE state, long uploaded) {
Intent intent = new Intent(UPLOAD_STATE_CHANGED_ACTION);
Bundle b = new Bundle();
b.putString(FILE_NAME_EXTRA, mManager.getFileName());
b.putString(XML_RESPONSE, mManager.getResponse());
b.putSerializable(STATE_EXTRA, state);
b.putLong(UPLOADED_BYTES_TAG, uploaded);
intent.putExtras(b);
sendBroadcast(intent);
if (UPLOADING_FROM) {
UPLOADED_BYTES = UPLOADED_BYTES + uploaded;
Log.e("@@@@", "uploading uploaded bytes=" + uploaded);
Log.e("@@@@", "uploading UPLOADED_BYTES bytes=" + UPLOADED_BYTES);
switch (state) {
case PREPARING:
Log.e("@@@@", "IntentService PREPARING");
broadcastProgress(UpoloadedId, UPLOADED_BYTES, TOTAL_BYTES);
break;
case STARTED:
Log.e("@@@@", "IntentService STARTED");
broadcastProgress(UpoloadedId, UPLOADED_BYTES, TOTAL_BYTES);
break;
case UPLOADING:
Log.e("@@@@", "IntentService UPLOADING");
broadcastProgress(UpoloadedId, UPLOADED_BYTES, TOTAL_BYTES);
break;
case COMPLETED:
Log.e("@@@@", "IntentService COMPLETED");
broadcastCompleted(UpoloadedId, 250, "Completed");
break;
case FAILED:
Log.e("@@@@", "IntentService FAILED");
broadcastError(UpoloadedId, null);
break;
case CANCLED:
Log.e("@@@@", "IntentService CANCLED");
break;
default:
break;
}
}
}
private void broadcastProgress(final String uploadId,
final long uploadedBytes, final long totalBytes) {
final int progress = (int) (uploadedBytes * 100 / totalBytes);
if (progress <= lastPublishedProgress)
return;
lastPublishedProgress = progress;
updateNotificationProgress(progress);
final Intent intent = new Intent(getActionBroadcast());
intent.putExtra(UPLOAD_ID, uploadId);
intent.putExtra(STATUS, STATUS_IN_PROGRESS);
intent.putExtra(PROGRESS, progress);
sendBroadcast(intent);
}
private void broadcastCompleted(final String uploadId,
final int responseCode, final String responseMessage) {
final String filteredMessage;
if (responseMessage == null) {
filteredMessage = "";
} else {
filteredMessage = responseMessage;
}
if (responseCode >= 200 && responseCode <= 299)
updateNotificationCompleted();
else
updateNotificationError();
final Intent intent = new Intent(getActionBroadcast());
intent.putExtra(UPLOAD_ID, uploadId);
intent.putExtra(STATUS, STATUS_COMPLETED);
intent.putExtra(SERVER_RESPONSE_CODE, responseCode);
intent.putExtra(SERVER_RESPONSE_MESSAGE, filteredMessage);
sendBroadcast(intent);
}
private void broadcastError(final String uploadId, final Exception exception) {
updateNotificationError();
final Intent intent = new Intent(getActionBroadcast());
intent.setAction(getActionBroadcast());
intent.putExtra(UPLOAD_ID, uploadId);
intent.putExtra(STATUS, STATUS_ERROR);
intent.putExtra(ERROR_EXCEPTION, exception);
sendBroadcast(intent);
}
private void createNotification() {
Log.e("@@@@", "Notification");
notification
.setContentTitle("Uploading")
.setContentIntent(
PendingIntent.getBroadcast(this, 0, new Intent(),
PendingIntent.FLAG_UPDATE_CURRENT))
.setSmallIcon(R.drawable.app_icon).setProgress(100, 0, true)
.setOngoing(true);
Notification n = notification.build();
notificationManager.notify(UPLOAD_NOTIFICATION_ID, n);
// startForeground(UPLOAD_NOTIFICATION_ID, notification.build());
}
private void updateNotificationProgress(final int progress) {
notification.setContentTitle("Uploading")
.setSmallIcon(R.drawable.app_icon)
.setProgress(100, progress, false).setOngoing(true);
Notification n = notification.build();
notificationManager.notify(UPLOAD_NOTIFICATION_ID, n);
// startForeground(UPLOAD_NOTIFICATION_ID, notification.build());
}
private void updateNotificationCompleted() {
notificationManager.cancel(UPLOAD_NOTIFICATION_ID);
// stopForeground(true);
notification.setContentTitle("Uploading").setContentText("Completed")
.setSmallIcon(R.drawable.app_icon).setProgress(0, 0, false)
.setOngoing(false);
Notification n = notification.build();
notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE, n);
// notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE,
// notification.build());
}
private void updateNotificationError() {
stopForeground(false);
notificationManager.cancel(UPLOAD_NOTIFICATION_ID);
notification.setContentTitle("Uploading")
.setContentText("Failed to upload.")
.setSmallIcon(R.drawable.app_icon).setProgress(0, 0, false)
.setOngoing(false);
Notification n = notification.build();
notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE, n);
// notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE,
// notification.build());
}
@Override
public void onDestroy() {
// nm.cancel(NOTIFY_ID_UPLOAD);
super.onDestroy();
}
}
Manager.java
public class Manager {
public enum UPLOAD_STATE {
PREPARING, STARTED, UPLOADING, COMPLETED, FAILED, CANCLED }
public interface OnUploadListener {
public void uploadProgress(UPLOAD_STATE uState, long uploaded);
}
public Manager(Context mContext) {
this.mContext = mContext;
}
private class UploadTask extends AsyncTask<Void, Void, Void> {
private File file;
private String FilePath;
private OnUploadListener onUploadListener;
private String filename;
boolean UPLOADING_FROM;
public UploadTask(Context context, File file, String FilePath,
String filename, boolean UPLOADING_FROM,
OnUploadListener onUploadListener) {
this.file = file;
this.FilePath = FilePath;
this.onUploadListener = onUploadListener;
this.filename = filename;
this.UPLOADING_FROM = UPLOADING_FROM;
mContext = context;
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
mState = UPLOAD_STATE.PREPARING;
if (onUploadListener != null)
onUploadListener.uploadProgress(mState, UPLOADED_BYTES);
upload(file, FilePath, filename, UPLOADING_FROM, onUploadListener);
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
// progressDialog.dismiss();
}
}
public String getFileName() {
return FileName;
}
//invoke the interface method based on progress status
onUploadListener.uploadProgress(mState, UPLOADED_BYTES);
public class NetworkService extends IntentService {
public static final String FILE_PATH = "file_path";
public static final String _FILE_PATH = "_path";
public static final String PERCENT_EXTRA = "percent";
public static final String FILE_NAME_EXTRA = "FileName";
public static final String STATE_EXTRA = "state";
public static final String IS_ALBUM_UPLAODING = "IsAlbumUplaoding";
public static final String XML = "xml";
public static final String TOKEN = "token";
public static final String MESSAGE = "mesasge";
public static final String POST_DATE = "postdate";
public static final String USERID = "userid";
public static final String GROUPID = "groupid";
public static final String PATH = "path";
public static final String FOLDER = "folder";
public static final String XML_RESPONSE = "response";
public static final String UPLOAD_STATE_CHANGED_ACTION = "com.package.UPLOAD_STATE_CHANGED_ACTION";
public static final String UPLOAD_CANCELLED_ACTION = "com.package.UPLOAD_CANCELLED_ACTION";
//
private NotificationManager notificationManager;
private Builder notification;
// private PowerManager.WakeLock wakeLock;
private static final String TAG = "UploadProcess";
private int lastPublishedProgress;
private static final int UPLOAD_NOTIFICATION_ID = 1234; // Something unique
private static final int UPLOAD_NOTIFICATION_ID_DONE = 1235; // Something
// unique
public static String NAMESPACE = "com.package";
private static final String BROADCAST_ACTION_SUFFIX = ".uploadservice.broadcast.status";
public static final String UPLOAD_ID = "id";
public static final String STATUS = "status";
public static final int STATUS_IN_PROGRESS = 1;
public static final int STATUS_COMPLETED = 2;
public static final int STATUS_ERROR = 3;
public static final String PROGRESS = "progress";
public static final String ERROR_EXCEPTION = "errorException";
public static final String SERVER_RESPONSE_CODE = "serverResponseCode";
public static final String SERVER_RESPONSE_MESSAGE = "serverResponseMessage";
private String UpoloadedId = "33";
public static long TOTAL_BYTES = 0;
public static long UPLOADED_BYTES = 0;
public static boolean UPLOADING_FROM;
public static String UPLOADED_BYTES_TAG;
public static String getActionBroadcast() {
return NAMESPACE + BROADCAST_ACTION_SUFFIX;
}
private Manager mManager;
// private NotificationManager nm;
// private static final int NOTIFY_ID_UPLOAD = 1337;
public NetworkService() {
super("huudle_file_upload_service");
}
@Override
public void onCreate() {
super.onCreate();
TOTAL_BYTES = 0;
UPLOADED_BYTES = 0;
mManager = new Manager(getApplicationContext());
notificationManager = (NotificationManager) getSystemService(NOTIFICATION_SERVICE);
notification = new NotificationCompat.Builder(this);
}
@Override
protected void onHandleIntent(Intent intent) {
lastPublishedProgress = 0;
String filePath = intent.getStringExtra(FILE_PATH);
String fileName = intent.getStringExtra(FILE_NAME_EXTRA);
String Path = intent
.getStringExtra(HuudleConstants.AMAZON_FOLDER_NAME);
//
String xml = intent.getStringExtra(XML);
String token = intent.getStringExtra(TOKEN);
String message = intent.getStringExtra(MESSAGE);
String date = intent.getStringExtra(POST_DATE);
String userid = intent.getStringExtra(USERID);
String groupid = intent.getStringExtra(GROUPID);
String path = intent.getStringExtra(PATH);
String folder = intent.getStringExtra(FOLDER);
UPLOADING_FROM = intent.getBooleanExtra(IS_ALBUM_UPLAODING, false);
File fileToUpload = new File(filePath);
if (fileToUpload.exists()) {
TOTAL_BYTES = fileToUpload.length();
Log.e("@@@@", "uploading total bytes=" + TOTAL_BYTES);
mManager.uploadFile(filePath,Path, fileName, UPLOADING_FROM,
onUploadListener);
if (UPLOADING_FROM) {
mManager.CallService(xml, token, message, date, userid,
groupid, path, folder);
createNotification();
}
}
}
private OnUploadListener onUploadListener = new OnUploadListener() {
@Override
public void uploadProgress(UPLOAD_STATE uState, long uploaded) {
broadcastState(uState, uploaded);
}
};
private void broadcastState(UPLOAD_STATE state, long uploaded) {
Intent intent = new Intent(UPLOAD_STATE_CHANGED_ACTION);
Bundle b = new Bundle();
b.putString(FILE_NAME_EXTRA, mManager.getFileName());
b.putString(XML_RESPONSE, mManager.getResponse());
b.putSerializable(STATE_EXTRA, state);
b.putLong(UPLOADED_BYTES_TAG, uploaded);
intent.putExtras(b);
sendBroadcast(intent);
if (UPLOADING_FROM) {
UPLOADED_BYTES = UPLOADED_BYTES + uploaded;
Log.e("@@@@", "uploading uploaded bytes=" + uploaded);
Log.e("@@@@", "uploading UPLOADED_BYTES bytes=" + UPLOADED_BYTES);
switch (state) {
case PREPARING:
Log.e("@@@@", "IntentService PREPARING");
broadcastProgress(UpoloadedId, UPLOADED_BYTES, TOTAL_BYTES);
break;
case STARTED:
Log.e("@@@@", "IntentService STARTED");
broadcastProgress(UpoloadedId, UPLOADED_BYTES, TOTAL_BYTES);
break;
case UPLOADING:
Log.e("@@@@", "IntentService UPLOADING");
broadcastProgress(UpoloadedId, UPLOADED_BYTES, TOTAL_BYTES);
break;
case COMPLETED:
Log.e("@@@@", "IntentService COMPLETED");
broadcastCompleted(UpoloadedId, 250, "Completed");
break;
case FAILED:
Log.e("@@@@", "IntentService FAILED");
broadcastError(UpoloadedId, null);
break;
case CANCLED:
Log.e("@@@@", "IntentService CANCLED");
break;
default:
break;
}
}
}
private void broadcastProgress(final String uploadId,
final long uploadedBytes, final long totalBytes) {
final int progress = (int) (uploadedBytes * 100 / totalBytes);
if (progress <= lastPublishedProgress)
return;
lastPublishedProgress = progress;
updateNotificationProgress(progress);
final Intent intent = new Intent(getActionBroadcast());
intent.putExtra(UPLOAD_ID, uploadId);
intent.putExtra(STATUS, STATUS_IN_PROGRESS);
intent.putExtra(PROGRESS, progress);
sendBroadcast(intent);
}
private void broadcastCompleted(final String uploadId,
final int responseCode, final String responseMessage) {
final String filteredMessage;
if (responseMessage == null) {
filteredMessage = "";
} else {
filteredMessage = responseMessage;
}
if (responseCode >= 200 && responseCode <= 299)
updateNotificationCompleted();
else
updateNotificationError();
final Intent intent = new Intent(getActionBroadcast());
intent.putExtra(UPLOAD_ID, uploadId);
intent.putExtra(STATUS, STATUS_COMPLETED);
intent.putExtra(SERVER_RESPONSE_CODE, responseCode);
intent.putExtra(SERVER_RESPONSE_MESSAGE, filteredMessage);
sendBroadcast(intent);
}
private void broadcastError(final String uploadId, final Exception exception) {
updateNotificationError();
final Intent intent = new Intent(getActionBroadcast());
intent.setAction(getActionBroadcast());
intent.putExtra(UPLOAD_ID, uploadId);
intent.putExtra(STATUS, STATUS_ERROR);
intent.putExtra(ERROR_EXCEPTION, exception);
sendBroadcast(intent);
}
private void createNotification() {
Log.e("@@@@", "Notification");
notification
.setContentTitle("Uploading")
.setContentIntent(
PendingIntent.getBroadcast(this, 0, new Intent(),
PendingIntent.FLAG_UPDATE_CURRENT))
.setSmallIcon(R.drawable.app_icon).setProgress(100, 0, true)
.setOngoing(true);
Notification n = notification.build();
notificationManager.notify(UPLOAD_NOTIFICATION_ID, n);
// startForeground(UPLOAD_NOTIFICATION_ID, notification.build());
}
private void updateNotificationProgress(final int progress) {
notification.setContentTitle("Uploading")
.setSmallIcon(R.drawable.app_icon)
.setProgress(100, progress, false).setOngoing(true);
Notification n = notification.build();
notificationManager.notify(UPLOAD_NOTIFICATION_ID, n);
// startForeground(UPLOAD_NOTIFICATION_ID, notification.build());
}
private void updateNotificationCompleted() {
notificationManager.cancel(UPLOAD_NOTIFICATION_ID);
// stopForeground(true);
notification.setContentTitle("Uploading").setContentText("Completed")
.setSmallIcon(R.drawable.app_icon).setProgress(0, 0, false)
.setOngoing(false);
Notification n = notification.build();
notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE, n);
// notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE,
// notification.build());
}
private void updateNotificationError() {
stopForeground(false);
notificationManager.cancel(UPLOAD_NOTIFICATION_ID);
notification.setContentTitle("Uploading")
.setContentText("Failed to upload.")
.setSmallIcon(R.drawable.app_icon).setProgress(0, 0, false)
.setOngoing(false);
Notification n = notification.build();
notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE, n);
// notificationManager.notify(UPLOAD_NOTIFICATION_ID_DONE,
// notification.build());
}
@Override
public void onDestroy() {
// nm.cancel(NOTIFY_ID_UPLOAD);
super.onDestroy();
}
}
Manager.java
public class Manager {
public enum UPLOAD_STATE {
PREPARING, STARTED, UPLOADING, COMPLETED, FAILED, CANCLED }
public interface OnUploadListener {
public void uploadProgress(UPLOAD_STATE uState, long uploaded);
}
public Manager(Context mContext) {
this.mContext = mContext;
}
private class UploadTask extends AsyncTask<Void, Void, Void> {
private File file;
private String FilePath;
private OnUploadListener onUploadListener;
private String filename;
boolean UPLOADING_FROM;
public UploadTask(Context context, File file, String FilePath,
String filename, boolean UPLOADING_FROM,
OnUploadListener onUploadListener) {
this.file = file;
this.FilePath = FilePath;
this.onUploadListener = onUploadListener;
this.filename = filename;
this.UPLOADING_FROM = UPLOADING_FROM;
mContext = context;
}
@Override
protected void onPreExecute() {
}
@Override
protected Void doInBackground(Void... params) {
mState = UPLOAD_STATE.PREPARING;
if (onUploadListener != null)
onUploadListener.uploadProgress(mState, UPLOADED_BYTES);
upload(file, FilePath, filename, UPLOADING_FROM, onUploadListener);
return null;
}
@Override
protected void onProgressUpdate(Void... values) {
super.onProgressUpdate(values);
}
@Override
protected void onPostExecute(Void result) {
// progressDialog.dismiss();
}
}
public String getFileName() {
return FileName;
}
//invoke the interface method based on progress status
onUploadListener.uploadProgress(mState, UPLOADED_BYTES);
Comments
Post a Comment