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.UserHandle;
import android.os.UserManager; import android.os.UserManager;
import android.provider.SearchIndexableResource; import android.provider.SearchIndexableResource;
import android.text.TextUtils;
import android.util.FeatureFlagUtils; import android.util.FeatureFlagUtils;
import android.util.Log; import android.util.Log;
@@ -97,6 +98,7 @@ public class TetherSettings extends RestrictedSettingsFragment
private SwitchPreference mEthernetTether; private SwitchPreference mEthernetTether;
private BroadcastReceiver mTetherChangeReceiver; private BroadcastReceiver mTetherChangeReceiver;
private BroadcastReceiver mBluetoothStateReceiver;
private String[] mBluetoothRegexs; private String[] mBluetoothRegexs;
private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<>(); private AtomicReference<BluetoothPan> mBluetoothPan = new AtomicReference<>();
@@ -167,6 +169,12 @@ public class TetherSettings extends RestrictedSettingsFragment
adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener, adapter.getProfileProxy(activity.getApplicationContext(), mProfileServiceListener,
BluetoothProfile.PAN); BluetoothProfile.PAN);
} }
if (mBluetoothStateReceiver == null) {
mBluetoothStateReceiver = new BluetoothStateReceiver();
mContext.registerReceiver(
mBluetoothStateReceiver,
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
setupTetherPreference(); setupTetherPreference();
setTopIntroPreferenceTitle(); setTopIntroPreferenceTitle();
@@ -216,6 +224,10 @@ public class TetherSettings extends RestrictedSettingsFragment
if (profile != null && adapter != null) { if (profile != null && adapter != null) {
adapter.closeProfileProxy(BluetoothProfile.PAN, profile); adapter.closeProfileProxy(BluetoothProfile.PAN, profile);
} }
if (mBluetoothStateReceiver != null) {
mContext.unregisterReceiver(mBluetoothStateReceiver);
mBluetoothStateReceiver = null;
}
super.onDestroy(); 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 { private class TetherChangeReceiver extends BroadcastReceiver {
@Override @Override
public void onReceive(Context content, Intent intent) { public void onReceive(Context content, Intent intent) {
@@ -558,13 +594,16 @@ public class TetherSettings extends RestrictedSettingsFragment
private BluetoothProfile.ServiceListener mProfileServiceListener = private BluetoothProfile.ServiceListener mProfileServiceListener =
new BluetoothProfile.ServiceListener() { new BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) { @Override
mBluetoothPan.set((BluetoothPan) proxy); public void onServiceConnected(int profile, BluetoothProfile proxy) {
} if (mBluetoothPan.get() == null) {
public void onServiceDisconnected(int profile) { mBluetoothPan.set((BluetoothPan) proxy);
mBluetoothPan.set(null); }
} }
};
@Override
public void onServiceDisconnected(int profile) { /* Do nothing */ }
};
public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER = public static final BaseSearchIndexProvider SEARCH_INDEX_DATA_PROVIDER =
new BaseSearchIndexProvider() { new BaseSearchIndexProvider() {

View File

@@ -27,8 +27,12 @@ import static com.android.settingslib.RestrictedLockUtilsInternal.checkIfRestric
import android.bluetooth.BluetoothAdapter; import android.bluetooth.BluetoothAdapter;
import android.bluetooth.BluetoothPan; import android.bluetooth.BluetoothPan;
import android.bluetooth.BluetoothProfile; import android.bluetooth.BluetoothProfile;
import android.content.BroadcastReceiver;
import android.content.Context; import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.UserHandle; import android.os.UserHandle;
import android.text.TextUtils;
import android.util.FeatureFlagUtils; import android.util.FeatureFlagUtils;
import android.util.Log; import android.util.Log;
@@ -66,17 +70,18 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
new BluetoothProfile.ServiceListener() { new BluetoothProfile.ServiceListener() {
@Override @Override
public void onServiceConnected(int profile, BluetoothProfile proxy) { public void onServiceConnected(int profile, BluetoothProfile proxy) {
mBluetoothPan.set((BluetoothPan) proxy); if (mBluetoothPan.get() == null) {
mBluetoothPan.set((BluetoothPan) proxy);
}
} }
@Override @Override
public void onServiceDisconnected(int profile) { public void onServiceDisconnected(int profile) { /* Do nothing */ }
mBluetoothPan.set(null);
}
}; };
private PrimarySwitchPreference mPreference; private PrimarySwitchPreference mPreference;
private TetherEnabler mTetherEnabler; private TetherEnabler mTetherEnabler;
private BroadcastReceiver mBluetoothStateReceiver;
@VisibleForTesting(otherwise = VisibleForTesting.NONE) @VisibleForTesting(otherwise = VisibleForTesting.NONE)
AllInOneTetherPreferenceController() { AllInOneTetherPreferenceController() {
@@ -164,6 +169,12 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
mBluetoothAdapter.getProfileProxy(mContext, mBtProfileServiceListener, mBluetoothAdapter.getProfileProxy(mContext, mBtProfileServiceListener,
BluetoothProfile.PAN); BluetoothProfile.PAN);
} }
if (mBluetoothStateReceiver == null) {
mBluetoothStateReceiver = new BluetoothStateReceiver();
mContext.registerReceiver(
mBluetoothStateReceiver,
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
} }
@OnLifecycleEvent(Event.ON_RESUME) @OnLifecycleEvent(Event.ON_RESUME)
@@ -186,6 +197,10 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
if (profile != null && mBluetoothAdapter != null) { if (profile != null && mBluetoothAdapter != null) {
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.PAN, profile); mBluetoothAdapter.closeProfileProxy(BluetoothProfile.PAN, profile);
} }
if (mBluetoothStateReceiver != null) {
mContext.unregisterReceiver(mBluetoothStateReceiver);
mBluetoothStateReceiver = null;
}
} }
void initEnabler(Lifecycle lifecycle) { void initEnabler(Lifecycle lifecycle) {
@@ -205,4 +220,27 @@ public class AllInOneTetherPreferenceController extends BasePreferenceController
mTetheringState = state; mTetheringState = state;
updateState(mPreference); 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.Handler;
import android.os.UserHandle; import android.os.UserHandle;
import android.provider.Settings; import android.provider.Settings;
import android.text.TextUtils;
import android.util.FeatureFlagUtils; import android.util.FeatureFlagUtils;
import android.util.Log;
import androidx.annotation.VisibleForTesting; import androidx.annotation.VisibleForTesting;
import androidx.preference.Preference; import androidx.preference.Preference;
@@ -56,6 +58,7 @@ import java.util.concurrent.atomic.AtomicReference;
public class TetherPreferenceController extends AbstractPreferenceController implements public class TetherPreferenceController extends AbstractPreferenceController implements
PreferenceControllerMixin, LifecycleObserver, OnCreate, OnResume, OnPause, OnDestroy { PreferenceControllerMixin, LifecycleObserver, OnCreate, OnResume, OnPause, OnDestroy {
private static final String TAG = "TetherPreferenceController";
private static final String KEY_TETHER_SETTINGS = "tether_settings"; private static final String KEY_TETHER_SETTINGS = "tether_settings";
private final boolean mAdminDisallowedTetherConfig; private final boolean mAdminDisallowedTetherConfig;
@@ -66,12 +69,13 @@ public class TetherPreferenceController extends AbstractPreferenceController imp
final BluetoothProfile.ServiceListener mBtProfileServiceListener = final BluetoothProfile.ServiceListener mBtProfileServiceListener =
new android.bluetooth.BluetoothProfile.ServiceListener() { new android.bluetooth.BluetoothProfile.ServiceListener() {
public void onServiceConnected(int profile, BluetoothProfile proxy) { public void onServiceConnected(int profile, BluetoothProfile proxy) {
mBluetoothPan.set((BluetoothPan) proxy); if (mBluetoothPan.get() == null) {
mBluetoothPan.set((BluetoothPan) proxy);
}
updateSummary(); updateSummary();
} }
public void onServiceDisconnected(int profile) { public void onServiceDisconnected(int profile) {
mBluetoothPan.set(null);
updateSummary(); updateSummary();
} }
}; };
@@ -79,6 +83,7 @@ public class TetherPreferenceController extends AbstractPreferenceController imp
private SettingObserver mAirplaneModeObserver; private SettingObserver mAirplaneModeObserver;
private Preference mPreference; private Preference mPreference;
private TetherBroadcastReceiver mTetherReceiver; private TetherBroadcastReceiver mTetherReceiver;
private BroadcastReceiver mBluetoothStateReceiver;
@VisibleForTesting(otherwise = VisibleForTesting.NONE) @VisibleForTesting(otherwise = VisibleForTesting.NONE)
TetherPreferenceController() { TetherPreferenceController() {
@@ -133,6 +138,12 @@ public class TetherPreferenceController extends AbstractPreferenceController imp
mBluetoothAdapter.getProfileProxy(mContext, mBtProfileServiceListener, mBluetoothAdapter.getProfileProxy(mContext, mBtProfileServiceListener,
BluetoothProfile.PAN); BluetoothProfile.PAN);
} }
if (mBluetoothStateReceiver == null) {
mBluetoothStateReceiver = new BluetoothStateReceiver();
mContext.registerReceiver(
mBluetoothStateReceiver,
new IntentFilter(BluetoothAdapter.ACTION_STATE_CHANGED));
}
} }
@Override @Override
@@ -165,6 +176,10 @@ public class TetherPreferenceController extends AbstractPreferenceController imp
if (profile != null && mBluetoothAdapter != null) { if (profile != null && mBluetoothAdapter != null) {
mBluetoothAdapter.closeProfileProxy(BluetoothProfile.PAN, profile); mBluetoothAdapter.closeProfileProxy(BluetoothProfile.PAN, profile);
} }
if (mBluetoothStateReceiver != null) {
mContext.unregisterReceiver(mBluetoothStateReceiver);
mBluetoothStateReceiver = null;
}
} }
public static boolean isTetherConfigDisallowed(Context context) { 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;
}
}
}
}
} }