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