parent
765f42d73c
commit
47993ba297
@ -0,0 +1,124 @@ |
||||
package com.arialyy.simple.pop; |
||||
|
||||
import android.content.Context; |
||||
import android.graphics.Color; |
||||
import android.graphics.drawable.ClipDrawable; |
||||
import android.graphics.drawable.ColorDrawable; |
||||
import android.os.Environment; |
||||
import android.view.View; |
||||
import android.widget.Button; |
||||
import android.widget.TextView; |
||||
import butterknife.Bind; |
||||
import butterknife.OnClick; |
||||
import com.arialyy.aria.core.AMTarget; |
||||
import com.arialyy.aria.core.Aria; |
||||
import com.arialyy.aria.core.DownloadEntity; |
||||
import com.arialyy.aria.core.task.Task; |
||||
import com.arialyy.aria.util.CommonUtil; |
||||
import com.arialyy.frame.core.AbsDialog; |
||||
import com.arialyy.frame.core.AbsPopupWindow; |
||||
import com.arialyy.simple.R; |
||||
import com.arialyy.simple.widget.HorizontalProgressBarWithNumber; |
||||
|
||||
/** |
||||
* Created by AriaL on 2017/1/2. |
||||
*/ |
||||
public class DownloadPopupWindow extends AbsPopupWindow { |
||||
@Bind(R.id.progressBar) HorizontalProgressBarWithNumber mPb; |
||||
@Bind(R.id.start) Button mStart; |
||||
@Bind(R.id.stop) Button mStop; |
||||
@Bind(R.id.cancel) Button mCancel; |
||||
@Bind(R.id.size) TextView mSize; |
||||
@Bind(R.id.speed) TextView mSpeed; |
||||
|
||||
private static final String DOWNLOAD_URL = |
||||
"http://static.gaoshouyou.com/d/3a/93/573ae1db9493a801c24bf66128b11e39.apk"; |
||||
|
||||
public DownloadPopupWindow(Context context) { |
||||
super(context, new ColorDrawable(Color.WHITE)); |
||||
initWidget(); |
||||
} |
||||
|
||||
@Override protected int setLayoutId() { |
||||
return R.layout.dialog_download; |
||||
} |
||||
|
||||
private void initWidget() { |
||||
if (Aria.get(this).taskExists(DOWNLOAD_URL)) { |
||||
AMTarget target = Aria.whit(this).load(DOWNLOAD_URL); |
||||
int p = (int) (target.getCurrentProgress() * 100 / target.getFileSize()); |
||||
mPb.setProgress(p); |
||||
} |
||||
Aria.whit(this).addSchedulerListener(new MyDialogDownloadCallback()); |
||||
DownloadEntity entity = Aria.get(this).getDownloadEntity(DOWNLOAD_URL); |
||||
if (entity != null) { |
||||
mSize.setText(CommonUtil.formatFileSize(entity.getFileSize())); |
||||
int state = entity.getState(); |
||||
setBtState(state != DownloadEntity.STATE_DOWNLOAD_ING); |
||||
} else { |
||||
setBtState(true); |
||||
} |
||||
} |
||||
|
||||
@OnClick({ R.id.start, R.id.stop, R.id.cancel }) public void onClick(View view) { |
||||
switch (view.getId()) { |
||||
case R.id.start: |
||||
Aria.whit(this) |
||||
.load(DOWNLOAD_URL) |
||||
.setDownloadPath(Environment.getExternalStorageDirectory().getPath() + "/daialog.apk") |
||||
.setDownloadName("daialog.apk") |
||||
.start(); |
||||
break; |
||||
case R.id.stop: |
||||
Aria.whit(this).load(DOWNLOAD_URL).stop(); |
||||
break; |
||||
case R.id.cancel: |
||||
Aria.whit(this).load(DOWNLOAD_URL).cancel(); |
||||
break; |
||||
} |
||||
} |
||||
|
||||
@Override protected void dataCallback(int result, Object obj) { |
||||
|
||||
} |
||||
|
||||
private void setBtState(boolean startEnable) { |
||||
mStart.setEnabled(startEnable); |
||||
mCancel.setEnabled(!startEnable); |
||||
mStop.setEnabled(!startEnable); |
||||
} |
||||
|
||||
private class MyDialogDownloadCallback extends Aria.SimpleSchedulerListener { |
||||
|
||||
@Override public void onTaskPre(Task task) { |
||||
super.onTaskPre(task); |
||||
mSize.setText(CommonUtil.formatFileSize(task.getFileSize())); |
||||
setBtState(false); |
||||
} |
||||
|
||||
@Override public void onTaskStop(Task task) { |
||||
super.onTaskStop(task); |
||||
setBtState(true); |
||||
mSpeed.setText("0.0kb/s"); |
||||
} |
||||
|
||||
@Override public void onTaskCancel(Task task) { |
||||
super.onTaskCancel(task); |
||||
setBtState(true); |
||||
mPb.setProgress(0); |
||||
mSpeed.setText("0.0kb/s"); |
||||
} |
||||
|
||||
@Override public void onTaskRunning(Task task) { |
||||
super.onTaskRunning(task); |
||||
long current = task.getCurrentProgress(); |
||||
long len = task.getFileSize(); |
||||
if (len == 0) { |
||||
mPb.setProgress(0); |
||||
} else { |
||||
mPb.setProgress((int) ((current * 100) / len)); |
||||
} |
||||
mSpeed.setText(CommonUtil.formatFileSize(task.getSpeed()) + "/s"); |
||||
} |
||||
} |
||||
} |
@ -0,0 +1,79 @@ |
||||
<?xml version="1.0" encoding="utf-8"?> |
||||
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
> |
||||
|
||||
<com.arialyy.simple.widget.HorizontalProgressBarWithNumber |
||||
android:id="@+id/progressBar" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="20dp" |
||||
android:layout_alignParentLeft="true" |
||||
android:layout_alignParentStart="true" |
||||
android:layout_alignParentTop="true" |
||||
android:layout_margin="16dp" |
||||
android:layout_toLeftOf="@+id/size" |
||||
android:max="100" |
||||
style="?android:attr/progressBarStyleHorizontal" |
||||
/> |
||||
|
||||
<TextView |
||||
android:id="@+id/size" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_alignParentRight="true" |
||||
android:layout_alignTop="@+id/progressBar" |
||||
android:layout_marginRight="16dp" |
||||
android:text="0mb" |
||||
android:textSize="16sp" |
||||
/> |
||||
|
||||
|
||||
<LinearLayout |
||||
android:id="@+id/handle_bar" |
||||
android:layout_width="match_parent" |
||||
android:layout_height="wrap_content" |
||||
android:layout_below="@+id/progressBar" |
||||
android:orientation="horizontal" |
||||
> |
||||
<TextView |
||||
android:id="@+id/speed" |
||||
android:layout_width="wrap_content" |
||||
android:layout_height="wrap_content" |
||||
android:layout_marginLeft="16dp" |
||||
android:text="0kb/s" |
||||
android:textColor="@color/black" |
||||
/> |
||||
|
||||
<Button |
||||
android:id="@+id/start" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:onClick="onClick" |
||||
android:text="开始" |
||||
style="?buttonBarButtonStyle" |
||||
/> |
||||
|
||||
<Button |
||||
android:id="@+id/stop" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:onClick="onClick" |
||||
android:text="暂停" |
||||
style="?buttonBarButtonStyle" |
||||
/> |
||||
|
||||
<Button |
||||
android:id="@+id/cancel" |
||||
android:layout_width="0dp" |
||||
android:layout_height="wrap_content" |
||||
android:layout_weight="1" |
||||
android:onClick="onClick" |
||||
android:text="删除" |
||||
style="?buttonBarButtonStyle" |
||||
/> |
||||
</LinearLayout> |
||||
|
||||
</RelativeLayout> |
After Width: | Height: | Size: 301 KiB |
After Width: | Height: | Size: 231 KiB |
Loading…
Reference in new issue