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