mirror of
https://github.com/openharmony/ability_ability_runtime.git
synced 2026-08-24 22:21:36 -04:00
!8069 OnBackPressed只在TargetAPI>=12时默认返回true
Merge pull request !8069 from zhangyuhang/back
This commit is contained in:
@@ -25,9 +25,6 @@ class Ability {
|
||||
onWindowStageDestroy() { }
|
||||
onForeground(want) { }
|
||||
onBackground() { }
|
||||
onBackPressed() {
|
||||
return true;
|
||||
}
|
||||
onPrepareToTerminate() {
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -1547,7 +1547,7 @@ void Ability::OnBackground()
|
||||
bool Ability::OnBackPress()
|
||||
{
|
||||
TAG_LOGD(AAFwkTag::ABILITY, "call");
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
|
||||
bool Ability::OnPrepareTerminate()
|
||||
|
||||
@@ -548,10 +548,10 @@ bool JsAbility::OnBackPress()
|
||||
HandleScope handleScope(jsRuntime_);
|
||||
auto env = jsRuntime_.GetNapiEnv();
|
||||
napi_value jsValue = CallObjectMethod("onBackPressed", nullptr, 0, true);
|
||||
bool ret = true;
|
||||
bool ret = false;
|
||||
if (!ConvertFromJsValue(env, jsValue, ret)) {
|
||||
TAG_LOGW(AAFwkTag::ABILITY, "Get js value failed");
|
||||
return true;
|
||||
return false;
|
||||
}
|
||||
TAG_LOGD(AAFwkTag::ABILITY, "end, ret = %{public}d", ret);
|
||||
return ret;
|
||||
|
||||
@@ -59,6 +59,8 @@ const std::string METHOD_NAME = "WindowScene::GoForeground";
|
||||
#endif
|
||||
// Numerical base (radix) that determines the valid characters and their interpretation.
|
||||
const int32_t BASE_DISPLAY_ID_NUM (10);
|
||||
constexpr const int32_t API12 = 12;
|
||||
constexpr const int32_t API_VERSION_MOD = 100;
|
||||
|
||||
napi_value PromiseCallback(napi_env env, napi_callback_info info)
|
||||
{
|
||||
@@ -619,11 +621,16 @@ bool JsUIAbility::OnBackPress()
|
||||
UIAbility::OnBackPress();
|
||||
HandleScope handleScope(jsRuntime_);
|
||||
auto env = jsRuntime_.GetNapiEnv();
|
||||
napi_value jsValue = CallObjectMethod("onBackPressed", nullptr, 0, true);
|
||||
bool ret = true;
|
||||
napi_value jsValue = CallObjectMethod("onBackPressed", nullptr, 0, true, false);
|
||||
bool defaultRet = BackPressDefaultValue();
|
||||
if (jsValue == nullptr) {
|
||||
TAG_LOGD(AAFwkTag::UIABILITY, "jsValue is nullptr, return defaultRet %{public}d.", defaultRet);
|
||||
return defaultRet;
|
||||
}
|
||||
bool ret = defaultRet;
|
||||
if (!ConvertFromJsValue(env, jsValue, ret)) {
|
||||
TAG_LOGE(AAFwkTag::UIABILITY, "Get js value failed.");
|
||||
return true;
|
||||
return defaultRet;
|
||||
}
|
||||
TAG_LOGD(AAFwkTag::UIABILITY, "End ret is %{public}d.", ret);
|
||||
return ret;
|
||||
@@ -1216,7 +1223,8 @@ sptr<IRemoteObject> JsUIAbility::CallRequest()
|
||||
return remoteCallee_;
|
||||
}
|
||||
|
||||
napi_value JsUIAbility::CallObjectMethod(const char *name, napi_value const *argv, size_t argc, bool withResult)
|
||||
napi_value JsUIAbility::CallObjectMethod(const char *name, napi_value const *argv, size_t argc, bool withResult,
|
||||
bool showMethodNotFoundLog)
|
||||
{
|
||||
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
|
||||
TAG_LOGD(AAFwkTag::UIABILITY, "Lifecycle: the begin of %{public}s", name);
|
||||
@@ -1237,7 +1245,9 @@ napi_value JsUIAbility::CallObjectMethod(const char *name, napi_value const *arg
|
||||
napi_value methodOnCreate = nullptr;
|
||||
napi_get_named_property(env, obj, name, &methodOnCreate);
|
||||
if (methodOnCreate == nullptr) {
|
||||
TAG_LOGE(AAFwkTag::UIABILITY, "Failed to get '%{public}s' from Ability object.", name);
|
||||
if (showMethodNotFoundLog) {
|
||||
TAG_LOGE(AAFwkTag::UIABILITY, "Failed to get '%{public}s' from Ability object.", name);
|
||||
}
|
||||
return nullptr;
|
||||
}
|
||||
TryCatch tryCatch(env);
|
||||
@@ -1474,5 +1484,21 @@ void JsUIAbility::UpdateJsWindowStage(napi_value windowStage)
|
||||
TAG_LOGD(AAFwkTag::UIABILITY, "Set context windowStage object.");
|
||||
napi_set_named_property(env, contextObj, "windowStage", windowStage);
|
||||
}
|
||||
|
||||
bool JsUIAbility::CheckSatisfyTargetAPIVersion(int32_t version)
|
||||
{
|
||||
auto applicationInfo = GetApplicationInfo();
|
||||
if (!applicationInfo) {
|
||||
TAG_LOGE(AAFwkTag::UIABILITY, "CheckTargetAPIVersion applicationInfo is nullptr.");
|
||||
return false;
|
||||
}
|
||||
TAG_LOGD(AAFwkTag::UIABILITY, "TargetAPIVersion: %{public}d.", applicationInfo->apiTargetVersion);
|
||||
return applicationInfo->apiTargetVersion % API_VERSION_MOD >= version;
|
||||
}
|
||||
|
||||
bool JsUIAbility::BackPressDefaultValue()
|
||||
{
|
||||
return CheckSatisfyTargetAPIVersion(API12) ? true : false;
|
||||
}
|
||||
} // namespace AbilityRuntime
|
||||
} // namespace OHOS
|
||||
|
||||
@@ -293,7 +293,7 @@ private:
|
||||
|
||||
private:
|
||||
napi_value CallObjectMethod(const char *name, napi_value const *argv = nullptr, size_t argc = 0,
|
||||
bool withResult = false);
|
||||
bool withResult = false, bool showMethodNotFoundLog = true);
|
||||
bool CheckPromise(napi_value result);
|
||||
bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo);
|
||||
bool CallPromise(napi_value result, int32_t &onContinueRes);
|
||||
@@ -308,6 +308,8 @@ private:
|
||||
void AddLifecycleEventBeforeJSCall(FreezeUtil::TimeoutState state, const std::string &methodName) const;
|
||||
void AddLifecycleEventAfterJSCall(FreezeUtil::TimeoutState state, const std::string &methodName) const;
|
||||
void CreateJSContext(napi_env env, napi_value &contextObj, int32_t screenMode);
|
||||
bool CheckSatisfyTargetAPIVersion(int32_t targetAPIVersion);
|
||||
bool BackPressDefaultValue();
|
||||
|
||||
JsRuntime &jsRuntime_;
|
||||
std::shared_ptr<NativeReference> shellContextRef_;
|
||||
|
||||
Reference in New Issue
Block a user