Added progress bar when downloading for installation/update

This commit is contained in:
Ciaran Gultnieks 2010-11-06 00:15:30 +00:00
parent f5d896b0a3
commit f95a546d6b
2 changed files with 104 additions and 76 deletions

View File

@ -90,4 +90,5 @@
<string name="details_installed">Version %s installed</string> <string name="details_installed">Version %s installed</string>
<string name="details_notinstalled">Not installed (%d available)</string> <string name="details_notinstalled">Not installed (%d available)</string>
<string name="inst">Installed</string> <string name="inst">Installed</string>
<string name="corrupt_download">Downloaded file is corrupt</string>
</resources> </resources>

View File

@ -112,20 +112,17 @@ public class AppDetails extends ListActivity {
} }
} }
private static String getFriendlySize(int size) private static String getFriendlySize(int size) {
{
double s = size; double s = size;
String[] format = new String[] { "%fb", "%.0fK", "%.1fM", "%.2fG" }; String[] format = new String[] { "%fb", "%.0fK", "%.1fM", "%.2fG" };
int i = 0; int i = 0;
while(i<format.length-1 && s>=1024) while (i < format.length - 1 && s >= 1024) {
{
s = (100 * s / 1024) / 100.0; s = (100 * s / 1024) / 100.0;
i++; i++;
} }
return String.format(format[i], s); return String.format(format[i], s);
} }
private static final int INSTALL = Menu.FIRST; private static final int INSTALL = Menu.FIRST;
private static final int UNINSTALL = Menu.FIRST + 1; private static final int UNINSTALL = Menu.FIRST + 1;
private static final int WEBSITE = Menu.FIRST + 2; private static final int WEBSITE = Menu.FIRST + 2;
@ -190,9 +187,11 @@ public class AppDetails extends ListActivity {
tv = (TextView) findViewById(R.id.status); tv = (TextView) findViewById(R.id.status);
int vnum = app.apks.size(); int vnum = app.apks.size();
if (app.installedVersion == null) if (app.installedVersion == null)
tv.setText(String.format(getString(R.string.details_notinstalled),vnum)); tv.setText(String.format(getString(R.string.details_notinstalled),
vnum));
else else
tv.setText(String.format(getString(R.string.details_installed), app.installedVersion)); tv.setText(String.format(getString(R.string.details_installed),
app.installedVersion));
tv = (TextView) findViewById(R.id.description); tv = (TextView) findViewById(R.id.description);
tv.setText(app.description); tv.setText(app.description);
@ -261,23 +260,6 @@ public class AppDetails extends ListActivity {
p.show(); p.show();
} }
// Install the version of this app denoted by 'curapk'.
private void install() {
new Thread() {
public void run() {
String apk_file = downloadFile(app, curapk);
if (apk_file == null) {
Message msg = new Message();
msg.arg1 = 1;
download_handler.sendMessage(msg);
download_error_handler.sendEmptyMessage(0);
} else {
installApk(apk_file);
}
}
}.start();
}
@Override @Override
public boolean onPrepareOptionsMenu(Menu menu) { public boolean onPrepareOptionsMenu(Menu menu) {
@ -351,34 +333,50 @@ public class AppDetails extends ListActivity {
return super.onOptionsItemSelected(item); return super.onOptionsItemSelected(item);
} }
// Download the requested apk file, given the DB.App and DB.Apk // Install the version of this app denoted by 'curapk'.
// that refer to it. Returns the path to the downloaded file, or private void install() {
// null if the download was not successful.
private String downloadFile(DB.App app, DB.Apk apk) { pd = new ProgressDialog(this);
pd.setProgressStyle(ProgressDialog.STYLE_HORIZONTAL);
pd.setMessage(getString(R.string.download_server));
new Thread() {
public void run() {
// Download the apk file from the repository...
String apk_file = null;
try { try {
String apkname = apk.apkName; String apkname = curapk.apkName;
String localfile = new String(LOCAL_PATH + "/" + apkname); String localfile = new String(LOCAL_PATH + "/" + apkname);
String remotefile = apk.server + "/" + apkname.replace(" ", "%20"); String remotefile = curapk.server + "/"
+ apkname.replace(" ", "%20");
Log.d("FDroid", "Downloading apk from " + remotefile); Log.d("FDroid", "Downloading apk from " + remotefile);
Message msg = new Message(); Message msg = new Message();
msg.arg1 = 0; msg.arg1 = 0;
msg.arg2 = curapk.size;
msg.obj = new String(remotefile); msg.obj = new String(remotefile);
download_handler.sendMessage(msg); download_handler.sendMessage(msg);
BufferedInputStream getit = new BufferedInputStream(new URL( BufferedInputStream getit = new BufferedInputStream(
remotefile).openStream(), 8192); new URL(remotefile).openStream(), 8192);
FileOutputStream saveit = new FileOutputStream(localfile); FileOutputStream saveit = new FileOutputStream(localfile);
BufferedOutputStream bout = new BufferedOutputStream(saveit, 1024); BufferedOutputStream bout = new BufferedOutputStream(
saveit, 1024);
byte data[] = new byte[1024]; byte data[] = new byte[1024];
int readed = getit.read(data, 0, 1024); int totalRead = 0;
while (readed != -1) { int bytesRead = getit.read(data, 0, 1024);
bout.write(data, 0, readed); while (bytesRead != -1) {
readed = getit.read(data, 0, 1024); bout.write(data, 0, bytesRead);
totalRead += bytesRead;
msg = new Message();
msg.arg1 = totalRead;
download_handler.sendMessage(msg);
bytesRead = getit.read(data, 0, 1024);
} }
bout.close(); bout.close();
getit.close(); getit.close();
@ -386,26 +384,45 @@ public class AppDetails extends ListActivity {
File f = new File(localfile); File f = new File(localfile);
Md5Handler hash = new Md5Handler(); Md5Handler hash = new Md5Handler();
if (apk.hash.equalsIgnoreCase(hash.md5Calc(f))) { if (curapk.hash.equalsIgnoreCase(hash.md5Calc(f))) {
return localfile; apk_file = localfile;
} else { } else {
return null; msg = new Message();
} msg.obj = getString(R.string.corrupt_download);
} catch (Exception e) { download_error_handler.sendMessage(msg);
Log.d("FDroid", "Download failed - " + e.getMessage());
return null;
}
} }
} catch (Exception e) {
Log.d("FDroid", "Download failed - " + e.getMessage());
Message msg = new Message();
msg.obj = e.getMessage();
download_error_handler.sendMessage(msg);
}
if (apk_file != null) {
Message msg = new Message();
msg.obj = apk_file;
download_complete_handler.sendMessage(msg);
}
}
}.start();
pd.show();
}
// Handler used to update the progress dialog while downloading. The
// message contains the progress (bytes read) in arg1. If this is 0,
// the message also contains the total bytes to read in arg2, and the
// message in obj.
private Handler download_handler = new Handler() { private Handler download_handler = new Handler() {
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
pd.setProgress(msg.arg1);
if (msg.arg1 == 0) { if (msg.arg1 == 0) {
pd = ProgressDialog.show(mctx, getString(R.string.download), pd.setMessage(getString(R.string.download_server) + ":\n "
getString(R.string.download_server) + ":\n " + msg.obj.toString());
+ msg.obj.toString(), true); pd.setMax(msg.arg2);
} else {
pd.dismiss();
} }
} }
}; };
@ -413,11 +430,21 @@ public class AppDetails extends ListActivity {
private Handler download_error_handler = new Handler() { private Handler download_error_handler = new Handler() {
@Override @Override
public void handleMessage(Message msg) { public void handleMessage(Message msg) {
Toast.makeText(mctx, getString(R.string.connection_error_msg), pd.dismiss();
Toast.makeText(mctx, (String)msg.obj,
Toast.LENGTH_LONG).show(); Toast.LENGTH_LONG).show();
} }
}; };
private Handler download_complete_handler = new Handler() {
@Override
public void handleMessage(Message msg) {
String apk_file = (String) msg.obj;
installApk(apk_file);
pd.dismiss();
}
};
private void removeApk(String id) { private void removeApk(String id) {
PackageInfo pkginfo; PackageInfo pkginfo;
try { try {