package com.example.jsontest;
import java.io.File;
import android.app.Activity;
import android.content.Intent;
import android.net.Uri;
import android.os.Bundle;
import android.os.Environment;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.ProgressBar;
import android.widget.TextView;
import android.widget.Toast;
public class MainActivity extends Activity implements IGAsyncTaskInterface, View.OnClickListener {
private Button m_downBtn;
private ProgressBar m_progressBar;
private TextView m_textView;
private IGAsyncDownloadTask m_currTask;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
m_downBtn = (Button)findViewById(R.id.TestDownBtn);
m_progressBar = (ProgressBar)findViewById(R.id.TestProgressBar);
m_textView = (TextView)findViewById(R.id.TestTextView);
m_downBtn.setOnClickListener(this);
deleteDownloadedFile();
protected void deleteDownloadedFile()
{
File dir = new File(getApplicationStoragePath() + "/pepolds1.zip");
if(dir.exists())
dir.delete();
}
protected String getApplicationStoragePath() {
String ext = Environment.getExternalStorageState();
String path = Environment.getExternalStorageDirectory().toString();
if(ext.equals(Environment.MEDIA_MOUNTED))
{
path = Environment.getExternalStorageDirectory().getAbsolutePath();
path = path + "/" + "pepolds1";
}
else
{
path = Environment.MEDIA_UNMOUNTED;
}
return path;
}
protected String getFileSize(int size)
{
String appendix = "Byte";
int calSize = size;
if(size > 1024 && size < 1024 * 1024)
{
appendix = "KB";
calSize = size / 1024;
}
else if(size > 1024 * 1024)
{
appendix = "MB";
calSize = size / (1024 * 1024);
}
return String.valueOf(calSize) + appendix;
}
@Override
public void onPreExecute() {
Toast.makeText(this, "Start Download!", Toast.LENGTH_SHORT).show();
}
@Override
public void onProgressUpdate(Integer... values) {
float curr = (float)m_progressBar.getProgress() + (float)values[0];
float total = (float)values[1];
float fPercent = curr / total;
int percent = (int)(fPercent * 100);
m_progressBar.setMax(values[1]);
m_progressBar.setProgress(m_progressBar.getProgress() + values[0]);
m_textView.setText(getFileSize(values[0] + m_progressBar.getProgress()) + "/" + getFileSize(values[1]) + " [" + String.valueOf(percent) + "%]");
}
@Override
public void onPostExecute(IGAsyncNetworkStatusEnum status) {
m_currTask = null;
Toast.makeText(this, "Download Complete!", Toast.LENGTH_SHORT).show();
}
@Override
public void onCancelled(IGAsyncNetworkStatusEnum status) {
m_currTask = null;
Toast.makeText(this, "Download Cancelled!", Toast.LENGTH_SHORT).show();
}
@Override
public void onClick(View v) {
if(m_currTask == null)
{
File dir = new File(getApplicationStoragePath());
if(!dir.exists())
dir.mkdirs();
String[] params = { null,
getApplicationStoragePath() + "/pepolds1.zip" };
m_currTask = new IGAsyncDownloadTask(this, false);
m_currTask.execute(params);
}
}
}