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