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