Java tutorial
/* This file was modified from or inspired by Apache Cordova. 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.polyvi.xface.extension.inappbrowser; import android.app.Dialog; import android.content.Context; import android.content.DialogInterface; import android.text.InputType; import android.util.TypedValue; import android.view.Gravity; import android.view.KeyEvent; import android.view.View; import android.view.View.OnClickListener; import android.view.View.OnKeyListener; import android.view.Window; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; import android.view.inputmethod.EditorInfo; import android.view.inputmethod.InputMethodManager; import android.webkit.WebView; import android.widget.Button; import android.widget.EditText; import android.widget.LinearLayout; import android.widget.RelativeLayout; import com.polyvi.xface.extension.XCallbackContext; import com.polyvi.xface.plugin.api.XIWebContext; /** *??WebView?? */ public class XInAppBrowser implements XIBrowserBuilder, XIBrowserListener { private static final int BUTTON_CONTAINER_ID = 1; private static final int BUTTON_BACKBUTTON_ID = 2; private static final int BUTTON_FORWARDBUTTON_ID = 3; private static final int EDITTEXT_ID = 4; private static final int BUTTON_CLOSE_ID = 5; private Dialog mDialog; private Context mContext; private EditText mEditText; private XIBrowserListener mBrowserListener; private String mUrl; private WebView mWebView; private boolean mIsShowToolBar; private XIWebContext mWebContext; public XInAppBrowser(Context context, XIBrowserListener listener, String url, XIWebContext webContext, boolean isShowToolBar) { mContext = context; mBrowserListener = listener; mUrl = url; mWebContext = webContext; mIsShowToolBar = isShowToolBar; } /** * MainLayout * @return */ protected LinearLayout buildMainContainerLayout() { return createMainContainerLayout(LinearLayout.VERTICAL); } /** * toolBar * @return */ protected RelativeLayout buildToolbarLayout() { return createToolbarLayout(Gravity.LEFT, Gravity.TOP); } /** * ButtonContainer * @return */ protected RelativeLayout buildActionButtonContainerLayout() { return createActionButtonContainer(BUTTON_CONTAINER_ID, new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT), Gravity.LEFT, Gravity.CENTER_VERTICAL); } /** * * @return */ protected Button buildBackButton() { RelativeLayout.LayoutParams backLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); backLayoutParams.addRule(RelativeLayout.ALIGN_LEFT); Button back = createButton(BUTTON_BACKBUTTON_ID, backLayoutParams, "Back Button", "<", new View.OnClickListener() { public void onClick(View v) { goBack(); } }); return back; } /** * ? * @return */ protected Button buildForwardButton() { RelativeLayout.LayoutParams forwardLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); forwardLayoutParams.addRule(RelativeLayout.RIGHT_OF, 2); Button forward = createButton(BUTTON_FORWARDBUTTON_ID, forwardLayoutParams, "Forward Button", ">", new View.OnClickListener() { public void onClick(View v) { goForward(); } }); return forward; } /** * * @return */ protected Button buildCloseButton() { RelativeLayout.LayoutParams closeLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.MATCH_PARENT); closeLayoutParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT); Button close = createButton(BUTTON_CLOSE_ID, closeLayoutParams, "Close Button", "Done", new View.OnClickListener() { public void onClick(View v) { closeDialog(); } }); return close; } /** * */ protected void buildEditText() { RelativeLayout.LayoutParams textLayoutParams = new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT); textLayoutParams.addRule(RelativeLayout.RIGHT_OF, 1); textLayoutParams.addRule(RelativeLayout.LEFT_OF, 5); OnKeyListener keyListener = new View.OnKeyListener() { public boolean onKey(View v, int keyCode, KeyEvent event) { if ((event.getAction() == KeyEvent.ACTION_DOWN) && (keyCode == KeyEvent.KEYCODE_ENTER)) { navigate(mEditText.getText().toString()); return true; } return false; } }; mEditText = createEditText(EDITTEXT_ID, textLayoutParams, true, mUrl, InputType.TYPE_TEXT_VARIATION_URI, EditorInfo.IME_ACTION_GO, keyListener); } /** * ??? * @param theme * @param animation * @param windowFeature ? * @return dialog */ protected Dialog createDialog(int theme, int animation, int windowFeature) { Dialog dialog = new Dialog(mContext, theme); dialog.getWindow().getAttributes().windowAnimations = animation; dialog.requestWindowFeature(windowFeature); dialog.setCancelable(true); dialog.setOnDismissListener(new DialogInterface.OnDismissListener() { public void onDismiss(DialogInterface dialog) { mBrowserListener.onDismiss(mWebContext); } }); return dialog; } /** * ??Dialog * @param layout ? * @return */ protected LinearLayout createMainContainerLayout(int layout) { LinearLayout mainLayout = new LinearLayout(mContext); mainLayout.setOrientation(layout); return mainLayout; } /** * ??? * @param horizontalGravity ?? * @param verticalGravity ?? * @return */ protected RelativeLayout createToolbarLayout(int horizontalGravity, int verticalGravity) { RelativeLayout toolbarLayOut = new RelativeLayout(mContext); toolbarLayOut.setLayoutParams(new RelativeLayout.LayoutParams(LayoutParams.MATCH_PARENT, dpToPixels(44))); toolbarLayOut.setPadding(dpToPixels(2), dpToPixels(2), dpToPixels(2), dpToPixels(2)); toolbarLayOut.setHorizontalGravity(horizontalGravity); toolbarLayOut.setVerticalGravity(verticalGravity); return toolbarLayOut; } /** * ??button * @param containerId id? * @param params ? * @param horizontalGravity ?? * @param verticalGravity ?? * @return */ protected RelativeLayout createActionButtonContainer(int containerId, RelativeLayout.LayoutParams params, int horizontalGravity, int verticalGravity) { RelativeLayout actionButtonContainer = new RelativeLayout(mContext); actionButtonContainer.setLayoutParams(params); actionButtonContainer.setHorizontalGravity(horizontalGravity); actionButtonContainer.setVerticalGravity(verticalGravity); actionButtonContainer.setId(containerId); return actionButtonContainer; } /** * ?? * @param id * @param layout ? * @param description ?? * @param text button * @param listener ? * @return */ protected Button createButton(int id, RelativeLayout.LayoutParams layout, String description, String text, OnClickListener listener) { Button button = new Button(mContext); button.setLayoutParams(layout); button.setContentDescription(description); button.setText(text); button.setId(id); button.setOnClickListener(listener); return button; } /** * * @param id * @param layoutParams ? * @param isSingleLine ?? * @param text * @param inputType * @param imeOption ime * @param listener ? * @return */ protected EditText createEditText(int id, RelativeLayout.LayoutParams layoutParams, boolean isSingleLine, String text, int inputType, int imeOption, View.OnKeyListener listener) { EditText editText = new EditText(mContext); editText.setLayoutParams(layoutParams); editText.setId(id); editText.setSingleLine(true); editText.setText(text); editText.setInputType(inputType); editText.setImeOptions(imeOption); editText.setOnKeyListener(listener); return editText; } /** * WebView * @param url WebViewurl * @return */ protected void createWebView(String url, LinearLayout mainLayout) { mWebView = new XInAppWebView(mContext, this, url, mWebContext); mainLayout.addView(mWebView); } /** * DIP? * * @param dipValue * dip * @return int */ protected int dpToPixels(int dipValue) { int value = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, (float) dipValue, mContext.getResources().getDisplayMetrics()); return value; } /** * ? */ public XInAppBrowser buildBrowser() { mDialog = createDialog(android.R.style.Theme_NoTitleBar, android.R.style.Animation_Dialog, Window.FEATURE_NO_TITLE); LinearLayout mainLayout = buildMainContainerLayout(); if (mIsShowToolBar) { buildToolbar(mainLayout); } createWebView(mUrl, mainLayout); WindowManager.LayoutParams layoutParams = new WindowManager.LayoutParams(); layoutParams.copyFrom(mDialog.getWindow().getAttributes()); layoutParams.width = WindowManager.LayoutParams.MATCH_PARENT; layoutParams.height = WindowManager.LayoutParams.MATCH_PARENT; mDialog.getWindow().setAttributes(layoutParams); mDialog.setContentView(mainLayout); return this; } /** * ToolBar * @param mainLayout dialog */ private void buildToolbar(LinearLayout mainLayout) { RelativeLayout toolBarLayout = buildToolbarLayout(); RelativeLayout buttonContainerLayout = buildActionButtonContainerLayout(); Button backButton = buildBackButton(); Button forwardButton = buildForwardButton(); Button closeButton = buildCloseButton(); buttonContainerLayout.addView(backButton); buttonContainerLayout.addView(forwardButton); buildEditText(); toolBarLayout.addView(buttonContainerLayout); toolBarLayout.addView(mEditText); toolBarLayout.addView(closeButton); mainLayout.addView(toolBarLayout); } /** * */ public void show() { mDialog.show(); } /** * ?? */ private void goBack() { if (mWebView.canGoBack()) { mWebView.goBack(); } } /** * ?? */ private void goForward() { if (mWebView.canGoForward()) { mWebView.goForward(); } } /** * ? * @param url * ??Url */ private void navigate(String url) { InputMethodManager imm = (InputMethodManager) mContext.getSystemService(Context.INPUT_METHOD_SERVICE); imm.hideSoftInputFromWindow(mEditText.getWindowToken(), 0); if (isCompleteFormUrl(url)) { mWebView.loadUrl(url); } else { mWebView.loadUrl("http://" + url); } mWebView.requestFocus(); } /** * ? */ public void closeDialog() { if (mDialog != null) { mDialog.dismiss(); mBrowserListener.onDismiss(mWebContext); } } @Override public void onLoadFinished(String url, XIWebContext webContext) { mBrowserListener.onLoadFinished(url, webContext); } @Override public void onLoadStart(String url, XIWebContext webContext) { String newloc; if (isCompleteFormUrl(url)) { newloc = url; } else { newloc = "http://" + url; } if (mEditText != null && !newloc.equals(mEditText.getText().toString())) { mEditText.setText(newloc); } mBrowserListener.onLoadStart(url, webContext); } @Override public void onDismiss(XIWebContext webContext) { mBrowserListener.onDismiss(webContext); } /** * url??httpfile * @param url * @return */ private boolean isCompleteFormUrl(String url) { return (url.startsWith("http") || url.startsWith("file")); } public void setInjectCallbackContext(XCallbackContext callbackCtx) { mWebView.setWebChromeClient(new InAppBrowserClient(callbackCtx)); } /** * ??InAppBrowserWebView(script or style) * *?????JSON?jsWrapper? * * @param source ??(filename or script/style text) * @param jsWrapper ????JS, ??null?JS? */ public void injectDeferredObject(String source, String jsWrapper) { String scriptToInject; if (jsWrapper != null) { org.json.JSONArray jsonEsc = new org.json.JSONArray(); jsonEsc.put(source); String jsonRepr = jsonEsc.toString(); String jsonSourceString = jsonRepr.substring(1, jsonRepr.length() - 1); scriptToInject = String.format(jsWrapper, jsonSourceString); } else { scriptToInject = source; } mWebView.loadUrl("javascript:" + scriptToInject); } }