Merge "network: fix binder object leakage in settings" am: bdcd3a3975 am: 5408baabdd

Original change: https://android-review.googlesource.com/c/platform/packages/apps/Settings/+/2136792

Change-Id: Ic7d0893d3c7793f000db51183cbce0ad49765805
Signed-off-by: Automerger Merge Worker <android-build-automerger-merge-worker@system.gserviceaccount.com>
This commit is contained in:
Treehugger Robot
2022-10-21 04:34:57 +00:00
committed by Automerger Merge Worker
3 changed files with 128 additions and 13 deletions

View File

@@ -43,6 +43,7 @@ import android.os.Handler;
import android.os.UserHandle;
import android.os.UserManager;
import android.provider.SearchIndexableResource;
import android.text.TextUtils;
import android.util.FeatureFlagUtils;
import android.util.Log;
@@ -97,6 +98,7 @@ public class TetherSettings extends RestrictedSettingsFragment
private SwitchPreference mEthernetTether;
private BroadcastReceiver mTetherChangeReceiver;
private BroadcastReceiver mBluetoothStateReceiver;
private String[] mBluetoothRegexs;
private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<>();
@@ -167,6 +169,12 @@ public class TetherSettings extends RestrictedSettingsFragment
adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
BluetoothProfile.PAN);
}
if (mBluetoothStateReceiver == null) {
mBluetoothStateReceiver = new BluetoothStateReceiver();
mContext.registerReceiver(
mBluetoothStateReceiver,
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
setupTetherPreference();
setTopIntroPreferenceTitle();
@@ -216,6 +224,10 @@ public class TetherSettings extends RestrictedSettingsFragment
if (profile != null && adapter != null) {
adapter.closeProfileProxy(BluetoothProfile.PAN, profile);
}
if (mBluetoothStateReceiver != null) {
mContext.unregisterReceiver(mBluetoothStateReceiver);
mBluetoothStateReceiver = null;
}
super.onDestroy();
}
@@ -255,6 +267,30 @@ public class TetherSettings extends RestrictedSettingsFragment
}
}
private class BluetoothStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.i(TAG, "onReceive: action: " + action);
if (TextUtils.equals(action, BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state =
intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
Log.i(TAG, "onReceive: state: " + BluetoothAdapter.nameForState(state));
final BluetoothProfile profile = mBluetoothPan.get();
switch(state) {
case BluetoothAdapter.STATE_ON:
BluetoothAdapter adapter = BluetoothAdapter.getDefaultAdapter();
if (profile == null && adapter != null) {
adapter.getProfileProxy(mContext, mProfileServiceListener,
BluetoothProfile.PAN);
}
break;
}
}
}
}
private class TetherChangeReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context content, Intent intent) {
@@ -558,13 +594,16 @@ public class TetherSettings extends RestrictedSettingsFragment
private BluetoothProfile.ServiceListener mProfileServiceListener =
new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
mBluetoothPan.set((BluetoothPan) proxy);
}
public void onServiceDisconnected(int profile) {
mBluetoothPan.set(null);
}
};
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
if (mBluetoothPan.get() == null) {
mBluetoothPan.set((BluetoothPan) proxy);
}
}
@Override
public void onServiceDisconnected(int profile) { /* Do nothing */ }
};
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() {

View File

@@ -27,8 +27,12 @@ import static com.android.settingslib.RestrictedLockUtilsInternal.checkIfRestric
import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.FeatureFlagUtils;
import android.util.Log;
@@ -66,17 +70,18 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
new BluetoothProfile.ServiceListener() {
@Override
public void onServiceConnected(int profile, BluetoothProfile proxy) {
mBluetoothPan.set((BluetoothPan) proxy);
if (mBluetoothPan.get() == null) {
mBluetoothPan.set((BluetoothPan) proxy);
}
}
@Override
public void onServiceDisconnected(int profile) {
mBluetoothPan.set(null);
}
public void onServiceDisconnected(int profile) { /* Do nothing */ }
};
private PrimarySwitchPreference mPreference;
private TetherEnabler mTetherEnabler;
private BroadcastReceiver mBluetoothStateReceiver;
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
AllInOneTetherPreferenceController() {
@@ -164,6 +169,12 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
mBluetoothAdapter.getProfileProxy(mContext, mBtProfileServiceListener,
BluetoothProfile.PAN);
}
if (mBluetoothStateReceiver == null) {
mBluetoothStateReceiver = new BluetoothStateReceiver();
mContext.registerReceiver(
mBluetoothStateReceiver,
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
}
@OnLifecycleEvent(Event.ON_RESUME)
@@ -186,6 +197,10 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
if (profile != null && mBluetoothAdapter != null) {
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.PAN, profile);
}
if (mBluetoothStateReceiver != null) {
mContext.unregisterReceiver(mBluetoothStateReceiver);
mBluetoothStateReceiver = null;
}
}
void initEnabler(Lifecycle lifecycle) {
@@ -205,4 +220,27 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
mTetheringState = state;
updateState(mPreference);
}
private class BluetoothStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.i(TAG, "onReceive: action: " + action);
if (TextUtils.equals(action, BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state =
intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
Log.i(TAG, "onReceive: state: " + BluetoothAdapter.nameForState(state));
final BluetoothProfile profile = mBluetoothPan.get();
switch(state) {
case BluetoothAdapter.STATE_ON:
if (profile == null && mBluetoothAdapter != null) {
mBluetoothAdapter.getProfileProxy(mContext, mBtProfileServiceListener,
BluetoothProfile.PAN);
}
break;
}
}
}
}
}

