Skip to content

Commit

Permalink
show profiles in checkup
Browse files Browse the repository at this point in the history
  • Loading branch information
newhinton committed Jan 6, 2024
1 parent af9948a commit b9fd6f1
Show file tree
Hide file tree
Showing 8 changed files with 237 additions and 28 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,26 @@ import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.fragment.app.Fragment
import androidx.recyclerview.widget.RecyclerView
import de.felixnuesse.timedsilence.databinding.FragmentCheckupBinding
import de.felixnuesse.timedsilence.extensions.e
import de.felixnuesse.timedsilence.model.contacts.Contact
import de.felixnuesse.timedsilence.model.contacts.ContactUtil
import de.felixnuesse.timedsilence.ui.ContactsListAdapter
import de.felixnuesse.timedsilence.ui.custom.NestedRecyclerManager
import de.felixnuesse.timedsilence.util.PermissionManager


class CheckupFragment : Fragment() {

companion object {
private const val TAG = "CheckupFragment"
}

private lateinit var viewAdapter: RecyclerView.Adapter<*>
private lateinit var viewManager: RecyclerView.LayoutManager

private var _binding: FragmentCheckupBinding? = null
private val binding get() = _binding!!

private var showContacts = false


override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?,
Expand All @@ -54,6 +58,12 @@ class CheckupFragment : Fragment() {
binding.gotoDND.setOnClickListener {
startActivity(Intent("android.settings.ZEN_MODE_SETTINGS"))
}

binding.contactsShowButton.setOnClickListener {
binding.contactsListView.visibility = View.VISIBLE
binding.contactsShowButton.visibility = View.GONE
showContacts = true
}
}

override fun onDestroyView() {
Expand All @@ -69,14 +79,18 @@ class CheckupFragment : Fragment() {
private fun updateData() {
val context = binding.root.context

var l = arrayListOf<Contact>()
var contactsList = arrayListOf<Contact>()

if(showContacts) {
binding.contactsListView.visibility = View.VISIBLE
binding.contactsShowButton.visibility = View.GONE
}

var permissionManager = PermissionManager(context)
if(permissionManager.grantedContacts()) {
binding.CheckupContentContainer.visibility = View.VISIBLE
binding.CheckupPermissionCheckContainer.visibility = View.GONE
l = ContactUtil(context).getContactList()
contactsList = ContactUtil(context).getContactList()
} else{
binding.CheckupContentContainer.visibility = View.GONE
binding.buttonRequestContactPermissions.setOnClickListener{
Expand All @@ -90,8 +104,6 @@ class CheckupFragment : Fragment() {
} else {
mNotificationManager?.notificationPolicy
}


/*
The following is the current (api 33) list of available categories:
Expand Down Expand Up @@ -153,15 +165,27 @@ class CheckupFragment : Fragment() {
1
}

if(contactsList.size == 0) {
binding.contactsShowButton.visibility = View.GONE
}

binding.checkboxHavePriorityContacts.isChecked = l.size > 0
binding.checkboxHavePriorityContacts.isChecked = contactsList.size > 0
binding.checkboxPriorityContactsCanBypass.isChecked = priorityCaller != 0
binding.checkboxRepeatCallerCanBypass.isChecked = repeatCaller != 0
binding.checkboxRepeatMessengerCanBypass.isChecked = repeatMessenger != 0
binding.checkboxRepeatMessengerCanBypass.isChecked = repeatMessenger != 0
binding.checkboxAlarmsCanBypass.isChecked = alarms != 0
binding.checkboxNotificationsVisible.isChecked = !areAllVisualEffectsSuppressed(suppressedVisuals)

viewManager = NestedRecyclerManager(context)
viewAdapter = ContactsListAdapter(contactsList)

binding.contactsListView.apply {
setHasFixedSize(true)
layoutManager = viewManager
adapter = viewAdapter
}

}

/**
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package de.felixnuesse.timedsilence.model.contacts

class Contact(var name: String) {

import android.graphics.Bitmap

class Contact(var name: String) {
var photo: Bitmap? = null
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,15 @@
package de.felixnuesse.timedsilence.model.contacts

import android.content.ContentResolver
import android.content.ContentUris
import android.content.Context
import android.database.Cursor
import android.graphics.Bitmap
import android.graphics.BitmapFactory
import android.provider.ContactsContract
import de.felixnuesse.timedsilence.R
import de.felixnuesse.timedsilence.extensions.e
import de.felixnuesse.timedsilence.util.PermissionManager
import java.io.IOException


class ContactUtil(private var mContext: Context) {
Expand All @@ -15,9 +20,10 @@ class ContactUtil(private var mContext: Context) {


private val PROJECTION = arrayOf(
ContactsContract.CommonDataKinds.Phone.CONTACT_ID,
ContactsContract.Contacts._ID,
ContactsContract.Contacts.DISPLAY_NAME,
ContactsContract.Contacts.STARRED
ContactsContract.Contacts.STARRED,
ContactsContract.Contacts.PHOTO_URI
)

fun getContactList(): ArrayList<Contact> {
Expand All @@ -28,24 +34,50 @@ class ContactUtil(private var mContext: Context) {
return contactList
}

val cr: ContentResolver = mContext.contentResolver
val cursor = cr.query(
ContactsContract.CommonDataKinds.Phone.CONTENT_URI,
PROJECTION,
null,
null,
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"
)
cursor?.use { cursor ->

getQuery()?.use { cursor ->
val nameIndex = cursor.getColumnIndex(ContactsContract.Contacts.DISPLAY_NAME)
val starredIndex = cursor.getColumnIndex(ContactsContract.Contacts.STARRED)
val photoIndex = cursor.getColumnIndex(ContactsContract.Contacts.PHOTO_URI)
val idIndex = cursor.getColumnIndex(ContactsContract.Contacts._ID)

while (cursor.moveToNext()) {
if(cursor.getInt(starredIndex) == 1) {
contactList.add(Contact(cursor.getString(nameIndex)))
}
var c = Contact(cursor.getString(nameIndex))
c.photo = getPhoto(cursor.getLong(idIndex))
contactList.add(c)
}

}
return contactList
}

private fun getQuery(): Cursor? {
return mContext.contentResolver.query(
ContactsContract.Contacts.CONTENT_URI,
PROJECTION,
ContactsContract.Contacts.STARRED + " = ?",
arrayOf("1"),
ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME + " ASC"
)
}

private fun getPhoto(id: Long): Bitmap {
var photo = BitmapFactory.decodeResource(mContext.resources, R.drawable.icon_person)

val uri = ContentUris.withAppendedId(ContactsContract.Contacts.CONTENT_URI, id)

try {
val inputStream = ContactsContract.Contacts.openContactPhotoInputStream(
mContext.contentResolver,
uri
)
if (inputStream != null) {
photo = BitmapFactory.decodeStream(inputStream)
}
inputStream?.close()
} catch (e: IOException) {
e.printStackTrace()
}
return photo
}

}
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
package de.felixnuesse.timedsilence.ui;

import androidx.recyclerview.widget.RecyclerView
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.appcompat.content.res.AppCompatResources
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_LOUD
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_SILENT
import de.felixnuesse.timedsilence.handler.volume.VolumeState.Companion.TIME_SETTING_VIBRATE
import de.felixnuesse.timedsilence.Constants.Companion.WIFI_TYPE_CONNECTED
import de.felixnuesse.timedsilence.Constants.Companion.WIFI_TYPE_SEARCHING
import de.felixnuesse.timedsilence.R
import de.felixnuesse.timedsilence.databinding.AdapterContactListBinding
import de.felixnuesse.timedsilence.databinding.AdapterWifiListBinding
import de.felixnuesse.timedsilence.model.contacts.Contact
import de.felixnuesse.timedsilence.model.data.WifiObject
import de.felixnuesse.timedsilence.model.database.DatabaseHandler
import kotlin.collections.ArrayList


/**
* Copyright (C) 2024 Felix Nüsse
* Created on 06.01.24 - 21:53
* <p>
* Edited by: Felix Nüsse felix.nuesse(at)t-online.de
* <p>
* <p>
* This program is released under the GPLv3 license
* <p>
* <p>
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 3 of the License, or
* (at your option) any later version.
* <p>
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
* <p>
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software Foundation,
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
*/
class ContactsListAdapter(private val myDataset: ArrayList<Contact>): RecyclerView.Adapter<ContactsListAdapter.ContactViewHolder>() {

override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ContactViewHolder {
val binding = AdapterContactListBinding.inflate(LayoutInflater.from(parent.context), parent, false)
return ContactViewHolder(binding)
}

override fun onBindViewHolder(holder: ContactViewHolder, position: Int) {
val context = holder.contactView.root.context
var contact = myDataset[position]
holder.contactView.name.text = contact.name
holder.contactView.profile.setImageBitmap(contact.photo)

}

override fun getItemCount() = myDataset.size

class ContactViewHolder(val contactView: AdapterContactListBinding) :
RecyclerView.ViewHolder(contactView.root)

}


11 changes: 11 additions & 0 deletions app/src/main/res/drawable/icon_person.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<vector android:height="24dp" android:tint="@color/iconColor"
android:viewportHeight="24" android:viewportWidth="24"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillAlpha="0.3"
android:fillColor="@android:color/white"
android:pathData="M12,16c-2.69,0 -5.77,1.28 -6,2h12c-0.2,-0.71 -3.3,-2 -6,-2z" android:strokeAlpha="0.3"/>
<path android:fillAlpha="0.3"
android:fillColor="@android:color/white"
android:pathData="M12,8m-2,0a2,2 0,1 1,4 0a2,2 0,1 1,-4 0" android:strokeAlpha="0.3"/>
<path android:fillColor="@android:color/white" android:pathData="M12,14c-2.67,0 -8,1.34 -8,4v2h16v-2c0,-2.66 -5.33,-4 -8,-4zM6,18c0.22,-0.72 3.31,-2 6,-2 2.7,0 5.8,1.29 6,2L6,18zM12,12c2.21,0 4,-1.79 4,-4s-1.79,-4 -4,-4 -4,1.79 -4,4 1.79,4 4,4zM12,6c1.1,0 2,0.9 2,2s-0.9,2 -2,2 -2,-0.9 -2,-2 0.9,-2 2,-2z"/>
</vector>
62 changes: 62 additions & 0 deletions app/src/main/res/layout/adapter_contact_list.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:layout_width="match_parent"
android:layout_height="wrap_content">


<androidx.cardview.widget.CardView
style="@style/tertiaryCard"
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_margin="@dimen/cardMargin"
android:padding="@dimen/cardPadding"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent">

<LinearLayout
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_margin="8dp"
android:gravity="center"
android:orientation="horizontal">

<ImageView
android:id="@+id/profile"
android:layout_width="32dp"
android:layout_height="32dp"
android:background="@drawable/shape_circle"
android:clipToOutline="true"
android:cropToPadding="true" />

<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:orientation="vertical"
android:paddingStart="12dp">

<TextView
android:id="@+id/name"
style="@style/tertiaryCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="name" />

<TextView
android:id="@+id/description"
style="@style/tertiaryCard"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="4dp"
android:text="description" />
</LinearLayout>

</LinearLayout>

</androidx.cardview.widget.CardView>


</androidx.constraintlayout.widget.ConstraintLayout>
13 changes: 12 additions & 1 deletion app/src/main/res/layout/fragment_checkup.xml
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,6 @@
app:layout_constraintTop_toTopOf="parent" />

<ScrollView
android:id="@+id/scrollView2"
android:layout_width="0dp"
android:layout_height="0dp"
android:layout_margin="@dimen/fragmentSideMargins"
Expand All @@ -92,6 +91,18 @@
android:drawableRight="?android:attr/listChoiceIndicatorMultiple"
android:text="@string/priority_contacts_available" />

<Button
android:id="@+id/contactsShowButton"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/show_contacts" />

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/contactsListView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:visibility="gone" />

<CheckBox
android:id="@+id/checkboxPriorityContactsCanBypass"
android:layout_width="match_parent"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -157,4 +157,5 @@
<string name="priority_contacts_can_bypass_dnd">Priority Contacts can bypass \"Do not Disturb\"</string>
<string name="priority_contacts_available">You have Priority Contacts</string>
<string name="notifications_visiblity">Notifications are visible during \"Do not Disturb\"</string>
<string name="show_contacts">Show</string>
</resources>

0 comments on commit b9fd6f1

Please sign in to comment.