blob: d75f0267f2110a239b5e94df0af0025a61fb55c1 [file] [log] [blame]
/*
* Copyright 2016 Fairphone B.V.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.fairphone.hiccup.app;
import android.content.Context;
import android.content.Intent;
import android.os.AsyncTask;
import android.support.v4.content.LocalBroadcastManager;
import java.util.Date;
/**
* This is convenience class for uploading reports. It wraps the synchronous
* interface of {@link HiccupAPIClient } into a asynchronous interface that
* can be called from services and UI threads.
*/
public class HiccupUploader {
private class UploaderTask extends AsyncTask<Long, Integer, Long> {
@Override
protected Long doInBackground(Long... unused) {
HiccupAPIClient client = null;
long ids[] = new HiccupReportDB(mContext).getIdsOfNotUploadedReports();
long uploaded_count = 0;
long size = ids.length;
if (size > 0) {
sendUploadStartedBroadcast(size);
}
try {
client = new HiccupAPIClient(mContext);
for (long id : ids) {
client.uploadReport(id);
uploaded_count++;
sendUploadProgressBroadcast(uploaded_count, size);
}
} catch (HiccupAPIClient.SendingOverMobileDataProhibitedException e) {
HiccupUtil.LOG("Not allowed over mobile data.");
} catch (HiccupAPIClient.HiccupServerProtocolViolationException e) {
HiccupUtil.LOG("Could not communicate with Hiccup Server.");
} catch (HiccupAPIClient.HiccupServerCommunicationException e) {
HiccupUtil.LOG("Could not communicate with Hiccup Server.");
}
sendUploadFinishedBroadcast(uploaded_count);
HiccupSettings.setLastSyncDate(mContext, new Date());
return uploaded_count;
}
}
public static final String upload_started_action = "com.fairphone.hiccup.UPLOAD_STARTED";
public static final String upload_progress_action = "com.fairphone.hiccup.UPLOAD_PROGRESS";
public static final String upload_finished_action = "com.fairphone.hiccup.UPLOAD_FINISHED";
private static HiccupUploader instance = null;
private Context mContext;
private final LocalBroadcastManager broadcastManager;
UploaderTask mUploaderTask;
private void sendUploadStartedBroadcast(long count) {
Intent intent = new Intent(upload_started_action);
intent.putExtra("total", count);
broadcastManager.sendBroadcast(intent);
}
private void sendUploadProgressBroadcast(long count, long total) {
Intent intent = new Intent(upload_progress_action);
intent.putExtra("count", count);
intent.putExtra("total", total);
broadcastManager.sendBroadcast(intent);
}
private void sendUploadFinishedBroadcast(long total) {
Intent intent = new Intent(upload_finished_action);
intent.putExtra("total", total);
broadcastManager.sendBroadcast(intent);
}
private HiccupUploader(Context ctx) {
this.mContext = ctx;
broadcastManager = LocalBroadcastManager.getInstance(ctx);
mUploaderTask = new UploaderTask();
}
/**
* This will cause the {@link HiccupUploader} to upload all pending crash reports.
* The progress, success or failure of the upload will be communicated through the
* upload_finished_action, upload_progress_action and upload_finished_action.
*
* @param ctx
*/
public static void trigger(Context ctx) {
HiccupUploader uploader = new HiccupUploader(ctx);
uploader.mUploaderTask.executeOnExecutor(AsyncTask.SERIAL_EXECUTOR);
}
}