Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

RSSimple - Use internal storage.

  • Loading branch information...
commit d14656f0bf4d2858cdf555e59c2d7af7efb0fe7b 1 parent 2fb65e8
@josepht authored
View
16 src/com/xenno/android/RSSimple.java
@@ -24,13 +24,10 @@
ArrayAdapter<String> aa;
File file;
RSSimpleFeedList feeds;
+ ListActivity activity = this;
private void loadFeeds() {
- String state = Environment.getExternalStorageState();
-
- if (Environment.MEDIA_MOUNTED.equals(state)) {
- feeds.getFeeds();
- }
+ feeds.getFeeds();
ArrayList<String> feed_list = new ArrayList<String>();
feed_list.add("Add new entry...");
@@ -53,14 +50,7 @@ public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
- String state = Environment.getExternalStorageState();
- if (Environment.MEDIA_MOUNTED.equals(state)) {
- file = new File(getExternalFilesDir(null), "RSSimpleFeeds.txt");
- } else {
- file = null;
- }
-
- feeds = new RSSimpleFeedList(file);
+ feeds = new RSSimpleFeedList(activity);
loadFeeds();
final Intent intent = new Intent(this, RSSimpleFeed.class);
View
13 src/com/xenno/android/RSSimpleFeed.java
@@ -31,7 +31,7 @@
private String data;
ArrayAdapter<RSSEntry> aa;
ArrayList<RSSEntry> feed_entries;
- File file;
+ ListActivity activity = this;
public RSSimpleFeed() throws Exception {
parser = new RSSParser();
@@ -69,14 +69,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
}
private void remove_feed(String feed_url) {
- RSSimpleFeedList feed_list = new RSSimpleFeedList(file);
+ RSSimpleFeedList feed_list = new RSSimpleFeedList(activity);
feed_list.getFeeds();
feed_list.removeFeed(feed_url);
this.finish();
}
private void remove_all_feeds() {
- RSSimpleFeedList feed_list = new RSSimpleFeedList(file);
+ RSSimpleFeedList feed_list = new RSSimpleFeedList(activity);
feed_list.getFeeds();
feed_list.removeAllFeeds();
this.finish();
@@ -90,13 +90,6 @@ public void onCreate(Bundle savedInstanceState)
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 {
url = new URL(intent.getStringExtra("url"));
} catch (MalformedURLException e) {
View
50 src/com/xenno/android/RSSimpleFeedAdd.java
@@ -34,50 +34,12 @@ public void onCreate(Bundle savedInstanceState)
add_button.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
- boolean mExternalStorageAvailable = false;
- boolean mExternalStorageWriteable = false;
- String state = Environment.getExternalStorageState();
-
- if (Environment.MEDIA_MOUNTED.equals(state)) {
- // We can read and write the media
- 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);
- }
-*/
- }
+ RSSimpleFeedList feed_list = new RSSimpleFeedList(activity);
+ feed_list.getFeeds();
+ TextView tv = (TextView) findViewById(R.id.new_feed);
+ String feed = tv.getText().toString();
+ feed_list.addFeed(tv.getText().toString());
+ activity.finish();
}
});
}
View
87 src/com/xenno/android/RSSimpleFeedList.java
@@ -13,15 +13,17 @@
import android.os.Environment;
import android.util.Log;
+import android.content.Context;
public class RSSimpleFeedList {
ArrayList<String> feeds;
- File file;
+ final String file = "RSSimpleFeeds.txt";
+ Context context;
- public RSSimpleFeedList(File file) {
+ public RSSimpleFeedList(Context context) {
feeds = new ArrayList<String>();
- this.file = file;
+ this.context = context;
}
public String[] array() {
@@ -31,62 +33,43 @@ public RSSimpleFeedList(File file) {
}
public void getFeeds() {
- String state = Environment.getExternalStorageState();
feeds.clear();
- if (Environment.MEDIA_MOUNTED.equals(state) && file != null) {
- try {
- InputStream is = new FileInputStream(file);
- byte[] buf = new byte[is.available()];
- is.read(buf);
- String[] lines = new String(buf).split("\\n");
-
- for (int i = 0; i < lines.length; i++) {
- if (lines[i].length() > 0)
- feeds.add(lines[i]);
- }
-
- is.close();
- } catch (FileNotFoundException e) {
- Log.w("ExternalStorage", "Can't find " + file, e);
- } catch (IOException e) {
- Log.w("ExternalStorage", "Error writing " + file, e);
- }
+ try {
+ InputStream is = context.openFileInput(file);
+ byte[] buf = new byte[is.available()];
+ is.read(buf);
+ String[] lines = new String(buf).split("\\n");
+
+ for (int i = 0; i < lines.length; i++) {
+ if (lines[i].length() > 0)
+ feeds.add(lines[i]);
+ }
+
+ is.close();
+ } catch (FileNotFoundException e) {
+ Log.w("InternalStorage", "Can't find " + file, e);
+ } catch (IOException e) {
+ Log.w("InternalStorage", "Error writing " + file, e);
}
}
public void saveFeeds() {
- boolean available = false;
- boolean writeable = false;
- String state = Environment.getExternalStorageState();
-
- if (Environment.MEDIA_MOUNTED.equals(state)) {
- available = writeable = true;
- } else if (Environment.MEDIA_MOUNTED_READ_ONLY.equals(state)) {
- available = true;
- writeable = false;
- } 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);
+ try {
+ OutputStream os = context.openFileOutput(file, Context.MODE_PRIVATE);
+
+ 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);
}
}
Please sign in to comment.
Something went wrong with that request. Please try again.