Skip to content

Commit 9af67e8

Browse files
George MountAndroid (Google) Code Review
authored andcommitted
Merge "Add getDefaultUserAgent to WebSettings." into jb-mr1-dev
2 parents df8ef4b + 9f410c5 commit 9af67e8

File tree

6 files changed

+49
-3
lines changed

6 files changed

+49
-3
lines changed

api/current.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26947,6 +26947,7 @@ package android.webkit {
2694726947
method public synchronized boolean getUseWideViewPort();
2694826948
method public deprecated synchronized int getUserAgent();
2694926949
method public synchronized java.lang.String getUserAgentString();
26950+
method public static java.lang.String getDefaultUserAgent(android.content.Context);
2695026951
method public void setAllowContentAccess(boolean);
2695126952
method public void setAllowFileAccess(boolean);
2695226953
method public abstract void setAllowFileAccessFromFileURLs(boolean);

core/java/android/webkit/WebSettings.java

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616

1717
package android.webkit;
1818

19+
import android.content.Context;
1920
import android.os.Message;
2021
import android.os.Build;
2122

@@ -1225,6 +1226,18 @@ public synchronized String getUserAgentString() {
12251226
throw new MustOverrideException();
12261227
}
12271228

1229+
/**
1230+
* Returns the default User-Agent used by a WebView.
1231+
* An instance of WebView could use a different User-Agent if a call
1232+
* is made to {@link WebSettings#setUserAgent(int)} or
1233+
* {@link WebSettings#setUserAgentString(String)}.
1234+
*
1235+
* @param context a Context object used to access application assets
1236+
*/
1237+
public static String getDefaultUserAgent(Context context) {
1238+
return WebView.getFactory().getDefaultUserAgent(context);
1239+
}
1240+
12281241
/**
12291242
* Tells the WebView whether it needs to set a node to have focus when
12301243
* {@link WebView#requestFocus(int, android.graphics.Rect)} is called. The

core/java/android/webkit/WebSettingsClassic.java

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -374,6 +374,21 @@ private synchronized String getCurrentUserAgent() {
374374
synchronized(sLockForLocaleSettings) {
375375
locale = sLocale;
376376
}
377+
return getDefaultUserAgentForLocale(mContext, locale);
378+
}
379+
380+
/**
381+
* Returns the default User-Agent used by a WebView.
382+
* An instance of WebView could use a different User-Agent if a call
383+
* is made to {@link WebSettings#setUserAgent(int)} or
384+
* {@link WebSettings#setUserAgentString(String)}.
385+
*
386+
* @param context a Context object used to access application assets
387+
* @param locale The Locale to use in the User-Agent string.
388+
* @see WebViewFactoryProvider#getDefaultUserAgent(Context)
389+
* @see WebView#getDefaultUserAgent(Context)
390+
*/
391+
public static String getDefaultUserAgentForLocale(Context context, Locale locale) {
377392
StringBuffer buffer = new StringBuffer();
378393
// Add version
379394
final String version = Build.VERSION.RELEASE;
@@ -417,9 +432,9 @@ private synchronized String getCurrentUserAgent() {
417432
buffer.append(" Build/");
418433
buffer.append(id);
419434
}
420-
String mobile = mContext.getResources().getText(
435+
String mobile = context.getResources().getText(
421436
com.android.internal.R.string.web_user_agent_target_content).toString();
422-
final String base = mContext.getResources().getText(
437+
final String base = context.getResources().getText(
423438
com.android.internal.R.string.web_user_agent).toString();
424439
return String.format(base, buffer, mobile);
425440
}

core/java/android/webkit/WebView.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1821,7 +1821,7 @@ private void ensureProviderCreated() {
18211821
}
18221822
}
18231823

1824-
private static synchronized WebViewFactoryProvider getFactory() {
1824+
static synchronized WebViewFactoryProvider getFactory() {
18251825
// For now the main purpose of this function (and the factory abstration) is to keep
18261826
// us honest and minimize usage of WebViewClassic internals when binding the proxy.
18271827
checkThread();

core/java/android/webkit/WebViewClassic.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,7 @@
132132
import java.util.HashMap;
133133
import java.util.HashSet;
134134
import java.util.List;
135+
import java.util.Locale;
135136
import java.util.Map;
136137
import java.util.Set;
137138
import java.util.Vector;
@@ -1308,6 +1309,12 @@ public WebStorage getWebStorage() {
13081309
public WebViewDatabase getWebViewDatabase(Context context) {
13091310
return WebViewDatabaseClassic.getInstance(context);
13101311
}
1312+
1313+
@Override
1314+
public String getDefaultUserAgent(Context context) {
1315+
return WebSettingsClassic.getDefaultUserAgentForLocale(context,
1316+
Locale.getDefault());
1317+
}
13111318
}
13121319

13131320
private void onHandleUiEvent(MotionEvent event, int eventType, int flags) {

core/java/android/webkit/WebViewFactoryProvider.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,4 +93,14 @@ interface Statics {
9393
* @return the singleton WebViewDatabase instance
9494
*/
9595
WebViewDatabase getWebViewDatabase(Context context);
96+
97+
/**
98+
* Returns the default User-Agent used by a WebView.
99+
* An instance of WebView could use a different User-Agent if a call
100+
* is made to {@link WebSettings#setUserAgent(int)} or
101+
* {@link WebSettings#setUserAgentString(String)}.
102+
*
103+
* @param context a Context object used to access application assets
104+
*/
105+
String getDefaultUserAgent(Context context);
96106
}

0 commit comments

Comments
 (0)