From 6695525f1bf4d7edf683243eb40b48f41fb767d9 Mon Sep 17 00:00:00 2001 From: "guofei.xue" Date: Tue, 14 Jul 2020 19:24:27 +0800 Subject: Use the try catch to deal with the StaleDataException. When test the monkey, the mCursor is not null and the mCursor is Closed. So that the mCursor cannot call the method of moveToPosition. Otherwise it will lead to StaleDataException. In order to prevent the android.database.StaleDataException and IllegalStateException, use the try catch to deal with the StaleDataException and IllegalStateException. Test:Perform the monkey test, and See if this android.database.StaleDataException will happen again when call the method of android.media.RingtoneManager#getRingtoneUri. Bug: 169292273 Change-Id: I577c41c6a4b0025ce562a2dc495a6f28b103cfaa --- media/java/android/media/RingtoneManager.java | 8 +++++++- 1 file changed, 7 insertions(+), 1 deletion(-) diff --git a/media/java/android/media/RingtoneManager.java b/media/java/android/media/RingtoneManager.java index c75296c73a90..9deeb8fbab16 100644 --- a/media/java/android/media/RingtoneManager.java +++ b/media/java/android/media/RingtoneManager.java @@ -34,6 +34,7 @@ import android.content.pm.PackageManager.NameNotFoundException; import android.content.pm.UserInfo; import android.content.res.AssetFileDescriptor; import android.database.Cursor; +import android.database.StaleDataException; import android.net.Uri; import android.os.Environment; import android.os.FileUtils; @@ -492,7 +493,12 @@ public class RingtoneManager { public Uri getRingtoneUri(int position) { // use cursor directly instead of requerying it, which could easily // cause position to shuffle. - if (mCursor == null || !mCursor.moveToPosition(position)) { + try { + if (mCursor == null || !mCursor.moveToPosition(position)) { + return null; + } + } catch (StaleDataException | IllegalStateException e) { + Log.e(TAG, "Unexpected Exception has been catched.", e); return null; } -- cgit v1.2.3-59-g8ed1b