Reduced number of FindBugs warnings

This commit is contained in:
daniel oeh 2013-08-19 15:12:44 +02:00
parent ffd1070270
commit fd5f1fcc0b
14 changed files with 207 additions and 258 deletions

View File

@ -4,6 +4,7 @@ import java.util.ArrayList;
import java.util.List;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.SearchManager;
import android.content.Intent;
import android.os.Bundle;
@ -140,34 +141,34 @@ public class SearchActivity extends ActionBarActivity implements AdapterView.OnI
@Override
public void run() {
Log.d(TAG, "Starting background work");
final Activity activity = SearchActivity.this;
final List<SearchResult> result = FeedSearcher
.performSearch(SearchActivity.this, query, feedID);
if (SearchActivity.this != null) {
SearchActivity.this.runOnUiThread(new Runnable() {
.performSearch(activity, query, feedID);
activity.runOnUiThread(new Runnable() {
@Override
public void run() {
if (AppConfig.DEBUG)
Log.d(TAG, "Background work finished");
if (AppConfig.DEBUG)
Log.d(TAG, "Found " + result.size()
+ " results");
@Override
public void run() {
if (AppConfig.DEBUG)
Log.d(TAG, "Background work finished");
if (AppConfig.DEBUG)
Log.d(TAG, "Found " + result.size()
+ " results");
searchAdapter.clear();
for (SearchResult s : result) {
searchAdapter.add(s);
}
searchAdapter.notifyDataSetChanged();
txtvStatus
.setText(R.string.search_status_no_results);
if (!searchAdapter.isEmpty()) {
txtvStatus.setVisibility(View.GONE);
} else {
txtvStatus.setVisibility(View.VISIBLE);
}
searchAdapter.clear();
for (SearchResult s : result) {
searchAdapter.add(s);
}
});
}
searchAdapter.notifyDataSetChanged();
txtvStatus
.setText(R.string.search_status_no_results);
if (!searchAdapter.isEmpty()) {
txtvStatus.setVisibility(View.GONE);
} else {
txtvStatus.setVisibility(View.VISIBLE);
}
}
});
}
};
thread.start();

View File

