SDK Updater: logic to load/save user sources and to delete them.

This commit is contained in:
Raphael
2009-06-25 17:13:57 -07:00
parent 7b2bb7452f
commit 426ecc9efd
6 changed files with 185 additions and 20 deletions

View File

@@ -47,7 +47,7 @@ import javax.xml.validation.Validator;
public class RepoSource implements IDescription {
private final String mUrl;
private final boolean mAddonOnly;
private final boolean mUserSource;
private Package[] mPackages;
private String mDescription;
@@ -55,12 +55,18 @@ public class RepoSource implements IDescription {
/**
* Constructs a new source for the given repository URL.
*/
public RepoSource(String url, boolean addonOnly) {
public RepoSource(String url, boolean userSource) {
mUrl = url;
mAddonOnly = addonOnly;
mUserSource = userSource;
setDefaultDescription();
}
/** Returns true if this is a user source. We only load addon and extra packages
* from a user source and ignore the rest. */
public boolean isUserSource() {
return mUserSource;
}
/** Returns the URL of the repository.xml file for this source. */
public String getUrl() {
return mUrl;
@@ -138,7 +144,7 @@ public class RepoSource implements IDescription {
}
private void setDefaultDescription() {
if (mAddonOnly) {
if (mUserSource) {
mDescription = String.format("Add-on Source: %1$s", mUrl);
} else {
mDescription = String.format("SDK Source: %1$s", mUrl);
@@ -274,13 +280,17 @@ public class RepoSource implements IDescription {
Package p = null;
try {
// We can load addon and extra packages from all sources, either
// internal or user sources.
if (SdkRepository.NODE_ADD_ON.equals(name)) {
p = new AddonPackage(this, child, licenses);
} else if (SdkRepository.NODE_EXTRA.equals(name)) {
p = new ExtraPackage(this, child, licenses);
} else if (!mAddonOnly) {
} else if (!mUserSource) {
// We only load platform, doc and tool packages from internal
// sources, never from user sources.
if (SdkRepository.NODE_PLATFORM.equals(name)) {
p = new PlatformPackage(this, child, licenses);
} else if (SdkRepository.NODE_DOC.equals(name)) {

View File

@@ -16,13 +16,28 @@
package com.android.sdklib.internal.repository;
import com.android.prefs.AndroidLocation;
import com.android.prefs.AndroidLocation.AndroidLocationException;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Iterator;
import java.util.Properties;
/**
* A list of sdk-repository sources.
*/
public class RepoSources {
private static final String KEY_COUNT = "count";
private static final String KEY_SRC = "src";
private static final String SRC_FILENAME = "repositories.cfg"; //$NON-NLS-1$
private ArrayList<RepoSource> mSources = new ArrayList<RepoSource>();
public RepoSources() {
@@ -35,10 +50,113 @@ public class RepoSources {
mSources.add(source);
}
/**
* Removes a source from the Sources list.
*/
public void remove(RepoSource source) {
mSources.remove(source);
}
/**
* Returns the sources list array. This is never null.
*/
public ArrayList<RepoSource> getSources() {
return mSources;
public RepoSource[] getSources() {
return mSources.toArray(new RepoSource[mSources.size()]);
}
/**
* Loads all user sources. This <em>replaces</em> all existing user sources
* by the ones from the property file.
*/
public void loadUserSources() {
// Remove all existing user sources
for (Iterator<RepoSource> it = mSources.iterator(); it.hasNext(); ) {
RepoSource s = it.next();
if (s.isUserSource()) {
it.remove();
}
}
// Load new user sources from property file
FileInputStream fis = null;
try {
String folder = AndroidLocation.getFolder();
File f = new File(folder, SRC_FILENAME);
if (f.exists()) {
fis = new FileInputStream(f);
Properties props = new Properties();
props.load(fis);
int count = Integer.parseInt(props.getProperty(KEY_COUNT, "0"));
for (int i = 0; i < count; i++) {
String url = props.getProperty(String.format("%s%02d", KEY_SRC, count)); //$NON-NLS-1$
if (url != null) {
mSources.add(new RepoSource(url, true /*userSource*/));
}
}
}
} catch (NumberFormatException e) {
// TODO print to log
e.printStackTrace();
} catch (AndroidLocationException e) {
// TODO print to log
e.printStackTrace();
} catch (IOException e) {
// TODO print to log
e.printStackTrace();
} finally {
if (fis != null) {
try {
fis.close();
} catch (IOException e) {
}
}
}
}
/**
* Saves all the user sources.
*/
public void saveUserSources() {
FileOutputStream fos = null;
try {
String folder = AndroidLocation.getFolder();
File f = new File(folder, SRC_FILENAME);
fos = new FileOutputStream(f);
Properties props = new Properties();
int count = 0;
for (RepoSource s : mSources) {
if (s.isUserSource()) {
count++;
props.setProperty(String.format("%s%02d", KEY_SRC, count), s.getUrl()); //$NON-NLS-1$
}
}
props.setProperty(KEY_COUNT, Integer.toString(count));
props.store( fos, "## User Sources for Android tool"); //$NON-NLS-1$
} catch (AndroidLocationException e) {
// TODO print to log
e.printStackTrace();
} catch (IOException e) {
// TODO print to log
e.printStackTrace();
} finally {
if (fos != null) {
try {
fos.close();
} catch (IOException e) {
}
}
}
}
}