下面列出了怎么用android.content.MutableContextWrapper的API类实例代码及写法,或者点击链接到github查看源代码。
public synchronized WrapperWebView getWebView() {
WrapperWebView webView = null;
if (sAvailable.size() > 0) {
webView = sAvailable.get(0);
sAvailable.remove(0);
} else {
// 无可用的webview时,自动扩容
webView = new WrapperWebView(new MutableContextWrapper(mContext));
ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
webView.setLayoutParams(layoutParams);
}
sInUse.add(webView);
webView.loadUrl("");
return webView;
}
public static Context getTopContext(View view) {
if (view == null) {
return null;
}
ViewParent parent = view.getParent();
if ((parent == null) || !(parent instanceof View)) {
if (view.getContext() instanceof MutableContextWrapper) {
return ((MutableContextWrapper) view.getContext()).getBaseContext();
}
return view.getContext();
}
//noinspection ConstantConditions
while ((parent.getParent() != null)
&& (parent.getParent() instanceof View)) {
parent = parent.getParent();
}
return ((View) parent).getContext();
}
@SuppressLint("SetJavaScriptEnabled")
public RedirectWebView(final Context context) {
super(new MutableContextWrapper(context));
WebviewUtil.setWebViewSettings(this);
this.setWebViewClient(new WebViewClient() {
@Override
public void onPageFinished(WebView view, String url) {
Clog.v(Clog.browserLogTag, "Opening URL: " + url);
ViewUtil.removeChildFromParent(RedirectWebView.this);
if (progressDialog != null && progressDialog.isShowing()) {
progressDialog.dismiss();
}
startBrowserActivity(context);
}
});
}
@Override
public void create() {
if ((AdView.mraidFullscreenContainer == null) || (AdView.mraidFullscreenImplementation == null)) {
Clog.e(Clog.baseLogTag, "Launched MRAID Fullscreen activity with invalid properties");
adActivity.finish();
return;
}
// remove from any old parents to be safe
ViewUtil.removeChildFromParent(AdView.mraidFullscreenContainer);
adActivity.setContentView(AdView.mraidFullscreenContainer);
if (AdView.mraidFullscreenContainer.getChildAt(0) instanceof AdWebView) {
webView = (AdWebView) AdView.mraidFullscreenContainer.getChildAt(0);
}
// Update the context
if(webView.getContext() instanceof MutableContextWrapper) {
((MutableContextWrapper) webView.getContext()).setBaseContext(adActivity);
}
mraidFullscreenImplementation = AdView.mraidFullscreenImplementation;
mraidFullscreenImplementation.setFullscreenActivity(adActivity);
if (AdView.mraidFullscreenListener != null) {
AdView.mraidFullscreenListener.onCreateCompleted();
}
}
public static void release(FastWebView webView) {
if (webView == null) {
return;
}
webView.release();
MutableContextWrapper wrapper = (MutableContextWrapper) webView.getContext();
wrapper.setBaseContext(wrapper.getApplicationContext());
sPool.release(webView);
LogUtils.d("release webview instance to pool.");
}
public void initWebViewPool(Context context) {
this.mContext = context;
for (int i = 0; i < mPoolSize; i++) {
ViewGroup.LayoutParams layoutParams = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT,
ViewGroup.LayoutParams.MATCH_PARENT);
// 引入Context中间层MutableContextWrapper
WrapperWebView webView = new WrapperWebView(new MutableContextWrapper(mContext));
webView.setLayoutParams(layoutParams);
sAvailable.add(webView);
}
}
public synchronized void resetWebView(WrapperWebView webView) {
((MutableContextWrapper) webView.getContext()).setBaseContext(mContext);
webView.reset();
sInUse.remove(webView);
if (sAvailable.size() < mPoolSize) {//保存个数不能大于池子的大小
sAvailable.add(webView);
} else { // 扩容出来的临时webview直接回收
webView.destroy();
}
}
@Override
public void onClick(View view) {
if (view.getId() == R.id.btn_close) {
if (getContext() instanceof MutableContextWrapper) {
Context baseContext = ((MutableContextWrapper) getContext()).getBaseContext();
if (baseContext instanceof Activity) {
((Activity) baseContext).finish();
}
} else if (getContext() instanceof Activity) {
((Activity) getContext()).finish();
}
} else if (view.getId() == R.id.btn_refresh) {
reload();
}
}
/**
* Pre-load {@link ReactRootView} to local {@link Map}, you may want to
* load it in previous activity.
*/
public static void init(Activity activity, ReactInfo reactInfo) {
if (CACHE_VIEW_MAP.get(reactInfo.getMainComponentName()) != null) {
return;
}
ReactRootView rootView = new ReactRootView(new MutableContextWrapper(activity));
rootView.startReactApplication(
((ReactApplication) activity.getApplication()).getReactNativeHost().getReactInstanceManager(),
reactInfo.getMainComponentName(),
reactInfo.getLaunchOptions());
CACHE_VIEW_MAP.put(reactInfo.getMainComponentName(), rootView);
}
/**
* <p>Creates the shared webView used throughout the lifetime of the TurbolinksSession.</p>
*
* @param applicationContext An application context.
* @return The shared WebView.
*/
static WebView createWebView(Context applicationContext) {
MutableContextWrapper contextWrapper = new MutableContextWrapper(applicationContext);
WebView webView = new WebView(contextWrapper);
configureWebViewDefaults(webView);
setWebViewLayoutParams(webView);
return webView;
}
public VideoWebView(Context context, VideoAd owner, AdViewRequestManager manager) {
super(new MutableContextWrapper(context));
this.owner = owner;
this.manager = manager;
setupSettings();
setup();
}
private void updateMutableContext(ViewGroup layout) {
// Update the MutableContext Wrapper. with the new activity context.
if(this.getContext() instanceof MutableContextWrapper) {
((MutableContextWrapper)this.getContext()).setBaseContext(layout.getContext());
}
// Update the MutableContext Wrapper. with the new activity context.
if(this.videoWebView.getContext() instanceof MutableContextWrapper) {
((MutableContextWrapper)this.videoWebView.getContext()).setBaseContext(layout.getContext());
}
}
private void setIAdView(InterstitialAdView av) {
adView = av;
if (adView == null) return;
adView.setAdImplementation(this);
layout.setBackgroundColor(adView.getBackgroundColor());
layout.removeAllViews();
if (adView.getParent() != null) {
((ViewGroup) adView.getParent()).removeAllViews();
}
InterstitialAdQueueEntry iAQE = adView.getAdQueue().poll();
// To be safe, ads from the future will be considered to have expired
// if now-p.first is less than 0, the ad will be considered to be from the future
while (iAQE != null
&& (now - iAQE.getTime() > InterstitialAdView.MAX_AGE || now - iAQE.getTime() < 0)) {
Clog.w(Clog.baseLogTag, Clog.getString(R.string.too_old));
iAQE = adView.getAdQueue().poll();
}
if ((iAQE == null)
|| !(iAQE.getView() instanceof AdWebView))
return;
webView = (AdWebView) iAQE.getView();
// Update the context
if (webView.getContext() instanceof MutableContextWrapper) {
((MutableContextWrapper) webView.getContext()).setBaseContext(adActivity);
}
// lock orientation to ad request orientation
//@TODO need to change this check condition to reflect MRAID spec
if (!(webView.getCreativeWidth() == 1 && webView.getCreativeHeight() == 1)) {
AdActivity.lockToConfigOrientation(adActivity, webView.getOrientation());
}
layout.addView(webView);
}
public AdWebView(AdView adView, UTAdRequester requester) {
super(new MutableContextWrapper(adView.getContext()));
this.adView = adView;
this.caller_requester = requester;
this.initialMraidStateString = MRAIDImplementation.MRAID_INIT_STATE_STRINGS[
MRAIDImplementation.MRAID_INIT_STATE.STARTING_DEFAULT.ordinal()];
setupSettings();
setup();
}
protected void close(int w, int h, MRAIDImplementation caller) {
// Remove MRAID close button
ViewUtil.removeChildFromParent(close_button);
close_button = null;
if (caller.owner.isFullScreen) {
ViewUtil.removeChildFromParent(caller.owner);
if (caller.getDefaultContainer() != null) {
caller.getDefaultContainer().addView(caller.owner, 0);
}
if (caller.getFullscreenActivity() != null) {
caller.getFullscreenActivity().finish();
}
// Reset the context of MutableContext wrapper for banner expand and close case.
if (getMediaType().equals(MediaType.BANNER) && (caller.owner.getContext() instanceof MutableContextWrapper)) {
((MutableContextWrapper) caller.owner.getContext()).setBaseContext(getContext());
}
}
// null these out for safety
mraidFullscreenContainer = null;
mraidFullscreenImplementation = null;
mraidFullscreenListener = null;
MRAIDChangeSize(w, h);
mraid_is_closing = true;
isMRAIDExpanded = false;
}
/**
* 绑定新的Context
*
* @param context
*/
public void bindNewContext(Context context) {
if (getContext() instanceof MutableContextWrapper) {
((MutableContextWrapper) getContext()).setBaseContext(context);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
if (getUseDeveloperSupport() && Build.VERSION.SDK_INT >= 23) {
// Get permission to show redbox in dev builds.
if (!Settings.canDrawOverlays(this)) {
Intent serviceIntent = new Intent(Settings.ACTION_MANAGE_OVERLAY_PERMISSION);
startActivity(serviceIntent);
FLog.w(ReactConstants.TAG, REDBOX_PERMISSION_MESSAGE);
Toast.makeText(this, REDBOX_PERMISSION_MESSAGE, Toast.LENGTH_LONG).show();
}
}
mReactRootView = ReactPreLoader.getRootView(getReactInfo());
if (mReactRootView != null) {
Log.i(TAG, "use pre-load view");
MutableContextWrapper contextWrapper = (MutableContextWrapper) mReactRootView.getContext();
contextWrapper.setBaseContext(this);
try {
ViewGroup viewGroup = (ViewGroup) mReactRootView.getParent();
if (viewGroup != null) {
viewGroup.removeView(mReactRootView);
}
} catch (Exception exception) {
Log.e(TAG, "getParent error", exception);
}
} else {
Log.i(TAG, "createRootView");
mReactRootView = createRootView();
if (mReactRootView != null) {
mReactRootView.startReactApplication(
getReactNativeHost().getReactInstanceManager(),
getMainComponentName(),
getLaunchOptions());
}
}
setContentView(mReactRootView);
mDoubleTapReloadRecognizer = new DoubleTapReloadRecognizer();
}
protected Context getContextFromMutableContext() {
if (this.getContext() instanceof MutableContextWrapper) {
return ((MutableContextWrapper) this.getContext()).getBaseContext();
}
return this.getContext();
}
InstreamVideoView(Context context, AttributeSet attrs, int defStyle) {
super(new MutableContextWrapper(context), attrs, defStyle);
setup(context);
}
protected Context getContextFromMutableContext() {
if (this.getContext() instanceof MutableContextWrapper) {
return ((MutableContextWrapper) this.getContext()).getBaseContext();
}
return this.getContext();
}