Skip to content

Commit

Permalink
Merge pull request #15 from kadbbz/dev-1.14.2
Browse files Browse the repository at this point in the history
Dev 1.14.3
  • Loading branch information
kadbbz authored Jan 23, 2024
2 parents 7b19351 + f26e3fb commit cb2524c
Show file tree
Hide file tree
Showing 14 changed files with 180 additions and 179 deletions.
2 changes: 1 addition & 1 deletion app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ android {
//noinspection ExpiredTargetSdkVersion
targetSdk 29 // 超过29后,文件权限要大改
versionCode 4
versionName '1.14.2-release'
versionName '1.14.3-release'

manifestPlaceholders = [
JPUSH_PKGNAME : applicationId,
Expand Down
2 changes: 1 addition & 1 deletion app/release/output-metadata.json
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
"filters": [],
"attributes": [],
"versionCode": 4,
"versionName": "1.14.2-release",
"versionName": "1.14.3-release",
"outputFile": "app-release.apk"
}
],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ public HACCrashHandler(Application context) {
@Override
public void uncaughtException(@NonNull Thread thread, @NonNull Throwable throwable) {

XLog.e("出现未捕获的异常:" + throwable.getMessage(), throwable);
XLog.e("出现未捕获的异常:" + throwable.getMessage()+" \r\n%s", throwable);

String message = "应用出现严重错误,请拍摄本界面或截屏后,与技术支持人员联系。";
message += "\r\n\n";
Expand Down
147 changes: 72 additions & 75 deletions app/src/main/java/com/huozige/lab/container/MainActivity.java
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,6 @@
import android.view.Menu;
import android.view.MenuItem;

import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;

import com.elvishew.xlog.XLog;
Expand All @@ -23,7 +21,6 @@
import com.huozige.lab.container.proxy.AbstractProxy;
import com.huozige.lab.container.proxy.ProxyRegister;
import com.huozige.lab.container.utilities.ConfigManager;
import com.huozige.lab.container.utilities.LifecycleUtility;
import com.huozige.lab.container.utilities.PermissionsUtility;
import com.huozige.lab.container.webview.HACDownloadListener;
import com.huozige.lab.container.webview.HACWebChromeClient;
Expand All @@ -36,6 +33,9 @@
*/
public class MainActivity extends BaseActivity {

public static final String INTENT_NAVIGATE_TO = "com.huozige.lab.container.navigate";
public static final String EXTRA_URL = "url";

// 构造Web平台相关的策略,用于组合
AbstractWebInterop _webInterop = new HZGWebInterop();
AbstractStaticFilesCacheFilter _cacheFilter = new HZGCacheFilter();
Expand All @@ -48,8 +48,6 @@ public class MainActivity extends BaseActivity {

HACDownloadListener _downloadListener; // 下载分流处理器

ActivityResultLauncher<Intent> _arc4QuickConfig; // 用来弹出配置页面。

static final int MENU_ID_HOME = 0;
static final int MENU_ID_REFRESH = 1;
static final int MENU_ID_SETTINGS = 2;
Expand All @@ -62,67 +60,74 @@ protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

// 0. 申请权限
PermissionsUtility.asyncRequirePermissions(this,new String[]{
PermissionsUtility.asyncRequirePermissions(this, new String[]{
Permission.WRITE_EXTERNAL_STORAGE
},()->XLog.v("日志记录的权限申请成功。"));
}, () -> XLog.v("日志记录的权限申请成功。"));

// 1. 设置页面标题
setTitle(getString(R.string.app_name));

// 2. 创建浏览器
try{
// 2. 检查是否已经完成配置
if (!ConfigManager.getInstance().isAppReady()) {
XLog.v("APP尚未完成配置,即将跳转到初始化界面");
// 跳转到设置页面
startActivity(new Intent(this, QuickConfigActivity.class));
} else {

try {
// 3. 创建浏览器并执行初始化
_webView = new HACWebView(this);

// 4. 创建并注册WebViewClient,处理页面事件
_webViewClient = new HACWebViewClient(this);
_webViewClient.setStaticFilesCacheFilter(_cacheFilter);
_webView.setWebViewClient(_webViewClient);

// 5. 创建并注册WebChromeClient,处理浏览器事件
_webChromeClient = new HACWebChromeClient(this);
_webView.setWebChromeClient(_webChromeClient);

// 6. 注册下载分流处理器
_downloadListener = new HACDownloadListener(_webView);
_webView.setDownloadListener(_downloadListener);

// 7. 将准备就绪的浏览器加载到页面
setContentView(_webView);

// 8. 使用WebView初始化WebInterop,处理和HTML的交互
_webInterop.setWebView(_webView);

// 9. 创建和初始化JS代理
for (AbstractProxy br : ProxyRegister.getInstance().getAllProxies()
) {
br.setInterop(_webInterop); // WebInterop
_webView.addJavascriptInterface(br, br.getName()); // 注册到浏览器
br.onActivityCreated(MainActivity.this); // 初始化以当前Activity为上下文的启动器,这一操作仅允许在当前阶段调用,否则会出错
}

_webView = new HACWebView(this);
// 10. 初始化ChromeClient的启动器
_webChromeClient.registryLaunchersOnCreated(); // ChromeClient的初始化

// 4. 创建并注册WebViewClient,处理页面事件
_webViewClient = new HACWebViewClient(this);
_webViewClient.setStaticFilesCacheFilter(_cacheFilter);
_webView.setWebViewClient(_webViewClient);
XLog.v("WebView初始化完成");

// 5. 创建并注册WebChromeClient,处理浏览器事件
_webChromeClient = new HACWebChromeClient(this);
_webView.setWebChromeClient(_webChromeClient);
// 11. 加载页面
_webView.navigateToDefaultPage();
} catch (Exception ex) {

// 6. 注册下载分流处理器
_downloadListener = new HACDownloadListener(_webView);
_webView.setDownloadListener(_downloadListener);
// 统一处理各种异常
XLog.e("WebView组件初始化失败。\r\n%s", ex);

// 7. 将准备就绪的浏览器加载到页面
setContentView(_webView);
String message = "应用初始化失败,这通常是操作系统和运行环境不兼容导致的,请按照下方提示操作。如果遇到困难,可拍摄本界面或截屏后,与技术支持人员联系。";
message += "\r\n\n";
message += ex.getMessage();

// 8. 使用WebView初始化WebInterop,处理和HTML的交互
_webInterop.setWebView(_webView);
Intent intent = new Intent(this, ShowErrorActivity.class);
intent.putExtra(ShowErrorActivity.EXTRA_KEY_MESSAGE, message);

// 9. 创建和初始化JS代理
for (AbstractProxy br : ProxyRegister.getInstance().getAllProxies()
) {
br.setConfigManager(ConfigManager.getInstance()); // 配置接口
br.setInterop(_webInterop); // WebInterop
_webView.addJavascriptInterface(br,br.getName()); // 注册到浏览器
br.onActivityCreated(MainActivity.this); // 初始化以当前Activity为上下文的启动器,这一操作仅允许在当前阶段调用,否则会出错
this.startActivity(intent);
}

// 10. 初始化启动器
_webChromeClient.registryLaunchersOnCreated(); // ChromeClient的初始化
_arc4QuickConfig = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> LifecycleUtility.restart(this)); // 打开设置页面,返回后重启应用

// 11. 加载页面
_webView.refreshWebView();
}catch (Exception ex){

XLog.e("WebView组件初始化失败",ex);

String message = "应用初始化失败,这通常是操作系统和运行环境不兼容导致的,请按照下方提示操作。如果遇到困难,可拍摄本界面或截屏后,与技术支持人员联系。";
message+="\r\n\n";
message+=ex.getMessage();

Intent intent = new Intent(this, ShowErrorActivity.class);
intent.putExtra(ShowErrorActivity.EXTRA_KEY_MESSAGE, message);

this.startActivity(intent);
}


}

/**
Expand All @@ -132,7 +137,7 @@ protected void onCreate(Bundle savedInstanceState) {
public void onDestroy() {

// 销毁浏览器
if(_webView!=null){
if (_webView != null) {
_webView.removeAllViews();
_webView.destroy();
}
Expand All @@ -147,34 +152,25 @@ public void onDestroy() {
public void onResume() {
super.onResume();

// 如果没有设置入口,视同”未配置“
if (ConfigManager.getInstance().getEntry().length() == 0) {
// 跳转到设置页面
_arc4QuickConfig.launch(new Intent(this, QuickConfigActivity.class)); // 弹出设置页面
}else{
// 判断是否需要自动跳转到特定页面
Intent intent = getIntent();

// 判断是否需要自动跳转到特定页面
Intent intent = getIntent();
// Action:com.huozige.lab.container.navigate
// Extra:url
if (intent != null && intent.getAction() != null && intent.getAction().equalsIgnoreCase(INTENT_NAVIGATE_TO)) {
String url = intent.getStringExtra(EXTRA_URL);

// Action:com.huozige.lab.container.navigate
// Extra:url
if(intent!=null && intent.getAction()!=null && intent.getAction().equalsIgnoreCase("com.huozige.lab.container.navigate")){
String url = intent.getStringExtra("url");
if (url != null) {
Uri u = Uri.parse(url);

if(url!=null){
Uri u = Uri.parse(url);
if (u != null) {

if(u!=null){

XLog.v("收到带跳转的Intent请求,URL:"+url);
this._webView.loadUrl(url);
}
XLog.v("收到带跳转的Intent请求,URL:" + url);
this._webView.loadUrl(url);
}
}

}


}

/**
Expand All @@ -199,13 +195,14 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {

/**
* 处理配置变更的事件,这里仅做日志记录
*
* @param newConfig The new device configuration.
*/
@Override
public void onConfigurationChanged(@NonNull Configuration newConfig) {
super.onConfigurationChanged(newConfig);

XLog.v("HAC_BaseActivity","Config changed: "+ newConfig);
XLog.v("HAC_BaseActivity", "Config changed: " + newConfig);

}

Expand Down Expand Up @@ -249,14 +246,14 @@ public boolean onOptionsItemSelected(MenuItem item) {
switch (item.getItemId()) {
case MENU_ID_HOME:
XLog.v("点击菜单【首页】");
_webView.refreshWebView(); // 页面初始化
_webView.navigateToDefaultPage(); // 页面初始化
break;
case MENU_ID_REFRESH:
XLog.v("点击菜单【刷新】");
_webView.reload(); // 仅刷新
break;
case MENU_ID_SETTINGS:
_arc4QuickConfig.launch(new Intent(this, SettingActivity.class)); // 弹出设置页面
startActivity(new Intent(this, SettingActivity.class));
break;
case MENU_ID_HELP:
XLog.v("点击菜单【帮助】");
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,6 @@

import android.content.Intent;
import android.os.Bundle;

import com.elvishew.xlog.XLog;

import android.view.View;
import android.widget.ImageButton;
import android.widget.TextView;
Expand All @@ -14,8 +11,10 @@
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.appcompat.app.AlertDialog;

import com.elvishew.xlog.XLog;
import com.hjq.permissions.Permission;
import com.huozige.lab.container.utilities.ConfigManager;
import com.huozige.lab.container.utilities.LifecycleUtility;
import com.huozige.lab.container.utilities.PermissionsUtility;
import com.king.zxing.CameraScan;
import com.king.zxing.CaptureActivity;
Expand All @@ -25,19 +24,20 @@
*/
public class QuickConfigActivity extends BaseActivity {


ActivityResultLauncher<Intent> _arc4TCA; // 用来弹出配置页面。

// 创建到ZXingLite的调用器
ActivityResultLauncher<Intent> _arcZxingLite = this.registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> {

XLog.v("扫描到配置码");

// 按照ZXingLite文档获取和解析扫码结果数据,如果出错或者取消,默认为空字符串,同官方APP
Intent data = result.getData();

if (null != data) {
String json = CameraScan.parseScanResult(data);

XLog.v("Load config from QRCode : " + json);
XLog.v("配置码的内容: " + json);

AlertDialog.Builder ab = new AlertDialog.Builder(QuickConfigActivity.this);
ab.setPositiveButton(QuickConfigActivity.this.getString(R.string.ui_button_ok), (dialogInterface, i) -> {
Expand All @@ -47,16 +47,16 @@ public class QuickConfigActivity extends BaseActivity {

if (isOk) {

XLog.v("QRCode config applied.");
XLog.v("配置码格式正确,已完成初始化");

// 提示正确的信息
Toast.makeText(QuickConfigActivity.this, getString(R.string.ui_message_quick_config_done), Toast.LENGTH_LONG).show();

// 重启生效
restart();
LifecycleUtility.restart(QuickConfigActivity.this);
} else {

XLog.v("QRCode config is broken.");
XLog.e("配置码不正确或版本过旧,内容为:" + json);

// 仅提示错误信息
Toast.makeText(QuickConfigActivity.this, getString(R.string.ui_message_quick_config_broken), Toast.LENGTH_LONG).show();
Expand All @@ -69,6 +69,8 @@ public class QuickConfigActivity extends BaseActivity {
ab.setMessage(R.string.ui_message_quick_config_confirm);
ab.setTitle(R.string.ui_menu_settings);
ab.show();
}else{
XLog.e("配置码格式不正确,无法读取有效内容");
}

});
Expand All @@ -88,7 +90,9 @@ protected void onCreate(Bundle savedInstanceState) {
TextView lblHelp = findViewById(R.id.textTCHelp);
lblHelp.setOnClickListener(gotoTextConfig);

_arc4TCA = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> restart()); // 打开设置页面,返回后刷新浏览器
_arc4TCA = registerForActivityResult(new ActivityResultContracts.StartActivityForResult(), result -> LifecycleUtility.restart(QuickConfigActivity.this)); // 打开设置页面,返回后刷新浏览器

XLog.v("快速配置界面准备就绪");
}

View.OnClickListener gotoTextConfig = view -> _arc4TCA.launch(new Intent(this, TextConfigActivity.class));
Expand All @@ -105,11 +109,4 @@ protected void onCreate(Bundle savedInstanceState) {
});
};

void restart() {
Intent intentR = getPackageManager().getLaunchIntentForPackage(getBaseContext().getPackageName());
if (intentR != null) {
intentR.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP);
startActivity(intentR);
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -73,15 +73,7 @@ public AbstractWebInterop getInterop() throws IllegalStateException {
return interop;
}

public ConfigManager getConfigManager() throws IllegalStateException {

if (null == configManager) {
throw new IllegalStateException ("ConfigManager has not be initialized.");
}
return configManager;
}

public void setConfigManager(ConfigManager configManager) {
this.configManager = configManager;
public ConfigManager getConfigManager() {
return ConfigManager.getInstance();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -124,7 +124,7 @@ public void onActivityCreated(AppCompatActivity activity) {
getInterop().writeLogIntoConsole("Error occurred during data exchanging: " + error);
}

XLog.v("code -> " + code + " payload - > " + payload + " err -> " + error);
XLog.v("蓝牙操作的返回信息:code -> " + code + " payload - > " + payload + " err -> " + error);

if (cellError != null && !cellError.isEmpty()) {
getInterop().setInputValue(cellError, error);
Expand Down
Loading

0 comments on commit cb2524c

Please sign in to comment.