Skip to content

Commit

Permalink
RSSimple - Use internal storage.
Browse files Browse the repository at this point in the history
  • Loading branch information
josepht committed Apr 26, 2011
1 parent 2fb65e8 commit d14656f
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 119 deletions.
16 changes: 3 additions & 13 deletions src/com/xenno/android/RSSimple.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -24,13 +24,10 @@ public class RSSimple extends ListActivity
ArrayAdapter<String> aa; ArrayAdapter<String> aa;
File file; File file;
RSSimpleFeedList feeds; RSSimpleFeedList feeds;
ListActivity activity = this;


private void loadFeeds() { private void loadFeeds() {
String state = Environment.getExternalStorageState(); feeds.getFeeds();

if (Environment.MEDIA_MOUNTED.equals(state)) {
feeds.getFeeds();
}


ArrayList<String> feed_list = new ArrayList<String>(); ArrayList<String> feed_list = new ArrayList<String>();
feed_list.add("Add new entry..."); feed_list.add("Add new entry...");
Expand All @@ -53,14 +50,7 @@ public void onCreate(Bundle savedInstanceState)
{ {
super.onCreate(savedInstanceState); super.onCreate(savedInstanceState);


String state = Environment.getExternalStorageState(); feeds = new RSSimpleFeedList(activity);
if (Environment.MEDIA_MOUNTED.equals(state)) {
file = new File(getExternalFilesDir(null), "RSSimpleFeeds.txt");
} else {
file = null;
}

feeds = new RSSimpleFeedList(file);
loadFeeds(); loadFeeds();


final Intent intent = new Intent(this, RSSimpleFeed.class); final Intent intent = new Intent(this, RSSimpleFeed.class);
Expand Down
13 changes: 3 additions & 10 deletions src/com/xenno/android/RSSimpleFeed.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public class RSSimpleFeed extends ListActivity
private String data; private String data;
ArrayAdapter<RSSEntry> aa; ArrayAdapter<RSSEntry> aa;
ArrayList<RSSEntry> feed_entries; ArrayList<RSSEntry> feed_entries;
File file; ListActivity activity = this;


public RSSimpleFeed() throws Exception { public RSSimpleFeed() throws Exception {
parser = new RSSParser(); parser = new RSSParser();
Expand Down Expand Up @@ -69,14 +69,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
} }


private void remove_feed(String feed_url) { private void remove_feed(String feed_url) {
RSSimpleFeedList feed_list = new RSSimpleFeedList(file); RSSimpleFeedList feed_list = new RSSimpleFeedList(activity);
feed_list.getFeeds(); feed_list.getFeeds();
feed_list.removeFeed(feed_url); feed_list.removeFeed(feed_url);
this.finish(); this.finish();
} }


private void remove_all_feeds() { private void remove_all_feeds() {
RSSimpleFeedList feed_list = new RSSimpleFeedList(file); RSSimpleFeedList feed_list = new RSSimpleFeedList(activity);
feed_list.getFeeds(); feed_list.getFeeds();
feed_list.removeAllFeeds(); feed_list.removeAllFeeds();
this.finish(); this.finish();
Expand All @@ -90,13 +90,6 @@ public void onCreate(Bundle savedInstanceState)


final Intent intent = getIntent(); final Intent intent = getIntent();


String state = Environment.getExternalStorageState();
if (Environment.MEDIA_MOUNTED.equals(state)) {
file = new File(getExternalFilesDir(null), "RSSimpleFeeds.txt");
} else {
file = null;
}

try { try {
url = new URL(intent.getStringExtra("url")); url = new URL(intent.getStringExtra("url"));
} catch (MalformedURLException e) { } catch (MalformedURLException e) {
Expand Down
50 changes: 6 additions & 44 deletions src/com/xenno/android/RSSimpleFeedAdd.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -34,50 +34,12 @@ public void onCreate(Bundle savedInstanceState)
add_button.setOnClickListener(new View.OnClickListener() { add_button.setOnClickListener(new View.OnClickListener() {


public void onClick(View v) { public void onClick(View v) {
boolean mExternalStorageAvailable = false; RSSimpleFeedList feed_list = new RSSimpleFeedList(activity);
boolean mExternalStorageWriteable = false; feed_list.getFeeds();
String state = Environment.getExternalStorageState(); TextView tv = (TextView) findViewById(R.id.new_feed);

String feed = tv.getText().toString();
if (Environment.MEDIA_MOUNTED.equals(state)) { feed_list.addFeed(tv.getText().toString());
// We can read and write the media activity.finish();
mExternalStorageAvailable = mExternalStorageWriteable = true;
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
// We can only read the media
mExternalStorageAvailable = true;
mExternalStorageWriteable = false;
} else {
// Something else is wrong. It may be one of many other states, but all we need
// to know is we can neither read nor write
mExternalStorageAvailable = mExternalStorageWriteable = false;
}

if (mExternalStorageAvailable && mExternalStorageWriteable)
{

File file = new File(getExternalFilesDir(null), "RSSimpleFeeds.txt");
RSSimpleFeedList feed_list = new RSSimpleFeedList(file);
feed_list.getFeeds();
TextView tv = (TextView) findViewById(R.id.new_feed);
String feed = tv.getText().toString();
feed_list.addFeed(tv.getText().toString());
activity.finish();
/*
try {
OutputStream os = new FileOutputStream(file, true);
TextView tv = (TextView) findViewById(R.id.new_feed);
String feed = tv.getText().toString();
byte[] buf = new String(feed + "\n").getBytes();
// XXX: check URL and such here
os.write(buf);
os.close();
activity.finish();
} catch (FileNotFoundException e) {
Log.w("ExternalStorage", "Can't find " + file, e);
} catch (IOException e) {
Log.w("ExternalStorage", "Error writing " + file, e);
}
*/
}
} }
}); });
} }
Expand Down
87 changes: 35 additions & 52 deletions src/com/xenno/android/RSSimpleFeedList.java
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -13,15 +13,17 @@


import android.os.Environment; import android.os.Environment;
import android.util.Log; import android.util.Log;
import android.content.Context;


public class RSSimpleFeedList { public class RSSimpleFeedList {


ArrayList<String> feeds; ArrayList<String> feeds;
File file; final String file = "RSSimpleFeeds.txt";
Context context;


public RSSimpleFeedList(File file) { public RSSimpleFeedList(Context context) {
feeds = new ArrayList<String>(); feeds = new ArrayList<String>();
this.file = file; this.context = context;
} }


public String[] array() { public String[] array() {
Expand All @@ -31,62 +33,43 @@ public String[] array() {
} }


public void getFeeds() { public void getFeeds() {
String state = Environment.getExternalStorageState();
feeds.clear(); feeds.clear();


if (Environment.MEDIA_MOUNTED.equals(state) && file != null) { try {
try { InputStream is = context.openFileInput(file);
InputStream is = new FileInputStream(file); byte[] buf = new byte[is.available()];
byte[] buf = new byte[is.available()]; is.read(buf);
is.read(buf); String[] lines = new String(buf).split("\\n");
String[] lines = new String(buf).split("\\n");

for (int i = 0; i < lines.length; i++) {
for (int i = 0; i < lines.length; i++) { if (lines[i].length() > 0)
if (lines[i].length() > 0) feeds.add(lines[i]);
feeds.add(lines[i]); }
}

is.close();
is.close(); } catch (FileNotFoundException e) {
} catch (FileNotFoundException e) { Log.w("InternalStorage", "Can't find " + file, e);
Log.w("ExternalStorage", "Can't find " + file, e); } catch (IOException e) {
} catch (IOException e) { Log.w("InternalStorage", "Error writing " + file, e);
Log.w("ExternalStorage", "Error writing " + file, e);
}
} }
} }


public void saveFeeds() { public void saveFeeds() {
boolean available = false; try {
boolean writeable = false; OutputStream os = context.openFileOutput(file, Context.MODE_PRIVATE);
String state = Environment.getExternalStorageState();

String feed_list = new String("");
if (Environment.MEDIA_MOUNTED.equals(state)) { for (int i = 0; i < feeds.size(); i++)
available = writeable = true; feed_list += feeds.get(i) + "\n";
} else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) { if (feed_list.length() > 0) {
available = true; byte[] buf = feed_list.getBytes();
writeable = false; os.write(buf);
} else {
available = writeable = false;
}

if (available && writeable && file != null)
{
try {
OutputStream os = new FileOutputStream(file, false);

String feed_list = new String("");
for (int i = 0; i < feeds.size(); i++)
feed_list += feeds.get(i) + "\n";
if (feed_list.length() > 0) {
byte[] buf = feed_list.getBytes();
os.write(buf);
}
os.close();
} catch (FileNotFoundException e) {
Log.w("RSSimple/ExternalStorage", "Can't find " + file, e);
} catch (IOException e) {
Log.w("RSSimple/ExternalStorage", "Error writing " + file, e);
} }
os.close();
} catch (FileNotFoundException e) {
Log.w("RSSimple/ExternalStorage", "Can't find " + file, e);
} catch (IOException e) {
Log.w("RSSimple/ExternalStorage", "Error writing " + file, e);
} }
} }


Expand Down

0 comments on commit d14656f

Please sign in to comment.