android-blogposter

Unnamed repository; edit this file 'description' to name the repository.
git clone http://git.code.weiherhei.de/android-blogposter.git
Log | Files | Refs

UploadIntentService.java (3284B)


      1 /**
      2  * 
      3  */
      4 package de.openrat.android.blog.service;
      5 
      6 import java.io.File;
      7 import java.io.IOException;
      8 
      9 import de.openrat.android.blog.Blog;
     10 import de.openrat.android.blog.FileUtils;
     11 import de.openrat.android.blog.HTTPRequest;
     12 import de.openrat.android.blog.R;
     13 import de.openrat.android.blog.R.string;
     14 
     15 import android.app.IntentService;
     16 import android.app.Notification;
     17 import android.app.NotificationManager;
     18 import android.app.PendingIntent;
     19 import android.content.Context;
     20 import android.content.Intent;
     21 import android.content.SharedPreferences;
     22 import android.preference.PreferenceManager;
     23 import android.util.Log;
     24 
     25 /**
     26  * @author dankert
     27  * 
     28  */
     29 public class UploadIntentService extends IntentService
     30 {
     31 
     32 	public static final String EXTRA_TITLE = "title";
     33 	public static final String EXTRA_TEXT = "text";
     34 	public static final String EXTRA_IMAGE = "image";
     35 	
     36 	private static final int NOTIFICATION_UPLOAD = 1;
     37 
     38 	public UploadIntentService()
     39 	{
     40 		super("UploadIntentService");
     41 	}
     42 
     43 	/**
     44 	 * 
     45 	 * {@inheritDoc}
     46 	 * 
     47 	 * @see android.app.IntentService#onHandleIntent(android.content.Intent)
     48 	 */
     49 	@Override
     50 	protected void onHandleIntent(Intent intent)
     51 	{
     52 		final String filePath = intent.getStringExtra(EXTRA_IMAGE);
     53 
     54 		final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE);
     55 
     56 		final Intent notificationIntent = new Intent(this, Blog.class);
     57 		final PendingIntent contentIntent = PendingIntent.getActivity(this, 0,
     58 				notificationIntent, 0);
     59 
     60 		final File file = new File(filePath);
     61 		final String tickerText = getResources()
     62 				.getString(R.string.upload_long);
     63 		final Notification notification = new Notification(
     64 				android.R.drawable.ic_menu_rotate, tickerText, System
     65 						.currentTimeMillis());
     66 		notification.setLatestEventInfo(getApplicationContext(), getResources()
     67 				.getString(R.string.upload), file.getName(), contentIntent);
     68 		notification.flags = Notification.FLAG_ONGOING_EVENT
     69 				| Notification.FLAG_NO_CLEAR;
     70 		nm.notify(NOTIFICATION_UPLOAD, notification);
     71 
     72 		SharedPreferences prefs = PreferenceManager
     73 				.getDefaultSharedPreferences(this);
     74 
     75 		try
     76 		{
     77 			HTTPRequest request = new HTTPRequest();
     78 			request.setFile(prefs.getString("param_image", "image"), FileUtils
     79 					.getBytesFromFile(file), "image.png", "image/png", "binary");
     80 			// Alles OK.
     81 			final String msgText = getResources().getString(R.string.upload_ok);
     82 			notification.tickerText = getResources().getString(
     83 					R.string.upload_ok_long);
     84 			notification.setLatestEventInfo(getApplicationContext(), msgText,
     85 					file.getName(), contentIntent);
     86 			notification.flags = Notification.FLAG_AUTO_CANCEL;
     87 			nm.notify(NOTIFICATION_UPLOAD, notification);
     88 			Log.d(this.getClass().getName(), msgText);
     89 		}
     90 		catch (IOException e)
     91 		{
     92 			// Fehler ist aufgetreten.
     93 			final String msgText = getResources().getString(
     94 					R.string.upload_fail);
     95 			notification.tickerText = getResources().getString(
     96 					R.string.upload_fail_long);
     97 			notification.setLatestEventInfo(getApplicationContext(), msgText, e
     98 					.getMessage()
     99 					+ ": " + file.getName(), contentIntent);
    100 			notification.flags = Notification.FLAG_AUTO_CANCEL;
    101 			nm.notify(NOTIFICATION_UPLOAD, notification);
    102 
    103 			Log.e(this.getClass().getName(), msgText, e);
    104 		}
    105 		finally
    106 		{
    107 		}
    108 	}
    109 }