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