package com.sailrobotics.posterize; import android.app.Activity; import android.content.Intent; import android.os.Bundle; import android.util.Log; import android.view.Menu; import android.view.MenuItem; import android.widget.Toast; import com.alexbbb.uploadservice.AbstractUploadServiceReceiver; import com.alexbbb.uploadservice.ContentType; import com.alexbbb.uploadservice.UploadRequest; import com.alexbbb.uploadservice.UploadService; import java.util.UUID; public class UploadPDFWebServer extends Activity { private static final String TAG = "Posterize"; String serverUrl; String fileToUpload; String fileName; String parameterName; private final AbstractUploadServiceReceiver uploadReceiver = new AbstractUploadServiceReceiver() { @Override public void onProgress(String uploadId, int progress) { Log.i(TAG, "" + uploadId + " " + progress); } @Override public void onError(String uploadId, Exception exception) { String message = "Error in upload with ID: " + uploadId + ". " + exception.getLocalizedMessage(); Log.e(TAG, message, exception); } @Override public void onCompleted(String uploadId, int serverResponseCode, String serverResponseMessage) { String message = "Upload with ID " + uploadId + " is completed: " + serverResponseCode + ", " + serverResponseMessage; Log.i(TAG, message); } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_upload_pdfweb_server); Intent intent = getIntent(); UploadService.NAMESPACE = "com.sailrobotics.posterize"; serverUrl = "http://54.200.90.145:8001/AndroidFileUpload/fileUpload.php"; fileToUpload = intent.getStringExtra("FilePath"); fileName = intent.getStringExtra("FileName"); parameterName = "image"; onUploadButtonClick(); Toast.makeText(getApplicationContext(), "Check Notification bar for file upload", Toast.LENGTH_SHORT).show(); finish(); } @Override protected void onResume() { super.onResume(); uploadReceiver.register(this); } @Override protected void onPause() { super.onPause(); uploadReceiver.unregister(this); } private void onUploadButtonClick() { final UploadRequest request = new UploadRequest(this, UUID.randomUUID().toString(), serverUrl); request.addFileToUpload(fileToUpload, parameterName, fileName, ContentType.APPLICATION_OCTET_STREAM); request.setNotificationConfig(R.drawable.ic_launcher, getString(R.string.app_name), getString(R.string.uploading), getString(R.string.upload_success), getString(R.string.upload_error), false); // if you comment the following line, the system default user-agent will be used request.setCustomUserAgent("UploadServiceDemo/1.0"); // set the intent to perform when the user taps on the upload notification. // currently tested only with intents that launches an activity // if you comment this line, no action will be performed when the user taps on the notification request.setNotificationClickIntent(new Intent(this, UploadPDFWebServer.class)); // set the maximum number of automatic upload retries on error request.setMaxRetries(2); try { UploadService.startUpload(request); Log.e(TAG, "http://54.200.90.145:8001/AndroidFileUpload/uploads/" + fileName); } catch (Exception exc) { Toast.makeText(this, "Malformed upload request. " + exc.getLocalizedMessage(), Toast.LENGTH_SHORT).show(); } } private void onCancelUploadButtonClick() { UploadService.stopCurrentUpload(); } @Override public boolean onCreateOptionsMenu(Menu menu) { // Inflate the menu; this adds items to the action bar if it is present. getMenuInflater().inflate(R.menu.menu_upload_pdfweb_server, menu); return true; } @Override public boolean onOptionsItemSelected(MenuItem item) { // Handle action bar item clicks here. The action bar will // automatically handle clicks on the Home/Up button, so long // as you specify a parent activity in AndroidManifest.xml. int id = item.getItemId(); //noinspection SimplifiableIfStatement if (id == R.id.action_settings) { return true; } return super.onOptionsItemSelected(item); } }