1 /* 2 * Copyright (C) 2014 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.webkit; 18 19 import android.annotation.SystemApi; 20 import android.app.ActivityThread; 21 import android.app.Application; 22 import android.content.Context; 23 import android.content.res.Resources; 24 import android.graphics.Canvas; 25 import android.net.http.ErrorStrings; 26 import android.os.SystemProperties; 27 import android.os.Trace; 28 import android.util.SparseArray; 29 import android.view.HardwareCanvas; 30 import android.view.View; 31 import android.view.ViewRootImpl; 32 33 /** 34 * Delegate used by the WebView provider implementation to access 35 * the required framework functionality needed to implement a {@link WebView}. 36 * 37 * @hide 38 */ 39 @SystemApi 40 public final class WebViewDelegate { 41 42 /* package */ WebViewDelegate() { } 43 44 /** 45 * Listener that gets notified whenever tracing has been enabled/disabled. 46 */ 47 public interface OnTraceEnabledChangeListener { 48 void onTraceEnabledChange(boolean enabled); 49 } 50 51 /** 52 * Register a callback to be invoked when tracing for the WebView component has been 53 * enabled/disabled. 54 */ 55 public void setOnTraceEnabledChangeListener(final OnTraceEnabledChangeListener listener) { 56 SystemProperties.addChangeCallback(new Runnable() { 57 @Override 58 public void run() { 59 listener.onTraceEnabledChange(isTraceTagEnabled()); 60 } 61 }); 62 } 63 64 /** 65 * Returns true if the WebView trace tag is enabled and false otherwise. 66 */ 67 public boolean isTraceTagEnabled() { 68 return Trace.isTagEnabled(Trace.TRACE_TAG_WEBVIEW); 69 } 70 71 /** 72 * Returns true if the draw GL functor can be invoked (see {@link #invokeDrawGlFunctor}) 73 * and false otherwise. 74 */ 75 public boolean canInvokeDrawGlFunctor(View containerView) { 76 ViewRootImpl viewRootImpl = containerView.getViewRootImpl(); 77 // viewRootImpl can be null during teardown when window is leaked. 78 return viewRootImpl != null; 79 } 80 81 /** 82 * Invokes the draw GL functor. If waitForCompletion is false the functor 83 * may be invoked asynchronously. 84 * 85 * @param nativeDrawGLFunctor the pointer to the native functor that implements 86 * system/core/include/utils/Functor.h 87 */ 88 public void invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor, 89 boolean waitForCompletion) { 90 ViewRootImpl viewRootImpl = containerView.getViewRootImpl(); 91 viewRootImpl.invokeFunctor(nativeDrawGLFunctor, waitForCompletion); 92 } 93 94 /** 95 * Calls the function specified with the nativeDrawGLFunctor functor pointer. This 96 * functionality is used by the WebView for calling into their renderer from the 97 * framework display lists. 98 * 99 * @param canvas a hardware accelerated canvas (see {@link Canvas#isHardwareAccelerated()}) 100 * @param nativeDrawGLFunctor the pointer to the native functor that implements 101 * system/core/include/utils/Functor.h 102 * @throws IllegalArgumentException if the canvas is not hardware accelerated 103 */ 104 public void callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor) { 105 if (!(canvas instanceof HardwareCanvas)) { 106 // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas. 107 throw new IllegalArgumentException(canvas.getClass().getName() 108 + " is not hardware accelerated"); 109 } 110 ((HardwareCanvas) canvas).callDrawGLFunction2(nativeDrawGLFunctor); 111 } 112 113 /** 114 * Detaches the draw GL functor. 115 * 116 * @param nativeDrawGLFunctor the pointer to the native functor that implements 117 * system/core/include/utils/Functor.h 118 */ 119 public void detachDrawGlFunctor(View containerView, long nativeDrawGLFunctor) { 120 ViewRootImpl viewRootImpl = containerView.getViewRootImpl(); 121 if (nativeDrawGLFunctor != 0 && viewRootImpl != null) { 122 viewRootImpl.detachFunctor(nativeDrawGLFunctor); 123 } 124 } 125 126 /** 127 * Returns the package id of the given {@code packageName}. 128 */ 129 public int getPackageId(Resources resources, String packageName) { 130 SparseArray<String> packageIdentifiers = 131 resources.getAssets().getAssignedPackageIdentifiers(); 132 for (int i = 0; i < packageIdentifiers.size(); i++) { 133 final String name = packageIdentifiers.valueAt(i); 134 135 if (packageName.equals(name)) { 136 return packageIdentifiers.keyAt(i); 137 } 138 } 139 throw new RuntimeException("Package not found: " + packageName); 140 } 141 142 /** 143 * Returns the application which is embedding the WebView. 144 */ 145 public Application getApplication() { 146 return ActivityThread.currentApplication(); 147 } 148 149 /** 150 * Returns the error string for the given {@code errorCode}. 151 */ 152 public String getErrorString(Context context, int errorCode) { 153 return ErrorStrings.getString(errorCode, context); 154 } 155 156 /** 157 * Adds the WebView asset path to {@link AssetManager}. 158 */ 159 public void addWebViewAssetPath(Context context) { 160 context.getAssets().addAssetPath( 161 WebViewFactory.getLoadedPackageInfo().applicationInfo.sourceDir); 162 } 163 } 164