android-openrat

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

commit 49527a0a84046f92443f1ce016e90a2f3578f1b4
parent 6e2495180f140cfe5514e3b691b94fbfc211ce85
Author: dankert <devnull@localhost>
Date:   Mon, 17 Oct 2011 23:00:19 +0200

Für alle Ladevorgänge asynchrone Tasks verwenden; Timeout setzen und auswerten.

Diffstat:
res/values/strings.xml | 1+
src/de/openrat/android/blog/FolderActivity.java | 2++
src/de/openrat/android/blog/OpenRatBlog.java | 85++++++++++++++++++++++++++++++++++++++++++++++++++++++-------------------------
src/de/openrat/android/blog/ProjectActivity.java | 174++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
src/de/openrat/android/blog/service/PublishIntentService.java | 20+++++++++++++++-----
src/de/openrat/android/blog/service/UploadIntentService.java | 40+++++++++++++++++++++++++---------------
src/de/openrat/client/HTTPRequest.java | 19+++++++++++++++++--
src/de/openrat/client/OpenRatClient.java | 90+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------
8 files changed, 304 insertions(+), 127 deletions(-)

diff --git a/res/values/strings.xml b/res/values/strings.xml @@ -27,6 +27,7 @@ <string name="loading">Warten auf Server</string> <string name="waitingforcontent">Inhalt wird geladen</string> <string name="waitingforprojects">Projekte werden geladen</string> + <string name="waitingforselectproject">Wähle Projekt</string> <string name="waitingforrootfolder">Einstiegsordner wird gesucht</string> <string name="waitingforlogin">Anmeldung am Server</string> <string name="language">Sprache</string> diff --git a/src/de/openrat/android/blog/FolderActivity.java b/src/de/openrat/android/blog/FolderActivity.java @@ -28,6 +28,7 @@ import android.net.Uri; import android.os.AsyncTask; import android.os.Bundle; import android.provider.MediaStore; +import android.util.Log; import android.view.ContextMenu; import android.view.Menu; import android.view.MenuInflater; @@ -110,6 +111,7 @@ public class FolderActivity extends ListActivity } catch (IOException e) { + Log.e(this.getClass().getName(),e.getMessage(),e); Toast.makeText(FolderActivity.this,e.getMessage(),Toast.LENGTH_SHORT); } diff --git a/src/de/openrat/android/blog/OpenRatBlog.java b/src/de/openrat/android/blog/OpenRatBlog.java @@ -20,14 +20,14 @@ package de.openrat.android.blog; import java.io.IOException; -import org.json.JSONObject; - import android.app.Activity; import android.app.ProgressDialog; import android.content.Intent; import android.content.SharedPreferences; +import android.os.AsyncTask; import android.os.Bundle; import android.preference.PreferenceManager; +import android.util.Log; import android.view.Menu; import android.view.MenuInflater; import android.view.MenuItem; @@ -35,7 +35,6 @@ import android.view.View; import android.view.View.OnClickListener; import android.widget.TextView; import android.widget.Toast; -import de.openrat.client.CMSRequest; import de.openrat.client.OpenRatClient; /** @@ -72,31 +71,63 @@ public class OpenRatBlog extends Activity @Override public void onClick(View v) { - try - { - final ProgressDialog dialog = ProgressDialog.show( - OpenRatBlog.this, getResources().getString( - R.string.loading), getResources() - .getString(R.string.waitingforlogin)); - - client.login(prefs.getString("username", ""), prefs - .getString("password", "")); - - dialog.dismiss(); - - // Verbindung und Login waren erfolgreich. - // Jetzt zur Projekt-Liste wechseln. - final Intent intent = new Intent(v.getContext(), - ProjectActivity.class); - intent.putExtra(ProjectActivity.CLIENT, client); - startActivity(intent); - } - catch (IOException e1) + AsyncTask<Void, Void, Void> loginTask = new AsyncTask<Void, Void, Void>() { - // Verbindung nicht möglich... - Toast.makeText(OpenRatBlog.this, e1.getMessage(), - Toast.LENGTH_LONG); - } + ProgressDialog dialog = new ProgressDialog(OpenRatBlog.this); + + @Override + protected void onPreExecute() + { + dialog.setTitle(R.string.loading); + dialog.setMessage(getResources().getString( + R.string.waitingforlogin)); + dialog.show(); + } + + @Override + protected void onPostExecute(Void result) + { + dialog.dismiss(); + } + + @Override + protected Void doInBackground(Void... params) + { + try + { + client.login(prefs.getString("username", ""), prefs + .getString("password", "")); + + + // Verbindung und Login waren erfolgreich. + // Jetzt zur Projekt-Liste wechseln. + final Intent intent = new Intent(OpenRatBlog.this, + ProjectActivity.class); + intent.putExtra(ProjectActivity.CLIENT, client); + startActivity(intent); + } + catch (final IOException e1) + { + dialog.dismiss(); + // Verbindung nicht möglich... + Log.i(this.getClass().getName(), e1.getMessage(), + e1); + runOnUiThread(new Runnable() + { + + @Override + public void run() + { + Toast.makeText(OpenRatBlog.this, e1 + .getMessage(), Toast.LENGTH_LONG); + } + }); + } + + return null; + } + }; + loginTask.execute(); } }); diff --git a/src/de/openrat/android/blog/ProjectActivity.java b/src/de/openrat/android/blog/ProjectActivity.java @@ -7,22 +7,20 @@ import java.io.IOException; import java.util.ArrayList; import java.util.List; -import org.json.JSONArray; -import org.json.JSONObject; - import android.app.ListActivity; import android.app.ProgressDialog; import android.content.Intent; +import android.os.AsyncTask; import android.os.Bundle; +import android.util.Log; import android.view.View; import android.widget.AdapterView; import android.widget.ListAdapter; import android.widget.ListView; import android.widget.Toast; import android.widget.AdapterView.OnItemClickListener; -import de.openrat.android.blog.FolderEntry.FType; import de.openrat.android.blog.adapter.FolderContentAdapter; -import de.openrat.client.CMSRequest; +import de.openrat.client.OpenRatClient; /** * @author dankert @@ -34,7 +32,7 @@ public class ProjectActivity extends ListActivity public static final String CLIENT = "client"; private static final String NAME = "name"; private static final String DESCRIPTION = "description"; - private CMSRequest request; + private OpenRatClient client; private List<FolderEntry> data; @Override @@ -48,50 +46,60 @@ public class ProjectActivity extends ListActivity ; String[] from = new String[] { NAME, DESCRIPTION }; ; - data = new ArrayList<FolderEntry>(); - request = (CMSRequest) getIntent().getSerializableExtra(CLIENT); + client = (OpenRatClient) getIntent().getSerializableExtra(CLIENT); - request.clearParameters(); - request.setParameter("action", "index"); - request.setParameter("subaction", "projectmenu"); - String response = null; - try + AsyncTask<String, Void, List<FolderEntry>> loadProjectsTask = new AsyncTask<String, Void, List<FolderEntry>>() { - ProgressDialog dialog = ProgressDialog.show(ProjectActivity.this, - getResources().getString(R.string.loading), getResources() - .getString(R.string.waitingforprojects)); - response = request.performRequest(); - dialog.dismiss(); - } catch (IOException e) - { - response = e.getMessage(); - } + ProgressDialog dialog = new ProgressDialog(ProjectActivity.this); - try - { - System.out.println(response); - JSONObject json = new JSONObject(response); - JSONArray projects = json.getJSONArray("projects"); - for (int i = 0; i < projects.length(); i++) + @Override + protected void onPreExecute() { - JSONObject project = projects.getJSONObject(i); + dialog.setTitle(getResources().getString(R.string.loading)); + dialog.setMessage(getResources().getString( + R.string.waitingforprojects)); + dialog.show(); + } - final FolderEntry entry = new FolderEntry(); - entry.type = FType.PROJECT; - entry.name = project.getString("name"); - entry.description = ""; - entry.id = project.getString("id"); + protected void onPostExecute(List<FolderEntry> result) + { + dialog.dismiss(); - data.add(entry); + final ListAdapter adapter = new FolderContentAdapter( + ProjectActivity.this, data); + setListAdapter(adapter); + }; - } + @Override + protected List<FolderEntry> doInBackground(String... params) + { + // + try + { + data = client.loadProjects(); + } + catch (final IOException e) + { + Log.e(this.getClass().getName(), e.getMessage(), e); + runOnUiThread(new Runnable() + { + + @Override + public void run() + { + Toast.makeText(ProjectActivity.this, + e.getMessage(), Toast.LENGTH_SHORT); + } + }); + data = new ArrayList<FolderEntry>(); + } - } catch (Exception e) - { - e.printStackTrace(); - } + return data; + } + }; + loadProjectsTask.execute(); ListView list = getListView(); @@ -102,43 +110,65 @@ public class ProjectActivity extends ListActivity public void onItemClick(AdapterView<?> parent, View view, int position, long id) { - final Intent i = new Intent(ProjectActivity.this, - FolderActivity.class); - i.putExtra(CLIENT, request); - // Projekt auswählen - String projectid = data.get(position).id; - - request = (CMSRequest) getIntent().getSerializableExtra(CLIENT); - - request.clearParameters(); - request.setParameter("action", "index"); - request.setParameter("subaction", "project"); - request.setParameter("id", projectid); - String response = null; - try - { - ProgressDialog dialog = ProgressDialog.show( - ProjectActivity.this, getResources().getString( - R.string.loading), getResources() - .getString(R.string.waitingforlogin)); - response = request.performRequest(); - dialog.dismiss(); - - } catch (IOException e) + final String projectid = data.get(position).id; + + AsyncTask<String, Void, Void> startProjectTask = /** + * Starten des + * ausgewählten Projektes. + * + * @author dankert + * + */ + new AsyncTask<String, Void, Void>() { - Toast.makeText(ProjectActivity.this, e.getMessage(), - Toast.LENGTH_LONG); - System.err - .println("Fehler bei Projektauswahl: " + response); - System.err.println(e.getMessage()); - } - startActivity(i); + ProgressDialog dialog = new ProgressDialog( + ProjectActivity.this); + + @Override + protected void onPreExecute() + { + dialog.setTitle(R.string.loading); + dialog.setMessage(getResources().getString( + R.string.waitingforselectproject)); + dialog.show(); + } + + protected void onPostExecute(Void result) + { + dialog.dismiss(); + }; + + @Override + protected Void doInBackground(String... params) + { + // + try + { + client.selectProject(projectid); + } + catch (IOException e) + { + Log.e(this.getClass().getName(), e.getMessage(), e); + Toast.makeText(ProjectActivity.this, + e.getMessage(), Toast.LENGTH_SHORT); + } + + final Intent i = new Intent(ProjectActivity.this, + FolderActivity.class); + i.putExtra(CLIENT, client); + + startActivity(i); + + return null; + } + }; + + startProjectTask.execute(); + } }); - final ListAdapter adapter = new FolderContentAdapter(this, data); - setListAdapter(adapter); } } diff --git a/src/de/openrat/android/blog/service/PublishIntentService.java b/src/de/openrat/android/blog/service/PublishIntentService.java @@ -43,7 +43,7 @@ public class PublishIntentService extends IntentService @Override protected void onHandleIntent(Intent intent) { - final OpenRatClient request = (OpenRatClient) intent + final OpenRatClient client = (OpenRatClient) intent .getSerializableExtra(EXTRA_REQUEST); String type = intent.getStringExtra(EXTRA_TYPE); @@ -52,9 +52,9 @@ public class PublishIntentService extends IntentService // 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"); + client.setParameter("subdirs", "1"); + client.setParameter("pages", "1"); + client.setParameter("files", "1"); final NotificationManager nm = (NotificationManager) getSystemService(Context.NOTIFICATION_SERVICE); @@ -75,7 +75,17 @@ public class PublishIntentService extends IntentService try { - request.publish(type, id); + try + { + Thread.sleep(5000); + } + catch (InterruptedException e) + { + } + + int old = client.setTimeout(3600000); // 1 Std. + client.publish(type, id); + client.setTimeout(old); // Alles OK. notification.setLatestEventInfo(getApplicationContext(), diff --git a/src/de/openrat/android/blog/service/UploadIntentService.java b/src/de/openrat/android/blog/service/UploadIntentService.java @@ -52,25 +52,34 @@ public class UploadIntentService extends IntentService final PendingIntent contentIntent = PendingIntent.getActivity(this, 0, notificationIntent, 0); + final File file = new File(filePath); + final String msgUpload = getResources().getString(R.string.upload); final Notification notification = new Notification(R.drawable.logo, - getResources().getString(R.string.upload), System - .currentTimeMillis()); - notification.setLatestEventInfo(getApplicationContext(), getResources() - .getString(R.string.upload_ok), filePath, contentIntent); - notification.flags |= Notification.FLAG_NO_CLEAR; + msgUpload, System.currentTimeMillis()); + notification.setLatestEventInfo(getApplicationContext(), msgUpload, + file.getName(), contentIntent); + notification.flags = Notification.FLAG_ONGOING_EVENT + | Notification.FLAG_NO_CLEAR; nm.notify(NOTIFICATION_UPLOAD, notification); - + try { - final File file = new File(filePath); + try + { + Thread.sleep(5000); + } + catch (InterruptedException e) + { + } - client.uploadFile(EXTRA_FILENAME,file); + int old = client.setTimeout(3600000); // 1 Std. + client.uploadFile(EXTRA_FILENAME, file); + client.setTimeout(old); // Alles OK. final String msgText = getResources().getString(R.string.upload_ok); - notification.setLatestEventInfo(getApplicationContext(), - msgText, file - .getName(), contentIntent); + notification.setLatestEventInfo(getApplicationContext(), msgText, + file.getName(), contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL; nm.notify(NOTIFICATION_UPLOAD, notification); Log.d(this.getClass().getName(), msgText); @@ -78,10 +87,11 @@ public class UploadIntentService extends IntentService catch (IOException e) { // Fehler ist aufgetreten. - final String msgText = getResources().getString(R.string.upload_fail); - notification.setLatestEventInfo(getApplicationContext(), - msgText, e.getMessage(), - contentIntent); + final String msgText = getResources().getString( + R.string.upload_fail); + notification.setLatestEventInfo(getApplicationContext(), msgText, e + .getMessage() + + ": " + file.getName(), contentIntent); notification.flags = Notification.FLAG_AUTO_CANCEL; nm.notify(NOTIFICATION_UPLOAD, notification); diff --git a/src/de/openrat/client/HTTPRequest.java b/src/de/openrat/client/HTTPRequest.java @@ -109,6 +109,7 @@ public class HTTPRequest implements Serializable private String cookieName; private String cookieValue; private String language; + private int timeout = 30000; /** * @@ -179,6 +180,20 @@ public class HTTPRequest implements Serializable this.proxyPort = port; } + + /** + * Timeout for Socket. + * @param timeout Timeout in milliseconds. + * @return old timeout + */ + public int setTimeout(int timeout) + { + int oldTimeout = this.timeout; + this.timeout = timeout; + + return oldTimeout; + } + /** * Set the HTTP Method. Default is "GET". * @@ -355,8 +370,8 @@ public class HTTPRequest implements Serializable socket.setKeepAlive(false); socket.setReuseAddress(false); - socket.setSoTimeout(5000); - socket.connect(socketAddress, 5000); + socket.setSoTimeout(timeout); + socket.connect(socketAddress, timeout); final StringBuffer header = new StringBuffer(); diff --git a/src/de/openrat/client/OpenRatClient.java b/src/de/openrat/client/OpenRatClient.java @@ -2,6 +2,7 @@ package de.openrat.client; import java.io.File; import java.io.IOException; +import java.net.SocketTimeoutException; import java.util.ArrayList; import java.util.List; @@ -9,6 +10,8 @@ import org.json.JSONArray; import org.json.JSONException; import org.json.JSONObject; +import android.util.Log; + import de.openrat.android.blog.FolderEntry; import de.openrat.android.blog.FolderEntry.FType; import de.openrat.android.blog.util.FileUtils; @@ -133,6 +136,11 @@ public class OpenRatClient extends CMSRequest { response = super.performRequest(); } + catch (SocketTimeoutException e) + { + throw new OpenRatClientException( + "Timeout exceeded", e); + } catch (IOException e) { throw new OpenRatClientException( @@ -145,15 +153,32 @@ public class OpenRatClient extends CMSRequest try { - // Versuchen, die 1. Notice zu lesen. Falls es eine gibt, Exception mit dem Notice-Text werfen. - final String msgText = json.getJSONArray("notices") - .getJSONObject(0).getString("text"); - throw new OpenRatClientException(msgText); + // Versuchen, die 1. Notice zu lesen. Falls es eine gibt, + // Exception mit dem Notice-Text werfen. + final String status = json.getString("notice_status"); + + if (!status.equalsIgnoreCase("ok")) + { + String msgText; + try + { + msgText = json.getJSONArray("notices").getJSONObject(0) + .getString("text"); + } + catch (JSONException e) + { + msgText = "Not OK (Server response does not include \"OK\" and does not include a notice text"; + } + throw new OpenRatClientException(msgText); + } + else + return json; // Alles OK. } catch (JSONException e) { - // Keine Notice gefunden. Das deutet auf eine fehlerfreie Ausführung hin :) - return json; + // throw new OpenRatClientException( + // "Server error: Response does not include a attribute 'notice_status'.\n"+response); + return json; // Alles OK, kann passieren, wenn es keine Notices gibt. } } @@ -302,6 +327,59 @@ public class OpenRatClient extends CMSRequest throw new OpenRatClientException(e); } } + } + + public List<FolderEntry> loadProjects() throws IOException + { + + List<FolderEntry> data = new ArrayList<FolderEntry>(); + + super.clearParameters(); + super.setParameter("action", "index"); + super.setParameter("subaction", "projectmenu"); + JSONObject json = readJSON(); + + try + { + JSONArray projects = json.getJSONArray("projects"); + + for (int i = 0; i < projects.length(); i++) + { + JSONObject project = projects.getJSONObject(i); + + final FolderEntry entry = new FolderEntry(); + entry.type = FType.PROJECT; + entry.name = project.getString("name"); + entry.description = ""; + entry.id = project.getString("id"); + + data.add(entry); + + } + } + catch (JSONException e) + { + Log.e(this.getClass().getName(), e.getMessage(), e); + } + + return data; + } + + /** + * Wählt ein Projekt. + * + * @param projectid + * Projekt-ID. + * @throws IOException + */ + public void selectProject(String projectid) throws IOException + { + + super.clearParameters(); + super.setParameter("action", "index"); + super.setParameter("subaction", "project"); + super.setParameter("id", projectid); + readJSON(); } }