1/*
2 * Copyright 2018 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 androidx.core.app;
18
19import android.os.Bundle;
20
21import androidx.annotation.NonNull;
22import androidx.annotation.Nullable;
23import androidx.core.graphics.drawable.IconCompat;
24
25/**
26 * Provides an immutable reference to an entity that appears repeatedly on different surfaces of the
27 * platform. For example, this could represent the sender of a message.
28 */
29public class Person {
30    private static final String NAME_KEY = "name";
31    private static final String ICON_KEY = "icon";
32    private static final String URI_KEY = "uri";
33    private static final String KEY_KEY = "key";
34    private static final String IS_BOT_KEY = "isBot";
35    private static final String IS_IMPORTANT_KEY = "isImportant";
36
37    /**
38     * Extracts and returns the {@link Person} written to the {@code bundle}. A bundle can be
39     * created from a {@link Person} using {@link #toBundle()}.
40     */
41    public static Person fromBundle(Bundle bundle) {
42        Bundle iconBundle = bundle.getBundle(ICON_KEY);
43        return new Builder()
44                .setName(bundle.getCharSequence(NAME_KEY))
45                .setIcon(iconBundle != null ? IconCompat.createFromBundle(iconBundle) : null)
46                .setUri(bundle.getString(URI_KEY))
47                .setKey(bundle.getString(KEY_KEY))
48                .setBot(bundle.getBoolean(IS_BOT_KEY))
49                .setImportant(bundle.getBoolean(IS_IMPORTANT_KEY))
50                .build();
51    }
52
53    @Nullable private CharSequence mName;
54    @Nullable private IconCompat mIcon;
55    @Nullable private String mUri;
56    @Nullable private String mKey;
57    private boolean mIsBot;
58    private boolean mIsImportant;
59
60    private Person(Builder builder) {
61        mName = builder.mName;
62        mIcon = builder.mIcon;
63        mUri = builder.mUri;
64        mKey = builder.mKey;
65        mIsBot = builder.mIsBot;
66        mIsImportant = builder.mIsImportant;
67    }
68
69    /**
70     * Writes and returns a new {@link Bundle} that represents this {@link Person}. This bundle can
71     * be converted back by using {@link #fromBundle(Bundle)}.
72     */
73    public Bundle toBundle() {
74        Bundle result = new Bundle();
75        result.putCharSequence(NAME_KEY, mName);
76        result.putBundle(ICON_KEY, mIcon != null ? mIcon.toBundle() : null);
77        result.putString(URI_KEY, mUri);
78        result.putString(KEY_KEY, mKey);
79        result.putBoolean(IS_BOT_KEY, mIsBot);
80        result.putBoolean(IS_IMPORTANT_KEY, mIsImportant);
81        return result;
82    }
83
84    /** Creates and returns a new {@link Builder} initialized with this Person's data. */
85    public Builder toBuilder() {
86        return new Builder(this);
87    }
88
89    /**
90     * Returns the name for this {@link Person} or {@code null} if no name was provided. This could
91     * be a full name, nickname, username, etc.
92     */
93    @Nullable
94    public CharSequence getName() {
95        return mName;
96    }
97
98    /** Returns the icon for this {@link Person} or {@code null} if no icon was provided. */
99    @Nullable
100    public IconCompat getIcon() {
101        return mIcon;
102    }
103
104    /**
105     * Returns the raw URI for this {@link Person} or {@code null} if no URI was provided. A URI can
106     * be any of the following:
107     * <ul>
108     *     <li>The {@code String} representation of a
109     *     {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}</li>
110     *     <li>A {@code mailto:} schema*</li>
111     *     <li>A {@code tel:} schema*</li>
112     * </ul>
113     *
114     * <p>*Note for these schemas, the path portion of the URI must exist in the contacts
115     * database in their appropriate column, otherwise the reference should be discarded.
116     */
117    @Nullable
118    public String getUri() {
119        return mUri;
120    }
121
122    /**
123     * Returns the key for this {@link Person} or {@code null} if no key was provided. This is
124     * provided as a unique identifier between other {@link Person}s.
125     */
126    @Nullable
127    public String getKey() {
128        return mKey;
129    }
130
131    /**
132     * Returns whether or not this {@link Person} is a machine rather than a human. Used primarily
133     * to identify automated tooling.
134     */
135    public boolean isBot() {
136        return mIsBot;
137    }
138
139    /**
140     * Returns whether or not this {@link Person} is important to the user of this device with
141     * regards to how frequently they interact.
142     */
143    public boolean isImportant() {
144        return mIsImportant;
145    }
146
147    /** Builder for the immutable {@link Person} class. */
148    public static class Builder {
149        @Nullable private CharSequence mName;
150        @Nullable private IconCompat mIcon;
151        @Nullable private String mUri;
152        @Nullable private String mKey;
153        private boolean mIsBot;
154        private boolean mIsImportant;
155
156        /** Creates a new, empty {@link Builder}. */
157        public Builder() { }
158
159        private Builder(Person person) {
160            mName = person.mName;
161            mIcon = person.mIcon;
162            mUri = person.mUri;
163            mKey = person.mKey;
164            mIsBot = person.mIsBot;
165            mIsImportant = person.mIsImportant;
166        }
167
168        /**
169         * Give this {@link Person} a name to use for display. This can be, for example, a full
170         * name, nickname, username, etc.
171         */
172        @NonNull
173        public Builder setName(@Nullable CharSequence name) {
174            mName = name;
175            return this;
176        }
177
178        /**
179         * Set an icon for this {@link Person}.
180         *
181         * <p>The system will prefer this icon over any images that are resolved from
182         * {@link #setUri(String)}.
183         */
184        @NonNull
185        public Builder setIcon(@Nullable IconCompat icon) {
186            mIcon = icon;
187            return this;
188        }
189
190        /**
191         * Set a URI for this {@link Person} which can be any of the following:
192         * <ul>
193         *     <li>The {@code String} representation of a
194         *     {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}</li>
195         *     <li>A {@code mailto:} schema*</li>
196         *     <li>A {@code tel:} schema*</li>
197         * </ul>
198         *
199         * <p>*Note for these schemas, the path portion of the URI must exist in the contacts
200         * database in their appropriate column, otherwise the reference will be discarded.
201         */
202        @NonNull
203        public Builder setUri(@Nullable String uri) {
204            mUri = uri;
205            return this;
206        }
207
208        /**
209         * Set a unique identifier for this {@link Person}. This is especially useful if the
210         * {@link #setName(CharSequence)} value isn't unique. This value is preferred for
211         * identification, but if it's not provided, the person's name will be used in its place.
212         */
213        @NonNull
214        public Builder setKey(@Nullable String key) {
215            mKey = key;
216            return this;
217        }
218
219        /**
220         * Sets whether or not this {@link Person} represents a machine rather than a human. This is
221         * used primarily for testing and automated tooling.
222         */
223        @NonNull
224        public Builder setBot(boolean bot) {
225            mIsBot = bot;
226            return this;
227        }
228
229        /**
230         * Sets whether this is an important person. Use this method to denote users who frequently
231         * interact with the user of this device when {@link #setUri(String)} isn't provided with
232         * {@link android.provider.ContactsContract.Contacts#CONTENT_LOOKUP_URI}, and instead with
233         * the {@code mailto:} or {@code tel:} schemas.
234         */
235        @NonNull
236        public Builder setImportant(boolean important) {
237            mIsImportant = important;
238            return this;
239        }
240
241        /** Creates and returns the {@link Person} this builder represents. */
242        @NonNull
243        public Person build() {
244            return new Person(this);
245        }
246    }
247}
248