Skip to content

Commit 8ecdad5

Browse files
committed
adjustable cache size
1 parent 741f26b commit 8ecdad5

File tree

5 files changed

+43
-22
lines changed

5 files changed

+43
-22
lines changed

res/values/strings.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -61,6 +61,7 @@
6161
<string name="reconnect">Переподключаться</string>
6262
<string name="reconnect_count">Кол-во попыток</string>
6363
<string name="reconnect_timeout">Таймаут (сек)</string>
64+
<string name="cache_size">Размер кэша (мин)</string>
6465

6566
<string name="debug_preferences">Отладка</string>
6667
<string name="debug_log_enabled">Лог</string>

res/xml/preferences.xml

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,15 @@
7777
android:entryValues="@array/reconnect_timeout"
7878
android:dialogTitle="@string/reconnect_timeout"/>
7979

80+
<EditTextPreference
81+
android:key="cache_size_key"
82+
android:numeric="integer"
83+
android:maxLength="2"
84+
android:defaultValue="5"
85+
android:title="@string/cache_size"
86+
android:dialogTitle="@string/cache_size"
87+
/>
88+
8089
</android.preference.PreferenceCategory>
8190

8291
<android.preference.PreferenceCategory android:title="@string/debug_preferences">

src/ru/piter/fm/activities/SettingsActivity.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ public class SettingsActivity extends PreferenceActivity implements SharedPrefer
2323
private CheckBoxPreference reconnectPref;
2424
private ListPreference reconnectCountPref;
2525
private ListPreference reconnectTimeoutPref;
26+
private EditTextPreference cacheSizePref;
2627
private CheckBoxPreference debugLogPref;
2728

2829
@Override
@@ -37,6 +38,12 @@ public void onCreate(Bundle savedInstanceState) {
3738
reconnectPref = (CheckBoxPreference) getPreferenceScreen().findPreference(Settings.RECONNECT);
3839
reconnectCountPref = (ListPreference) getPreferenceScreen().findPreference(Settings.RECONNECT_COUNT);
3940
reconnectTimeoutPref = (ListPreference) getPreferenceScreen().findPreference(Settings.RECONNECT_TIMEOUT);
41+
cacheSizePref = (EditTextPreference) getPreferenceScreen().findPreference(Settings.CACHE_SIZE);
42+
cacheSizePref.setOnPreferenceChangeListener(new Preference.OnPreferenceChangeListener() {@Override
43+
public boolean onPreferenceChange(Preference preference, Object newValue) {
44+
return Integer.parseInt((String) newValue) >= 2;
45+
}
46+
});
4047
debugLogPref = (CheckBoxPreference) getPreferenceScreen().findPreference(Settings.DEBUG_LOG_ENABLED);
4148
debugLogPref.setSummary(Utils.LOG_DIR.getAbsolutePath() + '/');
4249
}
@@ -57,6 +64,7 @@ protected void onResume() {
5764
reconnectPref.setSummary(Settings.isReconnect() ? on : off);
5865
reconnectCountPref.setSummary(String.valueOf(Settings.getReconnectCount()));
5966
reconnectTimeoutPref.setSummary(String.valueOf(Settings.getReconnectTimeout()));
67+
cacheSizePref.setSummary(String.valueOf(Settings.getCacheSize()));
6068

6169

6270
getPreferenceScreen().getSharedPreferences().registerOnSharedPreferenceChangeListener(this);
@@ -90,6 +98,8 @@ public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, Strin
9098
reconnectCountPref.setSummary(sharedPreferences.getString(Settings.RECONNECT_COUNT, "-1"));
9199
} else if (key.equals(Settings.RECONNECT_TIMEOUT)) {
92100
reconnectTimeoutPref.setSummary(sharedPreferences.getString(Settings.RECONNECT_TIMEOUT, "-1"));
101+
} else if (key.equals(Settings.CACHE_SIZE)) {
102+
cacheSizePref.setSummary(sharedPreferences.getString(Settings.CACHE_SIZE, "-1"));
93103
}
94104

95105
}

