Commit c5e680f4 authored by Thomas Krex's avatar Thomas Krex

-lesen des Files in AsyncTask verlagert

-anzeigen des DownloadProzesses in der Mache, androind download manager
passt nicht ins Konzept
parent 83dad761
......@@ -10,7 +10,7 @@
</uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<application
......
......@@ -10,7 +10,7 @@
</uses-permission>
<uses-sdk
android:minSdkVersion="8"
android:minSdkVersion="9"
android:targetSdkVersion="19" />
<application
......
......@@ -5,11 +5,13 @@ import java.util.ArrayList;
import tttclient.models.FeedItem;
import tttclient.models.FeedItemDbManager;
import tttclient.services.DownloadReceiver;
import tttclient.services.DownloadService;
import tttclient.services.ParseService;
import tttclient.utils.Consts;
import android.app.Activity;
import android.app.AlertDialog;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
......@@ -17,13 +19,13 @@ import android.content.Intent;
import android.content.IntentFilter;
import android.database.Cursor;
import android.os.Bundle;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.AdapterView.OnItemLongClickListener;
import android.widget.ListView;
import android.widget.ProgressBar;
import android.widget.SimpleCursorAdapter;
import com.example.test.R;
......@@ -37,12 +39,13 @@ public class FeedDetailActivity extends Activity implements
private static String path;
private static String link;
private static String title;
private static String length;
ArrayList<FeedItem> itemList;
ParseService feedParser;
FeedItemDbManager manager;
SimpleCursorAdapter adapter;
ProgressBar pBar;
public ProgressDialog progressDialog;
private final BroadcastReceiver receiver = new BroadcastReceiver() {
......@@ -54,7 +57,6 @@ public class FeedDetailActivity extends Activity implements
Log.d("Receiver", "intent received from " + action);
if (action.equals(DownloadService.BROADCAST_NAME)) {
pBar.setVisibility(View.GONE);
path = bundle.getString(DownloadService.FILE_PATH);
int result = bundle.getInt(DownloadService.FILE_RESULT);
......@@ -69,6 +71,7 @@ public class FeedDetailActivity extends Activity implements
}
} else if (action.equals(ParseService.BROADCAST_NAME)) {
itemList = bundle.getParcelableArrayList("result");
try {
manager.insertintoDB(itemList, feedId);
adapter.changeCursor(manager.getAllFromDB());
......@@ -91,7 +94,11 @@ public class FeedDetailActivity extends Activity implements
super.onCreate(savedInstanceState);
setContentView(R.layout.feed_details_activity);
pBar = (ProgressBar) findViewById(R.id.progressBar);
progressDialog = new ProgressDialog(FeedDetailActivity.this);
progressDialog.setMessage("Downloading File");
progressDialog.setIndeterminate(true);
progressDialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
// progressDialog.setCancelable(true);
feedId = getIntent().getStringExtra("id");
feedName = getIntent().getStringExtra("name");
......@@ -141,8 +148,10 @@ public class FeedDetailActivity extends Activity implements
if (position != -1) {
Cursor c = (Cursor) av.getAdapter().getItem(position);
path = c.getString(c.getColumnIndex(FeedItemDbManager.COLUMN_PATH));
link = c.getString(c.getColumnIndex("link"));
title = c.getString(c.getColumnIndex("title"));
link = c.getString(c.getColumnIndex(FeedItemDbManager.COLUMN_LINK));
title = c.getString(c
.getColumnIndex(FeedItemDbManager.COLUMN_TITLE));
if (path != null) {
if (checkPath(path)) {
startPlayer();
......@@ -171,13 +180,16 @@ public class FeedDetailActivity extends Activity implements
}
public void startDownloadService() {
progressDialog.show();
Intent intent = new Intent(this, DownloadService.class);
intent.putExtra(DownloadService.FILE_NAME, title);
intent.putExtra(DownloadService.FILE_URL, link);
intent.putExtra(DownloadService.PROGRESS_RECEIVER,
new DownloadReceiver(new Handler()));
// Start download service
startService(intent);
pBar.setVisibility(View.VISIBLE);
}
......
......@@ -7,8 +7,10 @@ import tttclient.services.DownloadService;
import tttclient.ttt.Recording;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.ProgressDialog;
import android.media.MediaPlayer;
import android.net.Uri;
import android.os.AsyncTask;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
......@@ -75,43 +77,69 @@ public class PlayerActivity extends Activity implements MediaPlayerControl {
audioController = new MediaController(this);
audioController.setMediaPlayer(this);
audioController.setHorizontalScrollBarEnabled(false);
final ProgressDialog dialog = new ProgressDialog(PlayerActivity.this);
dialog.setTitle("Please Wait");
dialog.setMessage("Prepare File ...");
dialog.setProgressStyle(ProgressDialog.STYLE_SPINNER);
// final ProgressDialog dialog = new ProgressDialog(
// getApplicationContext());
// dialog.setMessage("Reading File");
// dialog.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
AsyncTask<Void, Void, Void> initTask = new AsyncTask<Void, Void, Void>() {
// dialog.show();
try {
recording = new Recording(getApplicationContext(), tttFile,
audioPlayer, imgV, surfaceView, scrollView, screenWidth,
screenHeight);
@Override
protected void onPreExecute() {
// TODO Auto-generated method stub
super.onPreExecute();
dialog.show();
}
OnClickListener prevListener = new OnClickListener() {
@Override
protected Void doInBackground(Void... params) {
try {
recording = new Recording(getApplicationContext(), tttFile,
audioPlayer, imgV, surfaceView, scrollView,
screenWidth, screenHeight);
OnClickListener prevListener = new OnClickListener() {
@Override
public void onClick(View v) {
recording.previous();
}
};
OnClickListener nextListener = new OnClickListener() {
@Override
public void onClick(View v) {
recording.next();
}
};
audioController.setPrevNextListeners(nextListener,
prevListener);
audioController.setAnchorView(imgV);
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
@Override
public void onClick(View v) {
recording.previous();
return null;
}
}
};
OnClickListener nextListener = new OnClickListener() {
@Override
protected void onPostExecute(Void result) {
@Override
public void onClick(View v) {
recording.next();
dialog.dismiss();
audioController.show();
}
}
};
audioController.setPrevNextListeners(nextListener, prevListener);
};
initTask.execute();
audioController.setAnchorView(imgV);
// dialog.show();
// dialog.dismiss();
// dialog.dismiss();
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
// android.os.Message message = recording.graphicsContext().sizeHandler
// .obtainMessage(0, imgVArray);
// message.sendToTarget();
......
......@@ -4,7 +4,6 @@ import tttclient.utils.DBManager;
import android.content.Context;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;
public class FeedDbManager {
......@@ -22,10 +21,10 @@ public class FeedDbManager {
public FeedDbManager(Context context) {
db = DBManager.getDB(context);
Log.d("SQL", TABLE_CREATE);
// Log.d("SQL", TABLE_CREATE);
db.execSQL("CREATE TABLE IF NOT EXISTS feeds ("
+ "id INTEGER PRIMARY KEY AUTOINCREMENT, name VARCHAR, feedUrl VARCHAR)");
Log.d("SQL", this.getAllFromDB().getColumnNames().toString());
// Log.d("SQL", this.getAllFromDB().getColumnNames().toString());
// db.execSQL(TABLE_CREATE);
}
......
......@@ -8,6 +8,7 @@ public class FeedItem implements Parcelable {
private String link;
private String description;
private String pubDate;
private String length;
// private String[] enclosure;
......@@ -43,10 +44,19 @@ public class FeedItem implements Parcelable {
this.pubDate = pupDate;
}
public String getLength() {
return length;
}
public void setLength(String length) {
this.length = length;
}
@Override
public String toString() {
return "FeedItem [title=" + title + ", link=" + link + ", description="
+ description + ", pupDate=" + pubDate + "]";
+ description + ", pupDate=" + pubDate + ", length=" + length
+ "]";
}
public FeedItem copy() {
......@@ -55,6 +65,7 @@ public class FeedItem implements Parcelable {
copy.link = link;
copy.description = description;
copy.pubDate = pubDate;
copy.length = length;
return copy;
}
......@@ -73,6 +84,7 @@ public class FeedItem implements Parcelable {
link = in.readString();
description = in.readString();
pubDate = in.readString();
length = in.readString();
}
@Override
......@@ -81,6 +93,7 @@ public class FeedItem implements Parcelable {
dest.writeString(link);
dest.writeString(description);
dest.writeString(pubDate);
dest.writeString(length);
}
......
......@@ -28,16 +28,17 @@ public class FeedItemDbManager {
public FeedItemDbManager(Context context) {
db = DBManager.getDB(context);
Log.d("SQL", TABLE_CREATE);
// Log.d("SQL", TABLE_CREATE);
db.execSQL(TABLE_CREATE);
}
public Cursor getAllFromDB() {
Log.d("SQL", "SELECT " + COLUMN_TITLE + ", " + COLUMN_ID + " as _id , "
+ COLUMN_FEEDID + ", " + COLUMN_LINK + ", "
+ COLUMN_DESCRIPTION + ", " + COLUMN_PUBDATE + ", path"
+ " FROM " + TABLE_FEEDITEMS + " ORDER BY " + COLUMN_PUBDATE);
// Log.d("SQL", "SELECT " + COLUMN_TITLE + ", " + COLUMN_ID +
// " as _id , "
// + COLUMN_FEEDID + ", " + COLUMN_LINK + ", "
// + COLUMN_DESCRIPTION + ", " + COLUMN_PUBDATE + ", path"
// + " FROM " + TABLE_FEEDITEMS + " ORDER BY " + COLUMN_PUBDATE);
return db
.rawQuery(
"SELECT title, id as _id , feedId, link, description, pubDate, path FROM feedItems ORDER BY pubDate",
......
package tttclient.services;
import android.os.Bundle;
import android.os.Handler;
import android.os.ResultReceiver;
public class DownloadReceiver extends ResultReceiver {
public DownloadReceiver(Handler handler) {
super(handler);
}
@Override
protected void onReceiveResult(int resultCode, Bundle resultData) {
super.onReceiveResult(resultCode, resultData);
// if (resultCode == DownloadService.PROGRESS_UPDATE) {
// ProgressDialog pDialog = (ProgressDialog) progressDialog;
// int progress = resultData.getInt("progress");
//
// pDialog.setProgress(progress);
// if (progress == 100) {
// pDialog.dismiss();
// }
// }
}
}
......@@ -9,12 +9,17 @@ import java.net.HttpURLConnection;
import java.net.URL;
import tttclient.utils.UnzipManager;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.IntentService;
import android.app.ProgressDialog;
import android.content.Intent;
import android.os.Build;
import android.os.Bundle;
import android.os.ResultReceiver;
import android.util.Log;
@TargetApi(Build.VERSION_CODES.HONEYCOMB)
public class DownloadService extends IntentService {
public DownloadService() {
......@@ -23,6 +28,8 @@ public class DownloadService extends IntentService {
public static final String BROADCAST_NAME = "com.example.services.downloadService";
private int result = Activity.RESULT_CANCELED;
public static int PROGRESS_UPDATE = 1234;
public static String PROGRESS_RECEIVER = "progress_update";
public static String FILE_URL = "url";
public static String FILE_NAME = "name";
public static String FILE_PATH = "path";
......@@ -35,6 +42,9 @@ public class DownloadService extends IntentService {
Log.d("DownloadService", "started");
String urlString = intent.getStringExtra(FILE_URL);
String fileName = intent.getStringExtra(FILE_NAME);
ResultReceiver receiver = (ResultReceiver) intent
.getParcelableExtra(PROGRESS_RECEIVER);
InputStream input = null;
OutputStream output = null;
HttpURLConnection connection = null;
......@@ -47,6 +57,7 @@ public class DownloadService extends IntentService {
connection.connect();
int fileLength = connection.getContentLength();
Log.d("Donwload Service", "filelength: " + fileLength);
// download the file
File root = android.os.Environment.getExternalStorageDirectory();
......@@ -58,6 +69,20 @@ public class DownloadService extends IntentService {
if (lectureDir.exists() == false) {
lectureDir.mkdirs();
}
// use the Android DownlaodManager
// DownloadManager.Request request = new DownloadManager.Request(
// Uri.parse(urlString));
// request.setDescription("loading Lecture for TTT CLient");
// request.setTitle(fileName);
// request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
// request.setDestinationInExternalFilesDir(getApplicationContext(),
// lectureDir.getAbsolutePath(), fileName + ".zip");
//
// DownloadManager manager = (DownloadManager)
// getSystemService(Context.DOWNLOAD_SERVICE);
// manager.enqueue(request);
input = connection.getInputStream();
output = new FileOutputStream(lectureDir.getAbsolutePath() + "/"
+ fileName + ".zip");
......@@ -68,13 +93,17 @@ public class DownloadService extends IntentService {
// allow canceling with back button
total += count;
// publishing the progress....
Bundle resultData = new Bundle();
resultData.putInt("progress", (int) (total * 100 / fileLength));
receiver.send(PROGRESS_UPDATE, resultData);
if (fileLength > 0) // only if total length is known
// publishProgress((int) (total * 100 / fileLength));
output.write(data, 0, count);
Log.d("DownloadService", total + " bytes downloaded");
// Log.d("DownloadService", total + " bytes downloaded");
}
zipFile = new File(lectureDir.getAbsolutePath() + "/" + fileName
+ ".zip");
if (zipFile.exists()) {
......
......@@ -6,6 +6,8 @@ import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
import org.xml.sax.Attributes;
import tttclient.models.FeedItem;
import android.app.IntentService;
import android.content.Intent;
......@@ -13,6 +15,7 @@ import android.sax.Element;
import android.sax.EndElementListener;
import android.sax.EndTextElementListener;
import android.sax.RootElement;
import android.sax.StartElementListener;
import android.util.Log;
import android.util.Xml;
......@@ -59,47 +62,6 @@ public class ParseService extends IntentService {
final ArrayList<FeedItem> result = new ArrayList<FeedItem>();
RootElement root = new RootElement(RSS);
Element itemlist = root.getChild(CHANNEL);
// itemlist.getChild(TITLE).setEndTextElementListener(
// new EndTextElementListener() {
// @Override
// public void end(String body) {
// feed.setTitle(body);
// Log.d("feedtitle", body);
// }
// });
// itemlist.getChild(LINK).setEndTextElementListener(
// new EndTextElementListener() {
// @Override
// public void end(String body) {
// feed.setLink(body);
// Log.d("feedlink", body);
// }
// });
// itemlist.getChild(DESCRIPTION).setEndTextElementListener(
// new EndTextElementListener() {
// @Override
// public void end(String body) {
// feed.setDescription(body);
// Log.d("feeddesc", body);
// }
// });
// itemlist.getChild(PUB_DATE).setEndTextElementListener(
// new EndTextElementListener() {
// @Override
// public void end(String body) {
// feed.setPupDate(body);
// Log.d("feeddate", body);
// }
// });
//
// itemlist.getChild(LANGUAGE).setEndTextElementListener(
// new EndTextElementListener() {
// @Override
// public void end(String body) {
// feed.setLanguage(body);
// }
// });
Element item = itemlist.getChild(ITEM);
item.setEndElementListener(new EndElementListener() {
......@@ -136,6 +98,17 @@ public class ParseService extends IntentService {
feedItem.setPupDate(body);
}
});
Element enclosure = item.getChild(ENCLOSURE);
enclosure.setStartElementListener(new StartElementListener() {
@Override
public void start(Attributes attributes) {
feedItem.setLength(attributes.getValue(LENGTH));
Log.d("Parser", "length: " + attributes.getValue(LENGTH));
}
});
try {
Xml.parse(this.getInputStream(), Xml.Encoding.UTF_8,
root.getContentHandler());
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment