| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.notification; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.Nullable; |
| 21 |
import android.content.Context; |
| 22 |
import android.net.Uri; |
| 23 |
import android.os.Parcel; |
| 24 |
import android.os.Parcelable; |
| 25 |
import android.util.proto.ProtoOutputStream; |
| 26 |
|
| 27 |
import java.lang.annotation.Retention; |
| 28 |
import java.lang.annotation.RetentionPolicy; |
| 29 |
import java.util.Objects; |
| 30 |
|
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
public final class Condition implements Parcelable { |
| 37 |
|
| 38 |
public static final String SCHEME = "condition"; |
| 39 |
|
| 40 |
|
| 41 |
@IntDef(prefix = { "STATE_" }, value = { |
| 42 |
STATE_FALSE, |
| 43 |
STATE_TRUE, |
| 44 |
STATE_UNKNOWN, |
| 45 |
STATE_ERROR |
| 46 |
}) |
| 47 |
@Retention(RetentionPolicy.SOURCE) |
| 48 |
public @interface State {} |
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static final int STATE_FALSE = 0; |
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
public static final int STATE_TRUE = 1; |
| 60 |
|
| 61 |
public static final int STATE_UNKNOWN = 2; |
| 62 |
public static final int STATE_ERROR = 3; |
| 63 |
|
| 64 |
public static final int FLAG_RELEVANT_NOW = 1 << 0; |
| 65 |
public static final int FLAG_RELEVANT_ALWAYS = 1 << 1; |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
public final Uri id; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
public final String summary; |
| 78 |
|
| 79 |
public final String line1; |
| 80 |
public final String line2; |
| 81 |
|
| 82 |
|
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
@State |
| 88 |
public final int state; |
| 89 |
|
| 90 |
public final int flags; |
| 91 |
public final int icon; |
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static final int MAX_STRING_LENGTH = 1000; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
public Condition(Uri id, String summary, int state) { |
| 105 |
this(id, summary, "", "", -1, state, FLAG_RELEVANT_ALWAYS); |
| 106 |
} |
| 107 |
|
| 108 |
public Condition(Uri id, String summary, String line1, String line2, int icon, |
| 109 |
int state, int flags) { |
| 110 |
if (id == null) throw new IllegalArgumentException("id is required"); |
| 111 |
if (summary == null) throw new IllegalArgumentException("summary is required"); |
| 112 |
if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state); |
| 113 |
this.id = getTrimmedUri(id); |
| 114 |
this.summary = getTrimmedString(summary); |
| 115 |
this.line1 = getTrimmedString(line1); |
| 116 |
this.line2 = getTrimmedString(line2); |
| 117 |
this.icon = icon; |
| 118 |
this.state = state; |
| 119 |
this.flags = flags; |
| 120 |
} |
| 121 |
|
| 122 |
public Condition(Parcel source) { |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
this((Uri)source.readParcelable(Condition.class.getClassLoader(), android.net.Uri.class), |
| 127 |
source.readString(), |
| 128 |
source.readString(), |
| 129 |
source.readString(), |
| 130 |
source.readInt(), |
| 131 |
source.readInt(), |
| 132 |
source.readInt()); |
| 133 |
} |
| 134 |
|
| 135 |
private static boolean isValidState(int state) { |
| 136 |
return state >= STATE_FALSE && state <= STATE_ERROR; |
| 137 |
} |
| 138 |
|
| 139 |
@Override |
| 140 |
public void writeToParcel(Parcel dest, int flags) { |
| 141 |
dest.writeParcelable(id, 0); |
| 142 |
dest.writeString(summary); |
| 143 |
dest.writeString(line1); |
| 144 |
dest.writeString(line2); |
| 145 |
dest.writeInt(icon); |
| 146 |
dest.writeInt(state); |
| 147 |
dest.writeInt(this.flags); |
| 148 |
} |
| 149 |
|
| 150 |
@Override |
| 151 |
public String toString() { |
| 152 |
return new StringBuilder(Condition.class.getSimpleName()).append('[') |
| 153 |
.append("state=").append(stateToString(state)) |
| 154 |
.append(",id=").append(id) |
| 155 |
.append(",summary=").append(summary) |
| 156 |
.append(",line1=").append(line1) |
| 157 |
.append(",line2=").append(line2) |
| 158 |
.append(",icon=").append(icon) |
| 159 |
.append(",flags=").append(flags) |
| 160 |
.append(']').toString(); |
| 161 |
} |
| 162 |
|
| 163 |
|
| 164 |
public void dumpDebug(ProtoOutputStream proto, long fieldId) { |
| 165 |
final long token = proto.start(fieldId); |
| 166 |
|
| 167 |
|
| 168 |
proto.write(ConditionProto.ID, id.toString()); |
| 169 |
proto.write(ConditionProto.SUMMARY, summary); |
| 170 |
proto.write(ConditionProto.LINE_1, line1); |
| 171 |
proto.write(ConditionProto.LINE_2, line2); |
| 172 |
proto.write(ConditionProto.ICON, icon); |
| 173 |
proto.write(ConditionProto.STATE, state); |
| 174 |
proto.write(ConditionProto.FLAGS, flags); |
| 175 |
|
| 176 |
proto.end(token); |
| 177 |
} |
| 178 |
|
| 179 |
public static String stateToString(int state) { |
| 180 |
if (state == STATE_FALSE) return "STATE_FALSE"; |
| 181 |
if (state == STATE_TRUE) return "STATE_TRUE"; |
| 182 |
if (state == STATE_UNKNOWN) return "STATE_UNKNOWN"; |
| 183 |
if (state == STATE_ERROR) return "STATE_ERROR"; |
| 184 |
throw new IllegalArgumentException("state is invalid: " + state); |
| 185 |
} |
| 186 |
|
| 187 |
public static String relevanceToString(int flags) { |
| 188 |
final boolean now = (flags & FLAG_RELEVANT_NOW) != 0; |
| 189 |
final boolean always = (flags & FLAG_RELEVANT_ALWAYS) != 0; |
| 190 |
if (!now && !always) return "NONE"; |
| 191 |
if (now && always) return "NOW, ALWAYS"; |
| 192 |
return now ? "NOW" : "ALWAYS"; |
| 193 |
} |
| 194 |
|
| 195 |
@Override |
| 196 |
public boolean equals(@Nullable Object o) { |
| 197 |
if (!(o instanceof Condition)) return false; |
| 198 |
if (o == this) return true; |
| 199 |
final Condition other = (Condition) o; |
| 200 |
return Objects.equals(other.id, id) |
| 201 |
&& Objects.equals(other.summary, summary) |
| 202 |
&& Objects.equals(other.line1, line1) |
| 203 |
&& Objects.equals(other.line2, line2) |
| 204 |
&& other.icon == icon |
| 205 |
&& other.state == state |
| 206 |
&& other.flags == flags; |
| 207 |
} |
| 208 |
|
| 209 |
@Override |
| 210 |
public int hashCode() { |
| 211 |
return Objects.hash(id, summary, line1, line2, icon, state, flags); |
| 212 |
} |
| 213 |
|
| 214 |
@Override |
| 215 |
public int describeContents() { |
| 216 |
return 0; |
| 217 |
} |
| 218 |
|
| 219 |
public Condition copy() { |
| 220 |
final Parcel parcel = Parcel.obtain(); |
| 221 |
try { |
| 222 |
writeToParcel(parcel, 0); |
| 223 |
parcel.setDataPosition(0); |
| 224 |
return new Condition(parcel); |
| 225 |
} finally { |
| 226 |
parcel.recycle(); |
| 227 |
} |
| 228 |
} |
| 229 |
|
| 230 |
public static Uri.Builder newId(Context context) { |
| 231 |
return new Uri.Builder() |
| 232 |
.scheme(Condition.SCHEME) |
| 233 |
.authority(context.getPackageName()); |
| 234 |
} |
| 235 |
|
| 236 |
public static boolean isValidId(Uri id, String pkg) { |
| 237 |
return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority()); |
| 238 |
} |
| 239 |
|
| 240 |
public static final @android.annotation.NonNull Parcelable.Creator<Condition> CREATOR |
| 241 |
= new Parcelable.Creator<Condition>() { |
| 242 |
@Override |
| 243 |
public Condition createFromParcel(Parcel source) { |
| 244 |
return new Condition(source); |
| 245 |
} |
| 246 |
|
| 247 |
@Override |
| 248 |
public Condition[] newArray(int size) { |
| 249 |
return new Condition[size]; |
| 250 |
} |
| 251 |
}; |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
private static String getTrimmedString(String input) { |
| 257 |
if (input != null && input.length() > MAX_STRING_LENGTH) { |
| 258 |
return input.substring(0, MAX_STRING_LENGTH); |
| 259 |
} |
| 260 |
return input; |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
private static Uri getTrimmedUri(Uri input) { |
| 268 |
if (input != null && input.toString().length() > MAX_STRING_LENGTH) { |
| 269 |
return Uri.parse(getTrimmedString(input.toString())); |
| 270 |
} |
| 271 |
return input; |
| 272 |
} |
| 273 |
} |