5
5
using Android . Content ;
6
6
using Android . Database ;
7
7
using Android . Provider ;
8
+ using CommonDataKinds = Android . Provider . ContactsContract . CommonDataKinds ;
9
+ using StructuredName = Android . Provider . ContactsContract . CommonDataKinds . StructuredName ;
8
10
9
11
namespace Xamarin . Essentials
10
12
{
11
13
public static partial class Contacts
12
14
{
15
+ const string idCol = ContactsContract . Contacts . InterfaceConsts . Id ;
16
+ const string displayNameCol = ContactsContract . Contacts . InterfaceConsts . DisplayName ;
17
+ const string mimetypeCol = ContactsContract . Data . InterfaceConsts . Mimetype ;
18
+
19
+ const string contactIdCol = CommonDataKinds . Phone . InterfaceConsts . ContactId ;
20
+
13
21
static async Task < Contact > PlatformPickContactAsync ( )
14
22
{
15
- var intent = new Intent ( Intent . ActionPick ) ;
16
- intent . SetType ( ContactsContract . CommonDataKinds . Phone . ContentType ) ;
17
-
23
+ var intent = new Intent ( Intent . ActionPick , ContactsContract . Contacts . ContentUri ) ;
18
24
var result = await IntermediateActivity . StartAsync ( intent , Platform . requestCodePickContact ) . ConfigureAwait ( false ) ;
25
+ if ( result ? . Data == null )
26
+ return null ;
19
27
20
- if ( result ? . Data != null )
21
- return GetContact ( result . Data ) ;
28
+ using var cursor = Platform . ContentResolver . Query ( result ? . Data , null , null , null , null ) ;
29
+ if ( cursor ? . MoveToFirst ( ) != true )
30
+ return null ;
22
31
23
- return null ;
32
+ return GetContact ( cursor ) ;
24
33
}
25
34
26
35
static Task < IEnumerable < Contact > > PlatformGetAllAsync ( CancellationToken cancellationToken )
@@ -30,106 +39,95 @@ static Task<IEnumerable<Contact>> PlatformGetAllAsync(CancellationToken cancella
30
39
31
40
IEnumerable < Contact > GetEnumerable ( )
32
41
{
33
- if ( cursor ? . MoveToFirst ( ) ?? false )
42
+ if ( cursor ? . MoveToFirst ( ) == true )
34
43
{
35
44
do
36
45
{
37
- var contact = GetContact ( cursor , ContactsContract . Contacts . InterfaceConsts . Id ) ;
46
+ var contact = GetContact ( cursor ) ;
38
47
if ( contact != null )
39
48
yield return contact ;
40
49
}
41
50
while ( cursor . MoveToNext ( ) ) ;
42
51
}
43
52
44
- cursor . Close ( ) ;
53
+ cursor ? . Close ( ) ;
45
54
}
46
55
}
47
56
48
- internal static Contact GetContact ( global :: Android . Net . Uri contactUri )
57
+ static Contact GetContact ( ICursor cursor )
49
58
{
50
- if ( contactUri == null )
51
- return default ;
52
-
53
- using var cursor = Platform . ContentResolver . Query ( contactUri , null , null , null , null ) ;
59
+ var id = GetString ( cursor , idCol ) ;
60
+ var displayName = GetString ( cursor , displayNameCol ) ;
61
+ var phones = GetNumbers ( id ) ? . Select ( p => new ContactPhone ( p ) ) ;
62
+ var emails = GetEmails ( id ) ? . Select ( e => new ContactEmail ( e ) ) ;
63
+ var ( prefix , given , middle , family , suffix ) = GetName ( id ) ;
54
64
55
- if ( cursor . MoveToFirst ( ) )
56
- {
57
- return GetContact (
58
- cursor ,
59
- ContactsContract . CommonDataKinds . Phone . InterfaceConsts . ContactId ) ;
60
- }
61
-
62
- return default ;
65
+ return new Contact ( id , prefix , given , middle , family , suffix , phones , emails , displayName ) ;
63
66
}
64
67
65
- static Contact GetContact ( ICursor cursor , string idKey )
68
+ static IEnumerable < string > GetNumbers ( string id )
66
69
{
67
- var displayName = cursor . GetString ( cursor . GetColumnIndex ( ContactsContract . Contacts . InterfaceConsts . DisplayName ) ) ;
68
- var idQ = new string [ 1 ] { cursor . GetString ( cursor . GetColumnIndex ( idKey ) ) } ;
69
- var phones = GetNumbers ( idQ ) ? . Select (
70
- item => new ContactPhone ( item . data ) ) ;
71
- var emails = GetEmails ( idQ ) ? . Select (
72
- item => new ContactEmail ( item . data ) ) ;
73
- var name = GetName ( idQ [ 0 ] ) ;
74
-
75
- return new Contact ( idQ [ 0 ] , name . Prefix , name . Given , name . Middle , name . Family , name . Suffix , phones , emails , displayName ) ;
76
- }
70
+ var uri = CommonDataKinds . Phone . ContentUri
71
+ . BuildUpon ( )
72
+ . AppendQueryParameter ( ContactsContract . RemoveDuplicateEntries , "1" )
73
+ . Build ( ) ;
77
74
78
- static IEnumerable < ( string data , string type ) > GetNumbers ( string [ ] idQ )
79
- {
80
- var uri = ContactsContract . CommonDataKinds . Phone . ContentUri . BuildUpon ( ) . AppendQueryParameter ( ContactsContract . RemoveDuplicateEntries , "1" ) . Build ( ) ;
81
- var cursor = Platform . ContentResolver . Query ( uri , null , $ "{ ContactsContract . CommonDataKinds . Phone . InterfaceConsts . ContactId } =?", idQ , null ) ;
75
+ var cursor = Platform . ContentResolver . Query ( uri , null , $ "{ contactIdCol } =?", new [ ] { id } , null ) ;
82
76
83
- return ReadCursorItems ( cursor , ContactsContract . CommonDataKinds . Phone . Number , ContactsContract . CommonDataKinds . Phone . InterfaceConsts . Type ) ;
77
+ return ReadCursorItems ( cursor , CommonDataKinds . Phone . Number ) ;
84
78
}
85
79
86
- static IEnumerable < ( string data , string type ) > GetEmails ( string [ ] idQ )
80
+ static IEnumerable < string > GetEmails ( string id )
87
81
{
88
- var uri = ContactsContract . CommonDataKinds . Email . ContentUri . BuildUpon ( ) . AppendQueryParameter ( ContactsContract . RemoveDuplicateEntries , "1" ) . Build ( ) ;
89
- var cursor = Platform . ContentResolver . Query ( uri , null , $ "{ ContactsContract . CommonDataKinds . Phone . InterfaceConsts . ContactId } =?", idQ , null ) ;
82
+ var uri = CommonDataKinds . Email . ContentUri
83
+ . BuildUpon ( )
84
+ . AppendQueryParameter ( ContactsContract . RemoveDuplicateEntries , "1" )
85
+ . Build ( ) ;
86
+
87
+ var cursor = Platform . ContentResolver . Query ( uri , null , $ "{ contactIdCol } =?", new [ ] { id } , null ) ;
90
88
91
- return ReadCursorItems ( cursor , ContactsContract . CommonDataKinds . Email . Address , ContactsContract . CommonDataKinds . Email . InterfaceConsts . Type ) ;
89
+ return ReadCursorItems ( cursor , CommonDataKinds . Email . Address ) ;
92
90
}
93
91
94
- static IEnumerable < ( string data , string type ) > ReadCursorItems ( ICursor cursor , string dataKey , string typeKey )
92
+ static IEnumerable < string > ReadCursorItems ( ICursor cursor , string dataKey )
95
93
{
96
- if ( cursor ? . MoveToFirst ( ) ?? false )
94
+ if ( cursor ? . MoveToFirst ( ) == true )
97
95
{
98
96
do
99
97
{
100
- var data = cursor . GetString ( cursor . GetColumnIndex ( dataKey ) ) ;
101
- var type = cursor . GetString ( cursor . GetColumnIndex ( typeKey ) ) ;
102
-
98
+ var data = GetString ( cursor , dataKey ) ;
103
99
if ( data != null )
104
- yield return ( data , type ) ;
100
+ yield return data ;
105
101
}
106
102
while ( cursor . MoveToNext ( ) ) ;
107
103
}
108
104
cursor ? . Close ( ) ;
109
105
}
110
106
111
- static ( string Prefix , string Given , string Middle , string Family , string Suffix ) GetName ( string idQ )
107
+ static ( string Prefix , string Given , string Middle , string Family , string Suffix ) GetName ( string id )
112
108
{
113
- var whereNameParams = new string [ ] { ContactsContract . CommonDataKinds . StructuredName . ContentItemType } ;
114
- var whereName = $ "{ ContactsContract . Data . InterfaceConsts . Mimetype } = ? AND { ContactsContract . CommonDataKinds . StructuredName . InterfaceConsts . ContactId } = { idQ } ";
109
+ var selection = $ "{ mimetypeCol } =? AND { contactIdCol } =?";
110
+ var selectionArgs = new string [ ] { StructuredName . ContentItemType , id } ;
111
+
115
112
using var cursor = Platform . ContentResolver . Query (
116
113
ContactsContract . Data . ContentUri ,
117
114
null ,
118
- whereName ,
119
- whereNameParams ,
115
+ selection ,
116
+ selectionArgs ,
120
117
null ) ;
121
118
122
- if ( cursor ? . MoveToFirst ( ) ?? false )
123
- {
124
- return (
125
- cursor . GetString ( cursor . GetColumnIndex ( ContactsContract . CommonDataKinds . StructuredName . Prefix ) ) ,
126
- cursor . GetString ( cursor . GetColumnIndex ( ContactsContract . CommonDataKinds . StructuredName . GivenName ) ) ,
127
- cursor . GetString ( cursor . GetColumnIndex ( ContactsContract . CommonDataKinds . StructuredName . MiddleName ) ) ,
128
- cursor . GetString ( cursor . GetColumnIndex ( ContactsContract . CommonDataKinds . StructuredName . FamilyName ) ) ,
129
- cursor . GetString ( cursor . GetColumnIndex ( ContactsContract . CommonDataKinds . StructuredName . Suffix ) ) ) ;
130
- }
119
+ if ( cursor ? . MoveToFirst ( ) != true )
120
+ return ( null , null , null , null , null ) ;
131
121
132
- return ( null , null , null , null , null ) ;
122
+ return (
123
+ GetString ( cursor , StructuredName . Prefix ) ,
124
+ GetString ( cursor , StructuredName . GivenName ) ,
125
+ GetString ( cursor , StructuredName . MiddleName ) ,
126
+ GetString ( cursor , StructuredName . FamilyName ) ,
127
+ GetString ( cursor , StructuredName . Suffix ) ) ;
133
128
}
129
+
130
+ static string GetString ( ICursor cursor , string column ) =>
131
+ cursor . GetString ( cursor . GetColumnIndex ( column ) ) ;
134
132
}
135
133
}
0 commit comments