blob: 98e66b6f39d2d673baa98723f73276993913a459 [file] [log] [blame]
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001/*
2 * Copyright (C) 2007 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.content.Context;
20import android.os.Build;
21import android.os.Handler;
22import android.os.Message;
The Android Open Source Project9066cfe2009-03-03 19:31:44 -080023import android.provider.Checkin;
24
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -080025import java.lang.SecurityException;
26import android.content.pm.PackageManager;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -070027
28import java.util.Locale;
29
30/**
31 * Manages settings state for a WebView. When a WebView is first created, it
32 * obtains a set of default settings. These default settings will be returned
33 * from any getter call. A WebSettings object obtained from
34 * WebView.getSettings() is tied to the life of the WebView. If a WebView has
35 * been destroyed, any method call on WebSettings will throw an
36 * IllegalStateException.
37 */
38public class WebSettings {
39 /**
40 * Enum for controlling the layout of html.
41 * NORMAL means no rendering changes.
42 * SINGLE_COLUMN moves all content into one column that is the width of the
43 * view.
44 * NARROW_COLUMNS makes all columns no wider than the screen if possible.
45 */
46 // XXX: These must match LayoutAlgorithm in Settings.h in WebCore.
47 public enum LayoutAlgorithm {
48 NORMAL,
49 SINGLE_COLUMN,
50 NARROW_COLUMNS
51 }
52
53 /**
54 * Enum for specifying the text size.
55 * SMALLEST is 50%
56 * SMALLER is 75%
57 * NORMAL is 100%
58 * LARGER is 150%
59 * LARGEST is 200%
60 */
61 public enum TextSize {
62 SMALLEST(50),
63 SMALLER(75),
64 NORMAL(100),
65 LARGER(150),
66 LARGEST(200);
67 TextSize(int size) {
68 value = size;
69 }
70 int value;
71 }
72
73 /**
74 * Default cache usage pattern Use with {@link #setCacheMode}.
75 */
76 public static final int LOAD_DEFAULT = -1;
77
78 /**
79 * Normal cache usage pattern Use with {@link #setCacheMode}.
80 */
81 public static final int LOAD_NORMAL = 0;
82
83 /**
84 * Use cache if content is there, even if expired (eg, history nav)
85 * If it is not in the cache, load from network.
86 * Use with {@link #setCacheMode}.
87 */
88 public static final int LOAD_CACHE_ELSE_NETWORK = 1;
89
90 /**
91 * Don't use the cache, load from network
92 * Use with {@link #setCacheMode}.
93 */
94 public static final int LOAD_NO_CACHE = 2;
95
96 /**
97 * Don't use the network, load from cache only.
98 * Use with {@link #setCacheMode}.
99 */
100 public static final int LOAD_CACHE_ONLY = 3;
101
102 public enum RenderPriority {
103 NORMAL,
104 HIGH,
105 LOW
106 }
107
108 // BrowserFrame used to access the native frame pointer.
109 private BrowserFrame mBrowserFrame;
110 // Flag to prevent multiple SYNC messages at one time.
111 private boolean mSyncPending = false;
112 // Custom handler that queues messages until the WebCore thread is active.
113 private final EventHandler mEventHandler;
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100114
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700115 // Private settings so we don't have to go into native code to
116 // retrieve the values. After setXXX, postSync() needs to be called.
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100117 //
118 // The default values need to match those in WebSettings.cpp
119 // If the defaults change, please also update the JavaDocs so developers
120 // know what they are.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700121 private LayoutAlgorithm mLayoutAlgorithm = LayoutAlgorithm.NARROW_COLUMNS;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800122 private Context mContext;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700123 private TextSize mTextSize = TextSize.NORMAL;
124 private String mStandardFontFamily = "sans-serif";
125 private String mFixedFontFamily = "monospace";
126 private String mSansSerifFontFamily = "sans-serif";
127 private String mSerifFontFamily = "serif";
128 private String mCursiveFontFamily = "cursive";
129 private String mFantasyFontFamily = "fantasy";
130 private String mDefaultTextEncoding = "Latin-1";
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800131 private String mUserAgent;
132 private boolean mUseDefaultUserAgent;
133 private String mAcceptLanguage;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700134 private String mPluginsPath = "";
135 private int mMinimumFontSize = 8;
136 private int mMinimumLogicalFontSize = 8;
137 private int mDefaultFontSize = 16;
138 private int mDefaultFixedFontSize = 13;
139 private boolean mLoadsImagesAutomatically = true;
140 private boolean mBlockNetworkImage = false;
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700141 private boolean mBlockNetworkLoads;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700142 private boolean mJavaScriptEnabled = false;
143 private boolean mPluginsEnabled = false;
144 private boolean mJavaScriptCanOpenWindowsAutomatically = false;
145 private boolean mUseDoubleTree = false;
146 private boolean mUseWideViewport = false;
147 private boolean mSupportMultipleWindows = false;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800148 private boolean mShrinksStandaloneImagesToFit = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700149 // Don't need to synchronize the get/set methods as they
150 // are basic types, also none of these values are used in
151 // native WebCore code.
152 private RenderPriority mRenderPriority = RenderPriority.NORMAL;
153 private int mOverrideCacheMode = LOAD_DEFAULT;
154 private boolean mSaveFormData = true;
155 private boolean mSavePassword = true;
156 private boolean mLightTouchEnabled = false;
157 private boolean mNeedInitialFocus = true;
158 private boolean mNavDump = false;
159 private boolean mSupportZoom = true;
The Android Open Source Project10592532009-03-18 17:39:46 -0700160 private boolean mBuiltInZoomControls = false;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800161 private boolean mAllowFileAccess = true;
Andrei Popescu60a9a7d2009-04-17 10:43:42 +0100162 private String mAppCachePath = "";
163 private boolean mAppCacheEnabled = false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700164
165 // Class to handle messages before WebCore is ready.
166 private class EventHandler {
167 // Message id for syncing
168 static final int SYNC = 0;
169 // Message id for setting priority
170 static final int PRIORITY = 1;
171 // Actual WebCore thread handler
172 private Handler mHandler;
173
174 private synchronized void createHandler() {
175 // as mRenderPriority can be set before thread is running, sync up
176 setRenderPriority();
177
178 // create a new handler
179 mHandler = new Handler() {
180 @Override
181 public void handleMessage(Message msg) {
182 switch (msg.what) {
183 case SYNC:
184 synchronized (WebSettings.this) {
185 if (mBrowserFrame.mNativeFrame != 0) {
186 nativeSync(mBrowserFrame.mNativeFrame);
187 }
188 mSyncPending = false;
189 }
190 break;
191
192 case PRIORITY: {
193 setRenderPriority();
194 break;
195 }
196 }
197 }
198 };
199 }
200
201 private void setRenderPriority() {
202 synchronized (WebSettings.this) {
203 if (mRenderPriority == RenderPriority.NORMAL) {
204 android.os.Process.setThreadPriority(
205 android.os.Process.THREAD_PRIORITY_DEFAULT);
206 } else if (mRenderPriority == RenderPriority.HIGH) {
207 android.os.Process.setThreadPriority(
208 android.os.Process.THREAD_PRIORITY_FOREGROUND +
209 android.os.Process.THREAD_PRIORITY_LESS_FAVORABLE);
210 } else if (mRenderPriority == RenderPriority.LOW) {
211 android.os.Process.setThreadPriority(
212 android.os.Process.THREAD_PRIORITY_BACKGROUND);
213 }
214 }
215 }
216
217 /**
218 * Send a message to the private queue or handler.
219 */
220 private synchronized boolean sendMessage(Message msg) {
221 if (mHandler != null) {
222 mHandler.sendMessage(msg);
223 return true;
224 } else {
225 return false;
226 }
227 }
228 }
229
230 // User agent strings.
231 private static final String DESKTOP_USERAGENT =
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800232 "Mozilla/5.0 (Macintosh; U; Intel Mac OS X; en)"
233 + " AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2"
234 + " Safari/525.20.1";
235 private static final String IPHONE_USERAGENT =
236 "Mozilla/5.0 (iPhone; U; CPU iPhone 2_1 like Mac OS X; en)"
237 + " AppleWebKit/528.5+ (KHTML, like Gecko) Version/3.1.2"
238 + " Mobile/5F136 Safari/525.20.1";
239 private static Locale sLocale;
240 private static Object sLockForLocaleSettings;
241
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700242 /**
243 * Package constructor to prevent clients from creating a new settings
244 * instance.
245 */
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800246 WebSettings(Context context) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700247 mEventHandler = new EventHandler();
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800248 mContext = context;
249
250 if (sLockForLocaleSettings == null) {
251 sLockForLocaleSettings = new Object();
252 sLocale = Locale.getDefault();
253 }
254 mAcceptLanguage = getCurrentAcceptLanguage();
255 mUserAgent = getCurrentUserAgent();
256 mUseDefaultUserAgent = true;
257
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700258 mBlockNetworkLoads = mContext.checkPermission(
259 "android.permission.INTERNET", android.os.Process.myPid(),
260 android.os.Process.myUid()) != PackageManager.PERMISSION_GRANTED;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700261 }
262
263 /**
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800264 * Looks at sLocale and returns current AcceptLanguage String.
265 * @return Current AcceptLanguage String.
266 */
267 private String getCurrentAcceptLanguage() {
268 Locale locale;
269 synchronized(sLockForLocaleSettings) {
270 locale = sLocale;
271 }
272 StringBuffer buffer = new StringBuffer();
273 final String language = locale.getLanguage();
274 if (language != null) {
275 buffer.append(language);
276 final String country = locale.getCountry();
277 if (country != null) {
278 buffer.append("-");
279 buffer.append(country);
280 }
281 }
282 if (!locale.equals(Locale.US)) {
283 buffer.append(", ");
284 java.util.Locale us = Locale.US;
285 if (us.getLanguage() != null) {
286 buffer.append(us.getLanguage());
287 final String country = us.getCountry();
288 if (country != null) {
289 buffer.append("-");
290 buffer.append(country);
291 }
292 }
293 }
294
295 return buffer.toString();
296 }
297
298 /**
299 * Looks at sLocale and mContext and returns current UserAgent String.
300 * @return Current UserAgent String.
301 */
302 private synchronized String getCurrentUserAgent() {
303 Locale locale;
304 synchronized(sLockForLocaleSettings) {
305 locale = sLocale;
306 }
307 StringBuffer buffer = new StringBuffer();
308 // Add version
309 final String version = Build.VERSION.RELEASE;
310 if (version.length() > 0) {
311 buffer.append(version);
312 } else {
313 // default to "1.0"
314 buffer.append("1.0");
315 }
316 buffer.append("; ");
317 final String language = locale.getLanguage();
318 if (language != null) {
319 buffer.append(language.toLowerCase());
320 final String country = locale.getCountry();
321 if (country != null) {
322 buffer.append("-");
323 buffer.append(country.toLowerCase());
324 }
325 } else {
326 // default to "en"
327 buffer.append("en");
328 }
329
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800330 final String model = Build.MODEL;
331 if (model.length() > 0) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800332 buffer.append("; ");
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800333 buffer.append(model);
334 }
335 final String id = Build.ID;
336 if (id.length() > 0) {
337 buffer.append(" Build/");
338 buffer.append(id);
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800339 }
340 final String base = mContext.getResources().getText(
341 com.android.internal.R.string.web_user_agent).toString();
342 return String.format(base, buffer);
343 }
344
345 /**
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700346 * Enables dumping the pages navigation cache to a text file.
347 */
348 public void setNavDump(boolean enabled) {
349 mNavDump = enabled;
350 }
351
352 /**
353 * Returns true if dumping the navigation cache is enabled.
354 */
355 public boolean getNavDump() {
356 return mNavDump;
357 }
358
359 /**
360 * Set whether the WebView supports zoom
361 */
362 public void setSupportZoom(boolean support) {
363 mSupportZoom = support;
364 }
365
366 /**
367 * Returns whether the WebView supports zoom
368 */
369 public boolean supportZoom() {
370 return mSupportZoom;
371 }
372
373 /**
The Android Open Source Project10592532009-03-18 17:39:46 -0700374 * Sets whether the zoom mechanism built into WebView is used.
375 */
376 public void setBuiltInZoomControls(boolean enabled) {
377 mBuiltInZoomControls = enabled;
378 }
379
380 /**
381 * Returns true if the zoom mechanism built into WebView is being used.
382 */
383 public boolean getBuiltInZoomControls() {
384 return mBuiltInZoomControls;
385 }
386
387 /**
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800388 * Enable or disable file access within WebView. File access is enabled by
389 * default.
390 */
391 public void setAllowFileAccess(boolean allow) {
392 mAllowFileAccess = allow;
393 }
394
395 /**
396 * Returns true if this WebView supports file access.
397 */
398 public boolean getAllowFileAccess() {
399 return mAllowFileAccess;
400 }
401
402 /**
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700403 * Store whether the WebView is saving form data.
404 */
405 public void setSaveFormData(boolean save) {
406 mSaveFormData = save;
407 }
408
409 /**
410 * Return whether the WebView is saving form data.
411 */
412 public boolean getSaveFormData() {
413 return mSaveFormData;
414 }
415
416 /**
417 * Store whether the WebView is saving password.
418 */
419 public void setSavePassword(boolean save) {
420 mSavePassword = save;
421 }
422
423 /**
424 * Return whether the WebView is saving password.
425 */
426 public boolean getSavePassword() {
427 return mSavePassword;
428 }
429
430 /**
431 * Set the text size of the page.
432 * @param t A TextSize value for increasing or decreasing the text.
433 * @see WebSettings.TextSize
434 */
435 public synchronized void setTextSize(TextSize t) {
The Android Open Source Project9066cfe2009-03-03 19:31:44 -0800436 if (WebView.mLogEvent && mTextSize != t ) {
437 Checkin.updateStats(mContext.getContentResolver(),
438 Checkin.Stats.Tag.BROWSER_TEXT_SIZE_CHANGE, 1, 0.0);
439 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700440 mTextSize = t;
441 postSync();
442 }
443
444 /**
445 * Get the text size of the page.
446 * @return A TextSize enum value describing the text size.
447 * @see WebSettings.TextSize
448 */
449 public synchronized TextSize getTextSize() {
450 return mTextSize;
451 }
452
453 /**
454 * Enables using light touches to make a selection and activate mouseovers.
455 */
456 public void setLightTouchEnabled(boolean enabled) {
457 mLightTouchEnabled = enabled;
458 }
459
460 /**
461 * Returns true if light touches are enabled.
462 */
463 public boolean getLightTouchEnabled() {
464 return mLightTouchEnabled;
465 }
466
467 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100468 * @deprecated This setting controlled a rendering optimization
469 * that is no longer present. Setting it now has no effect.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700470 */
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100471 @Deprecated
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700472 public synchronized void setUseDoubleTree(boolean use) {
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100473 return;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700474 }
475
476 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100477 * @deprecated This setting controlled a rendering optimization
478 * that is no longer present. Setting it now has no effect.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700479 */
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100480 @Deprecated
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700481 public synchronized boolean getUseDoubleTree() {
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100482 return false;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700483 }
484
485 /**
486 * Tell the WebView about user-agent string.
487 * @param ua 0 if the WebView should use an Android user-agent string,
488 * 1 if the WebView should use a desktop user-agent string.
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800489 *
490 * @deprecated Please use setUserAgentString instead.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700491 */
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800492 @Deprecated
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700493 public synchronized void setUserAgent(int ua) {
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800494 String uaString = null;
495 if (ua == 1) {
496 if (DESKTOP_USERAGENT.equals(mUserAgent)) {
497 return; // do nothing
498 } else {
499 uaString = DESKTOP_USERAGENT;
500 }
501 } else if (ua == 2) {
502 if (IPHONE_USERAGENT.equals(mUserAgent)) {
503 return; // do nothing
504 } else {
505 uaString = IPHONE_USERAGENT;
506 }
507 } else if (ua != 0) {
508 return; // do nothing
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700509 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800510 setUserAgentString(uaString);
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700511 }
512
513 /**
514 * Return user-agent as int
515 * @return int 0 if the WebView is using an Android user-agent string.
516 * 1 if the WebView is using a desktop user-agent string.
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800517 * -1 if the WebView is using user defined user-agent string.
518 *
519 * @deprecated Please use getUserAgentString instead.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700520 */
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800521 @Deprecated
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700522 public synchronized int getUserAgent() {
523 if (DESKTOP_USERAGENT.equals(mUserAgent)) {
524 return 1;
525 } else if (IPHONE_USERAGENT.equals(mUserAgent)) {
526 return 2;
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800527 } else if (mUseDefaultUserAgent) {
528 return 0;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700529 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800530 return -1;
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700531 }
532
533 /**
534 * Tell the WebView to use the wide viewport
535 */
536 public synchronized void setUseWideViewPort(boolean use) {
537 if (mUseWideViewport != use) {
538 mUseWideViewport = use;
539 postSync();
540 }
541 }
542
543 /**
544 * @return True if the WebView is using a wide viewport
545 */
546 public synchronized boolean getUseWideViewPort() {
547 return mUseWideViewport;
548 }
549
550 /**
551 * Tell the WebView whether it supports multiple windows. TRUE means
552 * that {@link WebChromeClient#onCreateWindow(WebView, boolean,
553 * boolean, Message)} is implemented by the host application.
554 */
555 public synchronized void setSupportMultipleWindows(boolean support) {
556 if (mSupportMultipleWindows != support) {
557 mSupportMultipleWindows = support;
558 postSync();
559 }
560 }
561
562 /**
563 * @return True if the WebView is supporting multiple windows. This means
564 * that {@link WebChromeClient#onCreateWindow(WebView, boolean,
565 * boolean, Message)} is implemented by the host application.
566 */
567 public synchronized boolean supportMultipleWindows() {
568 return mSupportMultipleWindows;
569 }
570
571 /**
572 * Set the underlying layout algorithm. This will cause a relayout of the
573 * WebView.
574 * @param l A LayoutAlgorithm enum specifying the algorithm to use.
575 * @see WebSettings.LayoutAlgorithm
576 */
577 public synchronized void setLayoutAlgorithm(LayoutAlgorithm l) {
578 // XXX: This will only be affective if libwebcore was built with
579 // ANDROID_LAYOUT defined.
580 if (mLayoutAlgorithm != l) {
581 mLayoutAlgorithm = l;
582 postSync();
583 }
584 }
585
586 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100587 * Return the current layout algorithm. The default is NARROW_COLUMNS.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700588 * @return LayoutAlgorithm enum value describing the layout algorithm
589 * being used.
590 * @see WebSettings.LayoutAlgorithm
591 */
592 public synchronized LayoutAlgorithm getLayoutAlgorithm() {
593 return mLayoutAlgorithm;
594 }
595
596 /**
597 * Set the standard font family name.
598 * @param font A font family name.
599 */
600 public synchronized void setStandardFontFamily(String font) {
601 if (font != null && !font.equals(mStandardFontFamily)) {
602 mStandardFontFamily = font;
603 postSync();
604 }
605 }
606
607 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100608 * Get the standard font family name. The default is "sans-serif".
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700609 * @return The standard font family name as a string.
610 */
611 public synchronized String getStandardFontFamily() {
612 return mStandardFontFamily;
613 }
614
615 /**
616 * Set the fixed font family name.
617 * @param font A font family name.
618 */
619 public synchronized void setFixedFontFamily(String font) {
620 if (font != null && !font.equals(mFixedFontFamily)) {
621 mFixedFontFamily = font;
622 postSync();
623 }
624 }
625
626 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100627 * Get the fixed font family name. The default is "monospace".
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700628 * @return The fixed font family name as a string.
629 */
630 public synchronized String getFixedFontFamily() {
631 return mFixedFontFamily;
632 }
633
634 /**
635 * Set the sans-serif font family name.
636 * @param font A font family name.
637 */
638 public synchronized void setSansSerifFontFamily(String font) {
639 if (font != null && !font.equals(mSansSerifFontFamily)) {
640 mSansSerifFontFamily = font;
641 postSync();
642 }
643 }
644
645 /**
646 * Get the sans-serif font family name.
647 * @return The sans-serif font family name as a string.
648 */
649 public synchronized String getSansSerifFontFamily() {
650 return mSansSerifFontFamily;
651 }
652
653 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100654 * Set the serif font family name. The default is "sans-serif".
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700655 * @param font A font family name.
656 */
657 public synchronized void setSerifFontFamily(String font) {
658 if (font != null && !font.equals(mSerifFontFamily)) {
659 mSerifFontFamily = font;
660 postSync();
661 }
662 }
663
664 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100665 * Get the serif font family name. The default is "serif".
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700666 * @return The serif font family name as a string.
667 */
668 public synchronized String getSerifFontFamily() {
669 return mSerifFontFamily;
670 }
671
672 /**
673 * Set the cursive font family name.
674 * @param font A font family name.
675 */
676 public synchronized void setCursiveFontFamily(String font) {
677 if (font != null && !font.equals(mCursiveFontFamily)) {
678 mCursiveFontFamily = font;
679 postSync();
680 }
681 }
682
683 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100684 * Get the cursive font family name. The default is "cursive".
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700685 * @return The cursive font family name as a string.
686 */
687 public synchronized String getCursiveFontFamily() {
688 return mCursiveFontFamily;
689 }
690
691 /**
692 * Set the fantasy font family name.
693 * @param font A font family name.
694 */
695 public synchronized void setFantasyFontFamily(String font) {
696 if (font != null && !font.equals(mFantasyFontFamily)) {
697 mFantasyFontFamily = font;
698 postSync();
699 }
700 }
701
702 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100703 * Get the fantasy font family name. The default is "fantasy".
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700704 * @return The fantasy font family name as a string.
705 */
706 public synchronized String getFantasyFontFamily() {
707 return mFantasyFontFamily;
708 }
709
710 /**
711 * Set the minimum font size.
712 * @param size A non-negative integer between 1 and 72.
713 * Any number outside the specified range will be pinned.
714 */
715 public synchronized void setMinimumFontSize(int size) {
716 size = pin(size);
717 if (mMinimumFontSize != size) {
718 mMinimumFontSize = size;
719 postSync();
720 }
721 }
722
723 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100724 * Get the minimum font size. The default is 8.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700725 * @return A non-negative integer between 1 and 72.
726 */
727 public synchronized int getMinimumFontSize() {
728 return mMinimumFontSize;
729 }
730
731 /**
732 * Set the minimum logical font size.
733 * @param size A non-negative integer between 1 and 72.
734 * Any number outside the specified range will be pinned.
735 */
736 public synchronized void setMinimumLogicalFontSize(int size) {
737 size = pin(size);
738 if (mMinimumLogicalFontSize != size) {
739 mMinimumLogicalFontSize = size;
740 postSync();
741 }
742 }
743
744 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100745 * Get the minimum logical font size. The default is 8.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700746 * @return A non-negative integer between 1 and 72.
747 */
748 public synchronized int getMinimumLogicalFontSize() {
749 return mMinimumLogicalFontSize;
750 }
751
752 /**
753 * Set the default font size.
754 * @param size A non-negative integer between 1 and 72.
755 * Any number outside the specified range will be pinned.
756 */
757 public synchronized void setDefaultFontSize(int size) {
758 size = pin(size);
759 if (mDefaultFontSize != size) {
760 mDefaultFontSize = size;
761 postSync();
762 }
763 }
764
765 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100766 * Get the default font size. The default is 16.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700767 * @return A non-negative integer between 1 and 72.
768 */
769 public synchronized int getDefaultFontSize() {
770 return mDefaultFontSize;
771 }
772
773 /**
774 * Set the default fixed font size.
775 * @param size A non-negative integer between 1 and 72.
776 * Any number outside the specified range will be pinned.
777 */
778 public synchronized void setDefaultFixedFontSize(int size) {
779 size = pin(size);
780 if (mDefaultFixedFontSize != size) {
781 mDefaultFixedFontSize = size;
782 postSync();
783 }
784 }
785
786 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100787 * Get the default fixed font size. The default is 16.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700788 * @return A non-negative integer between 1 and 72.
789 */
790 public synchronized int getDefaultFixedFontSize() {
791 return mDefaultFixedFontSize;
792 }
793
794 /**
795 * Tell the WebView to load image resources automatically.
796 * @param flag True if the WebView should load images automatically.
797 */
798 public synchronized void setLoadsImagesAutomatically(boolean flag) {
799 if (mLoadsImagesAutomatically != flag) {
800 mLoadsImagesAutomatically = flag;
801 postSync();
802 }
803 }
804
805 /**
806 * Return true if the WebView will load image resources automatically.
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100807 * The default is true.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700808 * @return True if the WebView loads images automatically.
809 */
810 public synchronized boolean getLoadsImagesAutomatically() {
811 return mLoadsImagesAutomatically;
812 }
813
814 /**
815 * Tell the WebView to block network image. This is only checked when
816 * getLoadsImagesAutomatically() is true.
817 * @param flag True if the WebView should block network image
818 */
819 public synchronized void setBlockNetworkImage(boolean flag) {
820 if (mBlockNetworkImage != flag) {
821 mBlockNetworkImage = flag;
822 postSync();
823 }
824 }
825
826 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100827 * Return true if the WebView will block network image. The default is false.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700828 * @return True if the WebView blocks network image.
829 */
830 public synchronized boolean getBlockNetworkImage() {
831 return mBlockNetworkImage;
832 }
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100833
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800834 /**
835 * @hide
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100836 * Tell the WebView to block all network load requests.
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800837 * @param flag True if the WebView should block all network loads
838 */
839 public synchronized void setBlockNetworkLoads(boolean flag) {
840 if (mBlockNetworkLoads != flag) {
841 mBlockNetworkLoads = flag;
842 verifyNetworkAccess();
843 }
844 }
845
846 /**
847 * @hide
848 * Return true if the WebView will block all network loads.
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100849 * The default is false.
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800850 * @return True if the WebView blocks all network loads.
851 */
852 public synchronized boolean getBlockNetworkLoads() {
853 return mBlockNetworkLoads;
854 }
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100855
856
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800857 private void verifyNetworkAccess() {
858 if (!mBlockNetworkLoads) {
859 if (mContext.checkPermission("android.permission.INTERNET",
The Android Open Source Projectc39a6e02009-03-11 12:11:56 -0700860 android.os.Process.myPid(), android.os.Process.myUid()) !=
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800861 PackageManager.PERMISSION_GRANTED) {
862 throw new SecurityException
863 ("Permission denied - " +
864 "application missing INTERNET permission");
865 }
866 }
867 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700868
869 /**
870 * Tell the WebView to enable javascript execution.
871 * @param flag True if the WebView should execute javascript.
872 */
873 public synchronized void setJavaScriptEnabled(boolean flag) {
874 if (mJavaScriptEnabled != flag) {
875 mJavaScriptEnabled = flag;
876 postSync();
877 }
878 }
879
880 /**
881 * Tell the WebView to enable plugins.
882 * @param flag True if the WebView should load plugins.
883 */
884 public synchronized void setPluginsEnabled(boolean flag) {
885 if (mPluginsEnabled != flag) {
886 mPluginsEnabled = flag;
887 postSync();
888 }
889 }
890
891 /**
892 * Set a custom path to plugins used by the WebView. The client
893 * must ensure it exists before this call.
894 * @param pluginsPath String path to the directory containing plugins.
895 */
896 public synchronized void setPluginsPath(String pluginsPath) {
897 if (pluginsPath != null && !pluginsPath.equals(mPluginsPath)) {
898 mPluginsPath = pluginsPath;
899 postSync();
900 }
901 }
902
903 /**
Andrei Popescu60a9a7d2009-04-17 10:43:42 +0100904 * Tell the WebView to enable Application Caches API.
905 * @param flag True if the WebView should enable Application Caches.
906 * @hide pending api council approval
907 */
908 public synchronized void setAppCacheEnabled(boolean flag) {
909 if (mAppCacheEnabled != flag) {
910 mAppCacheEnabled = flag;
911 postSync();
912 }
913 }
914
915 /**
916 * Set a custom path to the Application Caches files. The client
917 * must ensure it exists before this call.
918 * @param appCachePath String path to the directory containing Application
919 * Caches files. The appCache path can be the empty string but should not
920 * be null. Passing null for this parameter will result in a no-op.
921 * @hide pending api council approval
922 */
923 public synchronized void setAppCachePath(String appCachePath) {
924 if (appCachePath != null && !appCachePath.equals(mAppCachePath)) {
925 mAppCachePath = appCachePath;
926 postSync();
927 }
928 }
929
930 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100931 * Return true if javascript is enabled. <b>Note: The default is false.</b>
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700932 * @return True if javascript is enabled.
933 */
934 public synchronized boolean getJavaScriptEnabled() {
935 return mJavaScriptEnabled;
936 }
937
938 /**
939 * Return true if plugins are enabled.
940 * @return True if plugins are enabled.
941 */
942 public synchronized boolean getPluginsEnabled() {
943 return mPluginsEnabled;
944 }
945
946 /**
947 * Return the current path used for plugins in the WebView.
948 * @return The string path to the WebView plugins.
949 */
950 public synchronized String getPluginsPath() {
951 return mPluginsPath;
952 }
953
954 /**
955 * Tell javascript to open windows automatically. This applies to the
956 * javascript function window.open().
957 * @param flag True if javascript can open windows automatically.
958 */
959 public synchronized void setJavaScriptCanOpenWindowsAutomatically(
960 boolean flag) {
961 if (mJavaScriptCanOpenWindowsAutomatically != flag) {
962 mJavaScriptCanOpenWindowsAutomatically = flag;
963 postSync();
964 }
965 }
966
967 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100968 * Return true if javascript can open windows automatically. The default
969 * is false.
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700970 * @return True if javascript can open windows automatically during
971 * window.open().
972 */
973 public synchronized boolean getJavaScriptCanOpenWindowsAutomatically() {
974 return mJavaScriptCanOpenWindowsAutomatically;
975 }
976
977 /**
978 * Set the default text encoding name to use when decoding html pages.
979 * @param encoding The text encoding name.
980 */
981 public synchronized void setDefaultTextEncodingName(String encoding) {
982 if (encoding != null && !encoding.equals(mDefaultTextEncoding)) {
983 mDefaultTextEncoding = encoding;
984 postSync();
985 }
986 }
987
988 /**
Mike Hearnadcd2ed2009-01-21 16:44:36 +0100989 * Get the default text encoding name. The default is "Latin-1".
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -0700990 * @return The default text encoding name as a string.
991 */
992 public synchronized String getDefaultTextEncodingName() {
993 return mDefaultTextEncoding;
994 }
995
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -0800996 /**
997 * Set the WebView's user-agent string. If the string "ua" is null or empty,
998 * it will use the system default user-agent string.
999 */
1000 public synchronized void setUserAgentString(String ua) {
1001 if (ua == null || ua.length() == 0) {
1002 synchronized(sLockForLocaleSettings) {
1003 Locale currentLocale = Locale.getDefault();
1004 if (!sLocale.equals(currentLocale)) {
1005 sLocale = currentLocale;
1006 mAcceptLanguage = getCurrentAcceptLanguage();
1007 }
1008 }
1009 ua = getCurrentUserAgent();
1010 mUseDefaultUserAgent = true;
1011 } else {
1012 mUseDefaultUserAgent = false;
1013 }
1014
1015 if (!ua.equals(mUserAgent)) {
1016 mUserAgent = ua;
1017 postSync();
1018 }
1019 }
1020
1021 /**
1022 * Return the WebView's user-agent string.
1023 */
1024 public synchronized String getUserAgentString() {
1025 if (DESKTOP_USERAGENT.equals(mUserAgent) ||
1026 IPHONE_USERAGENT.equals(mUserAgent) ||
1027 !mUseDefaultUserAgent) {
1028 return mUserAgent;
1029 }
1030
1031 boolean doPostSync = false;
1032 synchronized(sLockForLocaleSettings) {
1033 Locale currentLocale = Locale.getDefault();
1034 if (!sLocale.equals(currentLocale)) {
1035 sLocale = currentLocale;
1036 mUserAgent = getCurrentUserAgent();
1037 mAcceptLanguage = getCurrentAcceptLanguage();
1038 doPostSync = true;
1039 }
1040 }
1041 if (doPostSync) {
1042 postSync();
1043 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001044 return mUserAgent;
1045 }
1046
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08001047 /* package api to grab the Accept Language string. */
1048 /*package*/ synchronized String getAcceptLanguage() {
1049 synchronized(sLockForLocaleSettings) {
1050 Locale currentLocale = Locale.getDefault();
1051 if (!sLocale.equals(currentLocale)) {
1052 sLocale = currentLocale;
1053 mAcceptLanguage = getCurrentAcceptLanguage();
1054 }
1055 }
1056 return mAcceptLanguage;
1057 }
1058
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001059 /**
1060 * Tell the WebView whether it needs to set a node to have focus when
1061 * {@link WebView#requestFocus(int, android.graphics.Rect)} is called.
1062 *
1063 * @param flag
1064 */
1065 public void setNeedInitialFocus(boolean flag) {
1066 if (mNeedInitialFocus != flag) {
1067 mNeedInitialFocus = flag;
1068 }
1069 }
1070
1071 /* Package api to get the choice whether it needs to set initial focus. */
1072 /* package */ boolean getNeedInitialFocus() {
1073 return mNeedInitialFocus;
1074 }
1075
1076 /**
1077 * Set the priority of the Render thread. Unlike the other settings, this
Mike Hearnadcd2ed2009-01-21 16:44:36 +01001078 * one only needs to be called once per process. The default is NORMAL.
1079 *
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001080 * @param priority RenderPriority, can be normal, high or low.
1081 */
1082 public synchronized void setRenderPriority(RenderPriority priority) {
1083 if (mRenderPriority != priority) {
1084 mRenderPriority = priority;
1085 mEventHandler.sendMessage(Message.obtain(null,
1086 EventHandler.PRIORITY));
1087 }
1088 }
1089
1090 /**
1091 * Override the way the cache is used. The way the cache is used is based
1092 * on the navigation option. For a normal page load, the cache is checked
1093 * and content is re-validated as needed. When navigating back, content is
1094 * not revalidated, instead the content is just pulled from the cache.
1095 * This function allows the client to override this behavior.
1096 * @param mode One of the LOAD_ values.
1097 */
1098 public void setCacheMode(int mode) {
1099 if (mode != mOverrideCacheMode) {
1100 mOverrideCacheMode = mode;
1101 }
1102 }
1103
1104 /**
1105 * Return the current setting for overriding the cache mode. For a full
1106 * description, see the {@link #setCacheMode(int)} function.
1107 */
1108 public int getCacheMode() {
1109 return mOverrideCacheMode;
1110 }
The Android Open Source Projectf013e1a2008-12-17 18:05:43 -08001111
1112 /**
1113 * If set, webkit alternately shrinks and expands images viewed outside
1114 * of an HTML page to fit the screen. This conflicts with attempts by
1115 * the UI to zoom in and out of an image, so it is set false by default.
1116 * @param shrink Set true to let webkit shrink the standalone image to fit.
1117 * {@hide}
1118 */
1119 public void setShrinksStandaloneImagesToFit(boolean shrink) {
1120 if (mShrinksStandaloneImagesToFit != shrink) {
1121 mShrinksStandaloneImagesToFit = shrink;
1122 postSync();
1123 }
1124 }
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001125
1126 /**
1127 * Transfer messages from the queue to the new WebCoreThread. Called from
1128 * WebCore thread.
1129 */
1130 /*package*/
1131 synchronized void syncSettingsAndCreateHandler(BrowserFrame frame) {
1132 mBrowserFrame = frame;
Dave Bort42bc2ff2009-04-13 15:07:51 -07001133 if (WebView.DEBUG) {
The Android Open Source Project54b6cfa2008-10-21 07:00:00 -07001134 junit.framework.Assert.assertTrue(frame.mNativeFrame != 0);
1135 }
1136 nativeSync(frame.mNativeFrame);
1137 mSyncPending = false;
1138 mEventHandler.createHandler();
1139 }
1140
1141 private int pin(int size) {
1142 // FIXME: 72 is just an arbitrary max text size value.
1143 if (size < 1) {
1144 return 1;
1145 } else if (size > 72) {
1146 return 72;
1147 }
1148 return size;
1149 }
1150
1151 /* Post a SYNC message to handle syncing the native settings. */
1152 private synchronized void postSync() {
1153 // Only post if a sync is not pending
1154 if (!mSyncPending) {
1155 mSyncPending = mEventHandler.sendMessage(
1156 Message.obtain(null, EventHandler.SYNC));
1157 }
1158 }
1159
1160 // Synchronize the native and java settings.
1161 private native void nativeSync(int nativeFrame);
1162}