1 /*
2  * Copyright (C) 2019 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 
17 package com.android.car.telephony.common;
18 
19 import android.content.res.Resources;
20 import android.database.Cursor;
21 import android.net.Uri;
22 import android.os.Parcel;
23 import android.os.Parcelable;
24 import android.provider.ContactsContract;
25 import android.util.Log;
26 
27 import androidx.annotation.Nullable;
28 
29 /**
30  * Encapsulates data about an address entry. Typically loaded from the local Address store.
31  */
32 public class PostalAddress implements Parcelable {
33     private static final String TAG = "CD.PostalAddress";
34 
35     /**
36      * The formatted address.
37      */
38     private String mFormattedAddress;
39 
40     /**
41      * The address type. See more at {@link ContactsContract.CommonDataKinds.StructuredPostal#TYPE}
42      */
43     private int mType;
44 
45     /**
46      * The user defined label. See more at
47      * {@link ContactsContract.CommonDataKinds.StructuredPostal#LABEL}
48      */
49     @Nullable
50     private String mLabel;
51 
52     /**
53      * Parses a PostalAddress entry for a Cursor loaded from the Address Database.
54      */
fromCursor(Cursor cursor)55     public static PostalAddress fromCursor(Cursor cursor) {
56         int formattedAddressColumn = cursor.getColumnIndex(
57                 ContactsContract.CommonDataKinds.StructuredPostal.FORMATTED_ADDRESS);
58         int addressTypeColumn = cursor.getColumnIndex(
59                 ContactsContract.CommonDataKinds.StructuredPostal.TYPE);
60         int labelColumn = cursor.getColumnIndex(
61                 ContactsContract.CommonDataKinds.StructuredPostal.LABEL);
62 
63         PostalAddress postalAddress = new PostalAddress();
64         postalAddress.mFormattedAddress = cursor.getString(formattedAddressColumn);
65         postalAddress.mType = cursor.getInt(addressTypeColumn);
66         postalAddress.mLabel = cursor.getString(labelColumn);
67 
68         return postalAddress;
69     }
70 
71     @Override
equals(Object obj)72     public boolean equals(Object obj) {
73         return obj instanceof PostalAddress
74                 && mFormattedAddress.equals(((PostalAddress) obj).mFormattedAddress);
75     }
76 
77     /**
78      * Returns {@link #mFormattedAddress}
79      */
getFormattedAddress()80     public String getFormattedAddress() {
81         return mFormattedAddress;
82     }
83 
84     /**
85      * Returns {@link #mType}
86      */
getType()87     public int getType() {
88         return mType;
89     }
90 
91     /**
92      * Returns {@link #mLabel}
93      */
94     @Nullable
getLabel()95     public String getLabel() {
96         return mLabel;
97     }
98 
99     /**
100      * Returns a human readable string label. For example, Home, Work, etc.
101      */
getReadableLabel(Resources res)102     public CharSequence getReadableLabel(Resources res) {
103         return ContactsContract.CommonDataKinds.StructuredPostal.getTypeLabel(res, mType, mLabel);
104     }
105 
106     /**
107      * Returns the address Uri for {@link #mFormattedAddress}.
108      */
getAddressUri(Resources res)109     public Uri getAddressUri(Resources res) {
110         String address = String.format(res.getString(R.string.address_uri_format),
111                 Uri.encode(mFormattedAddress));
112         Log.d(TAG, "The address is: " + address);
113         return Uri.parse(address);
114     }
115 
116     /**
117      * Returns the navigation Uri for {@link #mFormattedAddress}.
118      */
getNavigationUri(Resources res)119     public Uri getNavigationUri(Resources res) {
120         String address = String.format(res.getString(R.string.navigation_uri_format),
121                 Uri.encode(mFormattedAddress));
122         Log.d(TAG, "The address is: " + address);
123         return Uri.parse(address);
124     }
125 
126     @Override
describeContents()127     public int describeContents() {
128         return 0;
129     }
130 
131     @Override
writeToParcel(Parcel dest, int flags)132     public void writeToParcel(Parcel dest, int flags) {
133         dest.writeInt(mType);
134         dest.writeString(mLabel);
135         dest.writeString(mFormattedAddress);
136     }
137 
138     /**
139      * Create {@link PostalAddress} object from saved parcelable.
140      */
141     public static Creator<PostalAddress> CREATOR = new Creator<PostalAddress>() {
142         @Override
143         public PostalAddress createFromParcel(Parcel source) {
144             PostalAddress postalAddress = new PostalAddress();
145             postalAddress.mType = source.readInt();
146             postalAddress.mLabel = source.readString();
147             postalAddress.mFormattedAddress = source.readString();
148             return postalAddress;
149         }
150 
151         @Override
152         public PostalAddress[] newArray(int size) {
153             return new PostalAddress[size];
154         }
155     };
156 }
157