[and] Progress and speed indication for download.
This commit is contained in:
parent
1e8b70c569
commit
7894b7362d
6 changed files with 77 additions and 43 deletions
|
@ -11,28 +11,36 @@
|
|||
|
||||
<ProgressBar
|
||||
android:id="@+id/downloadProgress"
|
||||
style="@android:style/Widget.ProgressBar.Horizontal"
|
||||
style="@style/HorizontalProgress"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_centerInParent="true"
|
||||
android:max="100" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/downloadSpeed"
|
||||
style="@android:style/TextAppearance.Small.Inverse"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentLeft="true"
|
||||
android:layout_below="@+id/downloadProgress" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/downloadTotal"
|
||||
style="@android:style/TextAppearance.Small.Inverse"
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentRight="true"
|
||||
android:layout_below="@+id/downloadProgress" />
|
||||
|
||||
<TextView
|
||||
android:id="@+id/downloadState"
|
||||
style="@android:style/TextAppearance.Large.Inverse"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_above="@+id/downloadProgress"
|
||||
android:text="Downloading ... "
|
||||
android:textColor="@color/yellow" />
|
||||
|
||||
<Button
|
||||
android:id="@+id/pauseResume"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/downloadProgress"
|
||||
android:text="Pause/Resume" />
|
||||
|
||||
<CheckBox
|
||||
android:id="@+id/mobNetDownload"
|
||||
style="@android:style/TextAppearance.Medium.Inverse"
|
||||
|
@ -40,6 +48,6 @@
|
|||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentBottom="true"
|
||||
android:checked="false"
|
||||
android:text="Use mobile network" />
|
||||
android:text="@string/use_mobile_network" />
|
||||
|
||||
</RelativeLayout>
|
|
@ -7,5 +7,7 @@
|
|||
<style name="AppBaseTheme" parent="android:Theme.Holo.Light.NoActionBar">
|
||||
<!-- API 11 theme customizations can go here. -->
|
||||
</style>
|
||||
|
||||
<style name="HorizontalProgress" parent="@android:style/Widget.Holo.ProgressBar.Horizontal"/>
|
||||
|
||||
</resources>
|
||||
|
|
|
@ -6,5 +6,6 @@
|
|||
<string name="loading">Loading ...</string>
|
||||
<string name="searching">Searching ...</string>
|
||||
<string name="nothing_found">Nothing Found</string>
|
||||
<string name="use_mobile_network">Use mobile network</string>
|
||||
|
||||
</resources>
|
||||
|
|
|
@ -16,5 +16,7 @@
|
|||
<style name="AppTheme" parent="AppBaseTheme">
|
||||
<!-- All customizations that are NOT specific to a particular API-level can go here. -->
|
||||
</style>
|
||||
|
||||
<style name="HorizontalProgress" parent="@android:style/Widget.ProgressBar.Horizontal"/>
|
||||
|
||||
</resources>
|
||||
|
|
|
@ -7,9 +7,6 @@ import android.content.Intent;
|
|||
import android.content.pm.PackageManager.NameNotFoundException;
|
||||
import android.os.Bundle;
|
||||
import android.os.Messenger;
|
||||
import android.view.View;
|
||||
import android.view.View.OnClickListener;
|
||||
import android.widget.Button;
|
||||
import android.widget.CheckBox;
|
||||
import android.widget.CompoundButton;
|
||||
import android.widget.CompoundButton.OnCheckedChangeListener;
|
||||
|
@ -17,6 +14,7 @@ import android.widget.ProgressBar;
|
|||
import android.widget.TextView;
|
||||
|
||||
import com.example.travelguide.expansion.ExpansionService;
|
||||
import com.example.travelguide.util.Utils;
|
||||
import com.google.android.vending.expansion.downloader.DownloadProgressInfo;
|
||||
import com.google.android.vending.expansion.downloader.DownloaderClientMarshaller;
|
||||
import com.google.android.vending.expansion.downloader.DownloaderServiceMarshaller;
|
||||
|
@ -27,7 +25,7 @@ import com.google.android.vending.expansion.downloader.IStub;
|
|||
import com.susanin.travelguide.R;
|
||||
|
||||
public class ExpansionActivity extends Activity
|
||||
implements IDownloaderClient, OnCheckedChangeListener, OnClickListener
|
||||
implements IDownloaderClient, OnCheckedChangeListener
|
||||
{
|
||||
|
||||
|
||||
|
@ -37,10 +35,10 @@ public class ExpansionActivity extends Activity
|
|||
private ProgressBar mProgressBar;
|
||||
private TextView mDownloadState;
|
||||
private CheckBox mMobNetworkDownload;
|
||||
private Button mPauseResume;
|
||||
|
||||
// TODO: should be binded to state
|
||||
private boolean mIsDownloading;
|
||||
private TextView mSpeed;
|
||||
private TextView mTotal;
|
||||
|
||||
|
||||
@Override
|
||||
protected void onCreate(Bundle savedInstanceState)
|
||||
|
@ -61,10 +59,7 @@ public class ExpansionActivity extends Activity
|
|||
if (startResult == DownloaderClientMarshaller.NO_DOWNLOAD_REQUIRED)
|
||||
forwardToApp(); // START!
|
||||
else
|
||||
{
|
||||
// Set up progress and more
|
||||
setUpForDownloading();
|
||||
}
|
||||
}
|
||||
catch (final NameNotFoundException e)
|
||||
{
|
||||
|
@ -95,12 +90,12 @@ public class ExpansionActivity extends Activity
|
|||
|
||||
mProgressBar = (ProgressBar) findViewById(R.id.downloadProgress);
|
||||
mDownloadState = (TextView) findViewById(R.id.downloadState);
|
||||
// TODO: save this preference
|
||||
|
||||
mMobNetworkDownload = (CheckBox) findViewById(R.id.mobNetDownload);
|
||||
mMobNetworkDownload.setOnCheckedChangeListener(this);
|
||||
|
||||
mPauseResume = (Button) findViewById(R.id.pauseResume);
|
||||
mPauseResume.setOnClickListener(this);
|
||||
mSpeed = (TextView) findViewById(R.id.downloadSpeed);
|
||||
mTotal = (TextView) findViewById(R.id.downloadTotal);
|
||||
}
|
||||
|
||||
private void forwardToApp()
|
||||
|
@ -158,21 +153,36 @@ public class ExpansionActivity extends Activity
|
|||
forwardToApp();
|
||||
return;
|
||||
}
|
||||
// TODO: add more cases
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onDownloadProgress(DownloadProgressInfo progress)
|
||||
{
|
||||
// TODO Auto-generated method stub
|
||||
final int currentProgressInPercent = (int) (100*progress.mOverallProgress/DATA_FILE.fileSize);
|
||||
final int currentProgressInPercent =
|
||||
(int) (mProgressBar.getMax()*progress.mOverallProgress/(float)progress.mOverallTotal);
|
||||
|
||||
String downloadSpeed = "";
|
||||
if (progress.mCurrentSpeed > 0)
|
||||
downloadSpeed = Utils.formatDataSize(Math.round(progress.mCurrentSpeed*1024))+ "/S";
|
||||
|
||||
final String progressStr = Utils.formatDataSize(progress.mOverallProgress) + "/"
|
||||
+ Utils.formatDataSize(progress.mOverallTotal);
|
||||
|
||||
mSpeed.setText(downloadSpeed);
|
||||
mTotal.setText(progressStr);
|
||||
mProgressBar.setProgress(currentProgressInPercent);
|
||||
}
|
||||
|
||||
private void setMobileNetworkDownloadEnabled(boolean enabled)
|
||||
{
|
||||
if (mRemoteService != null)
|
||||
mRemoteService.setDownloadFlags(enabled ? IDownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR: 0);
|
||||
if (enabled)
|
||||
{
|
||||
mRemoteService.setDownloadFlags(IDownloaderService.FLAGS_DOWNLOAD_OVER_CELLULAR);
|
||||
mRemoteService.requestContinueDownload();
|
||||
}
|
||||
else
|
||||
mRemoteService.setDownloadFlags(0);
|
||||
}
|
||||
|
||||
@Override
|
||||
|
@ -181,20 +191,4 @@ public class ExpansionActivity extends Activity
|
|||
if (buttonView == mMobNetworkDownload)
|
||||
setMobileNetworkDownloadEnabled(isChecked);
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onClick(View target)
|
||||
{
|
||||
if (target.getId() == R.id.pauseResume)
|
||||
pauseResumeDownload();
|
||||
}
|
||||
|
||||
private void pauseResumeDownload()
|
||||
{
|
||||
if (mRemoteService != null)
|
||||
{
|
||||
// TODO: track state and manage it
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -1,6 +1,9 @@
|
|||
package com.example.travelguide.util;
|
||||
|
||||
import java.io.File;
|
||||
import java.math.RoundingMode;
|
||||
import java.text.DecimalFormat;
|
||||
import java.text.NumberFormat;
|
||||
|
||||
import android.app.Activity;
|
||||
import android.content.Context;
|
||||
|
@ -120,4 +123,28 @@ public class Utils
|
|||
return new MWMPoint(info.getLat(), info.getLon(), info.getName(), id);
|
||||
}
|
||||
|
||||
|
||||
private final static NumberFormat df = DecimalFormat.getInstance(); // frequently used, so we dont recreate
|
||||
static
|
||||
{
|
||||
df.setMinimumFractionDigits(0);
|
||||
df.setMaximumFractionDigits(2);
|
||||
df.setRoundingMode(RoundingMode.DOWN);
|
||||
}
|
||||
|
||||
public static String formatDataSize(long bytes)
|
||||
{
|
||||
if (bytes < 0)
|
||||
return "";
|
||||
final String[] suffixes = {"B", "KB", "MB", "GB", "TB", "EB"};
|
||||
final int powerOf1024 = (int) log1024(bytes);
|
||||
final double shortValue = bytes/Math.pow(1024, powerOf1024);
|
||||
return df.format(shortValue) + suffixes[powerOf1024];
|
||||
}
|
||||
|
||||
public static double log1024(double value)
|
||||
{
|
||||
return Math.log(value)/Math.log(1024);
|
||||
}
|
||||
|
||||
}
|
||||
|
|
Reference in a new issue