Use temporary local storage while downloading and processing index, instead of SD

This commit is contained in:
Ciaran Gultnieks 2010-11-13 11:59:24 +00:00
parent 87ea1dfc3f
commit d6c4402356
3 changed files with 27 additions and 23 deletions

View File

@ -410,7 +410,7 @@ public class FDroid extends TabActivity implements OnItemClickListener {
|| netstate.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED) { || netstate.getNetworkInfo(0).getState() == NetworkInfo.State.CONNECTED) {
new Thread() { new Thread() {
public void run() { public void run() {
RepoXMLHandler.doUpdates(db); RepoXMLHandler.doUpdates(FDroid.this, db);
update_handler.sendEmptyMessage(0); update_handler.sendEmptyMessage(0);
} }
}.start(); }.start();

View File

@ -37,6 +37,7 @@ import org.xml.sax.SAXException;
import org.xml.sax.XMLReader; import org.xml.sax.XMLReader;
import org.xml.sax.helpers.DefaultHandler; import org.xml.sax.helpers.DefaultHandler;
import android.content.Context;
import android.util.Log; import android.util.Log;
public class RepoXMLHandler extends DefaultHandler { public class RepoXMLHandler extends DefaultHandler {
@ -181,11 +182,8 @@ public class RepoXMLHandler extends DefaultHandler {
} }
} }
private static String LOCAL_PATH = "/sdcard/.fdroid";
private static String XML_PATH = LOCAL_PATH + "/repotemp.xml";
// Returns the number of applications with updates. // Returns the number of applications with updates.
public static int doUpdates(DB db) { public static int doUpdates(Context ctx, DB db) {
db.beginUpdate(); db.beginUpdate();
Vector<DB.Repo> repos = db.getRepos(); Vector<DB.Repo> repos = db.getRepos();
for (DB.Repo repo : repos) { for (DB.Repo repo : repos) {
@ -193,17 +191,15 @@ public class RepoXMLHandler extends DefaultHandler {
try { try {
File f = new File(XML_PATH); FileOutputStream f = ctx.openFileOutput(
if (f.exists()) "tempindex.xml", Context.MODE_PRIVATE);
f.delete();
// Download the index file from the repo... // Download the index file from the repo...
BufferedInputStream getit = new BufferedInputStream( BufferedInputStream getit = new BufferedInputStream(
new URL(repo.address + "/index.xml").openStream()); new URL(repo.address + "/index.xml").openStream());
FileOutputStream saveit = new FileOutputStream(XML_PATH); BufferedOutputStream bout = new BufferedOutputStream(f,
BufferedOutputStream bout = new BufferedOutputStream( 1024);
saveit, 1024);
byte data[] = new byte[1024]; byte data[] = new byte[1024];
int readed = getit.read(data, 0, 1024); int readed = getit.read(data, 0, 1024);
@ -213,24 +209,27 @@ public class RepoXMLHandler extends DefaultHandler {
} }
bout.close(); bout.close();
getit.close(); getit.close();
saveit.close(); f.close();
// Process the index... // Process the index...
SAXParserFactory spf = SAXParserFactory.newInstance(); SAXParserFactory spf = SAXParserFactory.newInstance();
SAXParser sp = spf.newSAXParser(); SAXParser sp = spf.newSAXParser();
XMLReader xr = sp.getXMLReader(); XMLReader xr = sp.getXMLReader();
RepoXMLHandler handler = new RepoXMLHandler(repo.address, db); RepoXMLHandler handler = new RepoXMLHandler(repo.address,
db);
xr.setContentHandler(handler); xr.setContentHandler(handler);
InputStreamReader isr = new FileReader(new File(XML_PATH)); InputStreamReader isr = new FileReader(new File(ctx
.getFilesDir()
+ "/tempindex.xml"));
InputSource is = new InputSource(isr); InputSource is = new InputSource(isr);
xr.parse(is); xr.parse(is);
File xml_file = new File(XML_PATH);
xml_file.delete();
} catch (Exception e) { } catch (Exception e) {
Log.d("FDroid", "Exception updating from " + repo.address Log.d("FDroid", "Exception updating from " + repo.address
+ " - " + e.getMessage()); + " - " + e.getMessage());
} finally {
ctx.deleteFile("tempindex.xml");
} }
} }

View File

@ -106,7 +106,8 @@ public class UpdateService extends Service {
DB db = null; DB db = null;
try { try {
db = new DB(getBaseContext()); db = new DB(getBaseContext());
int updateNum = RepoXMLHandler.doUpdates(db); int updateNum = RepoXMLHandler.doUpdates(getBaseContext(),
db);
if (updateNum != 0) { if (updateNum != 0) {
// We have updates. // We have updates.
@ -120,9 +121,13 @@ public class UpdateService extends Service {
Context context = getApplicationContext(); Context context = getApplicationContext();
CharSequence contentTitle = "FDroid"; CharSequence contentTitle = "FDroid";
CharSequence contentText = "Updates are available."; CharSequence contentText = "Updates are available.";
Intent notificationIntent = new Intent(UpdateService.this, FDroid.class); Intent notificationIntent = new Intent(
PendingIntent contentIntent = PendingIntent.getActivity(UpdateService.this, 0, notificationIntent, 0); UpdateService.this, FDroid.class);
notification.setLatestEventInfo(context, contentTitle, contentText, contentIntent); PendingIntent contentIntent = PendingIntent
.getActivity(UpdateService.this, 0,
notificationIntent, 0);
notification.setLatestEventInfo(context,
contentTitle, contentText, contentIntent);
notification.flags |= Notification.FLAG_AUTO_CANCEL; notification.flags |= Notification.FLAG_AUTO_CANCEL;
n.notify(1, notification); n.notify(1, notification);
} }