Skip to content

Commit 78f2768

Browse files
committed
More cleanup and polishing
1 parent 32814ee commit 78f2768

File tree

8 files changed

+124
-103
lines changed

8 files changed

+124
-103
lines changed

res/values-ja/strings.xml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,7 +40,7 @@
4040
<string name="sort_by_date">日時順リスト</string>
4141
<string name="sort_by_black_player">先手名順リスト</string>
4242
<string name="sort_by_white_player">後手名順リスト</string>
43-
<string name="sort_by_player">棋士名順リスト</string>
43+
<string name="sort_by_player">対局者順リスト</string>
4444
<string name="sort_by_number_of_games">対局数順リスト</string>
4545
<string name="sort_by_tournament">棋戦順リスト</string>
4646
<string name="sort_by_opening_moves">戦型順リスト</string>
@@ -79,4 +79,6 @@
7979
<string name="opening_moves">戦型</string>
8080
<string name="search">検索</string>
8181
<string name="unspecified">(指定なし)</string>
82+
<string name="player_list">対局者リスト</string>
83+
<string name="query_result">検索結果</string>
8284
</resources>

res/values/strings.xml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -76,4 +76,6 @@
7676
<string name="opening_moves">Opening moves</string>
7777
<string name="search">Search</string>
7878
<string name="unspecified">(Unspecified)</string>
79+
<string name="player_list">Player list</string>
80+
<string name="query_result">Query result</string>
7981
</resources>

