Skip to content
Projects
Groups
Snippets
Help
This project
Loading...
Sign in / Register
Toggle navigation
D
Dunbar
Overview
Overview
Details
Activity
Cycle Analytics
Repository
Repository
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Issues
0
Issues
0
List
Board
Labels
Milestones
Merge Requests
0
Merge Requests
0
CI / CD
CI / CD
Pipelines
Jobs
Schedules
Charts
Wiki
Wiki
Snippets
Snippets
Members
Members
Collapse sidebar
Close sidebar
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Open sidebar
PLN
Dunbar
Commits
a36505eb
Unverified
Commit
a36505eb
authored
Nov 02, 2019
by
PLN (Algolia)
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
feat(Friends): Display contacts
parent
e803bf28
Hide whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
100 additions
and
39 deletions
+100
-39
Friend.kt
app/src/main/java/fr/plnech/dunbar/Friend.kt
+27
-0
Friends.kt
app/src/main/java/fr/plnech/dunbar/Friends.kt
+0
-6
MainActivity.kt
app/src/main/java/fr/plnech/dunbar/MainActivity.kt
+55
-17
user.jpeg
app/src/main/res/drawable/user.jpeg
+0
-0
contact.xml
app/src/main/res/layout/contact.xml
+16
-13
content_main.xml
app/src/main/res/layout/content_main.xml
+2
-3
No files found.
app/src/main/java/fr/plnech/dunbar/Friend.kt
0 → 100644
View file @
a36505eb
package
fr.plnech.dunbar
import
android.graphics.Bitmap
import
android.provider.ContactsContract
data class
Friend
(
val
map
:
MutableMap
<
String
,
String
?>,
val
photo
:
Bitmap
?)
{
override
fun
toString
():
String
=
"$id: $name ($timesContacted times, last $lastTime)"
val
name
:
String
?
get
()
=
map
[
ContactsContract
.
Contacts
.
DISPLAY_NAME
]
val
id
:
String
get
()
=
map
[
ContactsContract
.
Contacts
.
_ID
]
!!
val
lastTime
:
String
?
get
()
=
map
[
ContactsContract
.
Contacts
.
LAST_TIME_CONTACTED
]
val
timesContacted
:
String
?
get
()
=
map
[
ContactsContract
.
Contacts
.
TIMES_CONTACTED
]
val
isYou
:
String
?
get
()
=
map
[
ContactsContract
.
Contacts
.
IS_USER_PROFILE
]
val
visibleOutsideSearch
:
String
?
get
()
=
map
[
ContactsContract
.
Contacts
.
IN_DEFAULT_DIRECTORY
]
}
\ No newline at end of file
app/src/main/java/fr/plnech/dunbar/Friends.kt
deleted
100644 → 0
View file @
e803bf28
package
fr.plnech.dunbar
// * @see [contacts doc](https://developer.android.com/training/contacts-provider/retrieve-names)
class
Friends
{
}
\ No newline at end of file
app/src/main/java/fr/plnech/dunbar/MainActivity.kt
View file @
a36505eb
package
fr.plnech.dunbar
import
android.content.ContentUris
import
android.graphics.Bitmap
import
android.graphics.BitmapFactory
import
android.os.Bundle
import
android.provider.ContactsContract
import
android.util.Log
import
android.view.Menu
import
android.view.MenuItem
import
android.widget.ArrayAdapter
...
...
@@ -10,20 +14,18 @@ import androidx.appcompat.app.AppCompatActivity
import
com.google.android.material.snackbar.Snackbar
import
kotlinx.android.synthetic.main.activity_main.*
import
kotlinx.android.synthetic.main.content_main.*
import
java.io.IOException
class
MainActivity
:
AppCompatActivity
()
{
private
val
CONTENT_VIEW_ID
=
10101010
private
val
friends
=
mutableListOf
<
Friend
>()
override
fun
onCreate
(
savedInstanceState
:
Bundle
?)
{
super
.
onCreate
(
savedInstanceState
)
setContentView
(
R
.
layout
.
activity_main
)
setSupportActionBar
(
toolbar
)
fab
.
setOnClickListener
{
view
->
Snackbar
.
make
(
view
,
"Replace with your own action"
,
Snackbar
.
LENGTH_LONG
)
.
setAction
(
"Action"
,
null
).
show
()
...
...
@@ -49,6 +51,8 @@ class MainActivity : AppCompatActivity() {
}
fun
fetchFriends
()
{
Toast
.
makeText
(
this
@MainActivity
,
"Fetching friends..."
,
Toast
.
LENGTH_SHORT
).
show
()
contentResolver
.
query
(
ContactsContract
.
CommonDataKinds
.
Phone
.
CONTENT_URI
,
null
,
...
...
@@ -56,31 +60,65 @@ class MainActivity : AppCompatActivity() {
null
,
null
)
?.
let
{
val
contacts
=
mutableListOf
<
List
<
Pair
<
String
,
String
>>>()
while
(
it
.
moveToNext
())
{
contacts
.
add
(
mutableListOf
<
Pair
<
String
,
String
>>().
also
{
contact
->
mutableMapOf
<
String
,
String
?>().
also
{
map
->
val
indexID
=
it
.
getColumnIndex
(
ContactsContract
.
Contacts
.
_ID
)
val
id
=
it
.
getLong
(
indexID
)
val
photo
:
Bitmap
?
=
getPhoto
(
id
)
it
.
columnNames
.
forEach
{
name
->
contact
.
add
(
Pair
(
name
,
it
.
getString
(
it
.
getColumnIndex
(
name
))))
val
index
=
it
.
getColumnIndex
(
name
)
map
[
name
]
=
it
.
getString
(
index
)
}
})
friends
.
add
(
Friend
(
map
,
photo
))
}
}
friends
.
adapter
=
ArrayAdapter
<
String
>(
this
,
R
.
layout
.
contact
,
R
.
id
.
name
,
contacts
.
map
{
contact
->
contact
.
filter
{
!
it
.
second
.
isNullOrBlank
()
}
.
joinToString
{
"${it.first}:${it.second}"
}
})
val
adapter
=
ArrayAdapter
<
Friend
>(
this
,
R
.
layout
.
contact
,
R
.
id
.
name
,
friends
)
val
nbFriends
=
friends
.
adapter
.
count
friendsList
.
adapter
=
adapter
friendsList
.
setOnItemClickListener
{
parent
,
view
,
position
,
id
->
val
contact
:
String
=
adapter
.
getItem
(
position
)
?.
toString
()
?:
"NONE"
// Snackbar.make(friendsList, contact, Snackbar.LENGTH_INDEFINITE).show()
Toast
.
makeText
(
this
,
contact
,
Toast
.
LENGTH_LONG
).
show
()
}
val
nbFriends
=
adapter
.
count
welcomeTitle
.
text
=
"$nbFriends ${"
friend
".plural(nbFriends)} on Dunbar"
Toast
.
makeText
(
this
@MainActivity
,
"$nbFriends "
,
Toast
.
LENGTH_LONG
).
show
()
it
?
.
close
()
it
.
close
()
}
}
private
fun
getPhoto
(
id
:
Long
):
Bitmap
?
{
var
photo
:
Bitmap
?
=
null
try
{
val
inputStream
=
ContactsContract
.
Contacts
.
openContactPhotoInputStream
(
contentResolver
,
ContentUris
.
withAppendedId
(
ContactsContract
.
Contacts
.
CONTENT_URI
,
java
.
lang
.
Long
.
valueOf
(
id
)
)
)
if
(
inputStream
!=
null
)
{
photo
=
BitmapFactory
.
decodeStream
(
inputStream
)
}
inputStream
?.
close
()
}
catch
(
e
:
IOException
)
{
e
.
printStackTrace
()
}
Log
.
d
(
"--> "
,
photo
.
toString
())
return
photo
}
}
app/src/main/res/drawable/user.jpeg
0 → 100644
View file @
a36505eb
557 KB
app/src/main/res/layout/contact.xml
View file @
a36505eb
...
...
@@ -14,30 +14,33 @@
<ImageView
android:id=
"@+id/pic"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_margin=
"10dp"
android:layout_width=
"150dp"
android:layout_height=
"100dp"
android:layout_marginTop=
"5dp"
android:layout_marginEnd=
"10dp"
android:layout_marginBottom=
"5dp"
android:height=
"150dp"
android:
src=
"@android:drawable/
"
android:
contentDescription=
"TODO
"
android:elevation=
"2dp"
android:src=
"@drawable/user"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toLeftOf=
"@id/name"
app:layout_constraintTop_toTopOf=
"parent"
tools:src=
"@tools:sample/avatars"
android:contentDescription=
"TODO"
/>
app:layout_constraintStart_toStartOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
/>
<TextView
android:id=
"@+id/name"
android:layout_width=
"wrap_content"
android:layout_height=
"wrap_content"
android:layout_marginLeft=
"15dp"
android:clickable=
"true"
android:focusable=
"true"
android:maxLines=
"2"
android:layout_margin=
"15dp"
android:focusable=
"false"
android:focusableInTouchMode=
"false"
android:gravity=
"end"
android:maxLines=
"3"
android:textAppearance=
"@style/TextAppearance.AppCompat.Title"
android:textSize=
"
32
sp"
android:textSize=
"
24
sp"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintEnd_toEndOf=
"parent"
app:layout_constraintLeft_toRightOf=
"@id/pic"
app:layout_constraintRight_toLeftOf=
"@id/name"
app:layout_constraintTop_toTopOf=
"parent"
...
...
app/src/main/res/layout/content_main.xml
View file @
a36505eb
...
...
@@ -15,15 +15,14 @@
android:layout_height=
"wrap_content"
tools:text=
"N friends on Dunbar"
android:textSize=
"24sp"
app:layout_constraintBottom_toTopOf=
"@id/friends"
app:layout_constraintBottom_toTopOf=
"@id/friends
List
"
app:layout_constraintLeft_toLeftOf=
"parent"
app:layout_constraintRight_toRightOf=
"parent"
app:layout_constraintTop_toTopOf=
"parent"
/>
<ListView
android:id=
"@+id/friends"
android:id=
"@+id/friends
List
"
android:layout_width=
"match_parent"
android:padding=
"10dp"
android:layout_height=
"wrap_content"
app:layout_constraintBottom_toBottomOf=
"parent"
app:layout_constraintLeft_toLeftOf=
"parent"
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment