背景
在项目中专门有一个意见反馈的activity
现在要求:
就是要统一样式起来:最终形成这样的效果
问题的难点就在于这个要在原有的代码基础上实现,最好不要简单的在xml上构建。( 原来只是有红色框内的东西),上图是已经实现功能的图样。其他的关于我们
等等的都是采用aar包下的某个对象静态方法直接实现的。为了代码统一和实现的效果,最好不要大改。
如何在对aar包改动最小和对原有的上层代码修改最少呢?
采用继承的方式是最好~
原有代码-仅供参考学习
意见反馈的activity:
@@ -1,126 +0,0 @@
package im.zego.gocall.personal;
// file_need_to_be_deleted_for_open_source
import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.Nullable;
/**
* 反馈页面
*/
public class FeedBackActivity extends BaseActivity {
private WebView mWebView;
private String feedbackUrl;
private String TAG = "FeedbackLog";
private ZegoExpressEngine mZegoExpressEngine;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ZegoEngineProfile profile = new ZegoEngineProfile();
profile.appID = KeyCenter.APP_ID;
profile.appSign = KeyCenter.APP_SIGN;
profile.scenario = ZegoScenario.GENERAL;
profile.application = ApplicationHelper.getApplication();
mZegoExpressEngine = ZegoExpressEngine.createEngine(profile, null);
mWebView = findViewById(R.id.tv_feedbook_webview);
StringBuilder sb = new StringBuilder();
sb.append(BackendApiConstants.FEEDBACK_API_URL)
.append("/feedback/gocall")
.append("/index.html?platform=8")
.append("&system_version=android_")
.append(android.os.Build.VERSION.RELEASE)
.append("&app_version=")
.append(BuildConfig.VERSION_NAME)
.append("&sdk_version=")
.append(ZegoExpressEngine.getVersion())
.append("&device_id=")
.append(DeviceInfoManager.getAndroidID())
.append("&client=")
.append(DeviceInfoManager.getModel());
feedbackUrl = sb.toString();
loadFeedbackWeb(feedbackUrl);
}
@Override
protected int getLayoutId() {
return R.layout.gocall_activity_feedback;
}
@Override
protected boolean isFullScreen() {
return false;
}
@Override
protected boolean isStatusBarTextDark() {
return true;
}
public static void actionActivity(Activity activity) {
Intent intent = new Intent(activity, FeedBackActivity.class);
activity.startActivity(intent);
}
private void loadFeedbackWeb(String url) {
mWebView.loadUrl(url);
mWebView.getSettings().setJavaScriptEnabled(true);
mWebView.getSettings().setDomStorageEnabled(true);
mWebView.getSettings().setUseWideViewPort(true);
mWebView.getSettings().setLoadWithOverviewMode(true);
mWebView.getSettings().setSupportZoom(false);
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
mWebView.addJavascriptInterface(new CallAndroid(), "feedback");
}
class CallAndroid {
@JavascriptInterface
public void callback(int result) {
ZegoAppLog.i(TAG, "提交反馈结果:" + result);
}
@JavascriptInterface
public void uploadLog() {
ZegoAppLog.i(TAG, "开始上传日志");
mZegoExpressEngine.uploadLog();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mZegoExpressEngine != null) {
ZegoExpressEngine.destroyEngine(null);
}
}
}
其中的重点是webview
的操作,适应javaScript
mWebView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
mWebView.addJavascriptInterface(new CallAndroid(), "feedback");
aar中被其他复用的GoWebViewActivity对象
该aar是由咱自己编包的,所以可以修改。为了安全要保证修改最小。
用作对webView的操作的对象。
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.os.Build.VERSION;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnScrollChangeListener;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.widget.ImageView;
import android.widget.TextView;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import androidx.constraintlayout.widget.ConstraintLayout;
import im.zego.gologin.R.id;
import im.zego.gologin.R.layout;
public class GoWebViewActivity extends AppCompatActivity {
private ConstraintLayout llTitleDefault;
private ImageView leftIconDefault;
private TextView tvTitleDefault;
private ConstraintLayout llTitleBackStyle;
private ImageView leftIconBlackStyle;
private TextView tvTitleBlackStyle;
private WebView webView;
private String mWebViewUrl;
public static final int DEFAULT_COLOR_STYLE = 0;
public static final int BLACK_COLOR_STYLE = 1;
public static final String URL = "url";
public static final String TITLE = "title";
public static final String STYLE = "style";
public static final String SCROLLABLE = "scrollable";
private boolean isScrollEnabled = true;
private String title;
public GoWebViewActivity() {
}
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.setContentView(layout.login_activity_webview);
this.llTitleDefault = (ConstraintLayout)this.findViewById(id.ll_title_default);
this.webView = (WebView)this.findViewById(id.webView);
this.mWebViewUrl = this.getIntent().getStringExtra("url");
this.title = this.getIntent().getStringExtra("title");
this.isScrollEnabled = this.getIntent().getBooleanExtra("scrollable", true);
int style = this.getIntent().getIntExtra("style", 0);
if (TextUtils.isEmpty(this.mWebViewUrl)) {
this.finish();
} else {
this.initDifferStyle(style);
this.initWebViewLoad();
}
}
private void initWebViewLoad() {
this.webView.loadUrl(this.mWebViewUrl);
if (VERSION.SDK_INT >= 23 && !this.isScrollEnabled) {
this.webView.setOnScrollChangeListener(new OnScrollChangeListener() {
public void onScrollChange(View v, int scrollX, int scrollY, int oldScrollX, int oldScrollY) {
GoWebViewActivity.this.webView.scrollTo(0, 0);
}
});
}
WebSettings webSettings = this.webView.getSettings();
webSettings.setJavaScriptEnabled(true);
webSettings.setJavaScriptCanOpenWindowsAutomatically(true);
webSettings.setDomStorageEnabled(true);
}
public WebView getWebView() {
return this.webView;
}
private void initDifferStyle(int style) {
switch(style) {
case 1:
this.llTitleBackStyle = (ConstraintLayout)this.findViewById(id.ll_title_back_style);
this.llTitleDefault.setVisibility(8);
this.llTitleBackStyle.setVisibility(0);
this.leftIconBlackStyle = (ImageView)this.findViewById(id.left_icon_black_style);
this.tvTitleBlackStyle = (TextView)this.findViewById(id.tv_title_black_style);
this.tvTitleBlackStyle.setText(this.title);
this.leftIconBlackStyle.setOnClickListener((v) -> {
this.finish();
});
break;
default:
this.leftIconDefault = (ImageView)this.findViewById(id.left_icon_default);
this.tvTitleDefault = (TextView)this.findViewById(id.tv_title_default);
this.tvTitleDefault.setText(this.title);
this.leftIconDefault.setOnClickListener((v) -> {
this.finish();
});
}
}
public static void jumpActivity(Context context, String url, String title) {
Intent intent = new Intent(context, GoWebViewActivity.class);
intent.putExtra("url", url);
intent.putExtra("title", title);
context.startActivity(intent);
}
public static void jumpActivity(Context context, String url, String title, int style) {
Intent intent = new Intent(context, GoWebViewActivity.class);
intent.putExtra("url", url);
intent.putExtra("title", title);
intent.putExtra("style", style);
context.startActivity(intent);
}
public static void jumpActivity(Context context, String url, String title, int style, boolean isScrollEnabled) {
Intent intent = new Intent(context, GoWebViewActivity.class);
intent.putExtra("url", url);
intent.putExtra("title", title);
intent.putExtra("style", style);
intent.putExtra("scrollable", isScrollEnabled);
context.startActivity(intent);
}
}
解决
- 首先将aar包中的
webView
的属性调整为protected
保证继承后可用
- 在继承之后要实现对
webView
的更新
更新之后的反馈类:
import android.app.Activity;
import android.os.Bundle;
import android.webkit.JavascriptInterface;
import android.webkit.WebResourceRequest;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import androidx.annotation.Nullable;
import im.zego.call.KeyCenter;
import im.zego.callcommon.log.ZegoAppLog;
import im.zego.callcommon.provider.ApplicationHelper;
import im.zego.callcommon.utils.DeviceInfoManager;
import im.zego.gocall.BackendApiConstants;
import im.zego.gocall.BuildConfig;
import im.zego.gologin.login.GoWebViewActivity;
import im.zego.zegoexpress.ZegoExpressEngine;
import im.zego.zegoexpress.constants.ZegoScenario;
import im.zego.zegoexpress.entity.ZegoEngineProfile;
/**
* 反馈页面
*/
public class FeedBackActivity extends GoWebViewActivity {
private String TAG = "FeedbackLog";
private ZegoExpressEngine mZegoExpressEngine;
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
ZegoEngineProfile profile = new ZegoEngineProfile();
profile.appID = KeyCenter.APP_ID;
profile.appSign = KeyCenter.APP_SIGN;
profile.scenario = ZegoScenario.GENERAL;
profile.application = ApplicationHelper.getApplication();
mZegoExpressEngine = ZegoExpressEngine.createEngine(profile, null);
configFeedbackWeb();
}
public static String getFeedbackUrl() {
StringBuilder sb = new StringBuilder();
sb.append(BackendApiConstants.FEEDBACK_API_URL)
.append("/feedback/gocall")
.append("/index.html?platform=8")
.append("&system_version=android_")
.append(android.os.Build.VERSION.RELEASE)
.append("&app_version=")
.append(BuildConfig.VERSION_NAME)
.append("&sdk_version=")
.append(ZegoExpressEngine.getVersion())
.append("&device_id=")
.append(DeviceInfoManager.getAndroidID())
.append("&client=")
.append(DeviceInfoManager.getModel());
return sb.toString();
}
public static void actionActivity(Activity activity) {
}
private void configFeedbackWeb() {
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setSupportZoom(false);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
webView.addJavascriptInterface(new CallAndroid(), "feedback");
}
class CallAndroid {
@JavascriptInterface
public void callback(int result) {
ZegoAppLog.i(TAG, "提交反馈结果:" + result);
}
@JavascriptInterface
public void uploadLog() {
ZegoAppLog.i(TAG, "开始上传日志");
mZegoExpressEngine.uploadLog();
}
}
@Override
protected void onDestroy() {
super.onDestroy();
if (mZegoExpressEngine != null) {
ZegoExpressEngine.destroyEngine(null);
}
}
}
这样就可以对webView
操作:
private void configFeedbackWeb() {
webView.getSettings().setUseWideViewPort(true);
webView.getSettings().setLoadWithOverviewMode(true);
webView.getSettings().setSupportZoom(false);
webView.setWebViewClient(new WebViewClient() {
@Override
public boolean shouldOverrideUrlLoading(WebView view, WebResourceRequest request) {
view.loadUrl(request.getUrl().toString());
return true;
}
});
webView.addJavascriptInterface(new CallAndroid(), "feedback");
}
- 最后那里需要这个反馈类就通过
intend
的方式调用即可。
这样咱就是实现了对aar包的最少改动保证了整体的逻辑。