Skip to content
This repository has been archived by the owner on Mar 6, 2019. It is now read-only.

Commit

Permalink
增加手动更新数据接口update,特殊业务可使用手动更新
Browse files Browse the repository at this point in the history
  • Loading branch information
lijian committed Aug 4, 2017
1 parent 0bc0d56 commit 76e47e2
Show file tree
Hide file tree
Showing 8 changed files with 76 additions and 13 deletions.
6 changes: 3 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,14 @@ allprojects {
```
dependencies {
//如果项目中已有com.android.support.*包,则从Downloader中剔除;
compile('com.github.CodyyAndroid:Downloader:1.1.8') {
compile('com.github.CodyyAndroid:Downloader:1.1.9') {
exclude group: 'com.android.support'
}
//如果项目中无com.android.support.*包,则保留;
compile('com.github.CodyyAndroid:Downloader:1.1.8')
compile('com.github.CodyyAndroid:Downloader:1.1.9')
}
```
## [API](https://jitpack.io/com/github/CodyyAndroid/Downloader/1.1.8/javadoc/)
## [API](https://jitpack.io/com/github/CodyyAndroid/Downloader/1.1.9/javadoc/)


20 changes: 17 additions & 3 deletions app/src/main/java/com/codyy/downloader/MultiDownloadActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -74,9 +74,10 @@ public boolean onKey(View view, int i, KeyEvent keyEvent) {
recyclerView.setAdapter(new FileAdapter(mFileEntities));
// Downloader.getInstance(getApplicationContext()).download(getString(R.string.url_apk_file));
// Downloader.getInstance(getApplicationContext()).download(getString(R.string.url_small_file));
mEditText.setText(Formatter.formatFileSize(getBaseContext(), getAvailableStore(getExternalStoragePath()))+"/"+Formatter.formatFileSize(getBaseContext(), getTotalStore(getExternalStoragePath())));
mEditText.setText(Formatter.formatFileSize(getBaseContext(), getAvailableStore(getExternalStoragePath())) + "/" + Formatter.formatFileSize(getBaseContext(), getTotalStore(getExternalStoragePath())));

}

// 获取SD卡路径
public static String getExternalStoragePath() {
// 获取SdCard状态
Expand All @@ -98,6 +99,7 @@ public static String getExternalStoragePath() {
return null;

}

private String getAvailMemory() {// 获取android当前可用内存大小

ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
Expand All @@ -107,6 +109,7 @@ private String getAvailMemory() {// 获取android当前可用内存大小

return Formatter.formatFileSize(getBaseContext(), mi.availMem);// 将获取的内存大小规格化
}

public static long getAvailableStore(String filePath) {

// 取得sdcard文件路径
Expand All @@ -132,6 +135,7 @@ public static long getAvailableStore(String filePath) {
return availableSpare;

}

public static long getTotalStore(String filePath) {

// 取得sdcard文件路径
Expand All @@ -144,16 +148,17 @@ public static long getTotalStore(String filePath) {

// 获取BLOCK数量

long totalBlocks = statFs.getBlockCount();
long totalBlocks = statFs.getBlockCount();

// 可使用的Block的数量

long total = totalBlocks * blocSize;
long total = totalBlocks * blocSize;


return total;

}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
/*if (keyCode == KeyEvent.KEYCODE_DEL) {
Expand Down Expand Up @@ -217,6 +222,14 @@ public void queryAll(View view) {
}
}

public void update(View view) {
DownloadEntity entity = Downloader.getInstance(this).getDownloadRecord(getString(R.string.url_small_file));
if (entity != null && entity.getExtra2() != null) {
entity.setExtra2(new StringBuilder(entity.getExtra2()).append("\u0003").append("sultan").toString());
Downloader.getInstance(this).update(entity);
}
}

class FileViewHolder extends RecyclerViewHolder<FileEntity> {
TextView tvName;
TextView tvUrl;
Expand All @@ -240,6 +253,7 @@ public void onClick(View v) {
DownloadEntity entity = new DownloadEntity();
entity.setId(tvUrl.getText().toString());
entity.setUrl(tvUrl.getText().toString());
entity.setExtra2("lijian");
Downloader.getInstance(itemView.getContext()).download(entity);
} else if ("暂停".equals(btn.getText().toString())) {
btn.setText("下载");
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/layout/activity_multi_download.xml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,11 @@
android:layout_height="wrap_content"
android:onClick="queryAll"
android:text="queryAll"/>
<Button
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:onClick="update"
android:text="更新"/>
</LinearLayout>

<EditText
Expand Down
6 changes: 3 additions & 3 deletions downloadlibrary/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ android {
defaultConfig {
minSdkVersion 14
targetSdkVersion 26
versionCode 51
versionName "1.1.8"
versionCode 52
versionName "1.1.9"

testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"

Expand Down Expand Up @@ -56,7 +56,7 @@ javadoc {
author true
version true
links "http://docs.oracle.com/javase/7/docs/api"
title "下载库1.1.8"
title "下载库1.1.9"
}
}
// 制作文档(Javadoc)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
* 文件下载器
* Created by lijian on 2017/6/7.
*
* @version 1.1.8
* @version 1.1.9
*/

public class Downloader {
Expand Down Expand Up @@ -203,6 +203,13 @@ public void pause(@NonNull String... ids) {
}
}

/**
* 手动更新数据库(特殊业务使用)
*/
public boolean update(DownloadEntity entity) {
return mDownloadService != null && mDownloadService.update(entity);
}

/**
* 暂停下载
*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,11 @@ public interface DownloadDao {
*/
boolean updateStatus(String id, @DownloadFlag int status);

/**
* 手动更新数据(特殊情况使用)
*/
boolean update(DownloadEntity entity);

/**
* 更新保存路径
*/
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
package com.codyy.download.db;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;
import android.database.SQLException;
Expand Down Expand Up @@ -100,7 +101,7 @@ public synchronized boolean save(DownloadEntity entry) {
+ "," + DownloadTable.COLUMN_NAME_EXTRA1
+ "," + DownloadTable.COLUMN_NAME_EXTRA2
+ ") values(?,?,?,?,?,?,?,?,?,?,?)";
Object[] bindArgs = {entry.getId(),entry.getUrl(), entry.getCurrent(), entry.getTotal(), entry.getSavePath(), entry.getName(), entry.getStatus(), entry.getThumbnails(), entry.getTime(), entry.getExtra1(), entry.getExtra2()};
Object[] bindArgs = {entry.getId(), entry.getUrl(), entry.getCurrent(), entry.getTotal(), entry.getSavePath(), entry.getName(), entry.getStatus(), entry.getThumbnails(), entry.getTime(), entry.getExtra1(), entry.getExtra2()};
database.execSQL(sql, bindArgs);
database.setTransactionSuccessful();
} catch (Exception e) {
Expand Down Expand Up @@ -184,6 +185,30 @@ public synchronized boolean updateStatus(String id, @DownloadFlag int status) {
return count > 0;*/
}

@Override
public synchronized boolean update(DownloadEntity entity) {
ContentValues values = new ContentValues(1);
values.put(DownloadTable.COLUMN_NAME_CURRENT_POSITION, entity.getCurrent());
values.put(DownloadTable.COLUMN_NAME_TOTAL_SIZE, entity.getTotal());
values.put(DownloadTable.COLUMN_NAME_DOWNLOAD_URL, entity.getUrl());
values.put(DownloadTable.COLUMN_NAME_SAVE_PATH, entity.getSavePath());
values.put(DownloadTable.COLUMN_NAME_TITLE, entity.getName());
values.put(DownloadTable.COLUMN_NAME_STATUS, entity.getStatus());
values.put(DownloadTable.COLUMN_NAME_THUMBNAILS, entity.getThumbnails());
values.put(DownloadTable.COLUMN_NAME_DOWNLOAD_TIME, entity.getTime());
values.put(DownloadTable.COLUMN_NAME_EXTRA1, entity.getExtra1());
values.put(DownloadTable.COLUMN_NAME_EXTRA2, entity.getExtra2());
String selection = DownloadTable.COLUMN_NAME_ID + DBSelection.SELECTION_EQUAL;
String[] selectionArgs = {entity.getId()};
int count = mDbHelper.getWritableDatabase().update(
DownloadTable.TABLE_NAME,
values,
selection,
selectionArgs
);
return count > 0;
}

@Override
public synchronized void updatePath(String id, String savePath) {
SQLiteDatabase database = mDbHelper.getWritableDatabase();
Expand Down Expand Up @@ -284,7 +309,7 @@ public List<DownloadEntity> queryAll() {
selectionArgs,
null,
null,
"cast ("+ DownloadTable.COLUMN_NAME_DOWNLOAD_TIME+" as INTEGER)" + DBSelection.SELECTION_DESC
"cast (" + DownloadTable.COLUMN_NAME_DOWNLOAD_TIME + " as INTEGER)" + DBSelection.SELECTION_DESC
);
while (cursor.moveToNext()) {
list.add(getDownloadEntity(cursor));
Expand All @@ -305,7 +330,7 @@ public List<DownloadEntity> queryDoingOn() {
selectionArgs,
null,
null,
"cast ("+ DownloadTable.COLUMN_NAME_DOWNLOAD_TIME+" as INTEGER)" + DBSelection.SELECTION_DESC
"cast (" + DownloadTable.COLUMN_NAME_DOWNLOAD_TIME + " as INTEGER)" + DBSelection.SELECTION_DESC
);
while (cursor.moveToNext()) {
list.add(getDownloadEntity(cursor));
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -359,6 +359,13 @@ public void pause(@NonNull String... ids) {
}
}

/**
* 手动更新数据库
*/
public boolean update(DownloadEntity entity) {
return mDownloadDao.update(entity);
}

/**
* 获取所有下载记录
*
Expand Down

0 comments on commit 76e47e2

Please sign in to comment.