-
Notifications
You must be signed in to change notification settings - Fork 9
/
NativeContact.java
201 lines (164 loc) · 5.16 KB
/
NativeContact.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
package com.example.fst_app_flutter.contact;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.provider.ContactsContract;
import java.util.ArrayList;
import java.util.HashMap;
/**
* Contacts class for saving using the native Contacts UI
*/
@SuppressWarnings("unchecked")
public class NativeContact {
private String displayName;
private String note;
private String email;
private ArrayList<NativeContactPhone> phones;
private String website;
/**
* Creates an instance of NativeContact
*
* @param map a Map object passed in from dart saveNatively method in
* * contact_model.dart
*/
public NativeContact(HashMap<String, Object> map) {
this.displayName = (String) map.get("displayName");
this.email = (String) map.get("email");
this.note = (String) map.get("note");
this.website = (String) map.get("website");
this.phones = new ArrayList<>();
try {
ArrayList<HashMap<String, String>> tempPhoneList = (ArrayList<HashMap<String, String>>) map.get(
"phones");
if (tempPhoneList != null) {
for (HashMap<String, String> phone : tempPhoneList) {
this.phones.add(new NativeContactPhone(phone));
}
}
} catch (Exception e) {
}
}
/**
* Opens the native contact form with this contact's data pre-loaded in their respective fields.
* @param context The context from which to start the activity
*/
public void saveNatively(Context context) {
Intent intent = new Intent(ContactsContract.Intents.Insert.ACTION);
//Log.d("flutter-plugin", displayName);
ArrayList<ContentValues> dataList = new ArrayList<>();
//Log.d("email", getEmail());
ContentValues row = new ContentValues();
row.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Email.CONTENT_ITEM_TYPE);
row.put(ContactsContract.CommonDataKinds.Email.ADDRESS, getEmail());
row.put(ContactsContract.CommonDataKinds.Email.TYPE,
ContactsContract.CommonDataKinds.Email.TYPE_WORK);
dataList.add(row);
for (NativeContactPhone phone : getPhones()) {
//Log.d("phone", phone.getNumber());
row = new ContentValues();
row.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Phone.CONTENT_ITEM_TYPE);
row.put(ContactsContract.CommonDataKinds.Phone.NUMBER, phone.getNumber());
row.put(ContactsContract.CommonDataKinds.Phone.TYPE, phone.getLabel());
dataList.add(row);
}
//Log.d("website", getWebsite());
row = new ContentValues();
row.put(ContactsContract.Data.MIMETYPE,
ContactsContract.CommonDataKinds.Website.CONTENT_ITEM_TYPE);
row.put(ContactsContract.CommonDataKinds.Website.URL, getWebsite());
row.put(ContactsContract.CommonDataKinds.Website.TYPE,
ContactsContract.CommonDataKinds.Website.TYPE_WORK);
dataList.add(row);
intent.setType(ContactsContract.RawContacts.CONTENT_TYPE);
intent.putExtra(ContactsContract.Intents.Insert.NAME, getDisplayName());
intent.putParcelableArrayListExtra(ContactsContract.Intents.Insert.DATA,
dataList);
intent.putExtra(ContactsContract.Intents.Insert.NOTES, getNote());
intent.putExtra("finishActivityOnSaveCompleted", true);
context.startActivity(intent);
}
/**
* Gets this contact's display name / full name
*
* @return The display name for this contact
*/
public String getDisplayName() {
return displayName;
}
/**
* Gets the notes for this contact.
*
* @return the note for this contact
*/
public String getNote() {
return note;
}
/**
* Gets the email address for this contact
*
* @return the email for this contact
*/
public String getEmail() {
return email;
}
/**
* Gets the website for this contact.
*
* @return the website for this contact.
*/
public String getWebsite() {
return website;
}
/**
* Gets the list of phone numbers for this contact.
*
* @return The list of phone numbers for this contact.
*/
public ArrayList<NativeContactPhone> getPhones() {
return phones;
}
/**
* The class to manage phone numbers for NativeContact.
*/
private static class NativeContactPhone {
private int label;
private String number;
/**
* Creates an instance of NativeContactPhone.
*
* @param map A map object passed from the NativeContact constructor from the invoke
* method call arguments passed in dart saveNatively method in
* contact_model.dart.
*/
NativeContactPhone(HashMap<String, String> map) {
this.number = map.get("value");
String strLabel = map.get("label");
if (strLabel != null) {
if ("fax work".equals(strLabel)) {
this.label = ContactsContract.CommonDataKinds.Phone.TYPE_FAX_WORK;
} else {
this.label = ContactsContract.CommonDataKinds.Phone.TYPE_WORK;
}
}
}
/**
* GGets the phone label.
*
* @return The label / type of phone number for this phone number. This integer represents
* an enum value from ContactsContract.CommonDataKinds.Phone.
*/
public int getLabel() {
return label;
}
/**
* Gets the phone number.
*
* @return The phone number for this contact.
*/
public String getNumber() {
return number;
}
}
}