1、修复restart的一个空指针问题
2、优化Ftp demo的代码
This commit is contained in:
@@ -19,7 +19,7 @@ import java.io.File;
|
||||
public class FtpDownload extends BaseActivity<ActivityTestBinding> {
|
||||
String TAG = "TestFTPActivity";
|
||||
private final String URL = "ftp://192.168.1.3:21/download//AriaPrj.rar";
|
||||
private final String FILE_PATH = "/mnt/sdcard/AriaPrj.rar";
|
||||
private final String FILE_PATH = "/mnt/sdcard/";
|
||||
|
||||
@Override protected int setLayoutId() {
|
||||
return R.layout.activity_test;
|
||||
|
||||
@@ -15,64 +15,110 @@
|
||||
*/
|
||||
package com.arialyy.simple.core.download;
|
||||
|
||||
import android.arch.lifecycle.Observer;
|
||||
import android.arch.lifecycle.ViewModelProviders;
|
||||
import android.content.Intent;
|
||||
import android.net.Uri;
|
||||
import android.os.Bundle;
|
||||
import android.support.annotation.Nullable;
|
||||
import android.util.Log;
|
||||
import android.view.View;
|
||||
import com.arialyy.annotations.Download;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.arialyy.aria.core.download.DownloadTask;
|
||||
import com.arialyy.aria.core.download.FtpDownloadTarget;
|
||||
import com.arialyy.aria.core.inf.IEntity;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.aria.util.CommonUtil;
|
||||
import com.arialyy.frame.util.show.L;
|
||||
import com.arialyy.frame.util.show.T;
|
||||
import com.arialyy.simple.R;
|
||||
import com.arialyy.simple.base.BaseActivity;
|
||||
import com.arialyy.simple.common.DirChooseDialog;
|
||||
import com.arialyy.simple.common.ModifyUrlDialog;
|
||||
import com.arialyy.simple.databinding.ActivityFtpDownloadBinding;
|
||||
import com.arialyy.simple.util.AppUtil;
|
||||
import java.io.File;
|
||||
import java.io.IOException;
|
||||
|
||||
/**
|
||||
* Created by lyy on 2017/7/25.
|
||||
* Ftp下载测试
|
||||
* Ftp下载
|
||||
*/
|
||||
public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding> {
|
||||
private final String URL = "ftp://9.9.9.205:2121/Cyberduck-6.9.4.30164.zip";
|
||||
//private final String URL = "ftp://182.92.180.213:21/video/572fed5c2ad48_1024.jpg";
|
||||
//private final String URL = "ftp://182.92.180.213:21/DATA/20180205/rar/1111.rar";
|
||||
//private final String URL = "ftp://d:d@dygodj8.com:12311/咖啡风暴HD大陆公映意语中字[飘花www.piaohua.com].mp4";
|
||||
private String mUrl, mFilePath;
|
||||
private FtpDownloadModule mModule;
|
||||
private FtpDownloadTarget mTarget;
|
||||
|
||||
@Override protected void init(Bundle savedInstanceState) {
|
||||
super.init(savedInstanceState);
|
||||
setTitle("FTP文件下载");
|
||||
Aria.download(this).register();
|
||||
DownloadEntity entity = Aria.download(this).getDownloadEntity(URL);
|
||||
if (entity != null) {
|
||||
getBinding().setFileSize(entity.getConvertFileSize());
|
||||
if (entity.getFileSize() == 0) {
|
||||
getBinding().setProgress(0);
|
||||
} else {
|
||||
getBinding().setProgress(entity.isComplete() ? 100
|
||||
: (int) (entity.getCurrentProgress() * 100 / entity.getFileSize()));
|
||||
mModule = ViewModelProviders.of(this).get(FtpDownloadModule.class);
|
||||
|
||||
mModule.getFtpDownloadInfo(this).observe(this, new Observer<DownloadEntity>() {
|
||||
|
||||
@Override public void onChanged(@Nullable DownloadEntity entity) {
|
||||
if (entity == null) {
|
||||
return;
|
||||
}
|
||||
mTarget = Aria.download(FtpDownloadActivity.this).loadFtp(entity.getUrl());
|
||||
if (mTarget.getTaskState() == IEntity.STATE_STOP) {
|
||||
getBinding().setStateStr(getString(R.string.resume));
|
||||
} else if (mTarget.isRunning()) {
|
||||
getBinding().setStateStr(getString(R.string.stop));
|
||||
}
|
||||
|
||||
if (entity.getFileSize() != 0) {
|
||||
getBinding().setFileSize(CommonUtil.formatFileSize(entity.getFileSize()));
|
||||
getBinding().setProgress(entity.isComplete() ? 100
|
||||
: (int) (entity.getCurrentProgress() * 100 / entity.getFileSize()));
|
||||
}
|
||||
getBinding().setUrl(entity.getUrl());
|
||||
getBinding().setFilePath(entity.getFilePath());
|
||||
mUrl = entity.getUrl();
|
||||
mFilePath = entity.getFilePath();
|
||||
}
|
||||
});
|
||||
getBinding().setViewModel(this);
|
||||
try {
|
||||
getBinding().codeView.setSource(AppUtil.getHelpCode(this, "FtpDownload.java"));
|
||||
} catch (IOException e) {
|
||||
e.printStackTrace();
|
||||
}
|
||||
}
|
||||
|
||||
public void onClick(View view) {
|
||||
switch (view.getId()) {
|
||||
case R.id.start:
|
||||
Aria.download(this).loadFtp(URL)
|
||||
.login("N0rI", "0qcK")
|
||||
.setFilePath("/mnt/sdcard/", true)
|
||||
.start();
|
||||
break;
|
||||
case R.id.stop:
|
||||
Aria.download(this).loadFtp(URL).stop();
|
||||
if (mTarget.isRunning()) {
|
||||
getBinding().setStateStr(getString(R.string.resume));
|
||||
Aria.download(this).loadFtp(mUrl).stop();
|
||||
} else {
|
||||
getBinding().setStateStr(getString(R.string.stop));
|
||||
Aria.download(this).loadFtp(mUrl).login("N0rI", "0qcK")
|
||||
.setFilePath(mFilePath, true)
|
||||
.start();
|
||||
}
|
||||
break;
|
||||
case R.id.cancel:
|
||||
Aria.download(this).loadFtp(URL).cancel();
|
||||
Aria.download(this).loadFtp(mUrl).cancel();
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
public void chooseUrl() {
|
||||
ModifyUrlDialog dialog =
|
||||
new ModifyUrlDialog(this, getString(R.string.modify_url_dialog_title), mUrl);
|
||||
dialog.show(getSupportFragmentManager(), "ModifyUrlDialog");
|
||||
}
|
||||
|
||||
public void chooseFilePath() {
|
||||
DirChooseDialog dirChooseDialog = new DirChooseDialog(this);
|
||||
dirChooseDialog.show(getSupportFragmentManager(), "DirChooseDialog");
|
||||
}
|
||||
|
||||
@Download.onPre() protected void onPre(DownloadTask task) {
|
||||
L.d(TAG, "ftp pre");
|
||||
}
|
||||
@@ -119,4 +165,14 @@ public class FtpDownloadActivity extends BaseActivity<ActivityFtpDownloadBinding
|
||||
@Override protected int setLayoutId() {
|
||||
return R.layout.activity_ftp_download;
|
||||
}
|
||||
|
||||
@Override protected void dataCallback(int result, Object data) {
|
||||
super.dataCallback(result, data);
|
||||
if (result == ModifyUrlDialog.MODIFY_URL_DIALOG_RESULT) {
|
||||
mModule.uploadUrl(this, String.valueOf(data));
|
||||
}else if (result == DirChooseDialog.DIR_CHOOSE_DIALOG_RESULT) {
|
||||
mModule.updateFilePath(this, String.valueOf(data));
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -0,0 +1,111 @@
|
||||
/*
|
||||
* Copyright (C) 2016 AriaLyy(https://github.com/AriaLyy/Aria)
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
package com.arialyy.simple.core.download;
|
||||
|
||||
import android.arch.lifecycle.LiveData;
|
||||
import android.arch.lifecycle.MutableLiveData;
|
||||
import android.content.Context;
|
||||
import android.net.Uri;
|
||||
import android.os.Environment;
|
||||
import android.text.TextUtils;
|
||||
import com.arialyy.aria.core.Aria;
|
||||
import com.arialyy.aria.core.download.DownloadEntity;
|
||||
import com.arialyy.aria.util.ALog;
|
||||
import com.arialyy.frame.base.BaseViewModule;
|
||||
import com.arialyy.simple.util.AppUtil;
|
||||
import java.io.File;
|
||||
|
||||
public class FtpDownloadModule extends BaseViewModule {
|
||||
private final String FTP_URL_KEY = "FTP_URL_KEY";
|
||||
private final String FTP_PATH_KEY = "FTP_PATH_KEY";
|
||||
|
||||
private final String ftpDefUrl = "ftp://9.9.9.205:2121/Cyberduck-6.9.4.30164.zip";
|
||||
private final String ftpDefPath =
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
|
||||
|
||||
private MutableLiveData<DownloadEntity> liveData = new MutableLiveData<>();
|
||||
private DownloadEntity singDownloadInfo;
|
||||
|
||||
/**
|
||||
* xx
|
||||
* 单任务下载的信息
|
||||
*/
|
||||
LiveData<DownloadEntity> getFtpDownloadInfo(Context context) {
|
||||
String url = AppUtil.getConfigValue(context, FTP_URL_KEY, ftpDefUrl);
|
||||
String filePath = AppUtil.getConfigValue(context, FTP_PATH_KEY, ftpDefPath);
|
||||
|
||||
singDownloadInfo = Aria.download(context).getDownloadEntity(url);
|
||||
if (singDownloadInfo == null) {
|
||||
singDownloadInfo = new DownloadEntity();
|
||||
singDownloadInfo.setUrl(url);
|
||||
String name = getFileName(ftpDefUrl);
|
||||
singDownloadInfo.setFileName(name);
|
||||
singDownloadInfo.setFilePath(filePath + name);
|
||||
} else {
|
||||
AppUtil.setConfigValue(context, FTP_PATH_KEY, singDownloadInfo.getFilePath());
|
||||
AppUtil.setConfigValue(context, FTP_URL_KEY, singDownloadInfo.getUrl());
|
||||
}
|
||||
liveData.postValue(singDownloadInfo);
|
||||
|
||||
return liveData;
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新文件保存路径
|
||||
*
|
||||
* @param dirPath 文件保存文件夹
|
||||
*/
|
||||
void updateFilePath(Context context, String dirPath) {
|
||||
if (TextUtils.isEmpty(dirPath)) {
|
||||
ALog.e(TAG, "文件保存路径为空");
|
||||
return;
|
||||
}
|
||||
AppUtil.setConfigValue(context, FTP_PATH_KEY, dirPath);
|
||||
singDownloadInfo.setFilePath(dirPath + singDownloadInfo.getFileName());
|
||||
liveData.postValue(singDownloadInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 更新url
|
||||
*/
|
||||
void uploadUrl(Context context, String url) {
|
||||
if (TextUtils.isEmpty(url)) {
|
||||
ALog.e(TAG, "下载地址为空");
|
||||
return;
|
||||
}
|
||||
AppUtil.setConfigValue(context, FTP_URL_KEY, url);
|
||||
File file = new File(singDownloadInfo.getFilePath());
|
||||
String name = getFileName(url);
|
||||
singDownloadInfo.setFilePath(file.getParent() + name);
|
||||
singDownloadInfo.setFileName(name);
|
||||
singDownloadInfo.setUrl(url);
|
||||
liveData.postValue(singDownloadInfo);
|
||||
}
|
||||
|
||||
/**
|
||||
* 通过url获取文件名
|
||||
*
|
||||
* @param url ftp地址
|
||||
* @return "/aria.text"
|
||||
*/
|
||||
private String getFileName(String url) {
|
||||
Uri uri = Uri.parse(url);
|
||||
String path = uri.getPath();
|
||||
int index = path.lastIndexOf("/");
|
||||
return path.substring(index);
|
||||
}
|
||||
}
|
||||
@@ -28,14 +28,15 @@ import com.arialyy.frame.base.BaseViewModule;
|
||||
import com.arialyy.simple.util.AppUtil;
|
||||
import java.io.File;
|
||||
|
||||
public class DownloadModule1 extends BaseViewModule {
|
||||
private final String DOWNLOAD_URL_KEY = "DOWNLOAD_URL_KEY";
|
||||
private final String DOWNLOAD_PATH_KEY = "DOWNLOAD_PATH_KEY";
|
||||
public class HttpDownloadModule extends BaseViewModule {
|
||||
private final String HTTP_URL_KEY = "HTTP_URL_KEY";
|
||||
private final String HTTP_PATH_KEY = "HTTP_PATH_KEY";
|
||||
|
||||
private final String defUrl =
|
||||
"http://hzdown.muzhiwan.com/2017/05/08/nl.noio.kingdom_59104935e56f0.apk";
|
||||
private final String defFilePath =
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath();
|
||||
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS).getPath()
|
||||
+ "/test.apk";
|
||||
|
||||
private MutableLiveData<DownloadEntity> liveData = new MutableLiveData<>();
|
||||
private DownloadEntity singDownloadInfo;
|
||||
@@ -43,9 +44,9 @@ public class DownloadModule1 extends BaseViewModule {
|
||||
/**
|
||||
* 单任务下载的信息
|
||||
*/
|
||||
LiveData<DownloadEntity> getSingDownloadInfo(Context context) {
|
||||
String url = AppUtil.getConfigValue(context, DOWNLOAD_URL_KEY, defUrl);
|
||||
String filePath = AppUtil.getConfigValue(context, DOWNLOAD_PATH_KEY, defFilePath);
|
||||
LiveData<DownloadEntity> getHttpDownloadInfo(Context context) {
|
||||
String url = AppUtil.getConfigValue(context, HTTP_URL_KEY, defUrl);
|
||||
String filePath = AppUtil.getConfigValue(context, HTTP_PATH_KEY, defFilePath);
|
||||
|
||||
singDownloadInfo = Aria.download(context).getDownloadEntity(url);
|
||||
if (singDownloadInfo == null) {
|
||||
@@ -55,8 +56,8 @@ public class DownloadModule1 extends BaseViewModule {
|
||||
singDownloadInfo.setDownloadPath(filePath);
|
||||
singDownloadInfo.setFileName(temp.getName());
|
||||
} else {
|
||||
AppUtil.setConfigValue(context, DOWNLOAD_PATH_KEY, singDownloadInfo.getDownloadPath());
|
||||
AppUtil.setConfigValue(context, DOWNLOAD_URL_KEY, singDownloadInfo.getUrl());
|
||||
AppUtil.setConfigValue(context, HTTP_PATH_KEY, singDownloadInfo.getDownloadPath());
|
||||
AppUtil.setConfigValue(context, HTTP_URL_KEY, singDownloadInfo.getUrl());
|
||||
}
|
||||
liveData.postValue(singDownloadInfo);
|
||||
|
||||
@@ -74,7 +75,7 @@ public class DownloadModule1 extends BaseViewModule {
|
||||
return;
|
||||
}
|
||||
File temp = new File(filePath);
|
||||
AppUtil.setConfigValue(context, DOWNLOAD_PATH_KEY, filePath);
|
||||
AppUtil.setConfigValue(context, HTTP_PATH_KEY, filePath);
|
||||
singDownloadInfo.setFileName(temp.getName());
|
||||
singDownloadInfo.setDownloadPath(filePath);
|
||||
liveData.postValue(singDownloadInfo);
|
||||
@@ -88,7 +89,7 @@ public class DownloadModule1 extends BaseViewModule {
|
||||
ALog.e(TAG, "下载地址为空");
|
||||
return;
|
||||
}
|
||||
AppUtil.setConfigValue(context, DOWNLOAD_URL_KEY, url);
|
||||
AppUtil.setConfigValue(context, HTTP_URL_KEY, url);
|
||||
singDownloadInfo.setUrl(url);
|
||||
liveData.postValue(singDownloadInfo);
|
||||
}
|
||||
@@ -53,7 +53,7 @@ class KotlinDownloadActivity : BaseActivity<ActivitySingleKotlinBinding>() {
|
||||
|
||||
private var mUrl: String? = null
|
||||
private var mFilePath: String? = null
|
||||
private var mModule: DownloadModule1? = null
|
||||
private var mModule: HttpDownloadModule? = null
|
||||
private var mTarget: DownloadTarget? = null
|
||||
|
||||
internal var receiver: BroadcastReceiver = object : BroadcastReceiver() {
|
||||
@@ -93,8 +93,8 @@ class KotlinDownloadActivity : BaseActivity<ActivitySingleKotlinBinding>() {
|
||||
Aria.download(this)
|
||||
.register()
|
||||
mModule = ViewModelProviders.of(this)
|
||||
.get(DownloadModule1::class.java)
|
||||
mModule!!.getSingDownloadInfo(this)
|
||||
.get(HttpDownloadModule::class.java)
|
||||
mModule!!.getHttpDownloadInfo(this)
|
||||
.observe(this, Observer { entity ->
|
||||
if (entity == null) {
|
||||
return@Observer
|
||||
|
||||
@@ -55,7 +55,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
|
||||
private String mUrl;
|
||||
private String mFilePath;
|
||||
private DownloadModule1 mModule;
|
||||
private HttpDownloadModule mModule;
|
||||
private DownloadTarget mTarget;
|
||||
|
||||
BroadcastReceiver receiver = new BroadcastReceiver() {
|
||||
@@ -89,8 +89,8 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
super.init(savedInstanceState);
|
||||
setTitle("单任务下载");
|
||||
Aria.download(this).register();
|
||||
mModule = ViewModelProviders.of(this).get(DownloadModule1.class);
|
||||
mModule.getSingDownloadInfo(this).observe(this, new Observer<DownloadEntity>() {
|
||||
mModule = ViewModelProviders.of(this).get(HttpDownloadModule.class);
|
||||
mModule.getHttpDownloadInfo(this).observe(this, new Observer<DownloadEntity>() {
|
||||
|
||||
@Override public void onChanged(@Nullable DownloadEntity entity) {
|
||||
if (entity == null) {
|
||||
@@ -194,6 +194,7 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
void taskStart(DownloadTask task) {
|
||||
if (task.getKey().equals(mUrl)) {
|
||||
getBinding().setFileSize(task.getConvertFileSize());
|
||||
ALog.d(TAG, "isComplete = " + task.isComplete() + ", state = " + task.getState());
|
||||
}
|
||||
}
|
||||
|
||||
@@ -236,9 +237,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
*
|
||||
*/
|
||||
@Download.onTaskFail
|
||||
void taskFail(DownloadTask task, Exception e) {
|
||||
if (task.getKey().equals(mUrl)) {
|
||||
@@ -274,9 +272,6 @@ public class SingleTaskActivity extends BaseActivity<ActivitySingleBinding> {
|
||||
startD();
|
||||
}
|
||||
break;
|
||||
case R.id.stop:
|
||||
Aria.download(this).load(mUrl).stop();
|
||||
break;
|
||||
case R.id.cancel:
|
||||
Aria.download(this).load(mUrl).cancel(true);
|
||||
break;
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
<layout xmlns:android="http://schemas.android.com/apk/res/android"
|
||||
xmlns:bind="http://schemas.android.com/apk/res-auto"
|
||||
>
|
||||
|
||||
<data>
|
||||
<variable
|
||||
name="fileSize"
|
||||
@@ -15,22 +16,71 @@
|
||||
name="progress"
|
||||
type="int"
|
||||
/>
|
||||
<variable
|
||||
name="stateStr"
|
||||
type="String"
|
||||
/>
|
||||
|
||||
<variable
|
||||
name="url"
|
||||
type="String"
|
||||
/>
|
||||
<variable
|
||||
name="filePath"
|
||||
type="String"
|
||||
/>
|
||||
<variable
|
||||
name="viewModel"
|
||||
type="com.arialyy.simple.core.download.FtpDownloadActivity"
|
||||
/>
|
||||
</data>
|
||||
|
||||
<LinearLayout
|
||||
xmlns:tools="http://schemas.android.com/tools"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:fitsSystemWindows="true"
|
||||
android:orientation="vertical"
|
||||
tools:context=".core.download.SingleTaskActivity"
|
||||
>
|
||||
|
||||
<include layout="@layout/layout_bar"/>
|
||||
|
||||
<com.arialyy.simple.widget.SvgTextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:layout_marginTop="16dp"
|
||||
bind:iconClickListener="@{() -> viewModel.chooseUrl()}"
|
||||
bind:svg_text_view_icon="@drawable/ic_modify"
|
||||
bind:text="@{@string/url(url)}"
|
||||
/>
|
||||
|
||||
<com.arialyy.simple.widget.SvgTextView
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_marginLeft="16dp"
|
||||
android:layout_marginRight="16dp"
|
||||
android:layout_marginTop="8dp"
|
||||
bind:iconClickListener="@{() -> viewModel.chooseFilePath()}"
|
||||
bind:svg_text_view_icon="@drawable/ic_choose_file"
|
||||
bind:text="@{@string/file_path(filePath)}"
|
||||
/>
|
||||
|
||||
<include
|
||||
layout="@layout/content_single"
|
||||
layout="@layout/layout_content_single"
|
||||
bind:fileSize="@{fileSize}"
|
||||
bind:progress="@{progress}"
|
||||
bind:speed="@{speed}"
|
||||
bind:stateStr="@{stateStr}"
|
||||
/>
|
||||
|
||||
<com.arialyy.simple.widget.CodeView
|
||||
android:id="@+id/code_view"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
/>
|
||||
|
||||
</LinearLayout>
|
||||
</layout>
|
||||
</layout>
|
||||
Reference in New Issue
Block a user