17340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock/**
27340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * Copyright (c) 2014, The Android Open Source Project
37340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock *
47340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * Licensed under the Apache License, Version 2.0 (the "License");
57340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * you may not use this file except in compliance with the License.
67340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * You may obtain a copy of the License at
77340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock *
87340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock *     http://www.apache.org/licenses/LICENSE-2.0
97340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock *
107340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * Unless required by applicable law or agreed to in writing, software
117340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * distributed under the License is distributed on an "AS IS" BASIS,
127340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
137340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * See the License for the specific language governing permissions and
147340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * limitations under the License.
157340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock */
167340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
177340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockpackage android.service.notification;
187340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
199034f2452b94363bf4c868a32c4f7fed4ad6dc6aJohn Spurlockimport android.annotation.SystemApi;
20e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlockimport android.content.Context;
217340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockimport android.net.Uri;
227340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockimport android.os.Parcel;
237340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockimport android.os.Parcelable;
247340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
257340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockimport java.util.Objects;
267340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
277340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock/**
287340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * Condition information from condition providers.
297340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock *
307340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * @hide
317340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock */
329034f2452b94363bf4c868a32c4f7fed4ad6dc6aJohn Spurlock@SystemApi
337340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockpublic class Condition implements Parcelable {
347340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
35e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static final String SCHEME = "condition";
36e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
37e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static final int STATE_FALSE = 0;
38e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static final int STATE_TRUE = 1;
39e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static final int STATE_UNKNOWN = 2;
40e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static final int STATE_ERROR = 3;
41e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
427340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public static final int FLAG_RELEVANT_NOW = 1 << 0;
437340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public static final int FLAG_RELEVANT_ALWAYS = 1 << 1;
447340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
457340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public final Uri id;
46ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock    public final String summary;
47ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock    public final String line1;
48ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock    public final String line2;
49ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock    public final int icon;
50ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock    public final int state;
51ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock    public final int flags;
52ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock
53ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock    public Condition(Uri id, String summary, String line1, String line2, int icon,
54ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock            int state, int flags) {
557340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        if (id == null) throw new IllegalArgumentException("id is required");
56ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        if (summary == null) throw new IllegalArgumentException("summary is required");
57ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        if (line1 == null) throw new IllegalArgumentException("line1 is required");
58ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        if (line2 == null) throw new IllegalArgumentException("line2 is required");
59e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state);
607340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        this.id = id;
61ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        this.summary = summary;
62ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        this.line1 = line1;
63ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        this.line2 = line2;
64ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        this.icon = icon;
657340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        this.state = state;
667340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        this.flags = flags;
677340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
687340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
697340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    private Condition(Parcel source) {
70e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        this((Uri)source.readParcelable(Condition.class.getClassLoader()),
71e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock                source.readString(),
72ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock                source.readString(),
73ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock                source.readString(),
74ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock                source.readInt(),
75e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock                source.readInt(),
76e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock                source.readInt());
77e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    }
78e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
79e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    private static boolean isValidState(int state) {
80e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        return state >= STATE_FALSE && state <= STATE_ERROR;
817340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
827340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
837340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    @Override
847340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public void writeToParcel(Parcel dest, int flags) {
857340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        dest.writeParcelable(id, 0);
86ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        dest.writeString(summary);
87ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        dest.writeString(line1);
88ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        dest.writeString(line2);
89ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        dest.writeInt(icon);
90e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        dest.writeInt(state);
913b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        dest.writeInt(this.flags);
927340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
937340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
947340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    @Override
957340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public String toString() {
967340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        return new StringBuilder(Condition.class.getSimpleName()).append('[')
977340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            .append("id=").append(id)
98ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock            .append(",summary=").append(summary)
99ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock            .append(",line1=").append(line1)
100ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock            .append(",line2=").append(line2)
101ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock            .append(",icon=").append(icon)
102e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock            .append(",state=").append(stateToString(state))
1037340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            .append(",flags=").append(flags)
1047340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            .append(']').toString();
1057340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
1067340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
107e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static String stateToString(int state) {
108e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        if (state == STATE_FALSE) return "STATE_FALSE";
109e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        if (state == STATE_TRUE) return "STATE_TRUE";
110e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        if (state == STATE_UNKNOWN) return "STATE_UNKNOWN";
111e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        if (state == STATE_ERROR) return "STATE_ERROR";
112e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        throw new IllegalArgumentException("state is invalid: " + state);
113e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    }
114e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
1153b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    public static String relevanceToString(int flags) {
1163b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        final boolean now = (flags & FLAG_RELEVANT_NOW) != 0;
1173b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        final boolean always = (flags & FLAG_RELEVANT_ALWAYS) != 0;
1183b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (!now && !always) return "NONE";
1193b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        if (now && always) return "NOW, ALWAYS";
1203b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock        return now ? "NOW" : "ALWAYS";
1213b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock    }
1223b98b3f1f85aff0c84ebef4dd497c146d1b4d248John Spurlock
1237340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    @Override
1247340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public boolean equals(Object o) {
1257340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        if (!(o instanceof Condition)) return false;
1267340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        if (o == this) return true;
1277340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        final Condition other = (Condition) o;
1287340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        return Objects.equals(other.id, id)
129ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock                && Objects.equals(other.summary, summary)
130ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock                && Objects.equals(other.line1, line1)
131ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock                && Objects.equals(other.line2, line2)
132ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock                && other.icon == icon
1337340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock                && other.state == state
1347340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock                && other.flags == flags;
1357340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
1367340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
1377340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    @Override
1387340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public int hashCode() {
139ef5693bb73ab90cc30ec9b11ee78f4351a36719aJohn Spurlock        return Objects.hash(id, summary, line1, line2, icon, state, flags);
1407340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
1417340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
1427340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    @Override
1437340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public int describeContents() {
1447340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        return 0;
1457340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
1467340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
1477340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public Condition copy() {
1487340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        final Parcel parcel = Parcel.obtain();
1497340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        try {
1507340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            writeToParcel(parcel, 0);
1517340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            parcel.setDataPosition(0);
1527340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            return new Condition(parcel);
1537340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        } finally {
1547340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            parcel.recycle();
1557340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        }
1567340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
1577340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
158e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static Uri.Builder newId(Context context) {
159e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        return new Uri.Builder().scheme(SCHEME).authority(context.getPackageName());
160e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    }
161e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
162e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static boolean isValidId(Uri id, String pkg) {
163e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        return id != null && id.getScheme().equals(SCHEME) && id.getAuthority().equals(pkg);
164e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    }
165e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
1667340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public static final Parcelable.Creator<Condition> CREATOR
1677340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            = new Parcelable.Creator<Condition>() {
1687340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        @Override
1697340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        public Condition createFromParcel(Parcel source) {
1707340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            return new Condition(source);
1717340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        }
1727340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
1737340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        @Override
1747340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        public Condition[] newArray(int size) {
1757340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            return new Condition[size];
1767340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        }
1777340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    };
1787340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock}
179