android-openrat

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs

commit 59d65c7a730aa2f974b96d504f1bd2d43c3d1b85
parent 3ff7526b45c73c9e702c640ee3ea88d8dd3b4001
Author: dankert <devnull@localhost>
Date:   Sun, 16 Oct 2011 15:43:06 +0200

Upload und Veröffentlichung als IntentService.

Diffstat:
AndroidManifest.xml | 4+++-
src/de/openrat/android/blog/FolderActivity.java | 490++++++++++++++++++++++++++++++++-----------------------------------------------
src/de/openrat/android/blog/service/PublishIntentService.java | 132+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
src/de/openrat/android/blog/service/UploadIntentService.java | 107+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 443 insertions(+), 290 deletions(-)

diff --git a/AndroidManifest.xml b/AndroidManifest.xml @@ -18,7 +18,9 @@ <activity android:name="PageElementsActivity"></activity> <activity android:name="PropertiesActivity"></activity> - </application> + <service android:name=".service.PublishIntentService"></service> +<service android:name=".service.UploadIntentService"></service> +</application> <uses-permission android:name="android.permission.INTERNET" /> diff --git a/src/de/openrat/android/blog/FolderActivity.java b/src/de/openrat/android/blog/FolderActivity.java @@ -41,7 +41,8 @@ import android.widget.AdapterView.AdapterContextMenuInfo; import android.widget.AdapterView.OnItemClickListener; import de.openrat.android.blog.FolderEntry.FType; import de.openrat.android.blog.adapter.FolderContentAdapter; -import de.openrat.android.blog.util.FileUtils; +import de.openrat.android.blog.service.PublishIntentService; +import de.openrat.android.blog.service.UploadIntentService; import de.openrat.client.CMSRequest; /** @@ -51,8 +52,6 @@ import de.openrat.client.CMSRequest; public class FolderActivity extends ListActivity { - private static final int NOTIFICATION_UPLOAD = 1; - private static final int NOTIFICATION_PUBLISH = 2; private static final String ID2 = "id"; public static final String CLIENT = "client"; private static final String NAME = "name"; @@ -124,12 +123,14 @@ public class FolderActivity extends ListActivity // JSONObject inhalte = json.getJSONObject("object"); // folderid = ...; - } catch (IOException e) + } + catch (IOException e) { System.err.println("Fuck Folder"); System.err.println(response); System.err.println(e.getMessage()); - } catch (JSONException e) + } + catch (JSONException e) { // TODO Auto-generated catch block e.printStackTrace(); @@ -158,7 +159,8 @@ public class FolderActivity extends ListActivity System.out.println("Ordnerinhalt: " + response); dialog.dismiss(); - } catch (IOException e) + } + catch (IOException e) { System.err.println("Fuck Folder"); System.err.println(response); @@ -186,7 +188,8 @@ public class FolderActivity extends ListActivity } - } catch (Exception e) + } + catch (Exception e) { e.printStackTrace(); } @@ -212,21 +215,21 @@ public class FolderActivity extends ListActivity final Intent intent; switch (entry.type) { - case FOLDER: - - intent = new Intent(FolderActivity.this, - FolderActivity.class); - intent.putExtra(CLIENT, request); - intent.putExtra("folderid", entry.id); - startActivity(intent); - break; - case PAGE: - intent = new Intent(FolderActivity.this, - PageElementsActivity.class); - intent.putExtra(CLIENT, request); - intent.putExtra("pageid", entry.id); - startActivity(intent); - default: + case FOLDER: + + intent = new Intent(FolderActivity.this, + FolderActivity.class); + intent.putExtra(CLIENT, request); + intent.putExtra("folderid", entry.id); + startActivity(intent); + break; + case PAGE: + intent = new Intent(FolderActivity.this, + PageElementsActivity.class); + intent.putExtra(CLIENT, request); + intent.putExtra("pageid", entry.id); + startActivity(intent); + default: } } }); @@ -267,166 +270,120 @@ public class FolderActivity extends ListActivity switch (item.getItemId()) { - case R.id.menu_properties: - - // This is actually where the magic happens. - - // As we use an adapter view (which the ListView is) - - // We can cast item.getMenuInfo() to AdapterContextMenuInfo + case R.id.menu_properties: - AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item - .getMenuInfo(); + // This is actually where the magic happens. - // To get the id of the clicked item in the list use menuInfo.id + // As we use an adapter view (which the ListView is) - // Log.d("t", "list pos:" + menuInfo.position + " id:" + - // menuInfo.id); - FolderEntry entry = data.get(menuInfo.position); - final Intent intent; - intent = new Intent(FolderActivity.this, PropertiesActivity.class); - intent.putExtra(CLIENT, request); - intent.putExtra("objectid", entry.id); - intent.putExtra(TYP, entry.type); - startActivity(intent); - return true; + // We can cast item.getMenuInfo() to AdapterContextMenuInfo - case R.id.menu_delete: + AdapterView.AdapterContextMenuInfo menuInfo = (AdapterView.AdapterContextMenuInfo) item + .getMenuInfo(); - final AdapterContextMenuInfo mInfo = (AdapterView.AdapterContextMenuInfo) item - .getMenuInfo(); + // To get the id of the clicked item in the list use menuInfo.id - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage("Sicher?").setCancelable(false) - .setPositiveButton( - getResources().getString(R.string.delete), - new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dialog, - int id) + // Log.d("t", "list pos:" + menuInfo.position + " id:" + + // menuInfo.id); + FolderEntry entry = data.get(menuInfo.position); + final Intent intent; + intent = new Intent(FolderActivity.this, + PropertiesActivity.class); + intent.putExtra(CLIENT, request); + intent.putExtra("objectid", entry.id); + intent.putExtra(TYP, entry.type); + startActivity(intent); + return true; + + case R.id.menu_delete: + + final AdapterContextMenuInfo mInfo = (AdapterView.AdapterContextMenuInfo) item + .getMenuInfo(); + + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage("Sicher?").setCancelable(false) + .setPositiveButton( + getResources().getString(R.string.delete), + new DialogInterface.OnClickListener() { - // To get the id of the clicked item in the - // list use menuInfo.id - FolderEntry en = data.get(mInfo.position); - - request.clearParameters(); - request.trace=true; - request.setMethod("POST"); - request.setAction("folder"); - request.setActionMethod("multiple"); - request.setId(folderid); - - - request.setParameter("type", "delete"); - request.setParameter("ids", en.id); - // Erstmal alles aktivieren was geht - // TODO: Abfrage der gewünschten - // Einstellungen über AlertDialog. - request.setParameter("commit", "1"); - String response = null; - - // the next two lines initialize the - // Notification, using the - // configurations above - - try - { - response = request.performRequest(); - JSONObject json = new JSONObject( - response); - System.out.println("nachlöschen: " - + response); - - } catch (IOException e) - { - // System.err.println(response); - System.err.println(e.getMessage()); - } catch (JSONException e) - { - e.printStackTrace(); - } finally + public void onClick(DialogInterface dialog, + int id) { + // To get the id of the clicked item in + // the + // list use menuInfo.id + FolderEntry en = data + .get(mInfo.position); + + request.clearParameters(); + request.trace = true; + request.setMethod("POST"); + request.setAction("folder"); + request.setActionMethod("multiple"); + request.setId(folderid); + + request.setParameter("type", "delete"); + request.setParameter("ids", en.id); + // Erstmal alles aktivieren was geht + // TODO: Abfrage der gewünschten + // Einstellungen über AlertDialog. + request.setParameter("commit", "1"); + String response = null; + + // the next two lines initialize the + // Notification, using the + // configurations above + + try + { + response = request.performRequest(); + JSONObject json = new JSONObject( + response); + System.out.println("nachlöschen: " + + response); + + } + catch (IOException e) + { + // System.err.println(response); + System.err.println(e.getMessage()); + } + catch (JSONException e) + { + e.printStackTrace(); + } + finally + { + } + ; } - ; - } - }); - AlertDialog alert = builder.create(); - alert.show(); - - return true; - - case R.id.menu_publish: + }); + AlertDialog alert = builder.create(); + alert.show(); - menuInfo = (AdapterView.AdapterContextMenuInfo) item.getMenuInfo(); + return true; - // To get the id of the clicked item in the list use menuInfo.id - entry = data.get(menuInfo.position); + case R.id.menu_publish: - request.clearParameters(); - request.setAction(entry.type.name().toLowerCase()); - request.setActionMethod("pub"); + menuInfo = (AdapterView.AdapterContextMenuInfo) item + .getMenuInfo(); + entry = data.get(menuInfo.position); - // Erstmal alles aktivieren was geht - // TODO: Abfrage der gewünschten Einstellungen über AlertDialog. - request.setParameter("subdirs", "1"); - request.setParameter("pages", "1"); - request.setParameter("files", "1"); - String response = null; + final Intent publishIntent = new Intent(this,PublishIntentService.class); - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + publishIntent.putExtra(PublishIntentService.EXTRA_REQUEST,request); + publishIntent.putExtra(PublishIntentService.EXTRA_TYPE,entry.type.name().toLowerCase()); + publishIntent.putExtra(PublishIntentService.EXTRA_NAME,entry.name); + publishIntent.putExtra(PublishIntentService.EXTRA_ID,entry.id); + startService(publishIntent); + + Toast.makeText(this, R.string.publish, Toast.LENGTH_SHORT); - Intent notificationIntent = new Intent(this, FolderActivity.class); - PendingIntent contentIntent = PendingIntent.getActivity(this, 0, - notificationIntent, 0); + return true; - // the next two lines initialize the Notification, using the - // configurations above - Notification notification = new Notification(R.drawable.publish, - getResources().getString(R.string.publish), System - .currentTimeMillis()); - notification.setLatestEventInfo(getApplicationContext(), - getResources().getString(R.string.publish), entry.name, - contentIntent); - notification.flags = Notification.FLAG_ONGOING_EVENT - | Notification.FLAG_NO_CLEAR; + default: - nm.notify(NOTIFICATION_PUBLISH, notification); - - try - { - Thread.sleep(10000); - response = request.performRequest(); - JSONObject json = new JSONObject(response); - - } catch (IOException e) - { - System.err.println(response); - System.err.println(e.getMessage()); - } catch (JSONException e) - { - e.printStackTrace(); - } catch (InterruptedException e) - { - e.printStackTrace(); - } finally - { - // notification.flags |= Notification.FLAG_NO_CLEAR; - - notification.setLatestEventInfo(getApplicationContext(), - getResources().getString(R.string.publish_ok), - entry.name, contentIntent); - notification.flags = 0; - nm.notify(NOTIFICATION_PUBLISH, notification); - // nm.cancel(NOTIFICATION_PUBLISH); - } - - Toast.makeText(this, R.string.publish, Toast.LENGTH_SHORT); - - return true; - - default: - - return super.onContextItemSelected(item); + return super.onContextItemSelected(item); } @@ -448,57 +405,59 @@ public class FolderActivity extends ListActivity { switch (item.getItemId()) { - case R.id.menu_language: - AlertDialog.Builder builder = new AlertDialog.Builder(this); - builder.setMessage(R.string.language).setCancelable(false) - .setPositiveButton("OK", - new DialogInterface.OnClickListener() - { - public void onClick(DialogInterface dialog, - int id) + case R.id.menu_language: + AlertDialog.Builder builder = new AlertDialog.Builder(this); + builder.setMessage(R.string.language).setCancelable(false) + .setPositiveButton("OK", + new DialogInterface.OnClickListener() { - // Speichern - } - }).setSingleChoiceItems(new String[] { "a", "b" }, - 1, new OnClickListener() - { - - @Override - public void onClick(DialogInterface dialog, - int which) + public void onClick(DialogInterface dialog, + int id) + { + // Speichern + } + }).setSingleChoiceItems( + new String[] { "a", "b" }, 1, + new OnClickListener() { - } - }); - AlertDialog alert = builder.create(); - alert.show(); - return true; - - case R.id.menu_model: - - Toast.makeText(this, "Model", Toast.LENGTH_SHORT); - return true; - case R.id.menu_upload: - - Intent chooseFile; - Intent intent; - chooseFile = new Intent(Intent.ACTION_GET_CONTENT); - chooseFile.setType("file/*"); - intent = Intent.createChooser(chooseFile, "Choose a file"); - startActivityForResult(intent, ACTIVITY_CHOOSE_FILE); - return true; - case R.id.menu_upload_image: - - // Intent chooseFile; - // Intent intent; - chooseFile = new Intent(Intent.ACTION_GET_CONTENT); - chooseFile.setType("image/*"); - intent = Intent.createChooser(chooseFile, "Choose an image"); - startActivityForResult(intent, ACTIVITY_CHOOSE_IMAGE); - return true; - - default: - Toast.makeText(this, "??: " + item.getItemId(), Toast.LENGTH_SHORT); + @Override + public void onClick(DialogInterface dialog, + int which) + { + + } + }); + AlertDialog alert = builder.create(); + alert.show(); + return true; + + case R.id.menu_model: + + Toast.makeText(this, "Model", Toast.LENGTH_SHORT); + return true; + case R.id.menu_upload: + + Intent chooseFile; + Intent intent; + chooseFile = new Intent(Intent.ACTION_GET_CONTENT); + chooseFile.setType("file/*"); + intent = Intent.createChooser(chooseFile, "Choose a file"); + startActivityForResult(intent, ACTIVITY_CHOOSE_FILE); + return true; + case R.id.menu_upload_image: + + // Intent chooseFile; + // Intent intent; + chooseFile = new Intent(Intent.ACTION_GET_CONTENT); + chooseFile.setType("image/*"); + intent = Intent.createChooser(chooseFile, "Choose an image"); + startActivityForResult(intent, ACTIVITY_CHOOSE_IMAGE); + return true; + + default: + Toast.makeText(this, "??: " + item.getItemId(), + Toast.LENGTH_SHORT); } return false; @@ -509,84 +468,37 @@ public class FolderActivity extends ListActivity { switch (requestCode) { - case ACTIVITY_CHOOSE_FILE: - case ACTIVITY_CHOOSE_IMAGE: - { - if (resultCode == RESULT_OK) - { - // Upload durchführen - Uri uri = data.getData(); - - String filePath; - if (requestCode == ACTIVITY_CHOOSE_IMAGE) - { - - filePath = getPath(uri); - } else + case ACTIVITY_CHOOSE_FILE: + case ACTIVITY_CHOOSE_IMAGE: { - filePath = uri.getPath(); - } - - System.out.println(filePath); - - NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); - - Intent notificationIntent = new Intent(this, - FolderActivity.class); - PendingIntent contentIntent = PendingIntent.getActivity(this, - 0, notificationIntent, 0); - - request.clearParameters(); - request.setAction("folder"); - request.setActionMethod("createnewfile"); - request.setMethod("POST"); - request.trace = true; - - try - { - final File file = new File(filePath); - - // the next two lines initialize the Notification, using the - // configurations above - Notification notification = new Notification( - R.drawable.upload, getResources().getString( - R.string.upload), System - .currentTimeMillis()); - notification.setLatestEventInfo(getApplicationContext(), - getResources().getString(R.string.upload), file - .getName(), contentIntent); - notification.flags |= Notification.FLAG_SHOW_LIGHTS; - - nm.notify(NOTIFICATION_UPLOAD, notification); - - byte[] fileBytes = FileUtils.getBytesFromFile(file); - request.setFile("file", fileBytes, file.getName(), - "image/jpeg", "binary"); - // request.setFile("file", inputStream,file.length(), - // file.getName(), "image/jpeg", "binary"); - - ProgressDialog dialog = ProgressDialog.show( - FolderActivity.this, getResources().getString( - R.string.loading), getResources() - .getString(R.string.waitingforlogin)); - - String response = request.performRequest(); - dialog.dismiss(); - - // String response = request.performRequest("TEST TEST"); - System.out.println("nach dem Hochladen" + response); - Toast.makeText(this, R.string.publish, Toast.LENGTH_SHORT); - - } catch (IOException e) - { - e.printStackTrace(); - throw new RuntimeException(e); - } finally - { - nm.cancel(NOTIFICATION_UPLOAD); + if (resultCode == RESULT_OK) + { + // Upload durchführen + Uri uri = data.getData(); + + String filePath; + if (requestCode == ACTIVITY_CHOOSE_IMAGE) + { + + filePath = getPath(uri); + } + else + { + filePath = uri.getPath(); + } + + final Intent uploadIntent = new Intent(FolderActivity.this, + UploadIntentService.class); + uploadIntent.putExtra( + UploadIntentService.EXTRA_FILENAME, filePath); + uploadIntent.putExtra( + UploadIntentService.EXTRA_REQUEST, request); + startService(uploadIntent); + + Toast.makeText(this, R.string.publish, + Toast.LENGTH_SHORT); + } } - } - } } } diff --git a/src/de/openrat/android/blog/service/PublishIntentService.java b/src/de/openrat/android/blog/service/PublishIntentService.java @@ -0,0 +1,132 @@ +/** + * + */ +package de.openrat.android.blog.service; + +import java.io.IOException; + +import org.json.JSONException; +import org.json.JSONObject; + +import android.app.IntentService; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import de.openrat.android.blog.FolderActivity; +import de.openrat.android.blog.R; +import de.openrat.client.CMSRequest; + +/** + * @author dankert + * + */ +public class PublishIntentService extends IntentService +{ + + public static final String EXTRA_REQUEST = "request"; + public static final String EXTRA_ID = "objectid"; + public static final String EXTRA_TYPE = "type"; + public static final String EXTRA_NAME = "name"; + private static final int NOTIFICATION_PUBLISH = 2; + + public PublishIntentService() + { + super("PublishIntentService"); + } + + /** + * + * {@inheritDoc} + * + * @see android.app.IntentService#onHandleIntent(android.content.Intent) + */ + @Override + protected void onHandleIntent(Intent intent) + { + final CMSRequest request = (CMSRequest) intent + .getSerializableExtra(EXTRA_REQUEST); + + String type = intent.getStringExtra(EXTRA_TYPE); + String id = intent.getStringExtra(EXTRA_ID); + String name = intent.getStringExtra(EXTRA_NAME); + + request.clearParameters(); + request.setAction(type ); + request.setActionMethod("pub"); + request.setId(id); + + // Erstmal alles aktivieren was geht + // TODO: Abfrage der gewünschten Einstellungen über AlertDialog. + request.setParameter("subdirs", "1"); + request.setParameter("pages", "1"); + request.setParameter("files", "1"); + String response = null; + + NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + Intent notificationIntent = new Intent(this, + FolderActivity.class); + PendingIntent contentIntent = PendingIntent.getActivity(this, + 0, notificationIntent, 0); + + // the next two lines initialize the Notification, using the + // configurations above + Notification notification = new Notification( + R.drawable.publish, getResources().getString( + R.string.publish), System.currentTimeMillis()); + notification.setLatestEventInfo(getApplicationContext(), + getResources().getString(R.string.publish), name, + contentIntent); + notification.flags = Notification.FLAG_ONGOING_EVENT + | Notification.FLAG_NO_CLEAR; + + nm.notify(NOTIFICATION_PUBLISH, notification); + + try + { + try + { + Thread.sleep(2000); + } + catch (InterruptedException e) + { + } + + response = request.performRequest(); + JSONObject json = new JSONObject(response); + + notification.setLatestEventInfo(getApplicationContext(), + getResources().getString(R.string.publish_ok), + name, contentIntent); + notification.flags = 0; + nm.notify(NOTIFICATION_PUBLISH, notification); + } + catch (IOException e) + { + System.err.println(response); + System.err.println(e.getMessage()); + + notification.setLatestEventInfo(getApplicationContext(), + getResources().getString(R.string.publish_fail), + name, contentIntent); + notification.flags = 0; + nm.notify(NOTIFICATION_PUBLISH, notification); + } + catch (JSONException e) + { + notification.setLatestEventInfo(getApplicationContext(), + getResources().getString(R.string.publish_fail), + name, contentIntent); + notification.flags = 0; + nm.notify(NOTIFICATION_PUBLISH, notification); + e.printStackTrace(); + } + finally + { + } + + } + +} diff --git a/src/de/openrat/android/blog/service/UploadIntentService.java b/src/de/openrat/android/blog/service/UploadIntentService.java @@ -0,0 +1,107 @@ +/** + * + */ +package de.openrat.android.blog.service; + +import java.io.File; +import java.io.IOException; + +import android.app.IntentService; +import android.app.Notification; +import android.app.NotificationManager; +import android.app.PendingIntent; +import android.content.Context; +import android.content.Intent; +import de.openrat.android.blog.FolderActivity; +import de.openrat.android.blog.R; +import de.openrat.android.blog.util.FileUtils; +import de.openrat.client.CMSRequest; + +/** + * @author dankert + * + */ +public class UploadIntentService extends IntentService +{ + + public static final String EXTRA_REQUEST = "request"; + public static final String EXTRA_FILENAME = "file"; + private static final int NOTIFICATION_UPLOAD = 1; + + public UploadIntentService() + { + super("UploadIntentService"); + } + + /** + * + * {@inheritDoc} + * + * @see android.app.IntentService#onHandleIntent(android.content.Intent) + */ + @Override + protected void onHandleIntent(Intent intent) + { + final String filePath = intent.getStringExtra(EXTRA_FILENAME); + final CMSRequest request = (CMSRequest) intent + .getSerializableExtra(EXTRA_REQUEST); + + NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); + + Intent notificationIntent = new Intent(this, FolderActivity.class); + PendingIntent contentIntent = PendingIntent.getActivity(this, 0, + notificationIntent, 0); + + request.clearParameters(); + request.setAction("folder"); + request.setActionMethod("createnewfile"); + request.setMethod("POST"); + request.trace = true; + + final Notification notification = new Notification(R.drawable.upload, + getResources().getString(R.string.upload), System + .currentTimeMillis()); + notification.setLatestEventInfo(getApplicationContext(), getResources() + .getString(R.string.upload_ok), filePath, contentIntent); + + nm.notify(NOTIFICATION_UPLOAD, notification); + try + { + final File file = new File(filePath); + + notification.setLatestEventInfo(getApplicationContext(), + getResources().getString(R.string.upload), file.getName(), + contentIntent); + notification.flags |= Notification.FLAG_NO_CLEAR; + + byte[] fileBytes = FileUtils.getBytesFromFile(file); + request.setFile(EXTRA_FILENAME, fileBytes, file.getName(), + "image/jpeg", "binary"); + + String response = request.performRequest(); + + System.out.println("nach dem Hochladen" + response); + + notification.setLatestEventInfo(getApplicationContext(), + getResources().getString(R.string.upload_ok), file + .getName(), contentIntent); + notification.flags = 0; + nm.notify(NOTIFICATION_UPLOAD, notification); + } + catch (IOException e) + { + notification.setLatestEventInfo(getApplicationContext(), + getResources().getString(R.string.upload_fail), "", + contentIntent); + notification.flags = 0; + nm.notify(NOTIFICATION_UPLOAD, notification); + + e.printStackTrace(); + throw new RuntimeException(e); + } + finally + { + } + } + +}