[android] Updated UI for downloader

This commit is contained in:
Alex Zolotarev 2011-12-18 18:01:36 +03:00 committed by Alex Zolotarev
parent 960d861c8b
commit f39f8f9013

View file

@ -1,7 +1,6 @@
package com.mapswithme.maps;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.os.Bundle;
import android.preference.CheckBoxPreference;
@ -25,8 +24,10 @@ public class DownloadUI extends PreferenceActivity
private native void downloadCountry(int group, int country, int region);
private native void deleteCountry(int group, int country, int region);
private AlertDialog m_alert;
private AlertDialog.Builder m_alert;
private DialogInterface.OnClickListener m_alertCancelHandler = new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int which) { dlg.dismiss(); } };
@Override
protected void onCreate(Bundle savedInstanceState)
{
@ -35,14 +36,8 @@ public class DownloadUI extends PreferenceActivity
PreferenceScreen root = getPreferenceManager().createPreferenceScreen(this);
setPreferenceScreen(createCountriesHierarchy(root, -1, -1, -1));
m_alert = new AlertDialog.Builder(this).create();
m_alert = new AlertDialog.Builder(this);
m_alert.setCancelable(false);
m_alert.setButton(AlertDialog.BUTTON_NEGATIVE, "Cancel", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dialog, int which){
dialog.dismiss();
}
});
nativeCreate();
}
@ -55,163 +50,77 @@ public class DownloadUI extends PreferenceActivity
nativeDestroy();
}
public void onChangeCountryImpl(int group, int country, int region)
private String formatSizeString(long sizeInBytes)
{
int status = countryStatus(group, country, region);
Log.d(TAG, String.format("onChangeCountry %1$d, %2$d, %3$d, status=%4$d", group, country, region, status));
final long localBytes = countryLocalSizeInBytes(group, country, region);
final long remoteBytes = countryRemoteSizeInBytes(group, country, region);
Preference c = findPreference(group + " " + country + " " + region);
if (c == null)
{
Log.d(TAG, String.format("no preference found for %1$d %2$d %3$d", group, country, region));
return;
}
final String sizeString;
if (remoteBytes > 1024 * 1024)
sizeString = remoteBytes / (1024 * 1024) + "Mb";
if (sizeInBytes > 1024 * 1024)
return sizeInBytes / (1024 * 1024) + "Mb";
else if ((sizeInBytes + 1023) / 1024 > 999)
return "1Mb";
else
sizeString = remoteBytes / 1024 + "Kb";
return (sizeInBytes + 1023) / 1024 + "Kb";
}
private void updateCountryCell(final Preference cell, int group, int country, int region)
{
final int status = countryStatus(group, country, region);
switch (status)
{
case 0: // EOnDisk
c.setSummary("Click to delete " + sizeString);
cell.setSummary("Downloaded (" + formatSizeString(countryLocalSizeInBytes(group, country, region))
+ "), touch to delete");
break;
case 1: // ENotDownloaded
c.setSummary("Click to download " + sizeString);
cell.setSummary("Touch to download");// + formatSizeString(countryRemoteSizeInBytes(group, country, region)));
break;
case 2: // EDownloadFailed
c.setSummary("Download has failed, click again to retry");
cell.setSummary("Download has failed, touch again for one more try");
break;
case 3: // EDownloading
c.setSummary("Downloading...");
cell.setSummary("Downloading...");
break;
case 4: // EInQueue
c.setSummary("Marked to download");
cell.setSummary("Marked for downloading, touch to cancel");
break;
case 5: // EUnknown
c.setSummary("Unknown state :(");
cell.setSummary("Unknown state :(");
break;
}
}
class ChangeCountryFn implements Runnable
{
int m_group;
int m_country;
int m_region;
Object m_ui;
ChangeCountryFn(Object ui, int group, int country, int region)
{
m_ui = ui;
m_group = group;
m_country = country;
m_region = region;
}
public void run()
{
DownloadUI dui = (DownloadUI)m_ui;
dui.onChangeCountryImpl(m_group, m_country, m_region);
}
};
public void onChangeCountry(int group, int country, int region)
{
runOnUiThread(new ChangeCountryFn(this, group, country, region));
/// Should post a message onto gui thread as it could be called from the HttpThread
final Preference cell = findPreference(group + " " + country + " " + region);
if (cell == null)
{
Log.d(TAG, String.format("no preference found for %d %d %d", group, country, region));
return;
}
updateCountryCell(cell, group, country, region);
}
class ProgressFn implements Runnable
public void onProgress(int group, int country, int region, long current, long total)
{
int m_group;
int m_country;
int m_region;
long m_p1;
long m_p2;
Object m_ui;
ProgressFn(Object ui, int group, int country, int region, long p1, long p2)
{
m_ui = ui;
m_group = group;
m_country = country;
m_region = region;
m_p1 = p1;
m_p2 = p2;
}
public void run()
{
DownloadUI dui = (DownloadUI)m_ui;
dui.onProgressImpl(m_group, m_country, m_region, m_p1, m_p2);
}
};
public void onProgressImpl(int group, int country, int region, long p1, long p2)
{
Log.d(TAG, String.format("onProgress %1$d, %2$d, %3$d, %4$d, %5$d", group, country, region, p1, p2));
Preference c = findPreference(group + " " + country + " " + region);
final Preference c = findPreference(group + " " + country + " " + region);
if (c == null)
Log.d(TAG, String.format("no preference found for %1$d %2$d %3$d", group, country, region));
Log.d(TAG, String.format("no preference found for %d %d %d", group, country, region));
else
c.setSummary("Downloading... " + p1 * 100 / p2 + "%, touch to cancel");
}
public void onProgress(int group, int country, int region, long p1, long p2)
{
runOnUiThread(new ProgressFn(this, group, country, region, p1, p2));
/// Should post a message onto gui thread as it could be called from the HttpThread
c.setSummary("Downloading " + current * 100 / total + "%, touch to cancel");
}
private Preference createElement(int group, int country, int region)
{
final String name = countryName(group, country, region);
final int count = countriesCount(group, country, region);
if (count == 0)
if (countriesCount(group, country, region) == 0)
{ // it's downloadable country element
CheckBoxPreference c = new CheckBoxPreference(this);
c.setKey(group + " " + country + " " + region);
c.setTitle(name);
final CheckBoxPreference cell = new CheckBoxPreference(this);
cell.setKey(group + " " + country + " " + region);
cell.setTitle(name);
final long localBytes = countryLocalSizeInBytes(group, country, region);
final long remoteBytes = countryRemoteSizeInBytes(group, country, region);
updateCountryCell(cell, group, country, region);
final String sizeString;
if (remoteBytes > 1024 * 1024)
sizeString = remoteBytes / (1024 * 1024) + "Mb";
else
sizeString = remoteBytes / 1024 + "Kb";
int status = countryStatus(group, country, region);
switch (status)
{
case 0: // EOnDisk
c.setSummary("Click to delete " + sizeString);
break;
case 1: // ENotDownloaded
c.setSummary("Click to download " + sizeString);
break;
case 2: // EDownloadFailed
c.setSummary("Download has failed, click again to retry");
break;
case 3: // EDownloading
c.setSummary("Downloading...");
break;
case 4: // EInQueue
c.setSummary("Marked to download");
break;
case 5: // EUnknown
c.setSummary("Unknown state :(");
break;
}
return c;
return cell;
}
else
{ // it's parent element for downloadable countries
@ -248,83 +157,58 @@ public class DownloadUI extends PreferenceActivity
final int country = Integer.parseInt(keys[1]);
final int region = Integer.parseInt(keys[2]);
int status = countryStatus(group, country, region);
switch (status)
switch (countryStatus(group, country, region))
{
case 0: // EOnDisk
m_alert.setMessage("Delete " + countryName(group, country, region) + "?");
m_alert.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dlg, int which){
Log.d(TAG, String.format("deleting %1$d, %2$d, %3$d", group, country, region));
m_alert.setTitle(countryName(group, country, region));
m_alert.setPositiveButton("Delete", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int which) {
deleteCountry(group, country, region);
dlg.dismiss();
}
});
m_alert.show();
m_alert.setNegativeButton("Cancel", m_alertCancelHandler);
m_alert.create().show();
break;
case 1: // ENotDownloaded
/// ask to download, and start accordingly
m_alert.setMessage("Download " + countryName(group, country, region) + "?");
m_alert.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dlg, int which){
Log.d(TAG, String.format("downloading %1$d, %2$d, %3$d", group, country, region));
downloadCountry(group, country, region);
dlg.dismiss();
}
});
m_alert.show();
m_alert.setTitle(countryName(group, country, region));
m_alert.setPositiveButton("Download " + formatSizeString(countryRemoteSizeInBytes(group, country, region)),
new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int which) {
downloadCountry(group, country, region);
dlg.dismiss();
}
});
m_alert.setNegativeButton("Cancel", m_alertCancelHandler);
m_alert.create().show();
break;
case 2: // EDownloadFailed
/// ask to download, and start accordingly
m_alert.setMessage("Download " + countryName(group, country, region) + "?");
m_alert.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dlg, int which){
Log.d(TAG, String.format("downloading %1$d, %2$d, %3$d", group, country, region));
downloadCountry(group, country, region);
dlg.dismiss();
}
});
m_alert.show();
// Do not confirm download if status is failed, just start it
downloadCountry(group, country, region);
break;
case 3: // EDownloading
/// ask to download, and start accordingly
m_alert.setMessage("Cancel downloading " + countryName(group, country, region) + "?");
m_alert.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dlg, int which){
Log.d(TAG, String.format("deleting %1$d, %2$d, %3$d", group, country, region));
/// Confirm canceling
m_alert.setTitle(countryName(group, country, region));
m_alert.setPositiveButton("Cancel download", new DialogInterface.OnClickListener() {
public void onClick(DialogInterface dlg, int which) {
deleteCountry(group, country, region);
dlg.dismiss();
}
});
m_alert.setNegativeButton("Do nothing", m_alertCancelHandler);
m_alert.create().show();
break;
m_alert.show();
break;
case 4: // EInQueue
/// ask to download, and start accordingly
m_alert.setMessage("Remove " + countryName(group, country, region) + " from queue?");
m_alert.setButton(AlertDialog.BUTTON_POSITIVE, "Ok", new DialogInterface.OnClickListener(){
public void onClick(DialogInterface dlg, int which){
Log.d(TAG, String.format("deleting %1$d, %2$d, %3$d", group, country, region));
deleteCountry(group, country, region);
dlg.dismiss();
}
});
// Silently discard country from the queue
deleteCountry(group, country, region);
break;
m_alert.show();
break;
case 5: // EUnknown
//c.setSummary("Unknown state :(");
Log.d(TAG, "Unknown country state");
break;
}
}