src/com/ysaito/shogi/ExternalCacheManager.java

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,8 @@ public class ExternalCacheManager {
2424
private static final String TAG = "ExtenalCacheManager";
2525
private final File mDir;
2626
private final Context mContext;
27-
private static final int MAX_CACHE_STALENESS_MS = 7 * 24 * 3600 * 1000; // 1 week
27+
public static final int MAX_STATIC_PAGE_CACHE_STALENESS_MS = 7 * 24 * 3600 * 1000; // 1 week
28+
public static final int MAX_QUERY_CACHE_STALENSS_MS = 24 * 3600 * 1000; // 1 day
2829

2930
// Cache key -> last access time
3031
private HashMap<String, Long> mLastAccessTimes;
@@ -70,7 +71,7 @@ private ExternalCacheManager(Context context) {
7071
mContext = context;
7172
mDir = context.getExternalCacheDir();
7273
mDir.mkdirs();
73-
Log.d(TAG, "CACHE=" + mDir.getAbsolutePath());
74+
Log.d(TAG, "Cache=" + mDir.getAbsolutePath());
7475
mLastAccessTimes = new HashMap<String, Long>();
7576

7677
FileInputStream summary_in = null;
@@ -148,7 +149,9 @@ public static class ReadResult {
148149
public boolean needRefresh;
149150
}
150151

151-
public synchronized ReadResult read(String suppliedKey) {
152+
public synchronized ReadResult read(
153+
String suppliedKey,
154+
int maxCacheStalenessMillis) {
152155
final String key = toKey(suppliedKey);
153156
final long now = System.currentTimeMillis();
154157
ReadResult r = new ReadResult();
@@ -162,7 +165,7 @@ public synchronized ReadResult read(String suppliedKey) {
162165
CacheEntry ent = (CacheEntry)(new ObjectInputStream(data_in).readObject());
163166
if (ent != null) {
164167
r.obj = ent.obj;
165-
r.needRefresh = (now - ent.createMs >= MAX_CACHE_STALENESS_MS);
168+
r.needRefresh = (now - ent.createMs >= maxCacheStalenessMillis);
166169
}
167170
mLastAccessTimes.put(key, now);
168171
saveSummary();

src/com/ysaito/shogi/GenericListUpdater.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,25 @@ public void sort(Comparator<T> sorter) {
7878
private final Env<T> mEnv;
7979
private final ExternalCacheManager mCache;
8080
private final String mCacheKey;
81+
private final int mMaxCacheStalenessMillis;
8182
private final ProgressBar mProgressBar;
83+
private final T[] mTmpArray;
8284

83-
public GenericListUpdater(Env<T> env,
85+
public GenericListUpdater(
86+
Env<T> env,
8487
Context context,
8588
String cacheKey,
86-
ProgressBar progressBar) {
89+
int maxCacheStalenessMillis,
90+
ProgressBar progressBar,
91+
T[] tmpArray) {
8792
mContext = context;
8893
mAdapter = new MyAdapter(context);
8994
mEnv = env;
9095
mCache = ExternalCacheManager.getInstance(context.getApplicationContext());
96+
mMaxCacheStalenessMillis = maxCacheStalenessMillis;
9197
mCacheKey = cacheKey;
9298
mProgressBar = progressBar;
99+
mTmpArray = tmpArray;
93100
if (mProgressBar != null) mProgressBar.setVisibility(View.INVISIBLE);
94101
}
95102

@@ -127,14 +134,21 @@ public ListingStatus(T[] o, boolean d) {
127134
public final boolean deleteExistingObjects;
128135
}
129136

130-
private class ParallelFetcher {
131-
private ExecutorService mThreads;
132-
private ArrayList<T[]> mResults;
133-
private boolean[] mDone;
137+
/**
138+
* Helper class for fetching multiple streams, as provided by myEnv, in parallel.
139+
*
140+
* The results are always yielded in the order listed in myEnv.
141+
*/
142+
private static class ParallelFetcher<T> {
143+
private final Env<T> mEnv;
144+
private final ExecutorService mThreads;
145+
private final ArrayList<T[]> mResults;
146+
private final boolean[] mDone;
134147
private int mNextIndexToRead;
135148
private String mError;
136149

137-
public ParallelFetcher() {
150+
public ParallelFetcher(Env<T> env) {
151+
mEnv = env;
138152
final int numStreams = mEnv.numStreams();
139153
mNextIndexToRead = 0;
140154
int numThreads = numStreams;
@@ -154,6 +168,8 @@ public void shutdown() {
154168
mThreads.shutdown();
155169
}
156170

171+
public synchronized boolean hasNext() { return mNextIndexToRead < mDone.length; }
172+
157173
public synchronized T[] next() {
158174
while (!mDone[mNextIndexToRead]) {
159175
try {
@@ -163,12 +179,10 @@ public synchronized T[] next() {
163179
}
164180
T[] r = mResults.get(mNextIndexToRead);
165181
++mNextIndexToRead;
166-
Log.d(TAG, "RETURN: " + String.valueOf(mNextIndexToRead) + ": " + String.valueOf(r != null ? r.length : -1));
167182
return r;
168183
}
169184

170185
private synchronized void yieldResult(int index, T[] r) {
171-
Log.d(TAG, "Yield : " + String.valueOf(index) + ": " + String.valueOf(r != null ? r.length : -1));
172186
mResults.set(index, r);
173187
mDone[index] = true;
174188
notify();
@@ -179,11 +193,9 @@ private synchronized void setError(String e) {
179193
}
180194

181195
private class Fetcher implements Runnable {
182-
final int mIndex;
196+
private final int mIndex;
183197

184-
public Fetcher(int index) {
185-
mIndex = index;
186-
}
198+
public Fetcher(int index) { mIndex = index; }
187199

188200
public void run() {
189201
T[] objs = null;
@@ -200,51 +212,47 @@ public void run() {
200212
}
201213
}
202214

203-
/**
204-
* @param mode either FORCE_RELOAD or MAY_READ_FROM_CACHE
205-
*
206-
* @return an error message, or null on success
207-
*/
208215
private class ListThread extends AsyncTask<Integer, ListingStatus<T>, String> {
216+
/**
217+
* @param mode either FORCE_RELOAD or MAY_READ_FROM_CACHE
218+
*
219+
* @return an error message, or null on success
220+
*/
209221
@Override
210222
protected String doInBackground(Integer... mode) {
211-
ParallelFetcher fetcher = null;
223+
ParallelFetcher<T> fetcher = null;
212224
try {
213225
try {
214226
ExternalCacheManager.ReadResult r;
215227
if (mCacheKey == null || mode[0] == FORCE_RELOAD) {
216228
r = new ExternalCacheManager.ReadResult();
217229
r.needRefresh = true;
218230
} else {
219-
r = mCache.read(mCacheKey);
231+
r = mCache.read(mCacheKey, mMaxCacheStalenessMillis);
220232
}
221233
final boolean hitCache = (r.obj != null);
222234
if (hitCache) {
223-
Log.d(TAG, "Found cache");
224235
publishProgress(new ListingStatus<T>((T[])r.obj, false));
225236
}
226237
if (r.needRefresh) {
227238
ArrayList<T> aggregate = new ArrayList<T>();
228-
T[] objs = null;
229-
fetcher = new ParallelFetcher();
230-
231-
final int numStreams = mEnv.numStreams();
232-
for (int i = 0; i < numStreams; ++i) {
233-
objs = fetcher.next();
239+
fetcher = new ParallelFetcher<T>(mEnv);
240+
while (fetcher.hasNext()) {
241+
T[] objs = fetcher.next();
234242
if (objs != null) {
235243
for (T obj: objs) aggregate.add(obj);
236244
if (!hitCache) {
237245
// Incrementally update the screen as results arrive
238246
publishProgress(new ListingStatus<T>(objs, false));
239247
} else {
240-
// If the screet was already filled with a stale cache,
248+
// If the screen was already filled with a stale cache,
241249
// buffer the new contents until it is complete, so that
242250
// we don't have delete the screen contents midway.
243251
}
244252
}
245253
}
246254
if (fetcher.error() == null) {
247-
objs = aggregate.toArray(objs);
255+
T[] objs = aggregate.toArray(mTmpArray);
248256
if (mCacheKey != null) mCache.write(mCacheKey, objs);
249257
if (hitCache) {
250258
publishProgress(new ListingStatus<T>(objs, true));

src/com/ysaito/shogi/OptusGameLogListActivity.java

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -39,19 +39,27 @@ public void onCreate(Bundle savedInstanceState) {
3939

4040
final Bundle bundle = getIntent().getExtras();
4141
final OptusParser.Player player = (OptusParser.Player)bundle.getSerializable("player");
42-
42+
final OptusParser.LogRef tmpArray[] = new OptusParser.LogRef[0];
43+
4344
if (player != null) {
4445
setTitle(supportsCustomTitle, player.name);
46+
final ProgressBar progressBar = (ProgressBar)findViewById(R.id.title_bar_with_progress_progress); /*could be null*/
4547
mUpdater = new GenericListUpdater<OptusParser.LogRef>(
46-
new PlayerEnv(player), this, player.name,
47-
(ProgressBar)findViewById(R.id.title_bar_with_progress_progress)/*could be null*/);
48+
new PlayerEnv(player), this,
49+
player.name /*cache key*/,
50+
ExternalCacheManager.MAX_STATIC_PAGE_CACHE_STALENESS_MS,
51+
progressBar, tmpArray);
4852
} else {
4953
final OptusParser.SearchParameters mSearch =
5054
(OptusParser.SearchParameters)bundle.getSerializable("search");
51-
setTitle(supportsCustomTitle, "query");
55+
setTitle(supportsCustomTitle,
56+
getResources().getString(R.string.query_result));
57+
final ProgressBar progressBar = (ProgressBar)findViewById(R.id.title_bar_with_progress_progress); /*could be null*/
5258
mUpdater = new GenericListUpdater<OptusParser.LogRef>(
53-
new SearchEnv(mSearch), this, null,
54-
(ProgressBar)findViewById(R.id.title_bar_with_progress_progress)/*could be null*/);
59+
new SearchEnv(mSearch), this,
60+
mSearch.toString() /*cache key*/,
61+
ExternalCacheManager.MAX_QUERY_CACHE_STALENSS_MS,
62+
progressBar, tmpArray);
5563
}
5664
setListAdapter(mUpdater.adapter());
5765
mUpdater.startListing(GenericListUpdater.MAY_READ_FROM_CACHE);
@@ -149,7 +157,7 @@ public String getListLabel(OptusParser.LogRef l) {
149157

150158
@Override
151159
public OptusParser.LogRef[] readNthStream(int index) throws Throwable {
152-
return OptusParser.listLogRefs(mPlayer.hrefs[index]);
160+
return OptusParser.listLogsForPlayer(mPlayer.hrefs[index]);
153161
}
154162
}
155163

@@ -170,7 +178,7 @@ public String getListLabel(OptusParser.LogRef l) {
170178

171179
@Override
172180
public OptusParser.LogRef[] readNthStream(int index) throws Throwable {
173-
return OptusParser.runQuery(mSearch);
181+
return OptusParser.runSearch(mSearch);
174182
}
175183
}
176184

@@ -219,7 +227,9 @@ protected DownloadResult doInBackground(OptusParser.LogRef... logRefs) {
219227
final String cacheKey = text_href;
220228
DownloadResult dr = new DownloadResult();
221229
try {
222-
ExternalCacheManager.ReadResult r = mCache.read(cacheKey);
230+
ExternalCacheManager.ReadResult r = mCache.read(
231+
cacheKey,
232+
ExternalCacheManager.MAX_STATIC_PAGE_CACHE_STALENESS_MS);
223233
if (r.obj != null) {
224234
Log.d(TAG, "Found cache");
225235
dr.log = (GameLog)r.obj;

0 commit comments

Comments
 (0)