1 /* 2 * Copyright (C) 2009 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.sdklib.internal.repository; 18 19 import com.android.prefs.AndroidLocation; 20 import com.android.prefs.AndroidLocation.AndroidLocationException; 21 import com.android.sdklib.ISdkLog; 22 23 import java.io.File; 24 import java.io.FileInputStream; 25 import java.io.FileOutputStream; 26 import java.io.IOException; 27 import java.util.ArrayList; 28 import java.util.Iterator; 29 import java.util.Properties; 30 31 /** 32 * A list of sdk-repository sources. 33 */ 34 public class RepoSources { 35 36 private static final String KEY_COUNT = "count"; 37 38 private static final String KEY_SRC = "src"; 39 40 private static final String SRC_FILENAME = "repositories.cfg"; //$NON-NLS-1$ 41 42 private ArrayList<RepoSource> mSources = new ArrayList<RepoSource>(); 43 44 public RepoSources() { 45 } 46 47 /** 48 * Adds a new source to the Sources list. 49 */ 50 public void add(RepoSource source) { 51 mSources.add(source); 52 } 53 54 /** 55 * Removes a source from the Sources list. 56 */ 57 public void remove(RepoSource source) { 58 mSources.remove(source); 59 } 60 61 /** 62 * Returns the sources list array. This is never null. 63 */ 64 public RepoSource[] getSources() { 65 return mSources.toArray(new RepoSource[mSources.size()]); 66 } 67 68 /** 69 * Loads all user sources. This <em>replaces</em> all existing user sources 70 * by the ones from the property file. 71 */ 72 public void loadUserSources(ISdkLog log) { 73 74 // Remove all existing user sources 75 for (Iterator<RepoSource> it = mSources.iterator(); it.hasNext(); ) { 76 RepoSource s = it.next(); 77 if (s.isUserSource()) { 78 it.remove(); 79 } 80 } 81 82 // Load new user sources from property file 83 FileInputStream fis = null; 84 try { 85 String folder = AndroidLocation.getFolder(); 86 File f = new File(folder, SRC_FILENAME); 87 if (f.exists()) { 88 fis = new FileInputStream(f); 89 90 Properties props = new Properties(); 91 props.load(fis); 92 93 int count = Integer.parseInt(props.getProperty(KEY_COUNT, "0")); 94 95 for (int i = 0; i < count; i++) { 96 String url = props.getProperty(String.format("%s%02d", KEY_SRC, i)); //$NON-NLS-1$ 97 if (url != null) { 98 RepoSource s = new RepoSource(url, true /*userSource*/); 99 if (!hasSource(s)) { 100 mSources.add(s); 101 } 102 } 103 } 104 } 105 106 } catch (NumberFormatException e) { 107 log.error(e, null); 108 109 } catch (AndroidLocationException e) { 110 log.error(e, null); 111 112 } catch (IOException e) { 113 log.error(e, null); 114 115 } finally { 116 if (fis != null) { 117 try { 118 fis.close(); 119 } catch (IOException e) { 120 } 121 } 122 } 123 } 124 125 /** 126 * Returns true if there's already a similar source in the sources list. 127 * <p/> 128 * The search is O(N), which should be acceptable on the expectedly small source list. 129 */ 130 public boolean hasSource(RepoSource source) { 131 for (RepoSource s : mSources) { 132 if (s.equals(source)) { 133 return true; 134 } 135 } 136 return false; 137 } 138 139 /** 140 * Saves all the user sources. 141 * @param log Logger. Cannot be null. 142 */ 143 public void saveUserSources(ISdkLog log) { 144 FileOutputStream fos = null; 145 try { 146 String folder = AndroidLocation.getFolder(); 147 File f = new File(folder, SRC_FILENAME); 148 149 fos = new FileOutputStream(f); 150 151 Properties props = new Properties(); 152 153 int count = 0; 154 for (RepoSource s : mSources) { 155 if (s.isUserSource()) { 156 count++; 157 props.setProperty(String.format("%s%02d", KEY_SRC, count), s.getUrl()); //$NON-NLS-1$ 158 } 159 } 160 props.setProperty(KEY_COUNT, Integer.toString(count)); 161 162 props.store( fos, "## User Sources for Android tool"); //$NON-NLS-1$ 163 164 } catch (AndroidLocationException e) { 165 log.error(e, null); 166 167 } catch (IOException e) { 168 log.error(e, null); 169 170 } finally { 171 if (fos != null) { 172 try { 173 fos.close(); 174 } catch (IOException e) { 175 } 176 } 177 } 178 179 } 180 } 181