From 7eeb95b6f29a6f55e117e329468168d4bcbec0c8 Mon Sep 17 00:00:00 2001 From: Jack He Date: Tue, 6 Dec 2016 16:09:09 -0800 Subject: [PATCH] Remove disk IO read/write blocks in AdapterService for main thread * Call getSharedPreferences() asynchronously during onCreate() to cache preferences in memory as early as possible to prevent ANR * Change editor.commit() to editor.apply() in AdapterService for: setPhonebookAccessPermission setMessageAccessPermission setSimAccessPermission so that these methods will return true when editor.apply() is called. After that call, the preference will be updated in memory so that following calls that use such preferences will see the change. However, there is a possibility that write to persistent storage will fail in the background which reset the preference after reboot or service restart. In this case, we need to call these method again. * Same changes in Avrcp for: blackListCurrentDevice resetBlackList In these methods, the return values for editor.commit() are already ignored and therefore no user-visible effect. Bug: 33382877 Test: mannual test, connect to carkit and browse phone book Change-Id: I605dc00db4647f06c9fa797f272188a44f0e5724 --- src/com/android/bluetooth/avrcp/Avrcp.java | 4 ++-- .../bluetooth/btservice/AdapterService.java | 25 +++++++++++++++++++--- 2 files changed, 24 insertions(+), 5 deletions(-) diff --git a/src/com/android/bluetooth/avrcp/Avrcp.java b/src/com/android/bluetooth/avrcp/Avrcp.java index d40a22d1..656abb83 100644 --- a/src/com/android/bluetooth/avrcp/Avrcp.java +++ b/src/com/android/bluetooth/avrcp/Avrcp.java @@ -1383,7 +1383,7 @@ public final class Avrcp { Context.MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); editor.putBoolean(mAddress, true); - editor.commit(); + editor.apply(); } private int modifyRcFeatureFromBlacklist(int feature, String address) { @@ -1403,7 +1403,7 @@ public final class Avrcp { Context.MODE_PRIVATE); SharedPreferences.Editor editor = pref.edit(); editor.remove(address); - editor.commit(); + editor.apply(); } /** diff --git a/src/com/android/bluetooth/btservice/AdapterService.java b/src/com/android/bluetooth/btservice/AdapterService.java index aecf8998..20004be8 100644 --- a/src/com/android/bluetooth/btservice/AdapterService.java +++ b/src/com/android/bluetooth/btservice/AdapterService.java @@ -37,6 +37,7 @@ import android.content.Context; import android.content.Intent; import android.content.IntentFilter; import android.content.SharedPreferences; +import android.os.AsyncTask; import android.os.BatteryStats; import android.os.Binder; import android.os.Bundle; @@ -549,6 +550,21 @@ public class AdapterService extends Service { mProfileObserver.start(); setAdapterService(this); + + // First call to getSharedPreferences will result in a file read into + // memory cache. Call it here asynchronously to avoid potential ANR + // in the future + new AsyncTask() { + @Override + protected Void doInBackground(Void... params) { + getSharedPreferences( + PHONEBOOK_ACCESS_PERMISSION_PREFERENCE_FILE, Context.MODE_PRIVATE); + getSharedPreferences( + MESSAGE_ACCESS_PERMISSION_PREFERENCE_FILE, Context.MODE_PRIVATE); + getSharedPreferences(SIM_ACCESS_PERMISSION_PREFERENCE_FILE, Context.MODE_PRIVATE); + return null; + } + }.execute(); } @Override @@ -2155,7 +2171,8 @@ public class AdapterService extends Service { } else { editor.putBoolean(device.getAddress(), value == BluetoothDevice.ACCESS_ALLOWED); } - return editor.commit(); + editor.apply(); + return true; } int getMessageAccessPermission(BluetoothDevice device) { @@ -2180,7 +2197,8 @@ public class AdapterService extends Service { } else { editor.putBoolean(device.getAddress(), value == BluetoothDevice.ACCESS_ALLOWED); } - return editor.commit(); + editor.apply(); + return true; } int getSimAccessPermission(BluetoothDevice device) { @@ -2205,7 +2223,8 @@ public class AdapterService extends Service { } else { editor.putBoolean(device.getAddress(), value == BluetoothDevice.ACCESS_ALLOWED); } - return editor.commit(); + editor.apply(); + return true; } void sendConnectionStateChange(BluetoothDevice -- 2.11.0