下面列出了android.app.DownloadManager#STATUS_PENDING 实例代码,或者点击链接到github查看源代码,也可以在右侧发表评论。
protected void pollDownloadManager() {
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadId);
Cursor cursor = downloadManager.query(q);
cursor.moveToFirst();
final int bytesDownloaded = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
final String msg = PROGRESS_MSG + ((double)bytesDownloaded) / 1000000.0 + " MB";
publishProgress(msg);
status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
statusMessage = statusMessage(cursor, bytesDownloaded);
Log.d("OSMDownloader", statusMessage);
if (status != DownloadManager.STATUS_PENDING && status != DownloadManager.STATUS_RUNNING) {
downloading = false;
}
// throttle the thread
try {
Thread.sleep(200);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@StringRes
public int getStatusTextResId() {
int statusText = 0;
switch (status) {
case DownloadManager.STATUS_FAILED:
statusText = (R.string.STATUS_FAILED);
break;
case DownloadManager.STATUS_PAUSED:
statusText = (R.string.STATUS_PAUSED);
break;
case DownloadManager.STATUS_PENDING:
statusText = (R.string.STATUS_PENDING);
break;
case DownloadManager.STATUS_RUNNING:
statusText = (R.string.STATUS_RUNNING);
break;
case DownloadManager.STATUS_SUCCESSFUL:
statusText = (R.string.STATUS_SUCCESSFUL);
break;
}
return statusText;
}
protected void doGetStatus(long downloadId) {
try {
switch (getDownloadManagerStatus(downloadId)) {
case DownloadManager.STATUS_FAILED:
onDownloadComplete(getRecordingID(), STATUS_DOWNLOAD_CANCELED);
removeCurrentDownloadReferences();
break;
case DownloadManager.STATUS_SUCCESSFUL:
onDownloadComplete(getRecordingID(), STATUS_DOWNLOAD_COMPLETE);
removeCurrentDownloadReferences();
break;
case DownloadManager.STATUS_PENDING:
case DownloadManager.STATUS_PAUSED:
case DownloadManager.STATUS_RUNNING:
int progress = doGetDownloadProgress();
if (progress == 100) {
onDownloadComplete(getRecordingID(), STATUS_DOWNLOAD_COMPLETE);
removeCurrentDownloadReferences();
}
else {
onDownloadProgressChanged(progress, STATUS_DOWNLOAD_RUNNING);
}
break;
}
}
catch (Exception ex) {
removeCurrentDownloadReferences();
onDownloadFatalError(ex);
}
}
protected boolean downloadInProgress() {
long downloadId = getDownloadID();
if (downloadId == -1) {
return false;
}
try {
switch (getDownloadManagerStatus(downloadId)) {
case DownloadManager.STATUS_FAILED:
removeCurrentDownloadReferences();
return false;
case DownloadManager.STATUS_SUCCESSFUL:
removeCurrentDownloadReferences();
return false;
case DownloadManager.STATUS_PENDING:
case DownloadManager.STATUS_PAUSED:
case DownloadManager.STATUS_RUNNING:
return true;
default:
case -1:
return false;
}
}
catch (Exception ex) {
logger.error("Could not process download ID [{}]", downloadId, ex);
}
return false;
}
public static Download from(Cursor cursor) {
Download download = new Download();
download.mId = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_ID));
download.mUri = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_URI));
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch (status) {
case DownloadManager.STATUS_RUNNING:
download.mStatus = RUNNING;
break;
case DownloadManager.STATUS_FAILED:
download.mStatus = FAILED;
break;
case DownloadManager.STATUS_PAUSED:
download.mStatus = PAUSED;
break;
case DownloadManager.STATUS_PENDING:
download.mStatus = PENDING;
break;
case DownloadManager.STATUS_SUCCESSFUL:
download.mStatus = SUCCESSFUL;
break;
default:
download.mStatus = UNAVAILABLE;
}
download.mMediaType = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_MEDIA_TYPE));
download.mTitle = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_TITLE));
download.mOutputFile = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI));
download.mDescription = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION));
download.mSizeBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
download.mDownloadedBytes = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
download.mLastModified = cursor.getLong(cursor.getColumnIndex(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
download.mReason = cursor.getString(cursor.getColumnIndex(DownloadManager.COLUMN_REASON));
return download;
}
private String statusMessage(@NonNull Cursor c) {
String msg;
switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
case DownloadManager.STATUS_FAILED:
msg = "DownloadInfo failed";
break;
case DownloadManager.STATUS_PAUSED:
msg = "DownloadInfo paused";
break;
case DownloadManager.STATUS_PENDING:
msg = "DownloadInfo pending";
break;
case DownloadManager.STATUS_RUNNING:
msg = "DownloadInfo in progress";
break;
case DownloadManager.STATUS_SUCCESSFUL:
msg = "DownloadInfo complete";
break;
default:
msg = "DownloadInfo is nowhere in sight";
break;
}
return (msg);
}
private void checkStatus() {
//cause SQLiteException at 乐视 LE X820 Android 6.0.1,level 23
try{
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
Cursor c = downloadManager.query(query);
if (c.moveToFirst()) {
int status = c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS));
switch (status) {
case DownloadManager.STATUS_PAUSED:
break;
case DownloadManager.STATUS_PENDING:
break;
case DownloadManager.STATUS_RUNNING:
break;
case DownloadManager.STATUS_SUCCESSFUL:
String tip = mContext.getString(R.string.download_complete)
.concat("\n").concat(getFilePath());
Toasty.success(mContext, tip).show();
unregister();
break;
case DownloadManager.STATUS_FAILED:
Toasty.error(mContext, mContext.getString(R.string.download_failed)).show();
unregister();
break;
}
}
c.close();
}catch (SQLiteException e){
Logger.d(e);
unregister();
}
}
protected String statusMessage(Cursor c, int bytesDownloaded) {
String msg;
switch (c.getInt(c.getColumnIndex(DownloadManager.COLUMN_STATUS))) {
case DownloadManager.STATUS_FAILED:
msg = "Download failed: " + bytesDownloaded + " bytes downloaded.";
break;
case DownloadManager.STATUS_PAUSED:
msg = "Download paused: " + bytesDownloaded + " bytes downloaded.";
break;
case DownloadManager.STATUS_PENDING:
msg = "Download pending: " + bytesDownloaded + " bytes downloaded.";
break;
case DownloadManager.STATUS_RUNNING:
msg = "Download in progress: " + bytesDownloaded + " bytes downloaded.";
break;
case DownloadManager.STATUS_SUCCESSFUL:
msg = "Download complete: " + bytesDownloaded + " bytes downloaded.";
break;
default:
msg = "STATUS MESSAGE ERROR";
break;
}
return (msg);
}
public boolean isDownloading(Layer layer) {
int status = -1;
Long downloadId = layer.getDownloadId();
if (downloadId != null) {
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId);
try(Cursor cursor = downloadManager.query(query)) {
status = getDownloadStatus(cursor);
}
}
return status == DownloadManager.STATUS_RUNNING || status == DownloadManager.STATUS_PENDING;
}
@Override
@NonNull
public Status getStatus() {
if (nativeModel.status == DownloadManager.STATUS_FAILED) {
return Status.FAILED;
}
if (nativeModel.status == DownloadManager.STATUS_PENDING
|| nativeModel.size == -1) {
return Status.PENDING;
}
return Status.DOWNLOADING;
}
private void pollDownloadManager() {
while (downloading) {
DownloadManager.Query q = new DownloadManager.Query();
q.setFilterById(downloadIds);
Cursor cursor = downloadManager.query(q);
bytesDownloaded = 0;
filesCompleted = 0;
while(cursor.moveToNext()) {
bytesDownloaded += cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
int status = cursor.getInt(cursor.getColumnIndex(DownloadManager.COLUMN_STATUS));
if (status != DownloadManager.STATUS_PENDING && status != DownloadManager.STATUS_RUNNING) {
++filesCompleted;
}
}
if (!canceled) {
publishProgress();
}
if (deployment.fileCount() == filesCompleted) {
downloading = false;
}
// throttle the thread
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
@Override
public void run() {
if (mUrl == null) {
btnDownload.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
btnDownloadCancel.setVisibility(View.GONE);
btnRemove.setVisibility(View.GONE);
btnInstall.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
txtInfo.setVisibility(View.VISIBLE);
txtInfo.setText(R.string.download_view_no_url);
} else if (mInfo == null) {
btnDownload.setVisibility(View.VISIBLE);
btnSave.setVisibility(View.VISIBLE);
btnDownloadCancel.setVisibility(View.GONE);
btnRemove.setVisibility(View.GONE);
btnInstall.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
txtInfo.setVisibility(View.GONE);
} else {
switch (mInfo.status) {
case DownloadManager.STATUS_PENDING:
case DownloadManager.STATUS_PAUSED:
case DownloadManager.STATUS_RUNNING:
btnDownload.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
btnDownloadCancel.setVisibility(View.VISIBLE);
btnRemove.setVisibility(View.GONE);
btnInstall.setVisibility(View.GONE);
progressBar.setVisibility(View.VISIBLE);
txtInfo.setVisibility(View.VISIBLE);
if (mInfo.totalSize <= 0 || mInfo.status != DownloadManager.STATUS_RUNNING) {
progressBar.setIndeterminate(true);
txtInfo.setText(R.string.download_view_waiting);
} else {
progressBar.setIndeterminate(false);
progressBar.setMax(mInfo.totalSize);
progressBar.setProgress(mInfo.bytesDownloaded);
txtInfo.setText(getContext().getString(
R.string.download_view_running,
mInfo.bytesDownloaded / 1024,
mInfo.totalSize / 1024));
}
break;
case DownloadManager.STATUS_FAILED:
btnDownload.setVisibility(View.VISIBLE);
btnSave.setVisibility(View.VISIBLE);
btnDownloadCancel.setVisibility(View.GONE);
btnRemove.setVisibility(View.GONE);
btnInstall.setVisibility(View.GONE);
progressBar.setVisibility(View.GONE);
txtInfo.setVisibility(View.VISIBLE);
txtInfo.setText(getContext().getString(
R.string.download_view_failed, mInfo.reason));
break;
case DownloadManager.STATUS_SUCCESSFUL:
btnDownload.setVisibility(View.GONE);
btnSave.setVisibility(View.GONE);
btnDownloadCancel.setVisibility(View.GONE);
btnRemove.setVisibility(View.VISIBLE);
btnInstall.setVisibility(View.VISIBLE);
progressBar.setVisibility(View.GONE);
txtInfo.setVisibility(View.VISIBLE);
txtInfo.setText(R.string.download_view_successful);
break;
}
}
}
private HashMap<String, String> getDownloadStatus(Cursor cursor, long downloadId) {
int columnStatusIndex = cursor.getColumnIndex(DownloadManager.COLUMN_STATUS);
int STATUS = cursor.getInt(columnStatusIndex);
int columnReasonIndex = cursor.getColumnIndex(DownloadManager.COLUMN_REASON);
int REASON = cursor.getInt(columnReasonIndex);
int filenameIndex = cursor.getColumnIndex(DownloadManager.COLUMN_LOCAL_URI);
String filename = cursor.getString(filenameIndex);
String statusText = "";
String reasonText = "";
switch (STATUS) {
case DownloadManager.STATUS_FAILED:
statusText = "STATUS_FAILED";
switch (REASON) {
case DownloadManager.ERROR_CANNOT_RESUME:
reasonText = "ERROR_CANNOT_RESUME";
break;
case DownloadManager.ERROR_DEVICE_NOT_FOUND:
reasonText = "ERROR_DEVICE_NOT_FOUND";
break;
case DownloadManager.ERROR_FILE_ALREADY_EXISTS:
reasonText = "ERROR_FILE_ALREADY_EXISTS";
break;
case DownloadManager.ERROR_FILE_ERROR:
reasonText = "ERROR_FILE_ERROR";
break;
case DownloadManager.ERROR_HTTP_DATA_ERROR:
reasonText = "ERROR_HTTP_DATA_ERROR";
break;
case DownloadManager.ERROR_INSUFFICIENT_SPACE:
reasonText = "ERROR_INSUFFICIENT_SPACE";
break;
case DownloadManager.ERROR_TOO_MANY_REDIRECTS:
reasonText = "ERROR_TOO_MANY_REDIRECTS";
break;
case DownloadManager.ERROR_UNHANDLED_HTTP_CODE:
reasonText = "ERROR_UNHANDLED_HTTP_CODE";
break;
default:
reasonText = "ERROR_UNKNOWN";
break;
}
break;
case DownloadManager.STATUS_PAUSED:
statusText = "STATUS_PAUSED";
switch (REASON) {
case DownloadManager.PAUSED_QUEUED_FOR_WIFI:
reasonText = "PAUSED_QUEUED_FOR_WIFI";
break;
case DownloadManager.PAUSED_UNKNOWN:
reasonText = "PAUSED_UNKNOWN";
break;
case DownloadManager.PAUSED_WAITING_FOR_NETWORK:
reasonText = "PAUSED_WAITING_FOR_NETWORK";
break;
case DownloadManager.PAUSED_WAITING_TO_RETRY:
reasonText = "PAUSED_WAITING_TO_RETRY";
break;
default:
reasonText = "UNKNOWN";
}
break;
case DownloadManager.STATUS_PENDING:
statusText = "STATUS_PENDING";
break;
case DownloadManager.STATUS_RUNNING:
statusText = "STATUS_RUNNING";
break;
case DownloadManager.STATUS_SUCCESSFUL:
statusText = "STATUS_SUCCESSFUL";
reasonText = filename;
break;
default:
statusText = "STATUS_UNKNOWN";
reasonText = String.valueOf(STATUS);
break;
}
HashMap<String, String> result = new HashMap<>();
result.put("status", statusText);
result.put("reason", reasonText);
result.put("downloadId", String.valueOf(downloadId));
return result;
}
@SuppressLint("InlinedApi")
private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
final String docId = String.valueOf(id);
final String displayName = cursor.getString(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
String summary = cursor.getString(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
String mimeType = cursor.getString(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
if (mimeType == null) {
// Provide fake MIME type so it's openable
mimeType = "vnd.android.document/file";
}
Long size = cursor.getLong(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (size == -1) {
size = null;
}
final int status = cursor.getInt(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
switch (status) {
case DownloadManager.STATUS_SUCCESSFUL:
break;
case DownloadManager.STATUS_PAUSED:
summary = getContext().getString(R.string.download_queued);
break;
case DownloadManager.STATUS_PENDING:
summary = getContext().getString(R.string.download_queued);
break;
case DownloadManager.STATUS_RUNNING:
final long progress = cursor.getLong(cursor.getColumnIndexOrThrow(
DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
if (size != null) {
final long percent = progress * 100 / size;
summary = getContext().getString(R.string.download_running_percent, percent);
} else {
summary = getContext().getString(R.string.download_running);
}
break;
case DownloadManager.STATUS_FAILED:
default:
summary = getContext().getString(R.string.download_error);
break;
}
int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE;
if (mimeType != null && mimeType.startsWith("image/")) {
flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
}
final long lastModified = cursor.getLong(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
final RowBuilder row = result.newRow();
row.add(Document.COLUMN_DOCUMENT_ID, docId);
row.add(Document.COLUMN_DISPLAY_NAME, displayName);
row.add(Document.COLUMN_SUMMARY, summary);
row.add(Document.COLUMN_SIZE, size);
row.add(Document.COLUMN_MIME_TYPE, mimeType);
row.add(Document.COLUMN_LAST_MODIFIED, lastModified);
row.add(Document.COLUMN_FLAGS, flags);
}
@SuppressLint("InlinedApi")
private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
final String docId = String.valueOf(id);
final String displayName = cursor.getString(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
String summary = cursor.getString(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
String mimeType = cursor.getString(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
if (mimeType == null) {
// Provide fake MIME type so it's openable
mimeType = "vnd.android.document/file";
}
Long size = cursor.getLong(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (size == -1) {
size = null;
}
final int status = cursor.getInt(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
switch (status) {
case DownloadManager.STATUS_SUCCESSFUL:
break;
case DownloadManager.STATUS_PAUSED:
summary = getContext().getString(R.string.download_queued);
break;
case DownloadManager.STATUS_PENDING:
summary = getContext().getString(R.string.download_queued);
break;
case DownloadManager.STATUS_RUNNING:
final long progress = cursor.getLong(cursor.getColumnIndexOrThrow(
DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
if (size != null) {
final long percent = progress * 100 / size;
summary = getContext().getString(R.string.download_running_percent, percent);
} else {
summary = getContext().getString(R.string.download_running);
}
break;
case DownloadManager.STATUS_FAILED:
default:
summary = getContext().getString(R.string.download_error);
break;
}
int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE;
if (mimeType != null && mimeType.startsWith("image/")) {
flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
}
final long lastModified = cursor.getLong(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
final RowBuilder row = result.newRow();
row.add(Document.COLUMN_DOCUMENT_ID, docId);
row.add(Document.COLUMN_DISPLAY_NAME, displayName);
row.add(Document.COLUMN_SUMMARY, summary);
row.add(Document.COLUMN_SIZE, size);
row.add(Document.COLUMN_MIME_TYPE, mimeType);
row.add(Document.COLUMN_LAST_MODIFIED, lastModified);
row.add(Document.COLUMN_FLAGS, flags);
}
@SuppressLint("InlinedApi")
private void includeDownloadFromCursor(MatrixCursor result, Cursor cursor) {
final long id = cursor.getLong(cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_ID));
final String docId = String.valueOf(id);
final String displayName = cursor.getString(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TITLE));
String summary = cursor.getString(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_DESCRIPTION));
String mimeType = cursor.getString(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_MEDIA_TYPE));
if (mimeType == null) {
// Provide fake MIME type so it's openable
mimeType = "vnd.android.document/file";
}
Long size = cursor.getLong(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_TOTAL_SIZE_BYTES));
if (size == -1) {
size = null;
}
final int status = cursor.getInt(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_STATUS));
switch (status) {
case DownloadManager.STATUS_SUCCESSFUL:
break;
case DownloadManager.STATUS_PAUSED:
summary = getContext().getString(R.string.download_queued);
break;
case DownloadManager.STATUS_PENDING:
summary = getContext().getString(R.string.download_queued);
break;
case DownloadManager.STATUS_RUNNING:
final long progress = cursor.getLong(cursor.getColumnIndexOrThrow(
DownloadManager.COLUMN_BYTES_DOWNLOADED_SO_FAR));
if (size != null) {
final long percent = progress * 100 / size;
summary = getContext().getString(R.string.download_running_percent, percent);
} else {
summary = getContext().getString(R.string.download_running);
}
break;
case DownloadManager.STATUS_FAILED:
default:
summary = getContext().getString(R.string.download_error);
break;
}
int flags = Document.FLAG_SUPPORTS_DELETE | Document.FLAG_SUPPORTS_WRITE;
if (mimeType != null && mimeType.startsWith("image/")) {
flags |= Document.FLAG_SUPPORTS_THUMBNAIL;
}
final long lastModified = cursor.getLong(
cursor.getColumnIndexOrThrow(DownloadManager.COLUMN_LAST_MODIFIED_TIMESTAMP));
final RowBuilder row = result.newRow();
row.add(Document.COLUMN_DOCUMENT_ID, docId);
row.add(Document.COLUMN_DISPLAY_NAME, displayName);
row.add(Document.COLUMN_SUMMARY, summary);
row.add(Document.COLUMN_SIZE, size);
row.add(Document.COLUMN_MIME_TYPE, mimeType);
row.add(Document.COLUMN_LAST_MODIFIED, lastModified);
row.add(Document.COLUMN_FLAGS, flags);
}
public void download(UpdaterConfig updaterConfig) {
if (!UpdaterUtils.checkDownloadState(updaterConfig.getContext())) {
Toast.makeText(updaterConfig.getContext(), R.string.system_download_component_disable, Toast.LENGTH_SHORT).show();
UpdaterUtils.showDownloadSetting(updaterConfig.getContext());
return;
}
long downloadId = UpdaterUtils.getLocalDownloadId(updaterConfig.getContext());
Logger.get().d("local download id is " + downloadId);
if (downloadId != -1L) {
FileDownloadManager fdm = FileDownloadManager.get();
//获取下载状态
int status = fdm.getDownloadStatus(updaterConfig.getContext(), downloadId);
switch (status) {
//下载成功
case DownloadManager.STATUS_SUCCESSFUL:
Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_SUCCESSFUL");
Uri uri = fdm.getDownloadUri(updaterConfig.getContext(), downloadId);
if (uri != null) {
//本地的版本大于当前程序的版本直接安装
if (UpdaterUtils.compare(updaterConfig.getContext(), uri)) {
Logger.get().d("start install UI with local apk");
UpdaterUtils.startInstall(updaterConfig.getContext(), uri);
return;
} else {
//从FileDownloadManager中移除这个任务
fdm.getDM(updaterConfig.getContext()).remove(downloadId);
}
}
//重新下载
startDownload(updaterConfig);
break;
//下载失败
case DownloadManager.STATUS_FAILED:
Logger.get().d("download failed " + downloadId);
startDownload(updaterConfig);
break;
case DownloadManager.STATUS_RUNNING:
Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_RUNNING");
break;
case DownloadManager.STATUS_PENDING:
Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_PENDING");
break;
case DownloadManager.STATUS_PAUSED:
Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_PAUSED");
break;
case STATUS_UN_FIND:
Logger.get().d("downloadId=" + downloadId + " ,status = STATUS_UN_FIND");
startDownload(updaterConfig);
break;
default:
Logger.get().d("downloadId=" + downloadId + " ,status = " + status);
break;
}
} else {
startDownload(updaterConfig);
}
}
public static boolean isDownloading(int downloadManagerStatus) {
return downloadManagerStatus == DownloadManager.STATUS_RUNNING
|| downloadManagerStatus == DownloadManager.STATUS_PAUSED
|| downloadManagerStatus == DownloadManager.STATUS_PENDING;
}
public Index(Context context, SQLiteDatabase mapsDatabase, SQLiteDatabase hillshadesDatabase) {
mContext = context;
mMapsDatabase = mapsDatabase;
mHillshadeDatabase = hillshadesDatabase;
mDownloadManager = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
try {
Cursor cursor = mMapsDatabase.query(TABLE_MAPS, ALL_COLUMNS_MAPS, WHERE_MAPS_PRESENT, null, null, null, null);
cursor.moveToFirst();
while (!cursor.isAfterLast()) {
int x = cursor.getInt(cursor.getColumnIndex(COLUMN_MAPS_X));
int y = cursor.getInt(cursor.getColumnIndex(COLUMN_MAPS_Y));
short date = cursor.getShort(cursor.getColumnIndex(COLUMN_MAPS_DATE));
byte version = (byte) cursor.getShort(cursor.getColumnIndex(COLUMN_MAPS_VERSION));
logger.debug("index({}, {}, {}, {})", x, y, date, version);
if (x == -1 && y == -1) {
mBaseMapVersion = date;
cursor.moveToNext();
continue;
}
long downloading = cursor.getLong(cursor.getColumnIndex(COLUMN_MAPS_DOWNLOADING));
long hillshadeDownloading = cursor.getLong(cursor.getColumnIndex(COLUMN_MAPS_HILLSHADE_DOWNLOADING));
MapStatus mapStatus = getNativeMap(x, y);
mapStatus.created = date;
mapStatus.hillshadeVersion = version;
int status = checkDownloadStatus(downloading);
if (status == DownloadManager.STATUS_PAUSED
|| status == DownloadManager.STATUS_PENDING
|| status == DownloadManager.STATUS_RUNNING) {
mapStatus.downloading = downloading;
logger.debug(" map downloading: {}", downloading);
} else {
downloading = 0L;
setDownloading(x, y, downloading, hillshadeDownloading);
logger.debug(" cleared");
}
status = checkDownloadStatus(hillshadeDownloading);
if (status == DownloadManager.STATUS_PAUSED
|| status == DownloadManager.STATUS_PENDING
|| status == DownloadManager.STATUS_RUNNING) {
mapStatus.hillshadeDownloading = hillshadeDownloading;
logger.debug(" hillshade downloading: {}", downloading);
} else {
hillshadeDownloading = 0L;
setDownloading(x, y, downloading, hillshadeDownloading);
logger.debug(" cleared");
}
if (date > 0)
mLoadedMaps++;
cursor.moveToNext();
}
cursor.close();
} catch (SQLiteException e) {
logger.error("Failed to read map index", e);
mMapsDatabase.execSQL(MapTrekDatabaseHelper.SQL_CREATE_MAPS);
mMapsDatabase.execSQL(MapTrekDatabaseHelper.SQL_INDEX_MAPS);
}
mHasHillshades = DatabaseUtils.queryNumEntries(mHillshadeDatabase, TABLE_TILES) > 0;
//TODO Remove old basemap file
}
/**
* Sets the right status icon when the app is currently downloading or has downloaded an APK.
* @param info The object containing the information about the download.
* @param ctx The context of the application.
* @return Whether an icon was set.
*/
private boolean _set_action_icon_download(final DownloadInfo info, final Context ctx)
{
switch (info.get_status())
{
case DownloadManager.STATUS_SUCCESSFUL: // APK was downloaded. Install on click.
final File apk = new File(Uri.parse(info.get_local_uri()).getPath());
if (apk.exists())
{
_action_icon.setImageDrawable(ContextCompat.getDrawable(ctx, R.drawable.install));
_action_icon.setVisibility(View.VISIBLE);
_action_icon.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(Intent.ACTION_VIEW);
Uri apk_uri;
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N)
{
// Starting from Android N, file:// ACTION_VIEW intents can no longer be passed to other apps.
apk_uri = FileProvider.getUriForFile(ctx, BuildConfig.APPLICATION_ID + ".provider", apk);
}
else
{
// However, it seems that no system component handles content://[...].apk in previous
// versions, which is why the URI is still passed the old way here.
apk_uri = Uri.parse(info.get_local_uri());
}
i.setDataAndType(apk_uri, "application/vnd.android.package-archive");
i.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_GRANT_READ_URI_PERMISSION);
if (i.resolveActivity(ctx.getPackageManager()) != null) {
ctx.startActivity(i);
}
else
{
Log.v(MainActivity.TAG, "Could not find anyone to receive ACTION_VIEW for " +
"the downloaded APK. (" + info.get_local_uri() + ")");
}
}
});
return true;
}
else { // For some reason the APK is not present anymore. Remove the download information.
return false;
}
case DownloadManager.STATUS_PENDING:
case DownloadManager.STATUS_RUNNING:
_action_icon.setImageDrawable(ContextCompat.getDrawable(ctx, android.R.drawable.stat_sys_download));
// Fix the icon color for the white background.
_action_icon.setColorFilter(Color.GRAY, PorterDuff.Mode.MULTIPLY);
((Animatable) _action_icon.getDrawable()).start();
_action_icon.setVisibility(View.VISIBLE);
return true;
default:
return false;
}
}