wip: app details now reloads details of running downloads

This commit is contained in:
Toby Kurien 2015-09-04 13:43:47 +02:00 committed by Peter Serwylo
parent f9fee5beb0
commit 11caf22dc6
2 changed files with 136 additions and 66 deletions

View File

@ -41,6 +41,7 @@ import android.net.Uri;
import android.os.Bundle; import android.os.Bundle;
import android.os.Handler; import android.os.Handler;
import android.support.annotation.NonNull; import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment; import android.support.v4.app.Fragment;
import android.support.v4.app.ListFragment; import android.support.v4.app.ListFragment;
import android.support.v4.app.NavUtils; import android.support.v4.app.NavUtils;
@ -438,6 +439,17 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
} }
localBroadcastManager = LocalBroadcastManager.getInstance(this); localBroadcastManager = LocalBroadcastManager.getInstance(this);
// Check if a download is running for this app
if (AsyncDownloader.isDownloading(this, app.id) >= 0) {
// call install to re-setup the listeners and downloaders
// the AsyncDownloader will not restart the download since the download is running
refreshHeader();
refreshApkList();
final Apk apkToInstall = ApkProvider.Helper.find(this, app.id, app.suggestedVercode);
install(apkToInstall);
}
} }
// The signature of the installed version. // The signature of the installed version.
@ -822,12 +834,8 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
if (downloadHandler != null && !downloadHandler.isComplete()) if (downloadHandler != null && !downloadHandler.isComplete())
return; return;
final String[] projection = { RepoProvider.DataColumns.ADDRESS }; final String repoaddress = getRepoAddress(apk);
Repo repo = RepoProvider.Helper.findById(this, apk.repo, projection); if (repoaddress == null) return;
if (repo == null || repo.address == null) {
return;
}
final String repoaddress = repo.address;
if (!apk.compatible) { if (!apk.compatible) {
AlertDialog.Builder ask_alrt = new AlertDialog.Builder(this); AlertDialog.Builder ask_alrt = new AlertDialog.Builder(this);
@ -869,6 +877,17 @@ public class AppDetails extends AppCompatActivity implements ProgressListener, A
startDownload(apk, repoaddress); startDownload(apk, repoaddress);
} }
@Nullable
private String getRepoAddress(Apk apk) {
final String[] projection = { RepoProvider.DataColumns.ADDRESS };
Repo repo = RepoProvider.Helper.findById(this, apk.repo, projection);
if (repo == null || repo.address == null) {
return null;
}
final String repoaddress = repo.address;
return repoaddress;
}
private void startDownload(Apk apk, String repoAddress) { private void startDownload(Apk apk, String repoAddress) {
downloadHandler = new ApkDownloader(getBaseContext(), apk, repoAddress); downloadHandler = new ApkDownloader(getBaseContext(), apk, repoAddress);
localBroadcastManager.registerReceiver(downloaderProgressReceiver, localBroadcastManager.registerReceiver(downloaderProgressReceiver,

View File

@ -12,6 +12,8 @@ import android.os.Build;
import android.os.ParcelFileDescriptor; import android.os.ParcelFileDescriptor;
import org.fdroid.fdroid.AppDetails; import org.fdroid.fdroid.AppDetails;
import org.fdroid.fdroid.data.Apk;
import org.fdroid.fdroid.data.App;
import org.fdroid.fdroid.data.SanitizedFile; import org.fdroid.fdroid.data.SanitizedFile;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
@ -64,19 +66,39 @@ public class AsyncDownloader extends AsyncDownloadWrapper {
@Override @Override
public void download() { public void download() {
if ((downloadId = isDownloadComplete(context, appId)) > 0) {
// clear the notification
dm.remove(downloadId);
try {
// write the downloaded file to the expected location
ParcelFileDescriptor fd = dm.openDownloadedFile(downloadId);
InputStream is = new FileInputStream(fd.getFileDescriptor());
OutputStream os = new FileOutputStream(localFile);
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer, 0, buffer.length)) > 0) {
os.write(buffer, 0, count);
}
os.close();
listener.onDownloadComplete();
} catch (IOException e) {
listener.onErrorDownloading(e.getLocalizedMessage());
}
return;
}
downloadId = isDownloading(context, appId);
if (downloadId >= 0) return;
// set up download request // set up download request
DownloadManager.Request request = new DownloadManager.Request(Uri.parse(remoteAddress)); DownloadManager.Request request = new DownloadManager.Request(Uri.parse(remoteAddress));
request.setTitle(appName); request.setTitle(appName);
request.setDescription(appId); // we will retrieve this later from the description field request.setDescription(appId); // we will retrieve this later from the description field
if (listener != null) { this.downloadId = dm.enqueue(request);
IntentFilter intentFilter = new IntentFilter();
intentFilter.addAction(DownloadManager.ACTION_DOWNLOAD_COMPLETE);
intentFilter.addAction(DownloadManager.ACTION_NOTIFICATION_CLICKED);
context.registerReceiver(downloadReceiver, intentFilter);
}
downloadId = dm.enqueue(request);
} }
@Override @Override
@ -94,67 +116,96 @@ public class AsyncDownloader extends AsyncDownloadWrapper {
if (userRequested && downloadId >= 0) { if (userRequested && downloadId >= 0) {
dm.remove(downloadId); dm.remove(downloadId);
} }
context.unregisterReceiver(downloadReceiver);
} }
// Broadcast receiver to receive broadcasts from the download manager /**
private BroadcastReceiver downloadReceiver = new BroadcastReceiver() { * Extract the appId from a given download id.
@Override * @param context
public void onReceive(Context context, Intent intent) { * @param downloadId
if (listener == null) { * @return - appId or null if not found
// without a listener, install UI won't come up, so ignore this */
return;
}
if (DownloadManager.ACTION_DOWNLOAD_COMPLETE.equals(intent.getAction())) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (id == downloadId) {
context.unregisterReceiver(this);
// clear the notification
dm.remove(id);
try {
// write the downloaded file to the expected location
ParcelFileDescriptor fd = dm.openDownloadedFile(id);
InputStream is = new FileInputStream(fd.getFileDescriptor());
OutputStream os = new FileOutputStream(localFile);
byte[] buffer = new byte[1024];
int count = 0;
while ((count = is.read(buffer, 0, buffer.length)) > 0) {
os.write(buffer, 0, count);
}
os.close();
listener.onDownloadComplete();
} catch (IOException e) {
listener.onErrorDownloading(e.getLocalizedMessage());
return;
}
}
}
if (DownloadManager.ACTION_NOTIFICATION_CLICKED.equals(intent.getAction())) {
long id = intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
if (id == downloadId) {
// TODO - display app details screen for this app
}
}
}
};
public static String getAppId(Context context, long downloadId) { public static String getAppId(Context context, long downloadId) {
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE); DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query(); DownloadManager.Query query = new DownloadManager.Query();
query.setFilterById(downloadId); query.setFilterById(downloadId);
Cursor c = dm.query(query); Cursor c = dm.query(query);
if (c.moveToFirst()) {
// we use the description column to store the app id try {
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION); if (c.moveToFirst()) {
return c.getString(columnIndex); // we use the description column to store the app id
int columnIndex = c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION);
return c.getString(columnIndex);
}
} finally {
c.close();
} }
return null; return null;
} }
/**
* Get the downloadId from an Intent sent by the DownloadManagerReceiver
* @param intent
* @return
*/
public static long getDownloadId(Intent intent) {
if (intent != null) {
return intent.getLongExtra(DownloadManager.EXTRA_DOWNLOAD_ID, -1);
} else {
return -1;
}
}
/**
* Check if a download is running for the app
* @param context
* @param appId
* @return -1 if not downloading, else the downloadId
*/
public static long isDownloading(Context context, String appId) {
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
Cursor c = dm.query(query);
int columnAppId = c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION);
int columnId = c.getColumnIndex(DownloadManager.COLUMN_ID);
try {
while (c.moveToNext()) {
if (appId.equals(c.getString(columnAppId))) {
return c.getLong(columnId);
}
}
} finally {
c.close();
}
return -1;
}
/**
* Check if a download for an app is complete.
* @param context
* @param appId
* @return -1 if download is not complete, otherwise the download id
*/
public static long isDownloadComplete(Context context, String appId) {
DownloadManager dm = (DownloadManager) context.getSystemService(Context.DOWNLOAD_SERVICE);
DownloadManager.Query query = new DownloadManager.Query();
query.setFilterByStatus(DownloadManager.STATUS_SUCCESSFUL);
Cursor c = dm.query(query);
int columnAppId = c.getColumnIndex(DownloadManager.COLUMN_DESCRIPTION);
int columnId = c.getColumnIndex(DownloadManager.COLUMN_ID);
try {
while (c.moveToNext()) {
if (appId.equals(c.getString(columnAppId))) {
return c.getLong(columnId);
}
}
} finally {
c.close();
}
return -1;
}
} }