194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla/* 294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Copyright (C) 2014 The Android Open Source Project 394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * 494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Licensed under the Apache License, Version 2.0 (the "License"); 594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * you may not use this file except in compliance with the License. 694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * You may obtain a copy of the License at 794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * 894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * http://www.apache.org/licenses/LICENSE-2.0 994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * 1094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Unless required by applicable law or agreed to in writing, software 1194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * distributed under the License is distributed on an "AS IS" BASIS, 1294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * See the License for the specific language governing permissions and 1494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * limitations under the License. 1594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 1694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 1794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollapackage android.webkit; 1894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 19451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Sollaimport android.annotation.SystemApi; 2094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.app.ActivityThread; 2194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.app.Application; 2294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.content.Context; 2394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.content.res.Resources; 2494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.graphics.Canvas; 2594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.os.SystemProperties; 2694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.os.Trace; 2794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.util.SparseArray; 28f6829a0a618b4523619ec53c996b04d67e3186b9Chris Craikimport android.view.DisplayListCanvas; 2994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.view.View; 3094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollaimport android.view.ViewRootImpl; 3194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 3294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla/** 3394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Delegate used by the WebView provider implementation to access 3494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * the required framework functionality needed to implement a {@link WebView}. 3594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * 3694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * @hide 3794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 38451e338c51e8c45efc0d21536dfae6f78f6d5e06Ignacio Solla@SystemApi 3994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Sollapublic final class WebViewDelegate { 4094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 4194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /* package */ WebViewDelegate() { } 4294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 4394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 4494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Listener that gets notified whenever tracing has been enabled/disabled. 4594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 4694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public interface OnTraceEnabledChangeListener { 4794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla void onTraceEnabledChange(boolean enabled); 4894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 4994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 5094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 5194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Register a callback to be invoked when tracing for the WebView component has been 5294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * enabled/disabled. 5394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 5494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public void setOnTraceEnabledChangeListener(final OnTraceEnabledChangeListener listener) { 5594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla SystemProperties.addChangeCallback(new Runnable() { 5694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla @Override 5794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public void run() { 5894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla listener.onTraceEnabledChange(isTraceTagEnabled()); 5994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 6094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla }); 6194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 6294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 6394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 6494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Returns true if the WebView trace tag is enabled and false otherwise. 6594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 6694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public boolean isTraceTagEnabled() { 6794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla return Trace.isTagEnabled(Trace.TRACE_TAG_WEBVIEW); 6894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 6994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 7094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 7194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Returns true if the draw GL functor can be invoked (see {@link #invokeDrawGlFunctor}) 7294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * and false otherwise. 7394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 7494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public boolean canInvokeDrawGlFunctor(View containerView) { 7594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla ViewRootImpl viewRootImpl = containerView.getViewRootImpl(); 7694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla // viewRootImpl can be null during teardown when window is leaked. 7794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla return viewRootImpl != null; 7894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 7994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 8094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 8194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Invokes the draw GL functor. If waitForCompletion is false the functor 8294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * may be invoked asynchronously. 8394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * 8494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * @param nativeDrawGLFunctor the pointer to the native functor that implements 8594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * system/core/include/utils/Functor.h 8694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 8794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public void invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor, 8894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla boolean waitForCompletion) { 8994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla ViewRootImpl viewRootImpl = containerView.getViewRootImpl(); 9094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla viewRootImpl.invokeFunctor(nativeDrawGLFunctor, waitForCompletion); 9194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 9294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 9394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 9494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Calls the function specified with the nativeDrawGLFunctor functor pointer. This 9594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * functionality is used by the WebView for calling into their renderer from the 9694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * framework display lists. 9794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * 9894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()}) 9994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * @param nativeDrawGLFunctor the pointer to the native functor that implements 10094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * system/core/include/utils/Functor.h 10153f256948724b8d4009d8a8a47da96a06402f9a2Ignacio Solla * @throws IllegalArgumentException if the canvas is not hardware accelerated 10294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 10394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public void callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor) { 104f6829a0a618b4523619ec53c996b04d67e3186b9Chris Craik if (!(canvas instanceof DisplayListCanvas)) { 10594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas. 10694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla throw new IllegalArgumentException(canvas.getClass().getName() 107f6829a0a618b4523619ec53c996b04d67e3186b9Chris Craik + " is not a DisplayList canvas"); 10894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 109f6829a0a618b4523619ec53c996b04d67e3186b9Chris Craik ((DisplayListCanvas) canvas).callDrawGLFunction2(nativeDrawGLFunctor); 11094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 11194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 11294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 11394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Detaches the draw GL functor. 11494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * 11594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * @param nativeDrawGLFunctor the pointer to the native functor that implements 11694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * system/core/include/utils/Functor.h 11794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 11894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public void detachDrawGlFunctor(View containerView, long nativeDrawGLFunctor) { 11994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla ViewRootImpl viewRootImpl = containerView.getViewRootImpl(); 12094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla if (nativeDrawGLFunctor != 0 && viewRootImpl != null) { 12194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla viewRootImpl.detachFunctor(nativeDrawGLFunctor); 12294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 12394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 12494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 12594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 12694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Returns the package id of the given {@code packageName}. 12794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 12894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public int getPackageId(Resources resources, String packageName) { 12994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla SparseArray<String> packageIdentifiers = 13094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla resources.getAssets().getAssignedPackageIdentifiers(); 13194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla for (int i = 0; i < packageIdentifiers.size(); i++) { 13294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla final String name = packageIdentifiers.valueAt(i); 13394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 13494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla if (packageName.equals(name)) { 13594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla return packageIdentifiers.keyAt(i); 13694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 13794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 13894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla throw new RuntimeException("Package not found: " + packageName); 13994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 14094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 14194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 14294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Returns the application which is embedding the WebView. 14394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 14494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public Application getApplication() { 14594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla return ActivityThread.currentApplication(); 14694ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 14794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 14894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 14994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla * Returns the error string for the given {@code errorCode}. 15094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 15194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public String getErrorString(Context context, int errorCode) { 152f1a9b1bc249161fe1a9b0d85d4ed31153e4421c1Narayan Kamath return LegacyErrorStrings.getString(errorCode, context); 15394ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 15494ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla 15594ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla /** 156f6829a0a618b4523619ec53c996b04d67e3186b9Chris Craik * Adds the WebView asset path to {@link android.content.res.AssetManager}. 15794ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla */ 15894ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla public void addWebViewAssetPath(Context context) { 15994ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla context.getAssets().addAssetPath( 16094ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla WebViewFactory.getLoadedPackageInfo().applicationInfo.sourceDir); 16194ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla } 16294ef789f0cf0387df20c2c602857ada1e5abfdd6Ignacio Solla} 163