View File

@@ -33,7 +33,9 @@ import android.os.Bundle;
import android.os.Handler;
import android.os.UserHandle;
import android.provider.Settings;
import android.text.TextUtils;
import android.util.FeatureFlagUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference;
@@ -56,6 +58,7 @@ import java.util.concurrent.atomic.AtomicReference;
public class TetherPreferenceController extends AbstractPreferenceController implements
PreferenceControllerMixin, LifecycleObserver, OnCreate, OnResume, OnPause, OnDestroy {
private static final String TAG = "TetherPreferenceController";
private static final String KEY_TETHER_SETTINGS = "tether_settings";
private final boolean mAdminDisallowedTetherConfig;
@@ -66,12 +69,13 @@ public class TetherPreferenceController extends AbstractPreferenceController imp
final BluetoothProfile.ServiceListener mBtProfileServiceListener =
new android.bluetooth.BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) {
mBluetoothPan.set((BluetoothPan) proxy);
if (mBluetoothPan.get() == null) {
mBluetoothPan.set((BluetoothPan) proxy);
}
updateSummary();
}
public void onServiceDisconnected(int profile) {
mBluetoothPan.set(null);
updateSummary();
}
};
@@ -79,6 +83,7 @@ public class TetherPreferenceController extends AbstractPreferenceController imp
private SettingObserver mAirplaneModeObserver;
private Preference mPreference;
private TetherBroadcastReceiver mTetherReceiver;
private BroadcastReceiver mBluetoothStateReceiver;
@VisibleForTesting(otherwise = VisibleForTesting.NONE)
TetherPreferenceController() {
@@ -133,6 +138,12 @@ public class TetherPreferenceController extends AbstractPreferenceController imp
mBluetoothAdapter.getProfileProxy(mContext, mBtProfileServiceListener,
BluetoothProfile.PAN);
}
if (mBluetoothStateReceiver == null) {
mBluetoothStateReceiver = new BluetoothStateReceiver();
mContext.registerReceiver(
mBluetoothStateReceiver,
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
}
@Override
@@ -165,6 +176,10 @@ public class TetherPreferenceController extends AbstractPreferenceController imp
if (profile != null && mBluetoothAdapter != null) {
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.PAN, profile);
}
if (mBluetoothStateReceiver != null) {
mContext.unregisterReceiver(mBluetoothStateReceiver);
mBluetoothStateReceiver = null;
}
}
public static boolean isTetherConfigDisallowed(Context context) {
@@ -270,4 +285,27 @@ public class TetherPreferenceController extends AbstractPreferenceController imp
}
}
private class BluetoothStateReceiver extends BroadcastReceiver {
@Override
public void onReceive(Context context, Intent intent) {
final String action = intent.getAction();
Log.i(TAG, "onReceive: action: " + action);
if (TextUtils.equals(action, BluetoothAdapter.ACTION_STATE_CHANGED)) {
final int state =
intent.getIntExtra(BluetoothAdapter.EXTRA_STATE, BluetoothAdapter.ERROR);
Log.i(TAG, "onReceive: state: " + BluetoothAdapter.nameForState(state));
final BluetoothProfile profile = mBluetoothPan.get();
switch(state) {
case BluetoothAdapter.STATE_ON:
if (profile == null && mBluetoothAdapter != null) {
mBluetoothAdapter.getProfileProxy(mContext, mBtProfileServiceListener,
BluetoothProfile.PAN);
}
break;
}
}
}
}
}