Commit 2f18bc70 by BJQ

3rd commit

parent 0a1a1afe
......@@ -4,19 +4,27 @@ import org.apache.cordova.CordovaBridge;
import org.apache.cordova.ICordovaCookieManager;
import org.apache.cordova.engine.SystemWebViewEngine;
import android.Manifest;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.SharedPreferences;
import android.content.pm.PackageInfo;
import android.annotation.TargetApi;
import android.content.pm.PackageManager;
import android.graphics.Bitmap;
import android.net.Uri;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.support.v4.app.ActivityCompat;
import android.support.v4.content.ContextCompat;
import android.util.Log;
import android.view.View;
import android.webkit.ValueCallback;
import android.widget.SeekBar;
//import android.webkit.WebSettings;
//import android.webkit.WebView;
......@@ -26,6 +34,9 @@ import android.webkit.ValueCallback;
import com.tencent.smtt.export.external.interfaces.WebResourceRequest;
import com.tencent.smtt.export.external.interfaces.WebResourceResponse;
import com.tencent.smtt.sdk.QbSdk;
import com.tencent.smtt.sdk.TbsDownloader;
import com.tencent.smtt.sdk.TbsListener;
import com.tencent.smtt.sdk.WebSettings;
import com.tencent.smtt.sdk.WebView;
......@@ -52,11 +63,189 @@ public class TestWebViewEngine extends TestSystemWebViewEngine {
private static final String LAST_BINARY_VERSION_NAME = "lastBinaryVersionName";
/*
my codes under this line
*/
private Context mContext;
// runTime permission request code , no user here
private int PERMISSION_REQUEST = 100;
private boolean threadFlag = true;
// a dialog to show message include status,download progress and finally result
AlertDialog downloadDialog, finalDownload;
SeekBar seekBar;
// init views
private void initViews() {
seekBar = new SeekBar(mContext);
seekBar.setMax(100);
seekBar.setEnabled(false);
downloadDialog = new AlertDialog.Builder(mContext)
.setTitle("必要组件正在下载")
.setCancelable(false)
.setView(seekBar)
.create();
}
private void showDownloadDialog() {
this.downloadDialog.show();
}
private void dismissDownloadDialog() {
if (downloadDialog != null) {
downloadDialog.cancel();
downloadDialog.dismiss();
}
}
private void showFinalDialog(String msg) {
finalDownload = new AlertDialog.Builder(mContext)
.setTitle(msg)
.setCancelable(false)
.setPositiveButton("确定", new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finalDownload.dismiss();
restartActivity();
}
}).create();
finalDownload.show();
}
private void setDownloadProgress(int progress) {
if (seekBar != null) {
seekBar.setProgress(progress);
}
}
// 7.0+动态申请读写权限
private void permissionRequest() {
if (ContextCompat.checkSelfPermission(mContext, Manifest.permission.READ_EXTERNAL_STORAGE) != PackageManager.PERMISSION_GRANTED) {
ActivityCompat.requestPermissions((Activity) mContext, new String[]{Manifest.permission.READ_EXTERNAL_STORAGE}, PERMISSION_REQUEST);
// a thread waiting for check if we got the runTime permission
Thread waitThread = new Thread(new Runnable() {
@SuppressLint("WrongConstant")
@Override
public void run() {
while (threadFlag) {
// -1:no; 0:yes;
if (mContext.checkSelfPermission(Manifest.permission.READ_EXTERNAL_STORAGE) == 0) {
downloadTbs();
threadFlag = false; // 停止循环,待其自己回收
}
}
}
});
waitThread.start();
} else {
downloadTbs();
}
}
/*
when user download x5core or system has checked there is no need to download(TbsListener.onDownloadFinish() returns 110),
we should tell user to restart the app by using this function
*/
private void restartActivity() {
Log.e(TAG, "-------------------------restartActivity: ");
// mContext.startActivity(new Intent(mContext, MainActivity.class));
System.exit(0);
}
// function to set tbs download listener and start to download
private void downloadTbs() {
initViews();
showDownloadDialog();
QbSdk.setTbsListener(new TbsListener() {
@Override
public void onDownloadFinish(int i) {
Log.e(TAG, "-----------------------------onDownloadFinish: " + i);
dismissDownloadDialog();
if (i == 110) {
showFinalDialog("已检测到本地文件,请重启软件");
}
}
@Override
public void onInstallFinish(int i) {
Log.e(TAG, "----------------------------------onInstallFinish: " + i);
if (i == ErrorCode.DOWNLOAD_INSTALL_SUCCESS || i == ErrorCode.COPY_INSTALL_SUCCESS) {
// mContext.startActivity(new Intent(mContext, MainActivity.class));
// DialogUtils.dialogProgressDismiss();
// initQbs();
showFinalDialog("组件安装完成,请重启软件");
} else {
}
}
@Override
public void onDownloadProgress(int i) {
Log.e(TAG, "-------------------------------------onDownloadProgress: " + i);
setDownloadProgress(i);
}
});
TbsDownloader.startDownload(mContext, TbsDownloader.DOWNLOAD_OVERSEA_TBS);
}
private void initX5Webview() {
QbSdk.setDownloadWithoutWifi(true);
AlertDialog dialog = new AlertDialog.Builder(mContext)
.setMessage("初始化中...")
.setCancelable(false)
.create();
dialog.show();
QbSdk.initX5Environment(mContext, new QbSdk.PreInitCallback() {
@Override
public void onCoreInitFinished() {
}
@Override
public void onViewInitFinished(boolean b) {
dialog.dismiss();
if (b) {
Log.e(TAG, "-----------------------------------------onViewInitFinished: success");
} else {
Log.e(TAG, "-----------------------------------------onViewInitFinished: failure");
// 初始化x5Webview失败
boolean ifNeedDownload = TbsDownloader.needDownload(mContext, TbsDownloader.DOWNLOAD_OVERSEA_TBS);
Log.e(TAG, "----------------------------------------ifNeedDownload: " + ifNeedDownload);
permissionRequest();
}
}
});
}
/*
my codes above this line
*/
/**
* Used when created via reflection.
*/
public TestWebViewEngine(Context context, CordovaPreferences preferences) {
super(new TestSystemWebView(context), preferences);
this.mContext=context;
Log.e(TAG, "testSystem Web View Engine Starting Right Up 1...");
}
......@@ -67,6 +256,7 @@ public class TestWebViewEngine extends TestSystemWebViewEngine {
public TestWebViewEngine(TestSystemWebView webView, CordovaPreferences preferences) {
super(webView, preferences);
this.mContext = webView.getContext();
Log.e(TAG, "testSystem Web View Engine Starting Right Up 3...");
}
@Override
......@@ -76,6 +266,18 @@ public class TestWebViewEngine extends TestSystemWebViewEngine {
ConfigXmlParser parser = new ConfigXmlParser();
parser.parse(cordova.getActivity());
/*
my codes under this line
*/
initX5Webview();
/*
my codes above this line
*/
String hostname = preferences.getString("Hostname", "localhost");
scheme = preferences.getString("Scheme", "http");
CDV_LOCAL_SERVER = scheme + "://" + hostname;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment