How is this? Any ways to improve?
public void DownloadPDF(Uri uri) {
downloadProgress.setVisibility(View.VISIBLE);
downloadComplete.setVisibility(View.INVISIBLE);
openFile.setText("Wait..."); // My button
openFile.setEnabled(false);
if (uri != null) {
DownloadManager.Request request = new DownloadManager.Request(uri);
request.setTitle(pdf + ".pdf");
request.setDescription("Website: " + pdf);
request.setMimeType("application/pdf");
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.HONEYCOMB) {
request.allowScanningByMediaScanner();
request.setNotificationVisibility(DownloadManager.Request.VISIBILITY_VISIBLE_NOTIFY_COMPLETED);
}
request.setDestinationInExternalPublicDir(Environment.DIRECTORY_DOWNLOADS + "/SavedPDFs/", pdf + ".pdf");
manager = (DownloadManager) getSystemService(Context.DOWNLOAD_SERVICE);
downloadId = manager.enqueue(request);
onComplete = new BroadcastReceiver() {
public void onReceive(Context ctxt, Intent intent) {
try {
unregisterReceiver(onComplete);
} catch(IllegalArgumentException e){
e.printStackTrace();
}
downloadComplete.setVisibility(View.VISIBLE);
downloadProgress.setVisibility(View.INVISIBLE);
downloadStatus.setText("Download Complete!");
updateNumberOfFiles();
openFile.setText("Open!");
openFile.setEnabled(true);
}
};
registerReceiver(onComplete, new IntentFilter(DownloadManager.ACTION_DOWNLOAD_COMPLETE));
}
}