diff options
Diffstat (limited to 'core/java')
-rw-r--r-- | core/java/android/app/ContextImpl.java | 17 | ||||
-rw-r--r-- | core/java/android/content/ContentResolver.java | 4 | ||||
-rw-r--r-- | core/java/android/content/Context.java | 13 | ||||
-rw-r--r-- | core/java/android/content/EventLogTags.logtags | 6 | ||||
-rw-r--r-- | core/java/android/net/IThrottleManager.aidl | 40 | ||||
-rw-r--r-- | core/java/android/net/ThrottleManager.java | 206 | ||||
-rw-r--r-- | core/java/android/net/TrafficStats.java | 170 | ||||
-rw-r--r-- | core/java/android/os/RecoverySystem.java | 8 | ||||
-rw-r--r-- | core/java/android/provider/Settings.java | 45 | ||||
-rw-r--r-- | core/java/android/server/BluetoothA2dpService.java | 2 | ||||
-rw-r--r-- | core/java/android/webkit/HTML5VideoViewProxy.java | 5 | ||||
-rw-r--r-- | core/java/android/webkit/WebView.java | 156 |
12 files changed, 444 insertions, 228 deletions
diff --git a/core/java/android/app/ContextImpl.java b/core/java/android/app/ContextImpl.java index 950f34f..9019af6 100644 --- a/core/java/android/app/ContextImpl.java +++ b/core/java/android/app/ContextImpl.java @@ -67,6 +67,8 @@ import android.location.LocationManager; import android.media.AudioManager; import android.net.ConnectivityManager; import android.net.IConnectivityManager; +import android.net.ThrottleManager; +import android.net.IThrottleManager; import android.net.Uri; import android.net.wifi.IWifiManager; import android.net.wifi.WifiManager; @@ -164,6 +166,7 @@ class ContextImpl extends Context { private static AlarmManager sAlarmManager; private static PowerManager sPowerManager; private static ConnectivityManager sConnectivityManager; + private static ThrottleManager sThrottleManager; private static WifiManager sWifiManager; private static LocationManager sLocationManager; private static final HashMap<File, SharedPreferencesImpl> sSharedPrefs = @@ -929,6 +932,8 @@ class ContextImpl extends Context { return getPowerManager(); } else if (CONNECTIVITY_SERVICE.equals(name)) { return getConnectivityManager(); + } else if (THROTTLE_SERVICE.equals(name)) { + return getThrottleManager(); } else if (WIFI_SERVICE.equals(name)) { return getWifiManager(); } else if (NOTIFICATION_SERVICE.equals(name)) { @@ -1028,6 +1033,18 @@ class ContextImpl extends Context { return sConnectivityManager; } + private ThrottleManager getThrottleManager() + { + synchronized (sSync) { + if (sThrottleManager == null) { + IBinder b = ServiceManager.getService(THROTTLE_SERVICE); + IThrottleManager service = IThrottleManager.Stub.asInterface(b); + sThrottleManager = new ThrottleManager(service); + } + } + return sThrottleManager; + } + private WifiManager getWifiManager() { synchronized (sSync) { diff --git a/core/java/android/content/ContentResolver.java b/core/java/android/content/ContentResolver.java index 1cb2353..d114ecc 100644 --- a/core/java/android/content/ContentResolver.java +++ b/core/java/android/content/ContentResolver.java @@ -1307,7 +1307,7 @@ public abstract class ContentResolver { String blockingPackage = ActivityThread.currentPackageName(); EventLog.writeEvent( - EventLogTags.CONTENT_QUERY_OPERATION, + EventLogTags.CONTENT_QUERY_SAMPLE, uri.toString(), projectionBuffer.toString(), selection != null ? selection : "", @@ -1329,7 +1329,7 @@ public abstract class ContentResolver { } String blockingPackage = ActivityThread.currentPackageName(); EventLog.writeEvent( - EventLogTags.CONTENT_UPDATE_OPERATION, + EventLogTags.CONTENT_UPDATE_SAMPLE, uri.toString(), operation, selection != null ? selection : "", diff --git a/core/java/android/content/Context.java b/core/java/android/content/Context.java index 3a2aa55..30822d4 100644 --- a/core/java/android/content/Context.java +++ b/core/java/android/content/Context.java @@ -1398,7 +1398,7 @@ public abstract class Context { * @see android.os.Vibrator */ public static final String VIBRATOR_SERVICE = "vibrator"; - + /** * Use with {@link #getSystemService} to retrieve a {@link * android.app.StatusBarManager} for interacting with the status bar. @@ -1421,6 +1421,17 @@ public abstract class Context { /** * Use with {@link #getSystemService} to retrieve a {@link + * android.net.ThrottleManager} for handling management of + * throttling. + * + * @hide + * @see #getSystemService + * @see android.net.ThrottleManager + */ + public static final String THROTTLE_SERVICE = "throttle"; + + /** + * Use with {@link #getSystemService} to retrieve a {@link * android.net.NetworkManagementService} for handling management of * system network services * diff --git a/core/java/android/content/EventLogTags.logtags b/core/java/android/content/EventLogTags.logtags index 0a8c9b0..21ea90a 100644 --- a/core/java/android/content/EventLogTags.logtags +++ b/core/java/android/content/EventLogTags.logtags @@ -2,6 +2,6 @@ option java_package android.content; -52002 content_query_operation (uri|3),(projection|3),(selection|3),(sortorder|3),(time|1|3),(blocking_package|3),(sample_percent|1|6) -52003 content_update_operation (uri|3),(operation|3),(selection|3),(time|1|3),(blocking_package|3),(sample_percent|1|6) -52004 binder_operation (descriptor|3),(method_num|1|5),(time|1|3),(blocking_package|3),(sample_percent|1|6) +52002 content_query_sample (uri|3),(projection|3),(selection|3),(sortorder|3),(time|1|3),(blocking_package|3),(sample_percent|1|6) +52003 content_update_sample (uri|3),(operation|3),(selection|3),(time|1|3),(blocking_package|3),(sample_percent|1|6) +52004 binder_sample (descriptor|3),(method_num|1|5),(time|1|3),(blocking_package|3),(sample_percent|1|6) diff --git a/core/java/android/net/IThrottleManager.aidl b/core/java/android/net/IThrottleManager.aidl new file mode 100644 index 0000000..a12469d --- /dev/null +++ b/core/java/android/net/IThrottleManager.aidl @@ -0,0 +1,40 @@ +/** + * Copyright (c) 2010, The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net; + +import android.os.IBinder; + +/** + * Interface that answers queries about data transfer amounts and throttling + */ +/** {@hide} */ +interface IThrottleManager +{ + long getByteCount(String iface, int dir, int period, int ago); + + int getThrottle(String iface); + + long getResetTime(String iface); + + long getPeriodStartTime(String iface); + + long getCliffThreshold(String iface, int cliff); + + int getCliffLevel(String iface, int cliff); + + String getHelpUri(); +} diff --git a/core/java/android/net/ThrottleManager.java b/core/java/android/net/ThrottleManager.java new file mode 100644 index 0000000..79c2d6f --- /dev/null +++ b/core/java/android/net/ThrottleManager.java @@ -0,0 +1,206 @@ +/* + * Copyright (C) 2008 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package android.net; + +import android.annotation.SdkConstant; +import android.annotation.SdkConstant.SdkConstantType; +import android.os.Binder; +import android.os.RemoteException; + +/** + * Class that handles throttling. It provides read/write numbers per interface + * and methods to apply throttled rates. + * {@hide} + */ +public class ThrottleManager +{ + /** + * Broadcast each polling period to indicate new data counts. + * + * Includes four extras: + * EXTRA_CYCLE_READ - a long of the read bytecount for the current cycle + * EXTRA_CYCLE_WRITE -a long of the write bytecount for the current cycle + * EXTRA_CYLCE_START -a long of MS for the cycle start time + * EXTRA_CYCLE_END -a long of MS for the cycle stop time + * {@hide} + */ + public static final String THROTTLE_POLL_ACTION = "android.net.thrott.POLL_ACTION"; + /** + * The lookup key for a long for the read bytecount for this period. Retrieve with + * {@link android.content.Intent#getLongExtra(String)}. + * {@hide} + */ + public static final String EXTRA_CYCLE_READ = "cycleRead"; + /** + * contains a long of the number of bytes written in the cycle + * {@hide} + */ + public static final String EXTRA_CYCLE_WRITE = "cycleWrite"; + /** + * contains a long of the number of bytes read in the cycle + * {@hide} + */ + public static final String EXTRA_CYCLE_START = "cycleStart"; + /** + * contains a long of the ms since 1970 used to init a calendar, etc for the end + * of the cycle + * {@hide} + */ + public static final String EXTRA_CYCLE_END = "cycleEnd"; + + /** + * Broadcast when the thottle level changes. + * {@hide} + */ + public static final String THROTTLE_ACTION = "android.net.thrott.THROTTLE_ACTION"; + /** + * int of the current bandwidth in TODO + * {@hide} + */ + public static final String EXTRA_THROTTLE_LEVEL = "level"; + + // {@hide} + public static final int DIRECTION_TX = 0; + // {@hide} + public static final int DIRECTION_RX = 1; + + // {@hide} + public static final int PERIOD_CYCLE = 0; + // {@hide} + public static final int PERIOD_YEAR = 1; + // {@hide} + public static final int PERIOD_MONTH = 2; + // {@hide} + public static final int PERIOD_WEEK = 3; + // @hide + public static final int PERIOD_7DAY = 4; + // @hide + public static final int PERIOD_DAY = 5; + // @hide + public static final int PERIOD_24HOUR = 6; + // @hide + public static final int PERIOD_HOUR = 7; + // @hide + public static final int PERIOD_60MIN = 8; + // @hide + public static final int PERIOD_MINUTE = 9; + // @hide + public static final int PERIOD_60SEC = 10; + // @hide + public static final int PERIOD_SECOND = 11; + + /** + * returns a long of the ms from the epoch to the time the current cycle ends for the + * named interface + * {@hide} + */ + public long getResetTime(String iface) { + try { + return mService.getResetTime(iface); + } catch (RemoteException e) { + return -1; + } + } + + /** + * returns a long of the ms from the epoch to the time the current cycle started for the + * named interface + * {@hide} + */ + public long getPeriodStartTime(String iface) { + try { + return mService.getPeriodStartTime(iface); + } catch (RemoteException e) { + return -1; + } + } + + /** + * returns a long of the byte count either read or written on the named interface + * for the period described. Direction is either DIRECTION_RX or DIRECTION_TX and + * period may only be PERIOD_CYCLE for the current cycle (other periods may be supported + * in the future). Ago indicates the number of periods in the past to lookup - 0 means + * the current period, 1 is the last one, 2 was two periods ago.. + * {@hide} + */ + public long getByteCount(String iface, int direction, int period, int ago) { + try { + return mService.getByteCount(iface, direction, period, ago); + } catch (RemoteException e) { + return -1; + } + } + + /** + * returns the number of bytes read+written after which a particular cliff + * takes effect on the named iface. Currently only cliff #0 is supported (1 step) + * {@hide} + */ + public long getCliffThreshold(String iface, int cliff) { + try { + return mService.getCliffThreshold(iface, cliff); + } catch (RemoteException e) { + return -1; + } + } + + /** + * returns the thottling bandwidth (bps) for a given cliff # on the named iface. + * only cliff #0 is currently supported. + * {@hide} + */ + public int getCliffLevel(String iface, int cliff) { + try { + return mService.getCliffLevel(iface, cliff); + } catch (RemoteException e) { + return -1; + } + } + + /** + * returns the help URI for throttling + * {@hide} + */ + public String getHelpUri() { + try { + return mService.getHelpUri(); + } catch (RemoteException e) { + return null; + } + } + + + private IThrottleManager mService; + + /** + * Don't allow use of default constructor. + */ + @SuppressWarnings({"UnusedDeclaration"}) + private ThrottleManager() { + } + + /** + * {@hide} + */ + public ThrottleManager(IThrottleManager service) { + if (service == null) { + throw new IllegalArgumentException( + "ThrottleManager() cannot be constructed with null service"); + } + mService = service; + } +} diff --git a/core/java/android/net/TrafficStats.java b/core/java/android/net/TrafficStats.java index ad8e2bf..0d64dab 100644 --- a/core/java/android/net/TrafficStats.java +++ b/core/java/android/net/TrafficStats.java @@ -23,12 +23,12 @@ import java.io.RandomAccessFile; import java.io.IOException; /** - * Class that provides network traffic statistics. These statistics include bytes transmitted and - * received and network packets transmitted and received, over all interfaces, over the mobile - * interface, and on a per-UID basis. + * Class that provides network traffic statistics. These statistics include + * bytes transmitted and received and network packets transmitted and received, + * over all interfaces, over the mobile interface, and on a per-UID basis. * <p> - * These statistics may not be available on all platforms. If the statistics are not supported - * by this device, {@link #UNSUPPORTED} will be returned. + * These statistics may not be available on all platforms. If the statistics + * are not supported by this device, {@link #UNSUPPORTED} will be returned. */ public class TrafficStats { /** @@ -36,27 +36,13 @@ public class TrafficStats { */ public final static int UNSUPPORTED = -1; - // Logging tag. - private final static String TAG = "trafficstats"; - - // We pre-create all the File objects so we don't spend a lot of - // CPU at runtime converting from Java Strings to byte[] for the - // kernel calls. - private final static File[] MOBILE_TX_PACKETS = mobileFiles("tx_packets"); - private final static File[] MOBILE_RX_PACKETS = mobileFiles("rx_packets"); - private final static File[] MOBILE_TX_BYTES = mobileFiles("tx_bytes"); - private final static File[] MOBILE_RX_BYTES = mobileFiles("rx_bytes"); - private final static File SYS_CLASS_NET_DIR = new File("/sys/class/net"); - /** * Get the total number of packets transmitted through the mobile interface. * * @return number of packets. If the statistics are not supported by this device, * {@link #UNSUPPORTED} will be returned. */ - public static long getMobileTxPackets() { - return getMobileStat(MOBILE_TX_PACKETS); - } + public static native long getMobileTxPackets(); /** * Get the total number of packets received through the mobile interface. @@ -64,9 +50,7 @@ public class TrafficStats { * @return number of packets. If the statistics are not supported by this device, * {@link #UNSUPPORTED} will be returned. */ - public static long getMobileRxPackets() { - return getMobileStat(MOBILE_RX_PACKETS); - } + public static native long getMobileRxPackets(); /** * Get the total number of bytes transmitted through the mobile interface. @@ -74,9 +58,7 @@ public class TrafficStats { * @return number of bytes. If the statistics are not supported by this device, * {@link #UNSUPPORTED} will be returned. */ - public static long getMobileTxBytes() { - return getMobileStat(MOBILE_TX_BYTES); - } + public static native long getMobileTxBytes(); /** * Get the total number of bytes received through the mobile interface. @@ -84,9 +66,7 @@ public class TrafficStats { * @return number of bytes. If the statistics are not supported by this device, * {@link #UNSUPPORTED} will be returned. */ - public static long getMobileRxBytes() { - return getMobileStat(MOBILE_RX_BYTES); - } + public static native long getMobileRxBytes(); /** * Get the total number of packets sent through all network interfaces. @@ -94,9 +74,7 @@ public class TrafficStats { * @return the number of packets. If the statistics are not supported by this device, * {@link #UNSUPPORTED} will be returned. */ - public static long getTotalTxPackets() { - return getTotalStat("tx_packets"); - } + public static native long getTotalTxPackets(); /** * Get the total number of packets received through all network interfaces. @@ -104,9 +82,7 @@ public class TrafficStats { * @return number of packets. If the statistics are not supported by this device, * {@link #UNSUPPORTED} will be returned. */ - public static long getTotalRxPackets() { - return getTotalStat("rx_packets"); - } + public static native long getTotalRxPackets(); /** * Get the total number of bytes sent through all network interfaces. @@ -114,9 +90,7 @@ public class TrafficStats { * @return number of bytes. If the statistics are not supported by this device, * {@link #UNSUPPORTED} will be returned. */ - public static long getTotalTxBytes() { - return getTotalStat("tx_bytes"); - } + public static native long getTotalTxBytes(); /** * Get the total number of bytes received through all network interfaces. @@ -124,9 +98,7 @@ public class TrafficStats { * @return number of bytes. If the statistics are not supported by this device, * {@link #UNSUPPORTED} will be returned. */ - public static long getTotalRxBytes() { - return getTotalStat("rx_bytes"); - } + public static native long getTotalRxBytes(); /** * Get the number of bytes sent through the network for this UID. @@ -138,9 +110,7 @@ public class TrafficStats { * @return number of bytes. If the statistics are not supported by this device, * {@link #UNSUPPORTED} will be returned. */ - public static long getUidTxBytes(int uid) { - return getNumberFromFilePath("/proc/uid_stat/" + uid + "/tcp_snd"); - } + public static native long getUidTxBytes(int uid); /** * Get the number of bytes received through the network for this UID. @@ -151,115 +121,5 @@ public class TrafficStats { * @param uid The UID of the process to examine. * @return number of bytes */ - public static long getUidRxBytes(int uid) { - return getNumberFromFilePath("/proc/uid_stat/" + uid + "/tcp_rcv"); - } - - /** - * Returns the array of two possible File locations for a given - * statistic. - */ - private static File[] mobileFiles(String whatStat) { - // Note that we stat them at runtime to see which is - // available, rather than here, to guard against the files - // coming & going later as modules shut down (e.g. airplane - // mode) and whatnot. The runtime stat() isn't expensive compared - // to the previous charset conversion that happened before we - // were reusing File instances. - File[] files = new File[2]; - files[0] = new File("/sys/class/net/rmnet0/statistics/" + whatStat); - files[1] = new File("/sys/class/net/ppp0/statistics/" + whatStat); - return files; - } - - private static long getTotalStat(String whatStat) { - File netdir = new File("/sys/class/net"); - - File[] nets = SYS_CLASS_NET_DIR.listFiles(); - if (nets == null) { - return UNSUPPORTED; - } - long total = 0; - StringBuffer strbuf = new StringBuffer(); - for (File net : nets) { - strbuf.append(net.getPath()).append(File.separator).append("statistics") - .append(File.separator).append(whatStat); - total += getNumberFromFilePath(strbuf.toString()); - strbuf.setLength(0); - } - return total; - } - - private static long getMobileStat(File[] files) { - for (int i = 0; i < files.length; i++) { - File file = files[i]; - if (!file.exists()) { - continue; - } - try { - RandomAccessFile raf = new RandomAccessFile(file, "r"); - return getNumberFromFile(raf, file.getAbsolutePath()); - } catch (IOException e) { - Log.w(TAG, - "Exception opening TCP statistics file " + file.getAbsolutePath(), - e); - } - } - return UNSUPPORTED; - } - - // File will have format <number><newline> - private static long getNumberFromFilePath(String filename) { - RandomAccessFile raf = getFile(filename); - if (raf == null) { - return UNSUPPORTED; - } - return getNumberFromFile(raf, filename); - } - - // Private buffer for getNumberFromFile. Safe for re-use because - // getNumberFromFile is synchronized. - private final static byte[] buf = new byte[16]; - - private static synchronized long getNumberFromFile(RandomAccessFile raf, String filename) { - try { - raf.read(buf); - raf.close(); - } catch (IOException e) { - Log.w(TAG, "Exception getting TCP bytes from " + filename, e); - return UNSUPPORTED; - } finally { - if (raf != null) { - try { - raf.close(); - } catch (IOException e) { - Log.w(TAG, "Exception closing " + filename, e); - } - } - } - - long num = 0L; - for (int i = 0; i < buf.length; i++) { - if (buf[i] < '0' || buf[i] > '9') { - break; - } - num *= 10; - num += buf[i] - '0'; - } - return num; - } - - private static RandomAccessFile getFile(String filename) { - File f = new File(filename); - if (!f.canRead()) { - return null; - } - - try { - return new RandomAccessFile(f, "r"); - } catch (IOException e) { - Log.w(TAG, "Exception opening TCP statistics file " + filename, e); - return null; - } - } + public static native long getUidRxBytes(int uid); } diff --git a/core/java/android/os/RecoverySystem.java b/core/java/android/os/RecoverySystem.java index b3ec114..b6dc1b5 100644 --- a/core/java/android/os/RecoverySystem.java +++ b/core/java/android/os/RecoverySystem.java @@ -304,9 +304,7 @@ public class RecoverySystem { /** * Reboots the device in order to install the given update * package. - * Requires the {@link android.Manifest.permission#REBOOT} - * and {@link android.Manifest.permission#ACCESS_CACHE_FILESYSTEM} - * permissions. + * Requires the {@link android.Manifest.permission#REBOOT} permission. * * @param context the Context to use * @param packageFile the update package to install. Currently @@ -337,9 +335,7 @@ public class RecoverySystem { * sometimes called a "factory reset", which is something of a * misnomer because the system partition is not restored to its * factory state. - * Requires the {@link android.Manifest.permission#REBOOT} - * and {@link android.Manifest.permission#ACCESS_CACHE_FILESYSTEM} - * permissions. + * Requires the {@link android.Manifest.permission#REBOOT} permission. * * @param context the Context to use * diff --git a/core/java/android/provider/Settings.java b/core/java/android/provider/Settings.java index c07ac31..e640005 100644 --- a/core/java/android/provider/Settings.java +++ b/core/java/android/provider/Settings.java @@ -3295,7 +3295,50 @@ public final class Settings { * @hide */ public static final String DEFAULT_INSTALL_LOCATION = "default_install_location"; - + + /** + * The bandwidth throttle polling freqency in seconds + * @hide + */ + public static final String THROTTLE_POLLING_SEC = "throttle_polling_sec"; + + /** + * The bandwidth throttle threshold (long) + * @hide + */ + public static final String THROTTLE_THRESHOLD = "throttle_threshold"; + + /** + * The bandwidth throttle value (kbps) + * @hide + */ + public static final String THROTTLE_VALUE = "throttle_value"; + + /** + * The bandwidth throttle reset calendar day (1-28) + * @hide + */ + public static final String THROTTLE_RESET_DAY = "throttle_reset_day"; + + /** + * The throttling notifications we should send + * @hide + */ + public static final String THROTTLE_NOTIFICATION_TYPE = "throttle_notification_type"; + + /** + * The interface we throttle + * @hide + */ + public static final String THROTTLE_IFACE = "throttle_iface"; + + /** + * Help URI for data throttling policy + * @hide + */ + public static final String THROTTLE_HELP_URI = "throttle_help_uri"; + + /** * @hide */ diff --git a/core/java/android/server/BluetoothA2dpService.java b/core/java/android/server/BluetoothA2dpService.java index 096ad39..08cd2f0 100644 --- a/core/java/android/server/BluetoothA2dpService.java +++ b/core/java/android/server/BluetoothA2dpService.java @@ -313,6 +313,8 @@ public class BluetoothA2dpService extends IBluetoothA2dp.Stub { "Need BLUETOOTH_ADMIN permission"); if (DBG) log("connectSink(" + device + ")"); + if (!mBluetoothService.isEnabled()) return false; + // ignore if there are any active sinks if (lookupSinksMatchingStates(new int[] { BluetoothA2dp.STATE_CONNECTING, diff --git a/core/java/android/webkit/HTML5VideoViewProxy.java b/core/java/android/webkit/HTML5VideoViewProxy.java index 42e1539..eee8025 100644 --- a/core/java/android/webkit/HTML5VideoViewProxy.java +++ b/core/java/android/webkit/HTML5VideoViewProxy.java @@ -129,9 +129,10 @@ class HTML5VideoViewProxy extends Handler // is invoked. mTimer.cancel(); mTimer = null; - mCurrentProxy.playbackEnded(); + if (mVideoView.isPlaying()) { + mVideoView.stopPlayback(); + } mCurrentProxy = null; - mVideoView.stopPlayback(); mLayout.removeView(mVideoView); mVideoView = null; if (mProgressView != null) { diff --git a/core/java/android/webkit/WebView.java b/core/java/android/webkit/WebView.java index 0526362..bbe52a3 100644 --- a/core/java/android/webkit/WebView.java +++ b/core/java/android/webkit/WebView.java @@ -475,6 +475,7 @@ public class WebView extends AbsoluteLayout private static final int MOTIONLESS_FALSE = 0; private static final int MOTIONLESS_PENDING = 1; private static final int MOTIONLESS_TRUE = 2; + private static final int MOTIONLESS_IGNORE = 3; private int mHeldMotionless; // whether support multi-touch @@ -1274,30 +1275,57 @@ public class WebView extends AbsoluteLayout * overwritten with this WebView's picture data. * @return True if the picture was successfully saved. */ - public boolean savePicture(Bundle b, File dest) { + public boolean savePicture(Bundle b, final File dest) { if (dest == null || b == null) { return false; } final Picture p = capturePicture(); - try { - final FileOutputStream out = new FileOutputStream(dest); - p.writeToStream(out); - out.close(); - // now update the bundle - b.putInt("scrollX", mScrollX); - b.putInt("scrollY", mScrollY); - b.putFloat("scale", mActualScale); - b.putFloat("textwrapScale", mTextWrapScale); - b.putBoolean("overview", mInZoomOverview); - return true; - } catch (FileNotFoundException e){ - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } catch (RuntimeException e) { - e.printStackTrace(); - } - return false; + // Use a temporary file while writing to ensure the destination file + // contains valid data. + final File temp = new File(dest.getPath() + ".writing"); + new Thread(new Runnable() { + public void run() { + try { + FileOutputStream out = new FileOutputStream(temp); + p.writeToStream(out); + out.close(); + // Writing the picture succeeded, rename the temporary file + // to the destination. + temp.renameTo(dest); + } catch (Exception e) { + // too late to do anything about it. + } finally { + temp.delete(); + } + } + }).start(); + // now update the bundle + b.putInt("scrollX", mScrollX); + b.putInt("scrollY", mScrollY); + b.putFloat("scale", mActualScale); + b.putFloat("textwrapScale", mTextWrapScale); + b.putBoolean("overview", mInZoomOverview); + return true; + } + + private void restoreHistoryPictureFields(Picture p, Bundle b) { + int sx = b.getInt("scrollX", 0); + int sy = b.getInt("scrollY", 0); + float scale = b.getFloat("scale", 1.0f); + mDrawHistory = true; + mHistoryPicture = p; + mScrollX = sx; + mScrollY = sy; + mHistoryWidth = Math.round(p.getWidth() * scale); + mHistoryHeight = Math.round(p.getHeight() * scale); + // as getWidth() / getHeight() of the view are not available yet, set up + // mActualScale, so that when onSizeChanged() is called, the rest will + // be set correctly + mActualScale = scale; + mInvActualScale = 1 / scale; + mTextWrapScale = b.getFloat("textwrapScale", scale); + mInZoomOverview = b.getBoolean("overview"); + invalidate(); } /** @@ -1311,42 +1339,35 @@ public class WebView extends AbsoluteLayout if (src == null || b == null) { return false; } - if (src.exists()) { - Picture p = null; - try { - final FileInputStream in = new FileInputStream(src); - p = Picture.createFromStream(in); - in.close(); - } catch (FileNotFoundException e){ - e.printStackTrace(); - } catch (RuntimeException e) { - e.printStackTrace(); - } catch (IOException e) { - e.printStackTrace(); - } - if (p != null) { - int sx = b.getInt("scrollX", 0); - int sy = b.getInt("scrollY", 0); - float scale = b.getFloat("scale", 1.0f); - mDrawHistory = true; - mHistoryPicture = p; - mScrollX = sx; - mScrollY = sy; - mHistoryWidth = Math.round(p.getWidth() * scale); - mHistoryHeight = Math.round(p.getHeight() * scale); - // as getWidth() / getHeight() of the view are not - // available yet, set up mActualScale, so that when - // onSizeChanged() is called, the rest will be set - // correctly - mActualScale = scale; - mInvActualScale = 1 / scale; - mTextWrapScale = b.getFloat("textwrapScale", scale); - mInZoomOverview = b.getBoolean("overview"); - invalidate(); - return true; - } + if (!src.exists()) { + return false; } - return false; + try { + final FileInputStream in = new FileInputStream(src); + final Bundle copy = new Bundle(b); + new Thread(new Runnable() { + public void run() { + final Picture p = Picture.createFromStream(in); + if (p != null) { + // Post a runnable on the main thread to update the + // history picture fields. + mPrivateHandler.post(new Runnable() { + public void run() { + restoreHistoryPictureFields(p, copy); + } + }); + } + try { + in.close(); + } catch (Exception e) { + // Nothing we can do now. + } + } + }).start(); + } catch (FileNotFoundException e){ + e.printStackTrace(); + } + return true; } /** @@ -3339,6 +3360,7 @@ public class WebView extends AbsoluteLayout if (null == mWebViewCore) return; // CallbackProxy may trigger this if (mDrawHistory && mWebViewCore.pictureReady()) { mDrawHistory = false; + mHistoryPicture = null; invalidate(); int oldScrollX = mScrollX; int oldScrollY = mScrollY; @@ -4882,6 +4904,9 @@ public class WebView extends AbsoluteLayout // we will not rewrite drag code here, but we // will try fling if it applies. WebViewCore.reducePriority(); + // to get better performance, pause updating the + // picture + WebViewCore.pauseUpdatePicture(mWebViewCore); // fall through to TOUCH_DRAG_MODE } else { break; @@ -4899,9 +4924,6 @@ public class WebView extends AbsoluteLayout case TOUCH_DRAG_MODE: mPrivateHandler.removeMessages(DRAG_HELD_MOTIONLESS); mPrivateHandler.removeMessages(AWAKEN_SCROLL_BARS); - mHeldMotionless = MOTIONLESS_TRUE; - // redraw in high-quality, as we're done dragging - invalidate(); // if the user waits a while w/o moving before the // up, we don't want to do a fling if (eventTime - mLastTouchTime <= MIN_FLING_TIME) { @@ -4913,11 +4935,24 @@ public class WebView extends AbsoluteLayout + mDeferTouchProcess); } mVelocityTracker.addMovement(ev); + // set to MOTIONLESS_IGNORE so that it won't keep + // removing and sending message in + // drawCoreAndCursorRing() + mHeldMotionless = MOTIONLESS_IGNORE; doFling(); break; } + // redraw in high-quality, as we're done dragging + mHeldMotionless = MOTIONLESS_TRUE; + invalidate(); + // fall through + case TOUCH_DRAG_START_MODE: + // TOUCH_DRAG_START_MODE should not happen for the real + // device as we almost certain will get a MOVE. But this + // is possible on emulator. mLastVelocity = 0; WebViewCore.resumePriority(); + WebViewCore.resumeUpdatePicture(mWebViewCore); break; } stopTouch(); @@ -4963,6 +4998,8 @@ public class WebView extends AbsoluteLayout private void startDrag() { WebViewCore.reducePriority(); + // to get better performance, pause updating the picture + WebViewCore.pauseUpdatePicture(mWebViewCore); if (!mDragFromTextInput) { nativeHideCursor(); } @@ -5026,6 +5063,7 @@ public class WebView extends AbsoluteLayout } if (mTouchMode == TOUCH_DRAG_MODE) { WebViewCore.resumePriority(); + WebViewCore.resumeUpdatePicture(mWebViewCore); } mPrivateHandler.removeMessages(SWITCH_TO_SHORTPRESS); mPrivateHandler.removeMessages(SWITCH_TO_LONGPRESS); @@ -5360,6 +5398,7 @@ public class WebView extends AbsoluteLayout } if ((maxX == 0 && vy == 0) || (maxY == 0 && vx == 0)) { WebViewCore.resumePriority(); + WebViewCore.resumeUpdatePicture(mWebViewCore); return; } float currentVelocity = mScroller.getCurrVelocity(); @@ -6369,6 +6408,7 @@ public class WebView extends AbsoluteLayout break; case RESUME_WEBCORE_PRIORITY: WebViewCore.resumePriority(); + WebViewCore.resumeUpdatePicture(mWebViewCore); break; case LONG_PRESS_CENTER: |