diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml index 7392302..e4c0ecf 100644 --- a/app/src/main/AndroidManifest.xml +++ b/app/src/main/AndroidManifest.xml @@ -14,7 +14,7 @@ android:supportsRtl="true" android:theme="@style/AppTheme"> diff --git a/app/src/main/java/fr/plnech/dunbar/Messages.kt b/app/src/main/java/fr/plnech/dunbar/data/Messages.kt similarity index 97% rename from app/src/main/java/fr/plnech/dunbar/Messages.kt rename to app/src/main/java/fr/plnech/dunbar/data/Messages.kt index bccd691..446761a 100644 --- a/app/src/main/java/fr/plnech/dunbar/Messages.kt +++ b/app/src/main/java/fr/plnech/dunbar/data/Messages.kt @@ -1,4 +1,4 @@ -package fr.plnech.dunbar +package fr.plnech.dunbar.data import android.content.Context import android.net.Uri diff --git a/app/src/main/java/fr/plnech/dunbar/Friend.kt b/app/src/main/java/fr/plnech/dunbar/model/Friend.kt similarity index 91% rename from app/src/main/java/fr/plnech/dunbar/Friend.kt rename to app/src/main/java/fr/plnech/dunbar/model/Friend.kt index 22c6466..f740365 100644 --- a/app/src/main/java/fr/plnech/dunbar/Friend.kt +++ b/app/src/main/java/fr/plnech/dunbar/model/Friend.kt @@ -1,4 +1,4 @@ -package fr.plnech.dunbar +package fr.plnech.dunbar.model import android.graphics.Bitmap import android.provider.ContactsContract.Contacts @@ -15,6 +15,9 @@ data class Friend(val map: MutableMap, val photo: Bitmap?) { val id: Int get() = map[Contacts._ID]!!.toInt() + val phone: String? + get() = map["data1"] + val lastTimeStamp = map[Contacts.LAST_TIME_CONTACTED]!!.toLong() val lastDate: Date? diff --git a/app/src/main/java/fr/plnech/dunbar/notif/FriendReminder.kt b/app/src/main/java/fr/plnech/dunbar/notif/FriendReminder.kt new file mode 100644 index 0000000..c989bd6 --- /dev/null +++ b/app/src/main/java/fr/plnech/dunbar/notif/FriendReminder.kt @@ -0,0 +1,56 @@ +package fr.plnech.dunbar.notif + +import android.app.* +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.os.Build +import androidx.core.app.NotificationCompat +import fr.plnech.dunbar.model.Friend + + +val CHANNEL_ID = "dunbar" + +class FriendReminder(val ctx: Context) { + + var channelCreated = false + + private fun createNotificationChannel() { + // Create the NotificationChannel, but only on API 26+ because + // the NotificationChannel class is new and not in the support library + if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) { + val channel = NotificationChannel( + CHANNEL_ID, ctx.getString(fr.plnech.dunbar.R.string.channel_name), + NotificationManager.IMPORTANCE_DEFAULT + ).apply { description = ctx.getString(fr.plnech.dunbar.R.string.channel_description) } + + (ctx.getSystemService(Context.NOTIFICATION_SERVICE) as NotificationManager) + .createNotificationChannel(channel) + } + channelCreated = true + } + + + fun createNotification(friend: Friend, activity: Activity): Notification? { + if (!channelCreated) createNotificationChannel() + + val uriSms = Uri.parse("smsto:1234567899") + val intentSMS = Intent(Intent.ACTION_SENDTO, uriSms) + intentSMS.putExtra("sms_body", "The SMS text") + val pendingIntent: PendingIntent = PendingIntent.getActivity(activity, 0, intentSMS, 0) + + return NotificationCompat.Builder(ctx, CHANNEL_ID) + .setContentTitle("What is ${friend.name} up to?") + .setContentText("Ask them for some news!") + .setSmallIcon(fr.plnech.dunbar.R.drawable.ic_friend) +// .setStyle( +// NotificationCompat.BigTextStyle() +// .bigText("Much longer text that cannot fit one line...") +// ) + .setContentIntent(pendingIntent) + .setAutoCancel(true) + .also { + it.priority = NotificationCompat.PRIORITY_HIGH + }.build() + } +} \ No newline at end of file diff --git a/app/src/main/java/fr/plnech/dunbar/MainActivity.kt b/app/src/main/java/fr/plnech/dunbar/ui/FriendsActivity.kt similarity index 86% rename from app/src/main/java/fr/plnech/dunbar/MainActivity.kt rename to app/src/main/java/fr/plnech/dunbar/ui/FriendsActivity.kt index 8060628..6f10af2 100644 --- a/app/src/main/java/fr/plnech/dunbar/MainActivity.kt +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendsActivity.kt @@ -1,4 +1,4 @@ -package fr.plnech.dunbar +package fr.plnech.dunbar.ui import android.content.ContentUris import android.graphics.Bitmap @@ -9,20 +9,26 @@ import android.view.Menu import android.view.MenuItem import android.widget.Toast import androidx.appcompat.app.AppCompatActivity +import androidx.core.app.NotificationManagerCompat import androidx.recyclerview.widget.LinearLayoutManager -import kotlinx.android.synthetic.main.activity_main.* -import kotlinx.android.synthetic.main.content_main.* +import fr.plnech.dunbar.R +import fr.plnech.dunbar.data.Messages +import fr.plnech.dunbar.model.Friend +import fr.plnech.dunbar.notif.FriendReminder +import fr.plnech.dunbar.plural +import kotlinx.android.synthetic.main.activity_friends.* +import kotlinx.android.synthetic.main.content_friends.* import java.io.IOException -class MainActivity : AppCompatActivity() { +class FriendsActivity : AppCompatActivity() { private lateinit var messages: Messages private val friends = mutableListOf() override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) - setContentView(R.layout.activity_main) + setContentView(R.layout.activity_friends) setSupportActionBar(toolbar) // messages = Messages(applicationContext) @@ -32,6 +38,18 @@ class MainActivity : AppCompatActivity() { } reloadFriends() + notifyFriend() + } + + private fun notifyFriend() { + val idNotif = 0 + with(NotificationManagerCompat.from(this)) { + val notification = + FriendReminder(applicationContext).createNotification(friends[0], this@FriendsActivity) + notification?.let { + notify(idNotif, notification) + } + } } override fun onCreateOptionsMenu(menu: Menu): Boolean { diff --git a/app/src/main/java/fr/plnech/dunbar/FriendsAdapter.kt b/app/src/main/java/fr/plnech/dunbar/ui/FriendsAdapter.kt similarity index 79% rename from app/src/main/java/fr/plnech/dunbar/FriendsAdapter.kt rename to app/src/main/java/fr/plnech/dunbar/ui/FriendsAdapter.kt index 1e361ea..8c71792 100644 --- a/app/src/main/java/fr/plnech/dunbar/FriendsAdapter.kt +++ b/app/src/main/java/fr/plnech/dunbar/ui/FriendsAdapter.kt @@ -1,4 +1,4 @@ -package fr.plnech.dunbar +package fr.plnech.dunbar.ui import android.text.format.DateUtils import android.view.LayoutInflater @@ -6,6 +6,8 @@ import android.view.View import android.view.ViewGroup import android.widget.Toast import androidx.recyclerview.widget.RecyclerView +import fr.plnech.dunbar.R +import fr.plnech.dunbar.model.Friend import kotlinx.android.synthetic.main.contact.view.* @@ -14,7 +16,13 @@ class FriendsAdapter( ) : RecyclerView.Adapter() { override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): FriendsViewHolder = - FriendsViewHolder(LayoutInflater.from(parent.context).inflate(viewType, parent, false)) + FriendsViewHolder( + LayoutInflater.from(parent.context).inflate( + viewType, + parent, + false + ) + ) override fun getItemCount(): Int = friends.size @@ -39,9 +47,16 @@ class FriendsViewHolder(private val view: View) : RecyclerView.ViewHolder(view) } else { append("Never interacted") } + friend.phone?.let { + append("\n$it") + } + friend.photo?.let { + pic.setImageBitmap(it) + } } view.setOnClickListener { Toast.makeText(view.context, friend.mapString(), Toast.LENGTH_LONG).show() + println(friend.mapString()) } } diff --git a/app/src/main/res/drawable/ic_friend.png b/app/src/main/res/drawable/ic_friend.png new file mode 100644 index 0000000..9fa3937 Binary files /dev/null and b/app/src/main/res/drawable/ic_friend.png differ diff --git a/app/src/main/res/layout/activity_main.xml b/app/src/main/res/layout/activity_friends.xml similarity index 92% rename from app/src/main/res/layout/activity_main.xml rename to app/src/main/res/layout/activity_friends.xml index e9fa907..a4bf715 100644 --- a/app/src/main/res/layout/activity_main.xml +++ b/app/src/main/res/layout/activity_friends.xml @@ -4,7 +4,7 @@ xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" - tools:context=".MainActivity"> + tools:context=".ui.FriendsActivity"> - + + tools:text="2 interactions, last 1 hour ago" /> + + + + diff --git a/app/src/main/res/layout/content_main.xml b/app/src/main/res/layout/content_friends.xml similarity index 94% rename from app/src/main/res/layout/content_main.xml rename to app/src/main/res/layout/content_friends.xml index 3e769d4..14d32a1 100644 --- a/app/src/main/res/layout/content_main.xml +++ b/app/src/main/res/layout/content_friends.xml @@ -5,8 +5,8 @@ android:layout_width="match_parent" android:layout_height="match_parent" app:layout_behavior="@string/appbar_scrolling_view_behavior" - tools:context=".MainActivity" - tools:showIn="@layout/activity_main"> + tools:context=".ui.FriendsActivity" + tools:showIn="@layout/activity_friends"> + tools:context="fr.plnech.dunbar.ui.FriendsActivity"> #008577 #00574B - #D81B60 + #FF5900 diff --git a/app/src/main/res/values/strings.xml b/app/src/main/res/values/strings.xml index 85010f1..932fcfb 100644 --- a/app/src/main/res/values/strings.xml +++ b/app/src/main/res/values/strings.xml @@ -1,4 +1,6 @@ Dunbar + Dunbar + Updates about friends you don\'t want to forget Settings diff --git a/app/src/main/res/values/styles.xml b/app/src/main/res/values/styles.xml index 545b9c6..b18eab5 100644 --- a/app/src/main/res/values/styles.xml +++ b/app/src/main/res/values/styles.xml @@ -1,8 +1,7 @@ -