1 /* 2 * Copyright (C) 2023 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.net.wifi.sharedconnectivity.app; 18 19 import android.annotation.NonNull; 20 import android.annotation.Nullable; 21 import android.annotation.SystemApi; 22 import android.app.PendingIntent; 23 import android.os.Bundle; 24 import android.os.Parcel; 25 import android.os.Parcelable; 26 27 import java.util.Objects; 28 29 30 /** 31 * A data class representing the shared connectivity settings state. 32 * 33 * This class represents a snapshot of the settings and can be out of date if the settings changed 34 * after receiving an object of this class. 35 * 36 * @hide 37 */ 38 @SystemApi 39 public final class SharedConnectivitySettingsState implements Parcelable { 40 41 private final boolean mInstantTetherEnabled; 42 private final PendingIntent mInstantTetherSettingsPendingIntent; 43 private final Bundle mExtras; 44 45 /** 46 * Builder class for {@link SharedConnectivitySettingsState}. 47 */ 48 public static final class Builder { 49 private boolean mInstantTetherEnabled; 50 private PendingIntent mInstantTetherSettingsPendingIntent; 51 private Bundle mExtras = Bundle.EMPTY; 52 53 /** 54 * Sets the state of Instant Tether in settings 55 * 56 * @return Returns the Builder object. 57 */ 58 @NonNull setInstantTetherEnabled(boolean instantTetherEnabled)59 public Builder setInstantTetherEnabled(boolean instantTetherEnabled) { 60 mInstantTetherEnabled = instantTetherEnabled; 61 return this; 62 } 63 64 /** 65 * Sets the {@link PendingIntent} that will open the Instant Tether settings page. 66 * The pending intent must be set as {@link PendingIntent#FLAG_IMMUTABLE}. 67 * 68 * @return Returns the Builder object. 69 */ 70 @NonNull setInstantTetherSettingsPendingIntent(@onNull PendingIntent pendingIntent)71 public Builder setInstantTetherSettingsPendingIntent(@NonNull PendingIntent pendingIntent) { 72 mInstantTetherSettingsPendingIntent = pendingIntent; 73 return this; 74 } 75 76 /** 77 * Sets the extras bundle 78 * 79 * @return Returns the Builder object. 80 */ 81 @NonNull setExtras(@onNull Bundle extras)82 public Builder setExtras(@NonNull Bundle extras) { 83 mExtras = extras; 84 return this; 85 } 86 87 /** 88 * Builds the {@link SharedConnectivitySettingsState} object. 89 * 90 * @return Returns the built {@link SharedConnectivitySettingsState} object. 91 */ 92 @NonNull build()93 public SharedConnectivitySettingsState build() { 94 return new SharedConnectivitySettingsState(mInstantTetherEnabled, 95 mInstantTetherSettingsPendingIntent, mExtras); 96 97 } 98 } 99 validate(PendingIntent pendingIntent)100 private static void validate(PendingIntent pendingIntent) { 101 if (pendingIntent != null && !pendingIntent.isImmutable()) { 102 throw new IllegalArgumentException("Pending intent must be immutable"); 103 } 104 } 105 SharedConnectivitySettingsState(boolean instantTetherEnabled, PendingIntent pendingIntent, @NonNull Bundle extras)106 private SharedConnectivitySettingsState(boolean instantTetherEnabled, 107 PendingIntent pendingIntent, @NonNull Bundle extras) { 108 validate(pendingIntent); 109 mInstantTetherEnabled = instantTetherEnabled; 110 mInstantTetherSettingsPendingIntent = pendingIntent; 111 mExtras = extras; 112 } 113 114 /** 115 * Gets the state of Instant Tether in settings 116 * 117 * @return Returns true for enabled, false otherwise. 118 */ isInstantTetherEnabled()119 public boolean isInstantTetherEnabled() { 120 return mInstantTetherEnabled; 121 } 122 123 /** 124 * Gets the pending intent to open Instant Tether settings page. 125 * 126 * @return Returns the pending intent that opens the settings page, null if none. 127 */ 128 @Nullable getInstantTetherSettingsPendingIntent()129 public PendingIntent getInstantTetherSettingsPendingIntent() { 130 return mInstantTetherSettingsPendingIntent; 131 } 132 133 /** 134 * Gets the extras Bundle. 135 * 136 * @return Returns a Bundle object. 137 */ 138 @NonNull getExtras()139 public Bundle getExtras() { 140 return mExtras; 141 } 142 143 @Override equals(Object obj)144 public boolean equals(Object obj) { 145 if (!(obj instanceof SharedConnectivitySettingsState)) return false; 146 SharedConnectivitySettingsState other = (SharedConnectivitySettingsState) obj; 147 return mInstantTetherEnabled == other.isInstantTetherEnabled() 148 && Objects.equals(mInstantTetherSettingsPendingIntent, 149 other.getInstantTetherSettingsPendingIntent()); 150 } 151 152 @Override hashCode()153 public int hashCode() { 154 return Objects.hash(mInstantTetherEnabled, mInstantTetherSettingsPendingIntent); 155 } 156 157 @Override describeContents()158 public int describeContents() { 159 return 0; 160 } 161 162 @Override writeToParcel(@onNull Parcel dest, int flags)163 public void writeToParcel(@NonNull Parcel dest, int flags) { 164 PendingIntent.writePendingIntentOrNullToParcel(mInstantTetherSettingsPendingIntent, dest); 165 dest.writeBoolean(mInstantTetherEnabled); 166 dest.writeBundle(mExtras); 167 } 168 169 /** 170 * Creates a {@link SharedConnectivitySettingsState} object from a parcel. 171 * 172 * @hide 173 */ 174 @NonNull readFromParcel(@onNull Parcel in)175 public static SharedConnectivitySettingsState readFromParcel(@NonNull Parcel in) { 176 PendingIntent pendingIntent = PendingIntent.readPendingIntentOrNullFromParcel(in); 177 boolean instantTetherEnabled = in.readBoolean(); 178 Bundle extras = in.readBundle(); 179 return new SharedConnectivitySettingsState(instantTetherEnabled, pendingIntent, extras); 180 } 181 182 @NonNull 183 public static final Creator<SharedConnectivitySettingsState> CREATOR = new Creator<>() { 184 @Override 185 public SharedConnectivitySettingsState createFromParcel(Parcel in) { 186 return readFromParcel(in); 187 } 188 189 @Override 190 public SharedConnectivitySettingsState[] newArray(int size) { 191 return new SharedConnectivitySettingsState[size]; 192 } 193 }; 194 195 @Override toString()196 public String toString() { 197 return new StringBuilder("SharedConnectivitySettingsState[") 198 .append("instantTetherEnabled=").append(mInstantTetherEnabled) 199 .append("PendingIntent=").append(mInstantTetherSettingsPendingIntent) 200 .append("extras=").append(mExtras.toString()) 201 .append("]").toString(); 202 } 203 } 204