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