Condition.java revision e77bb36d48b6b8b5c3bb6a1195aca469bb237919
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(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    @Override
96    public boolean equals(Object o) {
97        if (!(o instanceof Condition)) return false;
98        if (o == this) return true;
99        final Condition other = (Condition) o;
100        return Objects.equals(other.id, id)
101                && Objects.equals(other.caption, caption)
102                && other.state == state
103                && other.flags == flags;
104    }
105
106    @Override
107    public int hashCode() {
108        return Objects.hash(id, caption, state, flags);
109    }
110
111    @Override
112    public int describeContents() {
113        return 0;
114    }
115
116    public Condition copy() {
117        final Parcel parcel = Parcel.obtain();
118        try {
119            writeToParcel(parcel, 0);
120            parcel.setDataPosition(0);
121            return new Condition(parcel);
122        } finally {
123            parcel.recycle();
124        }
125    }
126
127    public static Uri.Builder newId(Context context) {
128        return new Uri.Builder().scheme(SCHEME).authority(context.getPackageName());
129    }
130
131    public static boolean isValidId(Uri id, String pkg) {
132        return id != null && id.getScheme().equals(SCHEME) && id.getAuthority().equals(pkg);
133    }
134
135    public static final Parcelable.Creator<Condition> CREATOR
136            = new Parcelable.Creator<Condition>() {
137        @Override
138        public Condition createFromParcel(Parcel source) {
139            return new Condition(source);
140        }
141
142        @Override
143        public Condition[] newArray(int size) {
144            return new Condition[size];
145        }
146    };
147}
148