[Audiosharing] Created test for the name and password preferences.
Test: atest -c com.android.settings.connecteddevice.audiosharing Flag: com.android.settingslib.flags.enable_le_audio_qr_code_private_broadcast_sharing Bug: 345686602 Change-Id: I9036e560e29aba9555c207dce1b2018be010bca8
This commit is contained in:
@@ -19,12 +19,19 @@ package com.android.settings.connecteddevice.audiosharing;
|
||||
import static com.android.settings.connecteddevice.audiosharing.AudioSharingUtils.isBroadcasting;
|
||||
|
||||
import android.app.settings.SettingsEnums;
|
||||
import android.content.ContentResolver;
|
||||
import android.content.Context;
|
||||
import android.content.SharedPreferences;
|
||||
import android.database.ContentObserver;
|
||||
import android.os.Handler;
|
||||
import android.os.Looper;
|
||||
import android.provider.Settings;
|
||||
import android.util.Log;
|
||||
|
||||
import androidx.annotation.NonNull;
|
||||
import androidx.annotation.Nullable;
|
||||
import androidx.lifecycle.DefaultLifecycleObserver;
|
||||
import androidx.lifecycle.LifecycleOwner;
|
||||
import androidx.preference.PreferenceScreen;
|
||||
|
||||
import com.android.settings.R;
|
||||
@@ -41,15 +48,19 @@ import java.nio.charset.StandardCharsets;
|
||||
|
||||
public class AudioSharingPasswordPreferenceController extends BasePreferenceController
|
||||
implements ValidatedEditTextPreference.Validator,
|
||||
AudioSharingPasswordPreference.OnDialogEventListener {
|
||||
|
||||
AudioSharingPasswordPreference.OnDialogEventListener,
|
||||
DefaultLifecycleObserver {
|
||||
private static final String TAG = "AudioSharingPasswordPreferenceController";
|
||||
private static final String PREF_KEY = "audio_sharing_stream_password";
|
||||
private static final String SHARED_PREF_NAME = "audio_sharing_settings";
|
||||
private static final String SHARED_PREF_KEY = "default_password";
|
||||
@Nullable private final ContentResolver mContentResolver;
|
||||
@Nullable private final SharedPreferences mSharedPref;
|
||||
@Nullable private final LocalBluetoothManager mBtManager;
|
||||
@Nullable private final LocalBluetoothLeBroadcast mBroadcast;
|
||||
@Nullable private AudioSharingPasswordPreference mPreference;
|
||||
private final ContentObserver mSettingsObserver;
|
||||
private final SharedPreferences.OnSharedPreferenceChangeListener mSharedPrefChangeListener;
|
||||
private final AudioSharingPasswordValidator mAudioSharingPasswordValidator;
|
||||
private final MetricsFeatureProvider mMetricsFeatureProvider;
|
||||
|
||||
@@ -61,9 +72,44 @@ public class AudioSharingPasswordPreferenceController extends BasePreferenceCont
|
||||
? mBtManager.getProfileManager().getLeAudioBroadcastProfile()
|
||||
: null;
|
||||
mAudioSharingPasswordValidator = new AudioSharingPasswordValidator();
|
||||
mContentResolver = context.getContentResolver();
|
||||
mSettingsObserver = new PasswordSettingsObserver();
|
||||
mSharedPref = context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
|
||||
mSharedPrefChangeListener = new PasswordSharedPrefChangeListener();
|
||||
mMetricsFeatureProvider = FeatureFactory.getFeatureFactory().getMetricsFeatureProvider();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStart(@NonNull LifecycleOwner owner) {
|
||||
if (!isAvailable()) {
|
||||
Log.d(TAG, "Feature is not available.");
|
||||
return;
|
||||
}
|
||||
if (mContentResolver != null) {
|
||||
mContentResolver.registerContentObserver(
|
||||
Settings.Secure.getUriFor(Settings.Secure.BLUETOOTH_LE_BROADCAST_CODE),
|
||||
false,
|
||||
mSettingsObserver);
|
||||
}
|
||||
if (mSharedPref != null) {
|
||||
mSharedPref.registerOnSharedPreferenceChangeListener(mSharedPrefChangeListener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onStop(@NonNull LifecycleOwner owner) {
|
||||
if (!isAvailable()) {
|
||||
Log.d(TAG, "Feature is not available.");
|
||||
return;
|
||||
}
|
||||
if (mContentResolver != null) {
|
||||
mContentResolver.unregisterContentObserver(mSettingsObserver);
|
||||
}
|
||||
if (mSharedPref != null) {
|
||||
mSharedPref.unregisterOnSharedPreferenceChangeListener(mSharedPrefChangeListener);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public int getAvailabilityStatus() {
|
||||
return AudioSharingUtils.isFeatureEnabled() ? AVAILABLE : UNSUPPORTED_ON_DEVICE;
|
||||
@@ -125,7 +171,6 @@ public class AudioSharingPasswordPreferenceController extends BasePreferenceCont
|
||||
persistDefaultPassword(mContext, password);
|
||||
mBroadcast.setBroadcastCode(
|
||||
isPublicBroadcast ? new byte[0] : password.getBytes());
|
||||
updatePreference();
|
||||
mMetricsFeatureProvider.action(
|
||||
mContext,
|
||||
SettingsEnums.ACTION_AUDIO_STREAM_PASSWORD_UPDATED,
|
||||
@@ -164,32 +209,52 @@ public class AudioSharingPasswordPreferenceController extends BasePreferenceCont
|
||||
});
|
||||
}
|
||||
|
||||
private static void persistDefaultPassword(Context context, String defaultPassword) {
|
||||
private class PasswordSettingsObserver extends ContentObserver {
|
||||
PasswordSettingsObserver() {
|
||||
super(new Handler(Looper.getMainLooper()));
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onChange(boolean selfChange) {
|
||||
Log.d(TAG, "onChange, broadcast password has been changed");
|
||||
updatePreference();
|
||||
}
|
||||
}
|
||||
|
||||
private class PasswordSharedPrefChangeListener
|
||||
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
||||
@Override
|
||||
public void onSharedPreferenceChanged(
|
||||
SharedPreferences sharedPreferences, @Nullable String key) {
|
||||
if (!SHARED_PREF_KEY.equals(key)) {
|
||||
return;
|
||||
}
|
||||
Log.d(TAG, "onSharedPreferenceChanged, default password has been changed");
|
||||
updatePreference();
|
||||
}
|
||||
}
|
||||
|
||||
private void persistDefaultPassword(Context context, String defaultPassword) {
|
||||
if (getDefaultPassword(context).equals(defaultPassword)) {
|
||||
return;
|
||||
}
|
||||
|
||||
SharedPreferences sharedPref =
|
||||
context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
|
||||
if (sharedPref == null) {
|
||||
if (mSharedPref == null) {
|
||||
Log.w(TAG, "persistDefaultPassword(): sharedPref is empty!");
|
||||
return;
|
||||
}
|
||||
|
||||
SharedPreferences.Editor editor = sharedPref.edit();
|
||||
SharedPreferences.Editor editor = mSharedPref.edit();
|
||||
editor.putString(SHARED_PREF_KEY, defaultPassword);
|
||||
editor.apply();
|
||||
}
|
||||
|
||||
private static String getDefaultPassword(Context context) {
|
||||
SharedPreferences sharedPref =
|
||||
context.getSharedPreferences(SHARED_PREF_NAME, Context.MODE_PRIVATE);
|
||||
if (sharedPref == null) {
|
||||
private String getDefaultPassword(Context context) {
|
||||
if (mSharedPref == null) {
|
||||
Log.w(TAG, "getDefaultPassword(): sharedPref is empty!");
|
||||
return "";
|
||||
}
|
||||
|
||||
String value = sharedPref.getString(SHARED_PREF_KEY, "");
|
||||
String value = mSharedPref.getString(SHARED_PREF_KEY, "");
|
||||
if (value != null && value.isEmpty()) {
|
||||
Log.w(TAG, "getDefaultPassword(): default password is empty!");
|
||||
}
|
||||
|
Reference in New Issue
Block a user