Update SharedConnectivitySettingsState per feedback from API counsel
Changed builder to accept a PendingIntent instead of Intent. Bug: 273936825 Test: atest SharedConnectivitySettingsStateTest Change-Id: Ifbb99793fc94432a4563b4c2372771c0d231509b
This commit is contained in:
committed by
Isaac Katzenelson
parent
6816b082eb
commit
645333d4e0
@@ -10145,11 +10145,11 @@ package android.net.wifi.sharedconnectivity.app {
|
||||
}
|
||||
|
||||
public static final class SharedConnectivitySettingsState.Builder {
|
||||
ctor public SharedConnectivitySettingsState.Builder(@NonNull android.content.Context);
|
||||
ctor public SharedConnectivitySettingsState.Builder();
|
||||
method @NonNull public android.net.wifi.sharedconnectivity.app.SharedConnectivitySettingsState build();
|
||||
method @NonNull public android.net.wifi.sharedconnectivity.app.SharedConnectivitySettingsState.Builder setExtras(@NonNull android.os.Bundle);
|
||||
method @NonNull public android.net.wifi.sharedconnectivity.app.SharedConnectivitySettingsState.Builder setInstantTetherEnabled(boolean);
|
||||
method @NonNull public android.net.wifi.sharedconnectivity.app.SharedConnectivitySettingsState.Builder setInstantTetherSettingsPendingIntent(@NonNull android.content.Intent);
|
||||
method @NonNull public android.net.wifi.sharedconnectivity.app.SharedConnectivitySettingsState.Builder setInstantTetherSettingsPendingIntent(@NonNull android.app.PendingIntent);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
@@ -20,8 +20,6 @@ import android.annotation.NonNull;
|
||||
import android.annotation.Nullable;
|
||||
import android.annotation.SystemApi;
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.os.Bundle;
|
||||
import android.os.Parcel;
|
||||
import android.os.Parcelable;
|
||||
@@ -49,14 +47,9 @@ public final class SharedConnectivitySettingsState implements Parcelable {
|
||||
*/
|
||||
public static final class Builder {
|
||||
private boolean mInstantTetherEnabled;
|
||||
private Intent mInstantTetherSettingsIntent;
|
||||
private final Context mContext;
|
||||
private PendingIntent mInstantTetherSettingsPendingIntent;
|
||||
private Bundle mExtras = Bundle.EMPTY;
|
||||
|
||||
public Builder(@NonNull Context context) {
|
||||
mContext = context;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the state of Instant Tether in settings
|
||||
*
|
||||
@@ -69,16 +62,14 @@ public final class SharedConnectivitySettingsState implements Parcelable {
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the intent that will open the Instant Tether settings page.
|
||||
* The intent will be stored as a {@link PendingIntent} in the settings object. The pending
|
||||
* intent will be set as {@link PendingIntent#FLAG_IMMUTABLE} and
|
||||
* {@link PendingIntent#FLAG_ONE_SHOT}.
|
||||
* Sets the {@link PendingIntent} that will open the Instant Tether settings page.
|
||||
* The pending intent must be set as {@link PendingIntent#FLAG_IMMUTABLE}.
|
||||
*
|
||||
* @return Returns the Builder object.
|
||||
*/
|
||||
@NonNull
|
||||
public Builder setInstantTetherSettingsPendingIntent(@NonNull Intent intent) {
|
||||
mInstantTetherSettingsIntent = intent;
|
||||
public Builder setInstantTetherSettingsPendingIntent(@NonNull PendingIntent pendingIntent) {
|
||||
mInstantTetherSettingsPendingIntent = pendingIntent;
|
||||
return this;
|
||||
}
|
||||
|
||||
@@ -100,19 +91,21 @@ public final class SharedConnectivitySettingsState implements Parcelable {
|
||||
*/
|
||||
@NonNull
|
||||
public SharedConnectivitySettingsState build() {
|
||||
if (mInstantTetherSettingsIntent != null) {
|
||||
PendingIntent pendingIntent = PendingIntent.getActivity(mContext, 0,
|
||||
mInstantTetherSettingsIntent,
|
||||
PendingIntent.FLAG_IMMUTABLE | PendingIntent.FLAG_ONE_SHOT);
|
||||
return new SharedConnectivitySettingsState(mInstantTetherEnabled,
|
||||
pendingIntent, mExtras);
|
||||
}
|
||||
return new SharedConnectivitySettingsState(mInstantTetherEnabled, null, mExtras);
|
||||
return new SharedConnectivitySettingsState(mInstantTetherEnabled,
|
||||
mInstantTetherSettingsPendingIntent, mExtras);
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
private static void validate(PendingIntent pendingIntent) {
|
||||
if (pendingIntent != null && !pendingIntent.isImmutable()) {
|
||||
throw new IllegalArgumentException("Pending intent must be immutable");
|
||||
}
|
||||
}
|
||||
|
||||
private SharedConnectivitySettingsState(boolean instantTetherEnabled,
|
||||
PendingIntent pendingIntent, @NonNull Bundle extras) {
|
||||
validate(pendingIntent);
|
||||
mInstantTetherEnabled = instantTetherEnabled;
|
||||
mInstantTetherSettingsPendingIntent = pendingIntent;
|
||||
mExtras = extras;
|
||||
|
||||
@@ -206,7 +206,7 @@ public abstract class SharedConnectivityService extends Service {
|
||||
// Done lazily since creating it needs a context.
|
||||
if (mSettingsState == null) {
|
||||
mSettingsState = new SharedConnectivitySettingsState
|
||||
.Builder(getApplicationContext())
|
||||
.Builder()
|
||||
.setInstantTetherEnabled(false)
|
||||
.setExtras(Bundle.EMPTY).build();
|
||||
}
|
||||
|
||||
@@ -498,7 +498,7 @@ public class SharedConnectivityManagerTest {
|
||||
public void getSettingsState_serviceConnected_shouldReturnState() throws RemoteException {
|
||||
SharedConnectivityManager manager = SharedConnectivityManager.create(mContext);
|
||||
SharedConnectivitySettingsState state =
|
||||
new SharedConnectivitySettingsState.Builder(mContext).setInstantTetherEnabled(true)
|
||||
new SharedConnectivitySettingsState.Builder().setInstantTetherEnabled(true)
|
||||
.setExtras(new Bundle()).build();
|
||||
manager.setService(mService);
|
||||
when(mService.getSettingsState()).thenReturn(state);
|
||||
|
||||
@@ -18,6 +18,10 @@ package android.net.wifi.sharedconnectivity.app;
|
||||
|
||||
import static com.google.common.truth.Truth.assertThat;
|
||||
|
||||
import static org.junit.Assert.assertThrows;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.ComponentName;
|
||||
import android.content.Intent;
|
||||
import android.os.Parcel;
|
||||
|
||||
@@ -37,13 +41,24 @@ public class SharedConnectivitySettingsStateTest {
|
||||
private static final boolean INSTANT_TETHER_STATE_1 = false;
|
||||
private static final String INTENT_ACTION_1 = "instant.tether.settings1";
|
||||
|
||||
|
||||
/**
|
||||
* Verifies parcel serialization/deserialization.
|
||||
*/
|
||||
@Test
|
||||
public void testParcelOperation() {
|
||||
SharedConnectivitySettingsState state = buildSettingsStateBuilder().build();
|
||||
public void pendingIntentMutable_buildShouldThrow() {
|
||||
SharedConnectivitySettingsState.Builder builder =
|
||||
new SharedConnectivitySettingsState.Builder()
|
||||
.setInstantTetherEnabled(INSTANT_TETHER_STATE)
|
||||
.setInstantTetherSettingsPendingIntent(PendingIntent.getActivity(
|
||||
ApplicationProvider.getApplicationContext(), 0,
|
||||
new Intent(INTENT_ACTION).setComponent(new ComponentName(
|
||||
"com.test.package", "TestClass")),
|
||||
PendingIntent.FLAG_MUTABLE));
|
||||
|
||||
Exception e = assertThrows(IllegalArgumentException.class, builder::build);
|
||||
assertThat(e.getMessage()).contains("Pending intent must be immutable");
|
||||
}
|
||||
|
||||
@Test
|
||||
public void parcelOperation() {
|
||||
SharedConnectivitySettingsState state = buildSettingsStateBuilder(INTENT_ACTION).build();
|
||||
|
||||
Parcel parcel = Parcel.obtain();
|
||||
state.writeToParcel(parcel, 0);
|
||||
@@ -55,45 +70,46 @@ public class SharedConnectivitySettingsStateTest {
|
||||
assertThat(fromParcel.hashCode()).isEqualTo(state.hashCode());
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the Equals operation
|
||||
*/
|
||||
@Test
|
||||
public void testEqualsOperation() {
|
||||
SharedConnectivitySettingsState state1 = buildSettingsStateBuilder().build();
|
||||
SharedConnectivitySettingsState state2 = buildSettingsStateBuilder().build();
|
||||
public void equalsOperation() {
|
||||
SharedConnectivitySettingsState state1 = buildSettingsStateBuilder(INTENT_ACTION).build();
|
||||
SharedConnectivitySettingsState state2 = buildSettingsStateBuilder(INTENT_ACTION).build();
|
||||
assertThat(state1).isEqualTo(state2);
|
||||
|
||||
SharedConnectivitySettingsState.Builder builder = buildSettingsStateBuilder()
|
||||
SharedConnectivitySettingsState.Builder builder = buildSettingsStateBuilder(INTENT_ACTION)
|
||||
.setInstantTetherEnabled(INSTANT_TETHER_STATE_1);
|
||||
assertThat(builder.build()).isNotEqualTo(state1);
|
||||
|
||||
builder = buildSettingsStateBuilder()
|
||||
.setInstantTetherSettingsPendingIntent(new Intent(INTENT_ACTION_1));
|
||||
builder = buildSettingsStateBuilder(INTENT_ACTION_1);
|
||||
assertThat(builder.build()).isNotEqualTo(state1);
|
||||
}
|
||||
|
||||
/**
|
||||
* Verifies the get methods return the expected data.
|
||||
*/
|
||||
@Test
|
||||
public void testGetMethods() {
|
||||
SharedConnectivitySettingsState state = buildSettingsStateBuilder().build();
|
||||
public void getMethods() {
|
||||
SharedConnectivitySettingsState state = buildSettingsStateBuilder(INTENT_ACTION).build();
|
||||
|
||||
assertThat(state.isInstantTetherEnabled()).isEqualTo(INSTANT_TETHER_STATE);
|
||||
assertThat(state.getInstantTetherSettingsPendingIntent())
|
||||
.isEqualTo(buildPendingIntent(INTENT_ACTION));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testHashCode() {
|
||||
SharedConnectivitySettingsState state1 = buildSettingsStateBuilder().build();
|
||||
SharedConnectivitySettingsState state2 = buildSettingsStateBuilder().build();
|
||||
public void hashCodeCalculation() {
|
||||
SharedConnectivitySettingsState state1 = buildSettingsStateBuilder(INTENT_ACTION).build();
|
||||
SharedConnectivitySettingsState state2 = buildSettingsStateBuilder(INTENT_ACTION).build();
|
||||
|
||||
assertThat(state1.hashCode()).isEqualTo(state2.hashCode());
|
||||
}
|
||||
|
||||
private SharedConnectivitySettingsState.Builder buildSettingsStateBuilder() {
|
||||
return new SharedConnectivitySettingsState.Builder(
|
||||
ApplicationProvider.getApplicationContext())
|
||||
private SharedConnectivitySettingsState.Builder buildSettingsStateBuilder(String intentAction) {
|
||||
return new SharedConnectivitySettingsState.Builder()
|
||||
.setInstantTetherEnabled(INSTANT_TETHER_STATE)
|
||||
.setInstantTetherSettingsPendingIntent(new Intent(INTENT_ACTION));
|
||||
.setInstantTetherSettingsPendingIntent(buildPendingIntent(intentAction));
|
||||
}
|
||||
|
||||
private PendingIntent buildPendingIntent(String intentAction) {
|
||||
return PendingIntent.getActivity(
|
||||
ApplicationProvider.getApplicationContext(), 0,
|
||||
new Intent(intentAction), PendingIntent.FLAG_IMMUTABLE);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -32,6 +32,7 @@ import static org.mockito.Mockito.never;
|
||||
import static org.mockito.Mockito.verify;
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import android.app.PendingIntent;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.res.Resources;
|
||||
@@ -400,8 +401,10 @@ public class SharedConnectivityServiceTest {
|
||||
}
|
||||
|
||||
private SharedConnectivitySettingsState buildSettingsState() {
|
||||
return new SharedConnectivitySettingsState.Builder(mContext).setInstantTetherEnabled(true)
|
||||
.setInstantTetherSettingsPendingIntent(new Intent())
|
||||
return new SharedConnectivitySettingsState.Builder().setInstantTetherEnabled(true)
|
||||
.setInstantTetherSettingsPendingIntent(
|
||||
PendingIntent.getActivity(mContext, 0, new Intent(),
|
||||
PendingIntent.FLAG_IMMUTABLE))
|
||||
.setExtras(Bundle.EMPTY).build();
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user