src/ru/piter/fm/util/PiterFMCachingDownloader.java

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -98,9 +98,6 @@ public class PiterFMCachingDownloader {
9898
/** global object instance */
9999
public static final PiterFMCachingDownloader INSTANCE = new PiterFMCachingDownloader();
100100

101-
/** cache size */
102-
private static final int READ_AHEAD_NUM = 10;
103-
104101
/**
105102
* a dummy non-null value for {@link CacheEntry#m_url} to ensure the entry
106103
* is not picked by {@link #getFile(String, TrackCalendar)}. null can't be
@@ -114,26 +111,14 @@ public class PiterFMCachingDownloader {
114111
private final Object lock = new Object();
115112

116113
/** holds queued URLs */
117-
private ArrayList<String> urlQueue = new ArrayList<String>(READ_AHEAD_NUM);
114+
private ArrayList<String> urlQueue = new ArrayList<String>();
118115

119116
/** holds Cache Entries */
120-
private CacheEntry[] entries = new CacheEntry[READ_AHEAD_NUM];
117+
private ArrayList<CacheEntry> entries = new ArrayList<CacheEntry>();
121118

122119
/** current URL for download. If differs from thread's URL, then thread waits. Can be null. */
123120
private String currentUrl;
124121

125-
/** constant map of entries by path. See also {@link #byUrl(String)} */
126-
private final HashMap<String, CacheEntry> byPath = new HashMap<String, CacheEntry>(READ_AHEAD_NUM);
127-
128-
{
129-
for (int i = 0; i < READ_AHEAD_NUM; i++) {
130-
urlQueue.add(null);
131-
CacheEntry entry = new CacheEntry(i);
132-
entries[i] = entry;
133-
byPath.put(entry.file.getAbsolutePath(), entry);
134-
}
135-
}
136-
137122
/**
138123
* Get and lock a file with the provided params. This function is
139124
* synchronous and may block forever, if 5 other files stay locked.
@@ -148,13 +133,20 @@ public String getFile(String channelId, TrackCalendar trackTime) throws Interrup
148133
String trackUrl;
149134
// update the queue. Not queued entries may become victims, if not locked
150135
String prefix = CHANNEL_PREFIX + channelId + "/mp4/";
151-
for (int i = 0;; i++) {
136+
int cacheSize = Settings.getCacheSize();
137+
urlQueue.clear();
138+
urlQueue.ensureCapacity(cacheSize);
139+
for (int i = cacheSize - 1;; i--) {
152140
trackUrl = prefix + trackTime.asURLPart() + ".mp4";
153-
urlQueue.set(i, trackUrl);
154-
if (i == READ_AHEAD_NUM - 1)
141+
urlQueue.add(trackUrl);
142+
if (i == 0)
155143
break;
156144
trackTime.nextTrackTime();
157145
}
146+
entries.ensureCapacity(cacheSize);
147+
for (int i = entries.size(); i < cacheSize; i++) {
148+
entries.add(new CacheEntry(i));
149+
}
158150
resumeOrCreateNextDownloadNoLock();
159151
lock.notifyAll();
160152

@@ -197,7 +189,8 @@ public void releaseFile(String path, boolean badFile) {
197189
Log.v(Tag, funcname + ",synchronized before, dummyNo:197"); try {
198190
synchronized (lock) {
199191
Log.v(Tag, funcname + ",synchronized in, dummyNo:199");
200-
CacheEntry entry = byPath.get(path);
192+
String sIndex = path.substring(path.lastIndexOf('/') + 1, path.length() - 4);
193+
CacheEntry entry = entries.get(Integer.parseInt(sIndex));
201194
assertNull(entry.m_fos);
202195
assertTrue(entry.omniCount > 0);
203196
entry.omniCount--;
@@ -234,7 +227,10 @@ private void resumeOrCreateNextDownloadNoLock() {
234227

235228
// rank victim candidates. null > scheduled > complete unrefed
236229
entry = null;
237-
for (CacheEntry candidate : entries) {
230+
// CacheEntry candidate : entries1
231+
int cacheSize = urlQueue.size();
232+
for (int i = 0; i < cacheSize; i++) {
233+
CacheEntry candidate = entries.get(i);
238234
if (candidate.m_url == null) {
239235
entry = candidate; // best choice
240236
break;

src/ru/piter/fm/util/Settings.java

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ public class Settings {
2222
public static final String RECONNECT = "reconnect_key";
2323
public static final String RECONNECT_COUNT = "reconnect_count_key";
2424
public static final String RECONNECT_TIMEOUT = "reconnect_timeout_key";
25+
public static final String CACHE_SIZE = "cache_size_key";
2526
public static final String DEBUG_LOG_ENABLED = "debug_log_enabled_key";
2627

2728

@@ -72,4 +73,8 @@ public static int getReconnectCount(){
7273
public static int getReconnectTimeout(){
7374
return getInt(RECONNECT_TIMEOUT);
7475
}
76+
77+
public static int getCacheSize() {
78+
return getInt(CACHE_SIZE);
79+
}
7580
}

0 commit comments

Comments
 (0)