MailIntentService.java (3147B)
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 MailIntentService extends IntentService 30 { 31 32 private static final int NOTIFICATION_UPLOAD = 1; 33 34 public MailIntentService() 35 { 36 super("UploadIntentService"); 37 } 38 39 /** 40 * 41 * {@inheritDoc} 42 * 43 * @see android.app.IntentService#onHandleIntent(android.content.Intent) 44 */ 45 @Override 46 protected void onHandleIntent(Intent intent) 47 { 48 final String filePath = intent.getStringExtra(UploadIntentService.EXTRA_IMAGE); 49 50 final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); 51 52 final Intent notificationIntent = new Intent(this, Blog.class); 53 final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, 54 notificationIntent, 0); 55 56 final File file = new File(filePath); 57 final String tickerText = getResources() 58 .getString(R.string.upload_long); 59 final Notification notification = new Notification( 60 android.R.drawable.ic_menu_rotate, tickerText, System 61 .currentTimeMillis()); 62 notification.setLatestEventInfo(getApplicationContext(), getResources() 63 .getString(R.string.upload), file.getName(), contentIntent); 64 notification.flags = Notification.FLAG_ONGOING_EVENT 65 | Notification.FLAG_NO_CLEAR; 66 nm.notify(NOTIFICATION_UPLOAD, notification); 67 68 SharedPreferences prefs = PreferenceManager 69 .getDefaultSharedPreferences(this); 70 71 try 72 { 73 HTTPRequest request = new HTTPRequest(); 74 request.setFile(prefs.getString("param_image", "image"), FileUtils 75 .getBytesFromFile(file), "image.png", "image/png", "binary"); 76 // Alles OK. 77 final String msgText = getResources().getString(R.string.upload_ok); 78 notification.tickerText = getResources().getString( 79 R.string.upload_ok_long); 80 notification.setLatestEventInfo(getApplicationContext(), msgText, 81 file.getName(), contentIntent); 82 notification.flags = Notification.FLAG_AUTO_CANCEL; 83 nm.notify(NOTIFICATION_UPLOAD, notification); 84 Log.d(this.getClass().getName(), msgText); 85 } 86 catch (IOException e) 87 { 88 // Fehler ist aufgetreten. 89 final String msgText = getResources().getString( 90 R.string.upload_fail); 91 notification.tickerText = getResources().getString( 92 R.string.upload_fail_long); 93 notification.setLatestEventInfo(getApplicationContext(), msgText, e 94 .getMessage() 95 + ": " + file.getName(), contentIntent); 96 notification.flags = Notification.FLAG_AUTO_CANCEL; 97 nm.notify(NOTIFICATION_UPLOAD, notification); 98 99 Log.e(this.getClass().getName(), msgText, e); 100 } 101 finally 102 { 103 } 104 } 105 }