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