Condition.java revision ef5693bb73ab90cc30ec9b11ee78f4351a36719a
1/** 2 * Copyright (c) 2014, 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 17package android.service.notification; 18 19import android.content.Context; 20import android.net.Uri; 21import android.os.Parcel; 22import android.os.Parcelable; 23 24import java.util.Objects; 25 26/** 27 * Condition information from condition providers. 28 * 29 * @hide 30 */ 31public class Condition implements Parcelable { 32 33 public static final String SCHEME = "condition"; 34 35 public static final int STATE_FALSE = 0; 36 public static final int STATE_TRUE = 1; 37 public static final int STATE_UNKNOWN = 2; 38 public static final int STATE_ERROR = 3; 39 40 public static final int FLAG_RELEVANT_NOW = 1 << 0; 41 public static final int FLAG_RELEVANT_ALWAYS = 1 << 1; 42 43 public final Uri id; 44 public final String summary; 45 public final String line1; 46 public final String line2; 47 public final int icon; 48 public final int state; 49 public final int flags; 50 51 public Condition(Uri id, String summary, String line1, String line2, int icon, 52 int state, int flags) { 53 if (id == null) throw new IllegalArgumentException("id is required"); 54 if (summary == null) throw new IllegalArgumentException("summary is required"); 55 if (line1 == null) throw new IllegalArgumentException("line1 is required"); 56 if (line2 == null) throw new IllegalArgumentException("line2 is required"); 57 if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state); 58 this.id = id; 59 this.summary = summary; 60 this.line1 = line1; 61 this.line2 = line2; 62 this.icon = icon; 63 this.state = state; 64 this.flags = flags; 65 } 66 67 private Condition(Parcel source) { 68 this((Uri)source.readParcelable(Condition.class.getClassLoader()), 69 source.readString(), 70 source.readString(), 71 source.readString(), 72 source.readInt(), 73 source.readInt(), 74 source.readInt()); 75 } 76 77 private static boolean isValidState(int state) { 78 return state >= STATE_FALSE && state <= STATE_ERROR; 79 } 80 81 @Override 82 public void writeToParcel(Parcel dest, int flags) { 83 dest.writeParcelable(id, 0); 84 dest.writeString(summary); 85 dest.writeString(line1); 86 dest.writeString(line2); 87 dest.writeInt(icon); 88 dest.writeInt(state); 89 dest.writeInt(this.flags); 90 } 91 92 @Override 93 public String toString() { 94 return new StringBuilder(Condition.class.getSimpleName()).append('[') 95 .append("id=").append(id) 96 .append(",summary=").append(summary) 97 .append(",line1=").append(line1) 98 .append(",line2=").append(line2) 99 .append(",icon=").append(icon) 100 .append(",state=").append(stateToString(state)) 101 .append(",flags=").append(flags) 102 .append(']').toString(); 103 } 104 105 public static String stateToString(int state) { 106 if (state == STATE_FALSE) return "STATE_FALSE"; 107 if (state == STATE_TRUE) return "STATE_TRUE"; 108 if (state == STATE_UNKNOWN) return "STATE_UNKNOWN"; 109 if (state == STATE_ERROR) return "STATE_ERROR"; 110 throw new IllegalArgumentException("state is invalid: " + state); 111 } 112 113 public static String relevanceToString(int flags) { 114 final boolean now = (flags & FLAG_RELEVANT_NOW) != 0; 115 final boolean always = (flags & FLAG_RELEVANT_ALWAYS) != 0; 116 if (!now && !always) return "NONE"; 117 if (now && always) return "NOW, ALWAYS"; 118 return now ? "NOW" : "ALWAYS"; 119 } 120 121 @Override 122 public boolean equals(Object o) { 123 if (!(o instanceof Condition)) return false; 124 if (o == this) return true; 125 final Condition other = (Condition) o; 126 return Objects.equals(other.id, id) 127 && Objects.equals(other.summary, summary) 128 && Objects.equals(other.line1, line1) 129 && Objects.equals(other.line2, line2) 130 && other.icon == icon 131 && other.state == state 132 && other.flags == flags; 133 } 134 135 @Override 136 public int hashCode() { 137 return Objects.hash(id, summary, line1, line2, icon, state, flags); 138 } 139 140 @Override 141 public int describeContents() { 142 return 0; 143 } 144 145 public Condition copy() { 146 final Parcel parcel = Parcel.obtain(); 147 try { 148 writeToParcel(parcel, 0); 149 parcel.setDataPosition(0); 150 return new Condition(parcel); 151 } finally { 152 parcel.recycle(); 153 } 154 } 155 156 public static Uri.Builder newId(Context context) { 157 return new Uri.Builder().scheme(SCHEME).authority(context.getPackageName()); 158 } 159 160 public static boolean isValidId(Uri id, String pkg) { 161 return id != null && id.getScheme().equals(SCHEME) && id.getAuthority().equals(pkg); 162 } 163 164 public static final Parcelable.Creator<Condition> CREATOR 165 = new Parcelable.Creator<Condition>() { 166 @Override 167 public Condition createFromParcel(Parcel source) { 168 return new Condition(source); 169 } 170 171 @Override 172 public Condition[] newArray(int size) { 173 return new Condition[size]; 174 } 175 }; 176} 177