Skip to content
This repository has been archived by the owner on Jun 3, 2021. It is now read-only.

Commit

Permalink
[Android]create a new module (deviceInfo) to enable full screen heig…
Browse files Browse the repository at this point in the history
…ht (#2578)
  • Loading branch information
katherine95s authored and YorkShen committed Jun 19, 2019
1 parent 2a7f46f commit 10a0bc5
Show file tree
Hide file tree
Showing 5 changed files with 95 additions and 2 deletions.
2 changes: 2 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/WXSDKEngine.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,7 @@
import com.taobao.weex.ui.component.list.template.WXRecyclerTemplateList;
import com.taobao.weex.ui.component.richtext.WXRichText;
import com.taobao.weex.ui.config.AutoScanConfigRegister;
import com.taobao.weex.ui.module.WXDeviceInfoModule;
import com.taobao.weex.ui.module.ConsoleLogModule;
import com.taobao.weex.ui.module.WXLocaleModule;
import com.taobao.weex.ui.module.WXMetaModule;
Expand Down Expand Up @@ -374,6 +375,7 @@ private static void register() {
registerModule("meta", WXMetaModule.class);
registerModule("webSocket", WebSocketModule.class);
registerModule("locale", WXLocaleModule.class);
registerModule("deviceInfo", WXDeviceInfoModule.class);
registerModule("sdk-console-log", ConsoleLogModule.class);
} catch (WXException e) {
WXLogUtils.e("[WXSDKEngine] register:", e);
Expand Down
7 changes: 7 additions & 0 deletions android/sdk/src/main/java/com/taobao/weex/WXSDKInstance.java
Original file line number Diff line number Diff line change
Expand Up @@ -142,6 +142,7 @@ public class WXSDKInstance implements IWXActivityStateListener,View.OnLayoutChan
private boolean mNeedReLoad = false;
private boolean mUseScroller = false;
private int mInstanceViewPortWidth = 750;
private boolean enableFullScreenHeight = false;
private WXInstanceApm mApmForInstance;
private @NonNull
FlatGUIContext mFlatGUIContext =new FlatGUIContext();
Expand Down Expand Up @@ -418,6 +419,12 @@ public void setNeedLoad(boolean load) {
mNeedReLoad = load;
}

@RestrictTo(Scope.LIBRARY)
public void setEnableFullScreenHeight(boolean fullScreenHeight){enableFullScreenHeight = fullScreenHeight;}

@RestrictTo(Scope.LIBRARY)
public boolean isFullScreenHeightEnabled(){return enableFullScreenHeight;}

public boolean isUseScroller() {
return mUseScroller;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -244,7 +244,7 @@ public void triggerEvent(String type, Map<String, Object> args) {
*/
@Override
protected MeasureOutput measure(int width, int height) {
int screenH = WXViewUtils.getScreenHeight(WXEnvironment.sApplication);
int screenH = WXViewUtils.getScreenHeight(getInstanceId());
int weexH = WXViewUtils.getWeexHeight(getInstanceId());
int outHeight = height > (weexH >= screenH ? screenH : weexH) ? weexH - getAbsoluteY() : height;
return super.measure(width, outHeight);
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/**
* Licensed to the Apache Software Foundation (ASF) under one
* or more contributor license agreements. See the NOTICE file
* distributed with this work for additional information
* regarding copyright ownership. The ASF licenses this file
* to you under the Apache License, Version 2.0 (the
* "License"); you may not use this file except in compliance
* with the License. You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing,
* software distributed under the License is distributed on an
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
* KIND, either express or implied. See the License for the
* specific language governing permissions and limitations
* under the License.
*/
package com.taobao.weex.ui.module;

import com.alibaba.fastjson.JSONObject;
import com.taobao.weex.WXEnvironment;
import com.taobao.weex.annotation.JSMethod;
import com.taobao.weex.bridge.JSCallback;
import com.taobao.weex.common.WXModule;
import com.taobao.weex.utils.WXLogUtils;
import com.taobao.weex.utils.WXViewUtils;

import java.util.HashMap;

public class WXDeviceInfoModule extends WXModule {

@JSMethod(uiThread = false)
public void enableFullScreenHeight(final JSCallback callback,JSONObject extend){
if(mWXSDKInstance != null) {
mWXSDKInstance.setEnableFullScreenHeight(true);
if(callback != null) {
long fullScreenHeight = WXViewUtils.getScreenHeight(mWXSDKInstance.getInstanceId());
HashMap<String, String> ret = new HashMap();
ret.put("fullScreenHeight", String.valueOf(fullScreenHeight));
callback.invoke(ret);
}

}

}

}
38 changes: 37 additions & 1 deletion android/sdk/src/main/java/com/taobao/weex/utils/WXViewUtils.java
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@
import android.graphics.Canvas;
import android.graphics.Path;
import android.graphics.PixelFormat;
import android.graphics.Point;
import android.graphics.RectF;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.LayerDrawable;
Expand All @@ -36,6 +37,8 @@
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.view.WindowManager;

import com.taobao.weex.WXEnvironment;
import com.taobao.weex.WXSDKInstance;
import com.taobao.weex.WXSDKManager;
Expand Down Expand Up @@ -204,10 +207,42 @@ public static int getScreenHeight() {
return getScreenHeight(WXEnvironment.sApplication);
}


public static int getScreenHeight(String instanceId){
WXSDKInstance instance = WXSDKManager.getInstance().getSDKInstance(instanceId);
return instance.isFullScreenHeightEnabled()?getFullScreenHeight(WXEnvironment.sApplication):getScreenHeight(WXEnvironment.sApplication);
}

//get screen height with status bar on full screen
public static int getFullScreenHeight(Context cxt) {
if(cxt!=null){
WindowManager wm;
Resources res = cxt.getResources();
if(Build.VERSION.SDK_INT >= 17 && (wm = (WindowManager)cxt.getSystemService(Context.WINDOW_SERVICE)) != null
&& wm.getDefaultDisplay() != null){
Point size = new Point();
wm.getDefaultDisplay().getRealSize(size);
mScreenHeight = size.y;
}
else {
mScreenHeight = cxt.getResources().getDisplayMetrics().heightPixels;
}
if(WXEnvironment.SETTING_FORCE_VERTICAL_SCREEN){
mScreenWidth = res
.getDisplayMetrics()
.widthPixels;
mScreenHeight = mScreenHeight > mScreenWidth ? mScreenHeight : mScreenWidth;
}
} else if (WXEnvironment.isApkDebugable()){
throw new WXRuntimeException("Error Context is null When getScreenHeight");
}
return mScreenHeight;
}
// get screen height without status bar
public static int getScreenHeight(Context cxt) {
if(cxt!=null){
Resources res = cxt.getResources();
mScreenHeight =cxt.getResources().getDisplayMetrics().heightPixels;
mScreenHeight = res.getDisplayMetrics().heightPixels;
if(WXEnvironment.SETTING_FORCE_VERTICAL_SCREEN){
mScreenWidth = res
.getDisplayMetrics()
Expand All @@ -220,6 +255,7 @@ public static int getScreenHeight(Context cxt) {
return mScreenHeight;
}


/**
* Convert distance from JS,CSS to native. As the JS considers the width of the screen is 750px.
* There must be a transform when accessing distance from JS,CSS and use it.
Expand Down

0 comments on commit 10a0bc5

Please sign in to comment.