Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 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 | |
Bo Liu | ee34ef1 | 2016-04-01 10:29:05 -0700 | [diff] [blame] | 19 | import android.annotation.NonNull; |
| 20 | import android.annotation.Nullable; |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 21 | import android.annotation.SystemApi; |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 22 | import android.app.ActivityThread; |
| 23 | import android.app.Application; |
| 24 | import android.content.Context; |
| 25 | import android.content.res.Resources; |
| 26 | import android.graphics.Canvas; |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 27 | import android.os.SystemProperties; |
| 28 | import android.os.Trace; |
| 29 | import android.util.SparseArray; |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 30 | import android.view.DisplayListCanvas; |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 31 | import android.view.View; |
| 32 | import android.view.ViewRootImpl; |
| 33 | |
| 34 | /** |
| 35 | * Delegate used by the WebView provider implementation to access |
| 36 | * the required framework functionality needed to implement a {@link WebView}. |
| 37 | * |
| 38 | * @hide |
| 39 | */ |
Ignacio Solla | 451e338 | 2014-11-10 10:35:54 +0000 | [diff] [blame] | 40 | @SystemApi |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 41 | public final class WebViewDelegate { |
| 42 | |
| 43 | /* package */ WebViewDelegate() { } |
| 44 | |
| 45 | /** |
| 46 | * Listener that gets notified whenever tracing has been enabled/disabled. |
| 47 | */ |
| 48 | public interface OnTraceEnabledChangeListener { |
| 49 | void onTraceEnabledChange(boolean enabled); |
| 50 | } |
| 51 | |
| 52 | /** |
| 53 | * Register a callback to be invoked when tracing for the WebView component has been |
| 54 | * enabled/disabled. |
| 55 | */ |
| 56 | public void setOnTraceEnabledChangeListener(final OnTraceEnabledChangeListener listener) { |
| 57 | SystemProperties.addChangeCallback(new Runnable() { |
| 58 | @Override |
| 59 | public void run() { |
| 60 | listener.onTraceEnabledChange(isTraceTagEnabled()); |
| 61 | } |
| 62 | }); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * Returns true if the WebView trace tag is enabled and false otherwise. |
| 67 | */ |
| 68 | public boolean isTraceTagEnabled() { |
| 69 | return Trace.isTagEnabled(Trace.TRACE_TAG_WEBVIEW); |
| 70 | } |
| 71 | |
| 72 | /** |
| 73 | * Returns true if the draw GL functor can be invoked (see {@link #invokeDrawGlFunctor}) |
| 74 | * and false otherwise. |
| 75 | */ |
| 76 | public boolean canInvokeDrawGlFunctor(View containerView) { |
| 77 | ViewRootImpl viewRootImpl = containerView.getViewRootImpl(); |
| 78 | // viewRootImpl can be null during teardown when window is leaked. |
| 79 | return viewRootImpl != null; |
| 80 | } |
| 81 | |
| 82 | /** |
| 83 | * Invokes the draw GL functor. If waitForCompletion is false the functor |
| 84 | * may be invoked asynchronously. |
| 85 | * |
| 86 | * @param nativeDrawGLFunctor the pointer to the native functor that implements |
| 87 | * system/core/include/utils/Functor.h |
| 88 | */ |
| 89 | public void invokeDrawGlFunctor(View containerView, long nativeDrawGLFunctor, |
| 90 | boolean waitForCompletion) { |
John Reck | 44b49f0 | 2016-03-25 14:29:48 -0700 | [diff] [blame] | 91 | ViewRootImpl.invokeFunctor(nativeDrawGLFunctor, waitForCompletion); |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 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 |
Ignacio Solla | 53f2569 | 2014-11-12 21:55:47 +0000 | [diff] [blame] | 102 | * @throws IllegalArgumentException if the canvas is not hardware accelerated |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 103 | */ |
| 104 | public void callDrawGlFunction(Canvas canvas, long nativeDrawGLFunctor) { |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 105 | if (!(canvas instanceof DisplayListCanvas)) { |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 106 | // Canvas#isHardwareAccelerated() is only true for subclasses of HardwareCanvas. |
| 107 | throw new IllegalArgumentException(canvas.getClass().getName() |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 108 | + " is not a DisplayList canvas"); |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 109 | } |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 110 | ((DisplayListCanvas) canvas).callDrawGLFunction2(nativeDrawGLFunctor); |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 111 | } |
| 112 | |
| 113 | /** |
Bo Liu | ee34ef1 | 2016-04-01 10:29:05 -0700 | [diff] [blame] | 114 | * Set the Runnable callback the DrawGlFunction functor is detached and free to be destroyed. |
| 115 | * This will replace the previous callback, if any. |
| 116 | * |
| 117 | * @param view The view to set the callback. Should be the view where onDraw inserted |
| 118 | * DrawGLFunctor. |
| 119 | * @param callback The new callback to set on the view. |
| 120 | * @throws IllegalArgumentException if view is null. |
| 121 | * @return The previous callback on this view. |
| 122 | */ |
| 123 | public Runnable setDrawGlFunctionDetachedCallback( |
| 124 | @NonNull View view, @Nullable Runnable callback) { |
| 125 | if (view == null) { |
| 126 | throw new IllegalArgumentException("View cannot be null"); |
| 127 | } |
| 128 | return view.setRenderNodeDetachedCallback(callback); |
| 129 | } |
| 130 | |
| 131 | /** |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 132 | * Detaches the draw GL functor. |
| 133 | * |
| 134 | * @param nativeDrawGLFunctor the pointer to the native functor that implements |
| 135 | * system/core/include/utils/Functor.h |
| 136 | */ |
| 137 | public void detachDrawGlFunctor(View containerView, long nativeDrawGLFunctor) { |
| 138 | ViewRootImpl viewRootImpl = containerView.getViewRootImpl(); |
| 139 | if (nativeDrawGLFunctor != 0 && viewRootImpl != null) { |
| 140 | viewRootImpl.detachFunctor(nativeDrawGLFunctor); |
| 141 | } |
| 142 | } |
| 143 | |
| 144 | /** |
| 145 | * Returns the package id of the given {@code packageName}. |
| 146 | */ |
| 147 | public int getPackageId(Resources resources, String packageName) { |
| 148 | SparseArray<String> packageIdentifiers = |
| 149 | resources.getAssets().getAssignedPackageIdentifiers(); |
| 150 | for (int i = 0; i < packageIdentifiers.size(); i++) { |
| 151 | final String name = packageIdentifiers.valueAt(i); |
| 152 | |
| 153 | if (packageName.equals(name)) { |
| 154 | return packageIdentifiers.keyAt(i); |
| 155 | } |
| 156 | } |
| 157 | throw new RuntimeException("Package not found: " + packageName); |
| 158 | } |
| 159 | |
| 160 | /** |
| 161 | * Returns the application which is embedding the WebView. |
| 162 | */ |
| 163 | public Application getApplication() { |
| 164 | return ActivityThread.currentApplication(); |
| 165 | } |
| 166 | |
| 167 | /** |
| 168 | * Returns the error string for the given {@code errorCode}. |
| 169 | */ |
| 170 | public String getErrorString(Context context, int errorCode) { |
Narayan Kamath | f1a9b1b | 2014-11-27 17:20:21 +0000 | [diff] [blame] | 171 | return LegacyErrorStrings.getString(errorCode, context); |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 172 | } |
| 173 | |
| 174 | /** |
Chris Craik | f6829a0 | 2015-03-10 10:28:59 -0700 | [diff] [blame] | 175 | * Adds the WebView asset path to {@link android.content.res.AssetManager}. |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 176 | */ |
| 177 | public void addWebViewAssetPath(Context context) { |
Tao Bai | eb1368b | 2016-02-23 09:01:28 -0800 | [diff] [blame] | 178 | context.getAssets().addAssetPathAsSharedLibrary( |
Ignacio Solla | 94ef789 | 2014-10-29 12:47:46 +0000 | [diff] [blame] | 179 | WebViewFactory.getLoadedPackageInfo().applicationInfo.sourceDir); |
| 180 | } |
| 181 | } |