[Settings] Fix crash when user enter bluetooth page quickly.

- When SliceManager try to pinSlice, process may not have the permssion
   yet, so in Androix lib it use try/catch to avoid Security exception.
   However, if SliceManger quickly unpinSlice after pinSlice, process
   may still not get the permission yet, then due to no security
   exception, it cause the settings crash.

Bug: 283065718
Test: atest passed
Test: Manual test passed
Change-Id: I2293fca73e65dfaa34237abe57e0c6a3fe0f62bb
This commit is contained in:
tom hsu
2023-06-07 16:47:04 +08:00
committed by Tom Hsu
parent c4ed48cbe4
commit bf7865b27e
2 changed files with 28 additions and 7 deletions

View File

@@ -113,15 +113,27 @@ public class BlockingPrefWithSliceController extends BasePreferenceController im
@Override
public void onStart() {
if (mLiveData != null) {
if (mLiveData == null) {
return;
}
try {
mLiveData.observeForever(this);
} catch (SecurityException e) {
Log.w(TAG, "observeForever - no permission");
}
}
@Override
public void onStop() {
if (mLiveData != null) {
if (mLiveData == null) {
return;
}
try {
mLiveData.removeObserver(this);
} catch (SecurityException e) {
Log.w(TAG, "removeObserver - no permission");
}
}

View File

@@ -44,7 +44,6 @@ public class SlicePreferenceController extends BasePreferenceController implemen
LiveData<Slice> mLiveData;
@VisibleForTesting
SlicePreference mSlicePreference;
private boolean mIsObservering = false;
private Uri mUri;
public SlicePreferenceController(Context context, String preferenceKey) {
@@ -74,9 +73,14 @@ public class SlicePreferenceController extends BasePreferenceController implemen
@Override
public void onStart() {
if (mLiveData != null && !mIsObservering) {
mIsObservering = true;
if (mLiveData == null) {
return;
}
try {
mLiveData.observeForever(this);
} catch (SecurityException e) {
Log.w(TAG, "observeForever - no permission");
}
}
@@ -91,9 +95,14 @@ public class SlicePreferenceController extends BasePreferenceController implemen
}
private void removeLiveDataObserver() {
if (mLiveData != null && mIsObservering && mLiveData.hasActiveObservers()) {
mIsObservering = false;
if (mLiveData == null) {
return;
}
try {
mLiveData.removeObserver(this);
} catch (SecurityException e) {
Log.w(TAG, "removeLiveDataObserver - no permission");
}
}
}