Skip to content
This repository was archived by the owner on Jun 24, 2024. It is now read-only.
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions android/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ android {

defaultConfig {
minSdkVersion 16
ndk{abiFilters "armeabi-v7a"}
}
lintOptions {
disable 'InvalidPackage'
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,30 @@
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsListener;
import com.tencent.smtt.sdk.ValueCallback;
import com.tencent.smtt.sdk.WebView;

import java.util.HashMap;

import io.flutter.embedding.engine.plugins.activity.ActivityAware;
import io.flutter.embedding.engine.plugins.activity.ActivityPluginBinding;
import io.flutter.plugin.common.MethodCall;
import io.flutter.plugin.common.MethodChannel;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.embedding.engine.plugins.FlutterPlugin;


/**
* FlutterX5Plugin
*/
public class FlutterFileReaderPlugin implements MethodChannel.MethodCallHandler {
public class FlutterFileReaderPlugin implements FlutterPlugin, ActivityAware, MethodChannel.MethodCallHandler {

private int x5LoadStatus = -1; // -1 未加载状态 5 成功 10 失败

private static String TAG = "FileReader";

public static final String channelName = "wv.io/FileReader";
private FlutterPluginBinding flutterPluginBinding;

private Context ctx;
private Context act;
private Context activityContext;
private MethodChannel methodChannel;
private Handler mainHandler = new Handler(Looper.getMainLooper(), new Handler.Callback() {
@Override
Expand All @@ -43,24 +49,16 @@ public boolean handleMessage(Message msg) {
});


private FlutterFileReaderPlugin(Registrar registrar) {
ctx = registrar.context();
act = registrar.activity();
methodChannel = new MethodChannel(registrar.messenger(), channelName);
methodChannel.setMethodCallHandler(this);
initX5(registrar.context());
netBroadcastRegister(registrar.context());

public FlutterFileReaderPlugin(){
Log.d(TAG,"init");
}


/**
* Plugin registration.
*/
public static void registerWith(Registrar registrar) {
FlutterFileReaderPlugin plugin = new FlutterFileReaderPlugin(registrar);
registrar.platformViewRegistry().registerViewFactory("FileReader", new X5FileReaderFactory(registrar.messenger(), registrar.activity(),plugin));

private void loadParameters(FlutterPluginBinding binding) {
ctx = binding.getApplicationContext();
methodChannel = new MethodChannel(binding.getBinaryMessenger(), channelName);
methodChannel.setMethodCallHandler(this);
initX5(binding.getApplicationContext());
netBroadcastRegister(binding.getApplicationContext());
}


Expand All @@ -72,20 +70,18 @@ public void netBroadcastRegister(final Context context) {
@Override
public void onChangeListener(int status) {
if (!QbSdk.canLoadX5(context)) {
// Log.d("FileReader", "网络变化->加载x5内核");
// Log.d(TAG, "网络变化->加载x5内核");
initX5(context);
}
}
});
//注册广播接收
context.registerReceiver(netBroadcastReceiver, filter);


}


public void initX5(final Context context) {
Log.d("FileReader","初始化X5->"+QbSdk.canLoadX5(context));
Log.d(TAG,"初始化X5->"+QbSdk.canLoadX5(context));
if(!QbSdk.canLoadX5(context)){
//重要
QbSdk.reset(context);
Expand All @@ -101,7 +97,7 @@ public void initX5(final Context context) {
QbSdk.setTbsListener(new TbsListener() {
@Override
public void onDownloadFinish(int i) {
Log.d("FileReader", "TBS下载完成");
Log.d(TAG, "TBS下载完成");
}

@Override
Expand All @@ -111,14 +107,14 @@ public void onInstallFinish(int i) {

@Override
public void onDownloadProgress(int i) {
Log.d("FileReader", "TBS下载进度:" + i);
Log.d(TAG, "TBS下载进度:" + i);
}
});

QbSdk.initX5Environment(context, new QbSdk.PreInitCallback() {
@Override
public void onCoreInitFinished() {
Log.d("FileReader", "TBS内核初始化结束");
Log.d(TAG, "TBS内核初始化结束");
}

@Override
Expand All @@ -128,14 +124,12 @@ public void onViewInitFinished(boolean b) {
} else {
x5LoadStatus = 10;
}
// Log.d("FileReader", "view初始化完成状态:" + b);
Log.d("FileReader", "TBS内核状态:" + QbSdk.canLoadX5(context));
// Log.d(TAG, "view初始化完成状态:" + b);
Log.d(TAG, "TBS内核状态:" + QbSdk.canLoadX5(context));
onX5LoadComplete();
}
});



}

@Override
Expand All @@ -154,11 +148,10 @@ public boolean openFileByMiniQb(String filePath){
params.put("style", "1");
params.put("local", "false");


QbSdk.openFileReader(act, filePath, params, new ValueCallback<String>() {
QbSdk.openFileReader(activityContext, filePath, params, new ValueCallback<String>() {
@Override
public void onReceiveValue(String s) {
Log.d("FileReader","openFileReader->"+s);
Log.d(TAG,"openFileReader->"+s);
}
});

Expand All @@ -173,10 +166,46 @@ private void onX5LoadComplete() {

int isLoadX5() {
if(QbSdk.canLoadX5(ctx)){
// Log.d("FileReader","x5 is Load");
// Log.d(TAG,"x5 is Load");
x5LoadStatus = 5;
}
return x5LoadStatus;
}

@Override
public void onAttachedToEngine(FlutterPluginBinding binding) {
this.flutterPluginBinding = binding;
//
Log.d(TAG, "onAttachedToEngine");
}

@Override
public void onDetachedFromEngine(FlutterPluginBinding binding) {
Log.d(TAG, "onDetachedFromEngine");
}

@Override
public void onAttachedToActivity(ActivityPluginBinding binding) {
this.activityContext = binding.getActivity();
this.loadParameters(this.flutterPluginBinding);
this.flutterPluginBinding.getPlatformViewRegistry().registerViewFactory("FileReader", new X5FileReaderFactory(this.flutterPluginBinding.getBinaryMessenger(), this.activityContext, this));
Log.d(TAG, "onAttachedToActivity");
}

@Override
public void onDetachedFromActivityForConfigChanges() {
Log.d(TAG, "onDetachedFromActivityForConfigChanges");
}

@Override
public void onReattachedToActivityForConfigChanges(ActivityPluginBinding binding) {
Log.d(TAG, "onReattachedToActivityForConfigChanges");
}

@Override
public void onDetachedFromActivity() {
Log.d(TAG, "onDetachedFromActivity");
activityContext = null;
}
}