@ -144,53 +144,6 @@ public class ChapterListAdapter extends ArrayAdapter<Chapter> {
TextView link;
}
private LinkMovementMethod linkMovementMethod = new LinkMovementMethod() {
@Override
public boolean onTouchEvent(TextView widget, Spannable buffer,
MotionEvent event) {
Object text = widget.getText();
if (text instanceof Spanned) {
int action = event.getAction();
if (action == MotionEvent.ACTION_UP
|| action == MotionEvent.ACTION_DOWN) {
int x = (int) event.getX();
int y = (int) event.getY();
x -= widget.getTotalPaddingLeft();
y -= widget.getTotalPaddingTop();
x += widget.getScrollX();
y += widget.getScrollY();
Layout layout = widget.getLayout();
int line = layout.getLineForVertical(y);
int off = layout.getOffsetForHorizontal(line, x);
ClickableSpan[] link = buffer.getSpans(off, off,
ClickableSpan.class);
if (link.length != 0) {
if (action == MotionEvent.ACTION_UP) {
link[0].onClick(widget);
} else if (action == MotionEvent.ACTION_DOWN) {
Selection.setSelection(buffer,
buffer.getSpanStart(link[0]),
buffer.getSpanEnd(link[0]));
}
return true;
}
}
}
return false;
}
};
@Override
public int getCount() {
// ignore invalid chapters

View File

@ -77,7 +77,7 @@ public class ImageLoader {
});
}
public static ImageLoader getInstance() {
public static synchronized ImageLoader getInstance() {
if (singleton == null) {
singleton = new ImageLoader();
}

View File

@ -39,7 +39,7 @@ public class EventDistributor extends Observable {
events = new ConcurrentLinkedQueue<Integer>();
}
public static EventDistributor getInstance() {
public static synchronized EventDistributor getInstance() {
if (instance == null) {
instance = new EventDistributor();
}

View File

@ -87,11 +87,7 @@ public class Feed extends FeedFile {
*/
public Feed(String url, Date lastUpdate) {
super(null, url, false);
if (lastUpdate != null) {
this.lastUpdate = lastUpdate;
} else {
this.lastUpdate = null;
}
this.lastUpdate = (lastUpdate != null) ? (Date) lastUpdate.clone() : null;
}
/**
@ -323,19 +319,11 @@ public class Feed extends FeedFile {
}
public Date getLastUpdate() {
if (lastUpdate != null) {
return (Date) lastUpdate.clone();
} else {
return null;
}
return (lastUpdate != null) ? (Date) lastUpdate.clone() : null;
}
public void setLastUpdate(Date lastUpdate) {
if (lastUpdate != null) {
this.lastUpdate = (Date) lastUpdate.clone();
} else {
this.lastUpdate = null;
}
this.lastUpdate = (lastUpdate != null) ? (Date) lastUpdate.clone() : null;
}
public String getFeedIdentifier() {

View File

@ -265,9 +265,9 @@ public class FeedlistFragment extends Fragment implements
public boolean onItemLongClick(AdapterView<?> parent, View view,
int position, long id) {
Feed selection = fla.getItem(position);
if (AppConfig.DEBUG)
Log.d(TAG, "Selected Feed with title " + selection.getTitle());
if (selection != null) {
if (AppConfig.DEBUG)
Log.d(TAG, "Selected Feed with title " + selection.getTitle());
if (mActionMode != null) {
mActionMode.finish();
}

View File

@ -374,11 +374,10 @@ public class ItemDescriptionFragment extends Fragment {
Callable<String> shownotesLoadTask = shownotesProvider.loadShownotes();
final String shownotes = shownotesLoadTask.call();
data = "";
data = StringEscapeUtils.unescapeHtml4(shownotes);
Activity activity = getActivity();
if (activity != null) {
TypedArray res = getActivity()
TypedArray res = activity
.getTheme()
.obtainStyledAttributes(
new int[]{android.R.attr.textColorPrimary});

View File

@ -50,7 +50,6 @@ public class ItemlistFragment extends ListFragment {
public static final String EXTRA_SELECTED_FEEDITEM = "extra.de.danoeh.antennapod.activity.selected_feeditem";
public static final String ARGUMENT_FEED_ID = "argument.de.danoeh.antennapod.feed_id";
protected InternalFeedItemlistAdapter fila;
protected DownloadRequester requester = DownloadRequester.getInstance();
private Feed feed;
protected List<Long> queue;
@ -156,8 +155,6 @@ public class ItemlistFragment extends ListFragment {
} else {
Log.e(TAG, "Could not load queue");
}
if (result.getItems().isEmpty()) {
}
setEmptyViewIfListIsEmpty();
if (fila != null) {
fila.notifyDataSetChanged();

View File

@ -184,7 +184,7 @@ public class DownloadService extends Service {
public int onStartCommand(Intent intent, int flags, int startId) {
if (intent.getParcelableExtra(EXTRA_REQUEST) != null) {
onDownloadQueued(intent);
} else if (numberOfDownloads.equals(0)) {
} else if (numberOfDownloads.get() == 0) {
stopSelf();
}
return Service.START_NOT_STICKY;

View File

@ -29,170 +29,179 @@ import de.danoeh.antennapod.util.DownloadError;
import de.danoeh.antennapod.util.StorageUtils;
public class HttpDownloader extends Downloader {
private static final String TAG = "HttpDownloader";
private static final String TAG = "HttpDownloader";
private static final int MAX_REDIRECTS = 5;
private static final int MAX_REDIRECTS = 5;
private static final int BUFFER_SIZE = 8 * 1024;
private static final int CONNECTION_TIMEOUT = 30000;
private static final int SOCKET_TIMEOUT = 30000;
private static final int BUFFER_SIZE = 8 * 1024;
private static final int CONNECTION_TIMEOUT = 30000;
private static final int SOCKET_TIMEOUT = 30000;
public HttpDownloader(DownloadRequest request) {
super(request);
}
public HttpDownloader(DownloadRequest request) {
super(request);
}
private DefaultHttpClient createHttpClient() {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
params.setIntParameter("http.protocol.max-redirects", MAX_REDIRECTS);
params.setBooleanParameter("http.protocol.reject-relative-redirect",
false);
HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
HttpClientParams.setRedirecting(params, true);
private DefaultHttpClient createHttpClient() {
DefaultHttpClient httpClient = new DefaultHttpClient();
HttpParams params = httpClient.getParams();
params.setIntParameter("http.protocol.max-redirects", MAX_REDIRECTS);
params.setBooleanParameter("http.protocol.reject-relative-redirect",
false);
HttpConnectionParams.setSoTimeout(params, SOCKET_TIMEOUT);
HttpConnectionParams.setConnectionTimeout(params, CONNECTION_TIMEOUT);
HttpClientParams.setRedirecting(params, true);
// Workaround for broken URLs in redirection
((AbstractHttpClient) httpClient)
.setRedirectHandler(new APRedirectHandler());
return httpClient;
}
// Workaround for broken URLs in redirection
((AbstractHttpClient) httpClient)
.setRedirectHandler(new APRedirectHandler());
return httpClient;
}
@Override
protected void download() {
DefaultHttpClient httpClient = null;
BufferedOutputStream out = null;
InputStream connection = null;
try {
HttpGet httpGet = new HttpGet(request.getSource());
httpClient = createHttpClient();
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
int responseCode = response.getStatusLine().getStatusCode();
if (AppConfig.DEBUG)
Log.d(TAG, "Response code is " + responseCode);
if (responseCode == HttpURLConnection.HTTP_OK && httpEntity != null) {
if (StorageUtils.storageAvailable(PodcastApp.getInstance())) {
File destination = new File(request.getDestination());
if (!destination.exists()) {
connection = new BufferedInputStream(AndroidHttpClient
.getUngzippedContent(httpEntity));
out = new BufferedOutputStream(new FileOutputStream(
destination));
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
request.setStatusMsg(R.string.download_running);
if (AppConfig.DEBUG)
Log.d(TAG, "Getting size of download");
request.setSize(httpEntity.getContentLength());
if (AppConfig.DEBUG)
Log.d(TAG, "Size is " + request.getSize());
if (request.getSize() < 0) {
request.setSize(DownloadStatus.SIZE_UNKNOWN);
}
@Override
protected void download() {
DefaultHttpClient httpClient = null;
BufferedOutputStream out = null;
InputStream connection = null;
try {
HttpGet httpGet = new HttpGet(request.getSource());
httpClient = createHttpClient();
HttpResponse response = httpClient.execute(httpGet);
HttpEntity httpEntity = response.getEntity();
int responseCode = response.getStatusLine().getStatusCode();
if (AppConfig.DEBUG)
Log.d(TAG, "Response code is " + responseCode);
long freeSpace = StorageUtils.getFreeSpaceAvailable();
if (AppConfig.DEBUG)
Log.d(TAG, "Free space is " + freeSpace);
if (request.getSize() == DownloadStatus.SIZE_UNKNOWN
|| request.getSize() <= freeSpace) {
if (AppConfig.DEBUG)
Log.d(TAG, "Starting download");
while (!cancelled
&& (count = connection.read(buffer)) != -1) {
out.write(buffer, 0, count);
request.setSoFar(request.getSoFar() + count);
request.setProgressPercent((int) (((double) request
.getSoFar() / (double) request
.getSize()) * 100));
}
if (cancelled) {
onCancelled();
} else {
out.flush();
if (request.getSize() != DownloadStatus.SIZE_UNKNOWN &&
request.getSoFar() != request.getSize()) {
onFail(DownloadError.ERROR_IO_ERROR,
"Download completed but size: " +
request.getSoFar() +
" does not equal expected size " +
request.getSize());
return;
}
onSuccess();
}
} else {
onFail(DownloadError.ERROR_NOT_ENOUGH_SPACE, null);
}
} else {
Log.w(TAG, "File already exists");
onFail(DownloadError.ERROR_FILE_EXISTS, null);
}
} else {
onFail(DownloadError.ERROR_DEVICE_NOT_FOUND, null);
}
} else {
onFail(DownloadError.ERROR_HTTP_DATA_ERROR,
String.valueOf(responseCode));
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
onFail(DownloadError.ERROR_MALFORMED_URL, e.getMessage());
} catch (SocketTimeoutException e) {
e.printStackTrace();
onFail(DownloadError.ERROR_CONNECTION_ERROR, e.getMessage());
} catch (UnknownHostException e) {
e.printStackTrace();
onFail(DownloadError.ERROR_UNKNOWN_HOST, e.getMessage());
} catch (IOException e) {
e.printStackTrace();
onFail(DownloadError.ERROR_IO_ERROR, e.getMessage());
} catch (NullPointerException e) {
// might be thrown by connection.getInputStream()
e.printStackTrace();
onFail(DownloadError.ERROR_CONNECTION_ERROR, request.getSource());
} finally {
IOUtils.closeQuietly(out);
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
}
}
}
if (responseCode != HttpURLConnection.HTTP_OK || httpEntity == null) {
onFail(DownloadError.ERROR_HTTP_DATA_ERROR,
String.valueOf(responseCode));
return;
}
private void onSuccess() {
if (AppConfig.DEBUG)
Log.d(TAG, "Download was successful");
result.setSuccessful();
}
if (!StorageUtils.storageAvailable(PodcastApp.getInstance())) {
onFail(DownloadError.ERROR_DEVICE_NOT_FOUND, null);
return;
}
private void onFail(DownloadError reason, String reasonDetailed) {
if (AppConfig.DEBUG) {
Log.d(TAG, "Download failed");
}
File destination = new File(request.getDestination());
if (destination.exists()) {
Log.w(TAG, "File already exists");
onFail(DownloadError.ERROR_FILE_EXISTS, null);
return;
}
connection = new BufferedInputStream(AndroidHttpClient
.getUngzippedContent(httpEntity));
out = new BufferedOutputStream(new FileOutputStream(
destination));
byte[] buffer = new byte[BUFFER_SIZE];
int count = 0;
request.setStatusMsg(R.string.download_running);
if (AppConfig.DEBUG)
Log.d(TAG, "Getting size of download");
request.setSize(httpEntity.getContentLength());
if (AppConfig.DEBUG)
Log.d(TAG, "Size is " + request.getSize());
if (request.getSize() < 0) {
request.setSize(DownloadStatus.SIZE_UNKNOWN);
}
long freeSpace = StorageUtils.getFreeSpaceAvailable();
if (AppConfig.DEBUG)
Log.d(TAG, "Free space is " + freeSpace);
if (request.getSize() != DownloadStatus.SIZE_UNKNOWN
&& request.getSize() > freeSpace) {
onFail(DownloadError.ERROR_NOT_ENOUGH_SPACE, null);
return;
}
if (AppConfig.DEBUG)
Log.d(TAG, "Starting download");
while (!cancelled
&& (count = connection.read(buffer)) != -1) {
out.write(buffer, 0, count);
request.setSoFar(request.getSoFar() + count);
request.setProgressPercent((int) (((double) request
.getSoFar() / (double) request
.getSize()) * 100));
}
if (cancelled) {
onCancelled();
} else {
out.flush();
if (request.getSize() != DownloadStatus.SIZE_UNKNOWN &&
request.getSoFar() != request.getSize()) {
onFail(DownloadError.ERROR_IO_ERROR,
"Download completed but size: " +
request.getSoFar() +
" does not equal expected size " +
request.getSize());
return;
}
onSuccess();
}
} catch (IllegalArgumentException e) {
e.printStackTrace();
onFail(DownloadError.ERROR_MALFORMED_URL, e.getMessage());
} catch (SocketTimeoutException e) {
e.printStackTrace();
onFail(DownloadError.ERROR_CONNECTION_ERROR, e.getMessage());
} catch (UnknownHostException e) {
e.printStackTrace();
onFail(DownloadError.ERROR_UNKNOWN_HOST, e.getMessage());
} catch (IOException e) {
e.printStackTrace();
onFail(DownloadError.ERROR_IO_ERROR, e.getMessage());
} catch (NullPointerException e) {
// might be thrown by connection.getInputStream()
e.printStackTrace();
onFail(DownloadError.ERROR_CONNECTION_ERROR, request.getSource());
} finally {
IOUtils.closeQuietly(out);
if (httpClient != null) {
httpClient.getConnectionManager().shutdown();
}
}
}
private void onSuccess() {
if (AppConfig.DEBUG)
Log.d(TAG, "Download was successful");
result.setSuccessful();
}
private void onFail(DownloadError reason, String reasonDetailed) {
if (AppConfig.DEBUG) {
Log.d(TAG, "Download failed");
}
result.setFailed(reason, reasonDetailed);
cleanup();
}
cleanup();
}
private void onCancelled() {
if (AppConfig.DEBUG)
Log.d(TAG, "Download was cancelled");
private void onCancelled() {
if (AppConfig.DEBUG)
Log.d(TAG, "Download was cancelled");
result.setCancelled();
cleanup();
}
cleanup();
}
/** Deletes unfinished downloads. */
private void cleanup() {
if (request.getDestination() != null) {
File dest = new File(request.getDestination());
if (dest.exists()) {
boolean rc = dest.delete();
if (AppConfig.DEBUG)
Log.d(TAG, "Deleted file " + dest.getName() + "; Result: "
+ rc);
} else {
if (AppConfig.DEBUG)
Log.d(TAG, "cleanup() didn't delete file: does not exist.");
}
}
}
/**
* Deletes unfinished downloads.
*/
private void cleanup() {
if (request.getDestination() != null) {
File dest = new File(request.getDestination());
if (dest.exists()) {
boolean rc = dest.delete();
if (AppConfig.DEBUG)
Log.d(TAG, "Deleted file " + dest.getName() + "; Result: "
+ rc);
} else {
if (AppConfig.DEBUG)
Log.d(TAG, "cleanup() didn't delete file: does not exist.");
}
}
}
}

View File

@ -229,9 +229,11 @@ public final class DBReader {
title, item, link);
break;
}
chapter.setId(chapterCursor
.getLong(PodDBAdapter.KEY_ID_INDEX));
item.getChapters().add(chapter);
if (chapter != null) {
chapter.setId(chapterCursor
.getLong(PodDBAdapter.KEY_ID_INDEX));
item.getChapters().add(chapter);
}
} while (chapterCursor.moveToNext());
}
chapterCursor.close();

View File

@ -38,7 +38,7 @@ public class DownloadRequester {
downloads = new ConcurrentHashMap<String, DownloadRequest>();
}
public static DownloadRequester getInstance() {
public static synchronized DownloadRequester getInstance() {
if (downloader == null) {
downloader = new DownloadRequester();
}

View File

@ -17,7 +17,7 @@ public class FeedItemStatistics {
this.numberOfItems = numberOfItems;
this.numberOfNewItems = numberOfNewItems;
this.numberOfInProgressItems = numberOfInProgressItems;
this.lastUpdate = lastUpdate;
this.lastUpdate = (lastUpdate != null) ? (Date) lastUpdate.clone() : null;
}
public long getFeedID() {
@ -37,6 +37,6 @@ public class FeedItemStatistics {
}
public Date getLastUpdate() {
return lastUpdate;
return (lastUpdate != null) ? (Date) lastUpdate.clone() : null;
}
}

View File

@ -35,9 +35,9 @@ public class ChapterUtils {
* chapters.
*/
public static void readID3ChaptersFromPlayableStreamUrl(Playable p) {
if (AppConfig.DEBUG)
Log.d(TAG, "Reading id3 chapters from item " + p.getEpisodeTitle());
if (p != null && p.getStreamUrl() != null) {
if (AppConfig.DEBUG)
Log.d(TAG, "Reading id3 chapters from item " + p.getEpisodeTitle());
InputStream in = null;
try {
URL url = new URL(p.getStreamUrl());
@ -86,9 +86,9 @@ public class ChapterUtils {
* chapters.
*/
public static void readID3ChaptersFromPlayableFileUrl(Playable p) {
if (AppConfig.DEBUG)
Log.d(TAG, "Reading id3 chapters from item " + p.getEpisodeTitle());
if (p != null && p.localFileAvailable() && p.getLocalMediaUrl() != null) {
if (AppConfig.DEBUG)
Log.d(TAG, "Reading id3 chapters from item " + p.getEpisodeTitle());
File source = new File(p.getLocalMediaUrl());
if (source.exists()) {
ChapterReader reader = new ChapterReader();