Commit 58beccea authored by Thomas Krex's avatar Thomas Krex
Browse files

- code kommentiert und aufgeräumt

-shape-klassen strukturiert
parent 5cd2ac8a
......@@ -14,24 +14,36 @@
android:ems="10"
android:hint="Url" />
<EditText
android:id="@+id/tx_name"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:ems="10"
android:hint="Name" />
<Button
android:id="@+id/button_save"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignParentRight="true"
android:layout_below="@+id/tx_url"
android:layout_centerHorizontal="true"
android:layout_marginRight="52dp"
android:layout_marginTop="32dp"
android:background="@android:color/transparent"
android:text="Save" />
<EditText
android:id="@+id/tx_name"
android:layout_width="match_parent"
<Button
android:id="@+id/button_paste"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBaseline="@+id/button_save"
android:layout_alignBottom="@+id/button_save"
android:layout_alignParentLeft="true"
android:layout_alignParentTop="true"
android:layout_marginTop="23dp"
android:ems="10"
android:hint="Name" />
android:layout_marginLeft="58dp"
android:background="@android:color/transparent"
android:text="Paste URL" />
</RelativeLayout>
\ No newline at end of file
......@@ -17,7 +17,10 @@
<string name="not_found">File was not found</string>
<string name="deleted">File was deleted</string>
<string name="delete_feed">Do you want to delete this feed? All Lectures saved on this devices will be deleted</string>
<string name="delete_lecture"> Do you want to delete this lecture from your devices?</string>
<string name="delete">Delete</string>
<string name="add_feed">Add a new Feed</string>
<string name="parse_fail_title">Connection Failed!!!</string>
<string name="parse_fail_message">Please make sure that the URL is correct and reachable</string>
</resources>
\ No newline at end of file
......@@ -3,12 +3,16 @@ package tttclient.activities;
import tttclient.models.Feed;
import tttclient.models.FeedDbManager;
import tttclient.models.FeedItemDbManager;
import android.annotation.SuppressLint;
import android.app.AlertDialog;
import android.app.Dialog;
import android.content.ClipData;
import android.content.Context;
import android.content.DialogInterface;
import android.content.DialogInterface.OnCancelListener;
import android.content.Intent;
import android.database.Cursor;
import android.os.Build;
import android.os.Bundle;
import android.os.Vibrator;
import android.util.Log;
......@@ -30,6 +34,17 @@ import com.actionbarsherlock.view.MenuItem;
import de.tum.in.tttclient.R;
/**
*
* @author Thomas Krex
*
* Activity for adding and Displaying Feeds. This class uses the
* FeedDbManger to store feeds and a Listview to display them.
*
* @see FeedDbManager, Feed
*
*
*/
public class FeedActivity extends SherlockActivity implements
OnItemClickListener, OnItemLongClickListener {
......@@ -41,13 +56,14 @@ public class FeedActivity extends SherlockActivity implements
EditText nameTxt;
private int feedId;
private Vibrator myVib;
private ListView feedLv;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feed_activity);
myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
ListView feedList = (ListView) findViewById(R.id.feed_list);
feedLv = (ListView) findViewById(R.id.feed_list);
FeedDbManager fManager = new FeedDbManager(this);
Cursor cursor = fManager.getAllFromDB();
......@@ -57,33 +73,75 @@ public class FeedActivity extends SherlockActivity implements
FeedDbManager.COLUMN_FEEDURL }, new int[] {
android.R.id.text1, android.R.id.text2 });
feedList.setAdapter(adapter);
feedLv.setAdapter(adapter);
View empty = findViewById(R.id.empty);
feedList.setEmptyView(empty);
feedList.setOnItemClickListener(this);
feedList.setOnItemLongClickListener(this);
feedLv.setEmptyView(empty);
feedLv.setOnItemClickListener(this);
feedLv.setOnItemLongClickListener(this);
// initializing Dialog for adding Feeds
addDialog = new Dialog(this);
addDialog.setContentView(R.layout.dialog_feeds);
urlTxt = (EditText) addDialog.findViewById(R.id.tx_url);
nameTxt = (EditText) addDialog.findViewById(R.id.tx_name);
addDialog.setTitle(R.string.add_feed);
addDialog.setOnCancelListener(new OnCancelListener() {
@Override
public void onCancel(DialogInterface dialog) {
// emptying textfields after canceling dialog
urlTxt.setText("");
nameTxt.setText("");
}
});
// Button for pasting Url from the clipboard into the textfield
Button pasteButton = (Button) addDialog.findViewById(R.id.button_paste);
pasteButton.setOnClickListener(new OnClickListener() {
/**
* handles Click on "Paste URL" Button
*/
@SuppressLint("NewApi")
@Override
public void onClick(View v) {
// ClipBoardManager moved from android.text to android.content.
if (android.os.Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
android.content.ClipboardManager clipboard = (android.content.ClipboardManager) getSystemService(Context.CLIPBOARD_SERVICE);
if (!clipboard.hasPrimaryClip()) {
Toast.makeText(getApplicationContext(),
"no URL in clipboard", Toast.LENGTH_SHORT)
.show();
} else {
ClipData.Item item = clipboard.getPrimaryClip()
.getItemAt(0);
CharSequence pasteText = item.getText();
urlTxt.setText(pasteText);
}
} else {
// pasting for ANdroid Gingerbread
android.text.ClipboardManager clipboard = (android.text.ClipboardManager) getSystemService(CLIPBOARD_SERVICE);
if (!clipboard.hasText())
Toast.makeText(getApplicationContext(),
"no URL in clipboard", Toast.LENGTH_SHORT)
.show();
else {
urlTxt.setText(clipboard.getText());
}
}
}
});
Button saveButton = (Button) addDialog.findViewById(R.id.button_save);
saveButton.setOnClickListener(new OnClickListener() {
@Override
public void onClick(View v) {
// name has to contains of at least one character
String name = nameTxt.getText().toString();
if (name.isEmpty()) {
Toast.makeText(getApplicationContext(),
......@@ -92,9 +150,8 @@ public class FeedActivity extends SherlockActivity implements
return;
}
// TODO validate url
String url = urlTxt.getText().toString();
// validate url
if (!URLUtil.isValidUrl(url)) {
Toast.makeText(getApplicationContext(),
"Please insert a valid Url", Toast.LENGTH_LONG)
......@@ -102,7 +159,8 @@ public class FeedActivity extends SherlockActivity implements
return;
}
// delete all whitespaces from ulr before inserting into the
// database
url.replaceAll("\\s+", "");
Feed feed = new Feed(name, url);
......@@ -116,21 +174,23 @@ public class FeedActivity extends SherlockActivity implements
}
// refresh list
adapter.changeCursor(fm.getAllFromDB());
// clear textfields of dialog
nameTxt.setText("");
urlTxt.setText("");
// close dialog
addDialog.dismiss();
}
});
Intent intent = getIntent();
String action = intent.getAction();
if (Intent.ACTION_VIEW.equals(action)) {
Log.d("IntentFIlter", intent.getDataString());
urlTxt.setText(intent.getDataString());
addDialog.show();
// Intent intent = getIntent();
// String action = intent.getAction();
// if (Intent.ACTION_VIEW.equals(action)) {
// Log.d("IntentFIlter", intent.getDataString());
// urlTxt.setText(intent.getDataString());
// addDialog.show();
}
// }
}
......@@ -163,13 +223,13 @@ public class FeedActivity extends SherlockActivity implements
.getColumnIndex(FeedDbManager.COLUMN_FEEDURL));
feedName = c.getString(c.getColumnIndex(FeedDbManager.COLUMN_NAME));
feedId = c.getInt(c.getColumnIndex("_id"));
}
Feed feed = new Feed(feedId, feedName, feedUrl);
Intent intent = new Intent(this, FeedDetailActivity.class);
intent.putExtra("feedUrl", feedUrl);
intent.putExtra("name", feedName);
intent.putExtra("id", feedId);
startActivity(intent);
// send Feed to FeedDetailsActivity for displaying items
Intent intent = new Intent(this, FeedDetailActivity.class);
intent.putExtra("feed", feed);
startActivity(intent);
}
}
@Override
......@@ -178,6 +238,7 @@ public class FeedActivity extends SherlockActivity implements
if (id == -1) {
return false;
}
// haptic feedback for longclick
myVib.vibrate(50);
// confirm delete
......@@ -198,6 +259,7 @@ public class FeedActivity extends SherlockActivity implements
adapter.changeCursor(fm.getAllFromDB());
}
};
// show alert dialog to get confirmed delete process
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setMessage(R.string.delete_feed);
builder.setPositiveButton(R.string.delete, listener);
......
......@@ -6,11 +6,12 @@ import java.util.ArrayList;
import org.apache.commons.io.FileUtils;
import tttclient.models.Feed;
import tttclient.models.FeedItem;
import tttclient.models.FeedItemDbManager;
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;
......@@ -39,24 +40,32 @@ import com.actionbarsherlock.app.SherlockActivity;
import de.tum.in.tttclient.R;
/**
* Activity parses feed items at each start via ParseService and stores them in
* the database via FeedItemDbManager. Can download lectures via DOwnloadService
* and start the replay.
*
* @author Thomas Krex
*
*/
public class FeedDetailActivity extends SherlockActivity implements
OnItemClickListener, OnItemLongClickListener {
private static String feedUrl;
private static String feedName;
private static int feedId;
private static String path;
private static String link;
private static String title;
public Feed feed;
ArrayList<FeedItem> itemList;
ParseService feedParser;
FeedItemDbManager manager;
SimpleCursorAdapter adapter;
ProgressDialog progressDialog;
ListView lv;
ListView feedItemLv;
Vibrator myVib;
/**
* receiver for ParseService + DownloadServiec Intents
*/
private final BroadcastReceiver receiver = new BroadcastReceiver() {
@Override
......@@ -65,6 +74,7 @@ public class FeedDetailActivity extends SherlockActivity implements
if (bundle != null) {
String action = intent.getAction();
// intent from DownloadService
if (action.equals(DownloadService.BROADCAST_NAME)) {
path = bundle.getString(DownloadService.FILE_PATH);
......@@ -73,23 +83,54 @@ public class FeedDetailActivity extends SherlockActivity implements
if (result == RESULT_OK) {
Log.d("Download", "complete " + path);
manager.updatePath(title, path, true);
adapter.changeCursor(manager.getAllFromDB(feedId));
adapter.changeCursor(manager.getAllFromDB(feed.getId()));
startPlayer();
}
lv.setClickable(true);
} else if (action.equals(ParseService.BROADCAST_NAME)) {
itemList = bundle.getParcelableArrayList("result");
} else
// TODO nicht erfolgreichen Download handeln
feedItemLv.setClickable(true);
try {
manager.insertintoDB(itemList, feedId);
adapter.changeCursor(manager.getAllFromDB(feedId));
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
// intent from ParseService
else if (action.equals(ParseService.BROADCAST_NAME)) {
itemList = bundle.getParcelableArrayList("list");
int result = bundle.getInt("result");
// insert FeedItems in db if parsing was successful
if (result == Activity.RESULT_OK) {
try {
manager.insertintoDB(itemList, feed.getId());
adapter.changeCursor(manager.getAllFromDB(feed
.getId()));
} catch (Exception e) {
e.printStackTrace();
}
} else {
// show alert dialog for unsucessful parsing, returns to
// FeedActivity
AlertDialog.Builder builder = new AlertDialog.Builder(
FeedDetailActivity.this);
builder.setCancelable(false);
builder.setTitle(R.string.parse_fail_title);
builder.setMessage(R.string.parse_fail_message);
builder.setPositiveButton("OK",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog,
int which) {
// closes Activity
finish();
}
});
AlertDialog dialog = builder.create();
dialog.show();
}
}
}
......@@ -102,6 +143,7 @@ public class FeedDetailActivity extends SherlockActivity implements
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.feed_details_activity);
// haptic feedback for longclick
myVib = (Vibrator) this.getSystemService(VIBRATOR_SERVICE);
// init progress dialog for showing downloading/unzipping progress
......@@ -113,20 +155,21 @@ public class FeedDetailActivity extends SherlockActivity implements
progressDialog.setCanceledOnTouchOutside(false);
progressDialog.setCancelable(false);
feedId = getIntent().getIntExtra("id", 0);
feedName = getIntent().getStringExtra("name");
feedUrl = getIntent().getStringExtra("feedUrl");
this.setTitle(feedName);
lv = (ListView) findViewById(R.id.feedDetailsLv);
lv.setChoiceMode(ListView.CHOICE_MODE_MULTIPLE);
// get feed from intent
feed = getIntent().getParcelableExtra("feed");
// set title of Activity
this.setTitle(feed.getName());
feedItemLv = (ListView) findViewById(R.id.feedDetailsLv);
// displaying items in listview
manager = new FeedItemDbManager(this);
Cursor cursor = manager.getAllFromDB(feedId);
Cursor cursor = manager.getAllFromDB(feed.getId());
adapter = new SimpleCursorAdapter(this, R.layout.list_feeditems,
cursor, new String[] { FeedItemDbManager.COLUMN_TITLE,
FeedItemDbManager.COLUMN_STATUS }, new int[] {
R.id.itemTitle, R.id.statusIcon });
// set Binder to change icon depending on download status
adapter.setViewBinder(new SimpleCursorAdapter.ViewBinder() {
@Override
......@@ -155,20 +198,11 @@ public class FeedDetailActivity extends SherlockActivity implements
return false;
}
});
lv.setAdapter(adapter);
lv.setOnItemClickListener(this);
lv.setOnItemLongClickListener(this);
Intent intent = new Intent(this, ParseService.class);
intent.putExtra(Consts.SERVICE_ID, Consts.PARSE_SERVICE_ID);
intent.putExtra(ParseService.URL_STRING, feedUrl);
if (isNetworkAvailable(getApplicationContext()))
startService(intent);
else
Toast.makeText(getApplicationContext(),
"Internet Connection needed", Toast.LENGTH_LONG).show();
feedItemLv.setAdapter(adapter);
feedItemLv.setOnItemClickListener(this);
feedItemLv.setOnItemLongClickListener(this);
parseFeed();
}
@Override
......@@ -204,7 +238,7 @@ public class FeedDetailActivity extends SherlockActivity implements
// checks whether file has to be downloaded or can be played
if (status == 0)
startDownloadService();
downloadLecture();
// checks if saved path is correct
else if (status == 1) {
if (checkPath(path, title))
......@@ -217,10 +251,9 @@ public class FeedDetailActivity extends SherlockActivity implements
try {
FileUtils.deleteDirectory(new File(path));
} catch (IOException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
startDownloadService();
downloadLecture();
}
}
......@@ -228,6 +261,9 @@ public class FeedDetailActivity extends SherlockActivity implements
}
}
/**
* sends intent with file name and path to PlayerActivity
*/
public void startPlayer() {
Intent playerIntent = new Intent(this, PlayerActivity.class);
playerIntent.putExtra(DownloadService.FILE_NAME, title);
......@@ -236,7 +272,10 @@ public class FeedDetailActivity extends SherlockActivity implements
}
public void startDownloadService() {
/**
* Starts DownloadService by sending an intent with link and title
*/
public void downloadLecture() {
// check if network connection is available
if (isNetworkAvailable(getApplicationContext())) {
......@@ -275,9 +314,10 @@ public class FeedDetailActivity extends SherlockActivity implements
}
// checks if the ttt file (title.ttt) is in the correspondig directory
// checks if the files title.ttt and title.mp3 are in the corresponding
// directory
public boolean checkPath(String path, String title) {
Log.d("Path", path);
File file = new File(path + "/" + title + ".ttt");
return file.exists();
......@@ -291,8 +331,17 @@ public class FeedDetailActivity extends SherlockActivity implements
return false;
}
final Cursor c = (Cursor) av.getAdapter().getItem(position);
int status = c
.getInt(c.getColumnIndex(FeedItemDbManager.COLUMN_STATUS));
// if lecture is not downloaded yet, it can't be deleted --> no dialog
// shows up
if (status == 0) {
return true;
}
myVib.vibrate(50);
// confirm delete
// listener for delete button of dialog
DialogInterface.OnClickListener listener = new DialogInterface.OnClickListener() {
@Override
......@@ -304,8 +353,6 @@ public class FeedDetailActivity extends SherlockActivity implements
Toast foundToast = Toast.makeText(FeedDetailActivity.this,
R.string.not_found, Toast.LENGTH_SHORT);
Cursor c = (Cursor) av.getAdapter().getItem(position);
String path = c.getString(c
.getColumnIndex(FeedItemDbManager.COLUMN_PATH));
String title = c.getString(c
......@@ -321,6 +368,7 @@ public class FeedDetailActivity extends SherlockActivity implements
}
try {
if (dir.exists()) {
// delete directory
FileUtils.deleteDirectory(dir);
deletedToast.show();
} else {
......@@ -333,20 +381,38 @@ public class FeedDetailActivity extends SherlockActivity implements
FeedItemDbManager fm = new FeedItemDbManager(av.getContext());
// refresh feed item list
// update path of feeditem in db
fm.updatePath(title, path, false);
adapter.changeCursor(fm.getAllFromDB(feedId));
// refresh feed item list
adapter.changeCursor(fm.getAllFromDB(feed.getId()));
}
};
// alert dialog for deleting recording
AlertDialog.Builder builder = new AlertDialog.Builder(
FeedDetailActivity.this);
builder.setMessage("delete?");
builder.setPositiveButton("delete", listener);
builder.setNegativeButton("cancel", null);
builder.setMessage(R.string.delete_lecture);
builder.setPositiveButton(R.string.delete, listener);
builder.setNegativeButton(R.string.cancel, null);
builder.show();
return true;
}
/**
* Stars ParseService by sending an intent with the feed's url
*/
public void parseFeed() {
Intent intent = new Intent(this, ParseService.class);