Condition.java revision a62496d8f7cb9048331451af07466b1edc568c7d
126c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale/**
226c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * Copyright (c) 2014, The Android Open Source Project
326c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale *
426c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * Licensed under the Apache License, Version 2.0 (the "License");
526c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * you may not use this file except in compliance with the License.
626c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * You may obtain a copy of the License at
726c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale *
826c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale *     http://www.apache.org/licenses/LICENSE-2.0
926c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale *
1026c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * Unless required by applicable law or agreed to in writing, software
1126c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * distributed under the License is distributed on an "AS IS" BASIS,
1226c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1326c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * See the License for the specific language governing permissions and
1426c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * limitations under the License.
1526c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale */
1626c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
1726c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwalepackage android.service.notification;
1826c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
1926c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwaleimport android.annotation.SystemApi;
2026c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwaleimport android.content.Context;
2126c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwaleimport android.net.Uri;
2226c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwaleimport android.os.Parcel;
2326c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwaleimport android.os.Parcelable;
2426c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
2526c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwaleimport java.util.Objects;
2626c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
2726c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale/**
2826c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * Condition information from condition providers. Used to tell the system to enter Do Not Disturb
2926c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale * mode and request that the system exit Do Not Disturb mode.
3026c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale */
3126c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwalepublic class Condition implements Parcelable {
3226c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
3326c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public static final String SCHEME = "condition";
3426c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
3526c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public static final int STATE_FALSE = 0;
3626c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public static final int STATE_TRUE = 1;
3726c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public static final int STATE_UNKNOWN = 2;
3826c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public static final int STATE_ERROR = 3;
3926c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
4026c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public static final int FLAG_RELEVANT_NOW = 1 << 0;
4126c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public static final int FLAG_RELEVANT_ALWAYS = 1 << 1;
4226c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
4326c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    /**
4426c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * The URI representing the condition being updated.
4526c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * See {@link android.app.AutomaticZenRule#getConditionId()}.
4626c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     */
4726c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public final Uri id;
4826c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
4926c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    /**
5026c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * A summary of what the rule encoded in {@link #id} means when it is enabled. User visible
5126c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * if the state of the condition is {@link #STATE_TRUE}.
5226c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     */
5326c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public final String summary;
5426c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
5526c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    /**
5626c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * Additional information about what the rule encoded in {@link #id} means when it is enabled.
5726c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * User visible if the state of the condition is {@link #STATE_TRUE}.
5826c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     */
5926c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public final String line1;
6026c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
6126c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    /**
6226c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * Additional information about what the rule encoded in {@link #id} means when it is enabled.
6326c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * User visible if the state of the condition is {@link #STATE_TRUE}.
6426c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     */
6526c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public final String line2;
6626c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
6726c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    /**
6826c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * The state of this condition. {@link #STATE_TRUE} will enable Do Not Disturb mode. Any other
6926c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * state will turn Do Not Disturb off for this rule. Note that Do Not Disturb might still be
7026c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     * enabled globally if other conditions are in a {@link #STATE_TRUE} state.
7126c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale     */
7226c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public final int state;
7326c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
7426c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    @SystemApi
7526c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public final int flags;
7626c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    @SystemApi
7726c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public final int icon;
7826c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
7926c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    public Condition(Uri id, String summary, String line1, String line2, int state) {
8026c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale        this(id, summary, line1, line2, -1, state, FLAG_RELEVANT_ALWAYS);
8126c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    }
8226c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale
8326c0dfed7a0cd54abafdd0ccbb5b757506d51c76Wale Ogunwale    @SystemApi
84    public Condition(Uri id, String summary, String line1, String line2, int icon,
85            int state, int flags) {
86        if (id == null) throw new IllegalArgumentException("id is required");
87        if (summary == null) throw new IllegalArgumentException("summary is required");
88        if (line1 == null) throw new IllegalArgumentException("line1 is required");
89        if (line2 == null) throw new IllegalArgumentException("line2 is required");
90        if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state);
91        this.id = id;
92        this.summary = summary;
93        this.line1 = line1;
94        this.line2 = line2;
95        this.icon = icon;
96        this.state = state;
97        this.flags = flags;
98    }
99
100    private Condition(Parcel source) {
101        this((Uri)source.readParcelable(Condition.class.getClassLoader()),
102                source.readString(),
103                source.readString(),
104                source.readString(),
105                source.readInt(),
106                source.readInt(),
107                source.readInt());
108    }
109
110    private static boolean isValidState(int state) {
111        return state >= STATE_FALSE && state <= STATE_ERROR;
112    }
113
114    @Override
115    public void writeToParcel(Parcel dest, int flags) {
116        dest.writeParcelable(id, 0);
117        dest.writeString(summary);
118        dest.writeString(line1);
119        dest.writeString(line2);
120        dest.writeInt(icon);
121        dest.writeInt(state);
122        dest.writeInt(this.flags);
123    }
124
125    @Override
126    public String toString() {
127        return new StringBuilder(Condition.class.getSimpleName()).append('[')
128            .append("id=").append(id)
129            .append(",summary=").append(summary)
130            .append(",line1=").append(line1)
131            .append(",line2=").append(line2)
132            .append(",icon=").append(icon)
133            .append(",state=").append(stateToString(state))
134            .append(",flags=").append(flags)
135            .append(']').toString();
136    }
137
138    public static String stateToString(int state) {
139        if (state == STATE_FALSE) return "STATE_FALSE";
140        if (state == STATE_TRUE) return "STATE_TRUE";
141        if (state == STATE_UNKNOWN) return "STATE_UNKNOWN";
142        if (state == STATE_ERROR) return "STATE_ERROR";
143        throw new IllegalArgumentException("state is invalid: " + state);
144    }
145
146    public static String relevanceToString(int flags) {
147        final boolean now = (flags & FLAG_RELEVANT_NOW) != 0;
148        final boolean always = (flags & FLAG_RELEVANT_ALWAYS) != 0;
149        if (!now && !always) return "NONE";
150        if (now && always) return "NOW, ALWAYS";
151        return now ? "NOW" : "ALWAYS";
152    }
153
154    @Override
155    public boolean equals(Object o) {
156        if (!(o instanceof Condition)) return false;
157        if (o == this) return true;
158        final Condition other = (Condition) o;
159        return Objects.equals(other.id, id)
160                && Objects.equals(other.summary, summary)
161                && Objects.equals(other.line1, line1)
162                && Objects.equals(other.line2, line2)
163                && other.icon == icon
164                && other.state == state
165                && other.flags == flags;
166    }
167
168    @Override
169    public int hashCode() {
170        return Objects.hash(id, summary, line1, line2, icon, state, flags);
171    }
172
173    @Override
174    public int describeContents() {
175        return 0;
176    }
177
178    public Condition copy() {
179        final Parcel parcel = Parcel.obtain();
180        try {
181            writeToParcel(parcel, 0);
182            parcel.setDataPosition(0);
183            return new Condition(parcel);
184        } finally {
185            parcel.recycle();
186        }
187    }
188
189    public static Uri.Builder newId(Context context) {
190        return new Uri.Builder().scheme(SCHEME).authority(context.getPackageName());
191    }
192
193    public static boolean isValidId(Uri id, String pkg) {
194        return id != null && SCHEME.equals(id.getScheme()) && pkg.equals(id.getAuthority());
195    }
196
197    public static final Parcelable.Creator<Condition> CREATOR
198            = new Parcelable.Creator<Condition>() {
199        @Override
200        public Condition createFromParcel(Parcel source) {
201            return new Condition(source);
202        }
203
204        @Override
205        public Condition[] newArray(int size) {
206            return new Condition[size];
207        }
208    };
209}
210