StatusHints.java revision 03d30a573b8bc8e169e153a0fffa053ffedcd5ee
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.telecomm;
18
19import android.content.ComponentName;
20import android.content.Context;
21import android.content.pm.PackageManager;
22import android.graphics.drawable.Drawable;
23import android.os.Bundle;
24import android.os.Parcel;
25import android.os.Parcelable;
26
27import java.util.MissingResourceException;
28import java.util.Objects;
29
30/**
31 * Contains status label and icon displayed in the in-call UI.
32 */
33public final class StatusHints implements Parcelable {
34
35    private final ComponentName mComponentName;
36    private final CharSequence mLabel;
37    private final int mIconResId;
38    private final Bundle mExtras;
39
40    public StatusHints(ComponentName componentName, CharSequence label, int iconResId, Bundle extras) {
41        mComponentName = componentName;
42        mLabel = label;
43        mIconResId = iconResId;
44        mExtras = extras;
45    }
46
47    /**
48     * @return A component used to load the icon.
49     */
50    public ComponentName getComponentName() {
51        return mComponentName;
52    }
53
54    /**
55     * @return The label displayed in the in-call UI.
56     */
57    public CharSequence getLabel() {
58        return mLabel;
59    }
60
61    /**
62     * The icon resource ID for the icon to show.
63     *
64     * @return A resource ID.
65     */
66    public int getIconResId() {
67        return mIconResId;
68    }
69
70    /**
71     * @return An icon displayed in the in-call UI.
72     */
73    public Drawable getIcon(Context context) {
74        return getIcon(context, mIconResId);
75    }
76
77    /**
78     * @return Extra data used to display status.
79     */
80    public Bundle getExtras() {
81        return mExtras;
82    }
83
84    @Override
85    public int describeContents() {
86        return 0;
87    }
88
89    @Override
90    public void writeToParcel(Parcel out, int flags) {
91        out.writeParcelable(mComponentName, flags);
92        out.writeCharSequence(mLabel);
93        out.writeInt(mIconResId);
94        out.writeParcelable(mExtras, 0);
95    }
96
97    public static final Creator<StatusHints> CREATOR
98            = new Creator<StatusHints>() {
99        public StatusHints createFromParcel(Parcel in) {
100            return new StatusHints(in);
101        }
102
103        public StatusHints[] newArray(int size) {
104            return new StatusHints[size];
105        }
106    };
107
108    private StatusHints(Parcel in) {
109        mComponentName = in.readParcelable(getClass().getClassLoader());
110        mLabel = in.readCharSequence();
111        mIconResId = in.readInt();
112        mExtras = in.readParcelable(getClass().getClassLoader());
113    }
114
115    private Drawable getIcon(Context context, int resId) {
116        Context packageContext;
117        try {
118            packageContext = context.createPackageContext(mComponentName.getPackageName(), 0);
119        } catch (PackageManager.NameNotFoundException e) {
120            Log.e(this, e, "Cannot find package %s", mComponentName.getPackageName());
121            return null;
122        }
123        try {
124            return packageContext.getDrawable(resId);
125        } catch (MissingResourceException e) {
126            Log.e(this, e, "Cannot find icon %d in package %s",
127                    resId, mComponentName.getPackageName());
128            return null;
129        }
130    }
131
132    @Override
133    public boolean equals(Object other) {
134        if (other != null && other instanceof StatusHints) {
135            StatusHints otherHints = (StatusHints) other;
136            return Objects.equals(otherHints.getComponentName(), getComponentName()) &&
137                    Objects.equals(otherHints.getLabel(), getLabel()) &&
138                    otherHints.getIconResId() == getIconResId() &&
139                    Objects.equals(otherHints.getExtras(), getExtras());
140        }
141        return false;
142    }
143
144    @Override
145    public int hashCode() {
146        return Objects.hashCode(mComponentName) + Objects.hashCode(mLabel) + mIconResId +
147                Objects.hashCode(mExtras);
148    }
149}
150