Condition.java revision e77bb36d48b6b8b5c3bb6a1195aca469bb237919
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
19e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlockimport android.content.Context;
207340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockimport android.net.Uri;
217340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockimport android.os.Parcel;
227340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockimport android.os.Parcelable;
237340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
247340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockimport java.util.Objects;
257340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
267340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock/**
277340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * Condition information from condition providers.
287340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock *
297340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock * @hide
307340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock */
317340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlockpublic class Condition implements Parcelable {
327340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
33e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static final String SCHEME = "condition";
34e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
35e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static final int STATE_FALSE = 0;
36e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static final int STATE_TRUE = 1;
37e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static final int STATE_UNKNOWN = 2;
38e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static final int STATE_ERROR = 3;
39e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
407340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public static final int FLAG_RELEVANT_NOW = 1 << 0;
417340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public static final int FLAG_RELEVANT_ALWAYS = 1 << 1;
427340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
437340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public final Uri id;
447340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public String caption;
45e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public int state;
467340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public int flags;
477340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
48e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public Condition(Uri id, String caption, int state, int flags) {
497340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        if (id == null) throw new IllegalArgumentException("id is required");
507340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        if (caption == null) throw new IllegalArgumentException("caption is required");
51e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        if (!isValidState(state)) throw new IllegalArgumentException("state is invalid: " + state);
527340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        this.id = id;
537340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        this.caption = caption;
547340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        this.state = state;
557340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        this.flags = flags;
567340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
577340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
587340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    private Condition(Parcel source) {
59e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        this((Uri)source.readParcelable(Condition.class.getClassLoader()),
60e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock                source.readString(),
61e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock                source.readInt(),
62e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock                source.readInt());
63e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    }
64e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
65e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    private static boolean isValidState(int state) {
66e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        return state >= STATE_FALSE && state <= STATE_ERROR;
677340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
687340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
697340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    @Override
707340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public void writeToParcel(Parcel dest, int flags) {
717340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        dest.writeParcelable(id, 0);
727340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        dest.writeString(caption);
73e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        dest.writeInt(state);
747340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        dest.writeInt(flags);
757340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
767340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
777340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    @Override
787340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public String toString() {
797340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        return new StringBuilder(Condition.class.getSimpleName()).append('[')
807340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            .append("id=").append(id)
817340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            .append(",caption=").append(caption)
82e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock            .append(",state=").append(stateToString(state))
837340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            .append(",flags=").append(flags)
847340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            .append(']').toString();
857340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
867340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
87e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static String stateToString(int state) {
88e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        if (state == STATE_FALSE) return "STATE_FALSE";
89e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        if (state == STATE_TRUE) return "STATE_TRUE";
90e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        if (state == STATE_UNKNOWN) return "STATE_UNKNOWN";
91e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        if (state == STATE_ERROR) return "STATE_ERROR";
92e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        throw new IllegalArgumentException("state is invalid: " + state);
93e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    }
94e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
957340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    @Override
967340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public boolean equals(Object o) {
977340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        if (!(o instanceof Condition)) return false;
987340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        if (o == this) return true;
997340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        final Condition other = (Condition) o;
1007340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        return Objects.equals(other.id, id)
1017340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock                && Objects.equals(other.caption, caption)
1027340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock                && other.state == state
1037340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock                && other.flags == flags;
1047340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
1057340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
1067340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    @Override
1077340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public int hashCode() {
1087340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        return Objects.hash(id, caption, state, flags);
1097340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
1107340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
1117340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    @Override
1127340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public int describeContents() {
1137340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        return 0;
1147340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
1157340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
1167340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public Condition copy() {
1177340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        final Parcel parcel = Parcel.obtain();
1187340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        try {
1197340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            writeToParcel(parcel, 0);
1207340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            parcel.setDataPosition(0);
1217340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            return new Condition(parcel);
1227340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        } finally {
1237340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            parcel.recycle();
1247340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        }
1257340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    }
1267340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
127e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static Uri.Builder newId(Context context) {
128e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        return new Uri.Builder().scheme(SCHEME).authority(context.getPackageName());
129e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    }
130e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
131e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    public static boolean isValidId(Uri id, String pkg) {
132e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock        return id != null && id.getScheme().equals(SCHEME) && id.getAuthority().equals(pkg);
133e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock    }
134e77bb36d48b6b8b5c3bb6a1195aca469bb237919John Spurlock
1357340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    public static final Parcelable.Creator<Condition> CREATOR
1367340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            = new Parcelable.Creator<Condition>() {
1377340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        @Override
1387340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        public Condition createFromParcel(Parcel source) {
1397340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            return new Condition(source);
1407340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        }
1417340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock
1427340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        @Override
1437340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        public Condition[] newArray(int size) {
1447340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock            return new Condition[size];
1457340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock        }
1467340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock    };
1477340fc8665ae3f9f1978f42aa0e5e1da85036158John Spurlock}
148