Skip to content

Commit

Permalink
fix bug #6, #9, #11
Browse files Browse the repository at this point in the history
  • Loading branch information
hnvn committed Aug 21, 2018
1 parent 33c362e commit f8fa727
Show file tree
Hide file tree
Showing 8 changed files with 57 additions and 8 deletions.
2 changes: 1 addition & 1 deletion android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,5 @@ android {
}

dependencies {
api "android.arch.work:work-runtime:1.0.0-alpha06"
api "android.arch.work:work-runtime:1.0.0-alpha07"
}
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ public class DownloadWorker extends Worker {
@Override
public Result doWork() {
Context context = getApplicationContext();
dbHelper = new TaskDbHelper(context);
dbHelper = TaskDbHelper.getInstance(context);

String url = getInputData().getString(ARG_URL);
String fileName = getInputData().getString(ARG_FILE_NAME);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,16 +13,21 @@
import android.os.Bundle;
import android.provider.BaseColumns;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;


import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.UUID;
import java.util.concurrent.Executors;
import java.util.concurrent.LinkedBlockingDeque;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;

import androidx.work.BackoffPolicy;
import androidx.work.Configuration;
import androidx.work.Constraints;
import androidx.work.Data;
import androidx.work.NetworkType;
Expand All @@ -34,7 +39,6 @@
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.MethodChannel.MethodCallHandler;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.view.FlutterNativeView;

public class FlutterDownloaderPlugin implements MethodCallHandler {
private static final String CHANNEL = "vn.hunghd/downloader";
Expand All @@ -43,6 +47,8 @@ public class FlutterDownloaderPlugin implements MethodCallHandler {
private MethodChannel flutterChannel;
private TaskDbHelper dbHelper;

public static int maximumConcurrentTask;

private final BroadcastReceiver updateProcessEventReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Expand All @@ -56,7 +62,11 @@ public void onReceive(Context context, Intent intent) {
private FlutterDownloaderPlugin(Context context, BinaryMessenger messenger) {
flutterChannel = new MethodChannel(messenger, CHANNEL);
flutterChannel.setMethodCallHandler(this);
dbHelper = new TaskDbHelper(context);
dbHelper = TaskDbHelper.getInstance(context);
Log.d(TAG, "maximumConcurrentTask = " + maximumConcurrentTask);
WorkManager.initialize(context, new Configuration.Builder()
.setExecutor(Executors.newFixedThreadPool(Math.max(maximumConcurrentTask, 1)))
.build());
}

@SuppressLint("NewApi")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ public class TaskDbHelper extends SQLiteOpenHelper {
public static final int DATABASE_VERSION = 1;
public static final String DATABASE_NAME = "download_tasks.db";

private static TaskDbHelper instance = null;

private static final String SQL_CREATE_ENTRIES =
"CREATE TABLE " + TaskEntry.TABLE_NAME + " (" +
TaskEntry._ID + " INTEGER PRIMARY KEY," +
Expand All @@ -23,7 +25,20 @@ public class TaskDbHelper extends SQLiteOpenHelper {
private static final String SQL_DELETE_ENTRIES =
"DROP TABLE IF EXISTS " + TaskEntry.TABLE_NAME;

public TaskDbHelper(Context context) {

public static TaskDbHelper getInstance(Context ctx) {

// Use the application context, which will ensure that you
// don't accidentally leak an Activity's context.
// See this article for more information: http://bit.ly/6LRzfx
if (instance == null) {
instance = new TaskDbHelper(ctx.getApplicationContext());
}
return instance;
}


private TaskDbHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,11 +3,13 @@
import android.os.Bundle;
import io.flutter.app.FlutterActivity;
import io.flutter.plugins.GeneratedPluginRegistrant;
import vn.hunghd.flutterdownloader.FlutterDownloaderPlugin;

public class MainActivity extends FlutterActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
FlutterDownloaderPlugin.maximumConcurrentTask = 2;
GeneratedPluginRegistrant.registerWith(this);
}
}
3 changes: 3 additions & 0 deletions example/ios/Runner/AppDelegate.m
Original file line number Diff line number Diff line change
@@ -1,10 +1,13 @@
#include "AppDelegate.h"
#include "GeneratedPluginRegistrant.h"
#include "FlutterDownloaderPlugin.h"

@implementation AppDelegate

- (BOOL)application:(UIApplication *)application
didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {

[FlutterDownloaderPlugin setMaximumConcurrentTask: 2];
[GeneratedPluginRegistrant registerWithRegistry:self];
// Override point for customization after application launch.
return [super application:application didFinishLaunchingWithOptions:launchOptions];
Expand Down
7 changes: 6 additions & 1 deletion ios/Classes/FlutterDownloaderPlugin.h
Original file line number Diff line number Diff line change
@@ -1,4 +1,9 @@
#import <Flutter/Flutter.h>

@interface FlutterDownloaderPlugin : NSObject<FlutterPlugin>
@end

+ (int) maximumConcurrentTask;

+ (void) setMaximumConcurrentTask:(int)val;

@end
18 changes: 16 additions & 2 deletions ios/Classes/FlutterDownloaderPlugin.m
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,20 @@ @interface FlutterDownloaderPlugin()<NSURLSessionTaskDelegate, NSURLSessionDownl

@implementation FlutterDownloaderPlugin

static int maximumConcurrentTask;

+ (int)maximumConcurrentTask {
@synchronized(self) {
return maximumConcurrentTask;
}
}

+ (void)setMaximumConcurrentTask:(int)val {
@synchronized(self) {
maximumConcurrentTask = val;
}
}

+ (void)registerWithRegistrar:(NSObject<FlutterPluginRegistrar>*)registrar {

FlutterDownloaderPlugin* instance = [[FlutterDownloaderPlugin alloc] initWithBinaryMessenger:registrar.messenger];
Expand Down Expand Up @@ -93,9 +107,9 @@ - (instancetype)initWithBinaryMessenger: (NSObject<FlutterBinaryMessenger>*) mes
_flutterChannel = [FlutterMethodChannel
methodChannelWithName:@"vn.hunghd/downloader"
binaryMessenger:messenger];

NSLog(@"maximumConcurrentTask: %d", FlutterDownloaderPlugin.maximumConcurrentTask);
NSURLSessionConfiguration *sessionConfiguration = [NSURLSessionConfiguration backgroundSessionConfigurationWithIdentifier:[NSString stringWithFormat:@"%@.download.background", NSBundle.mainBundle.bundleIdentifier]];
sessionConfiguration.HTTPMaximumConnectionsPerHost = 5;
sessionConfiguration.HTTPMaximumConnectionsPerHost = MAX(FlutterDownloaderPlugin.maximumConcurrentTask, 1);
_session = [NSURLSession sessionWithConfiguration:sessionConfiguration delegate:self delegateQueue:nil];

_downloadInfo = [NSMutableDictionary new];
Expand Down

0 comments on commit f8fa727

Please sign in to comment.