Use LocalBroadcastManager instead of ContactsUpdatedListener.
This commit is contained in:
parent
b73e142a73
commit
aadf0dc1f9
4 changed files with 59 additions and 45 deletions
|
@ -2,9 +2,10 @@ package com.nutomic.ensichat.util
|
|||
|
||||
import java.util.concurrent.CountDownLatch
|
||||
|
||||
import android.content.Context
|
||||
import android.content.{IntentFilter, Intent, BroadcastReceiver, Context}
|
||||
import android.database.DatabaseErrorHandler
|
||||
import android.database.sqlite.SQLiteDatabase
|
||||
import android.support.v4.content.LocalBroadcastManager
|
||||
import android.test.{AndroidTestCase, mock}
|
||||
import com.nutomic.ensichat.protocol.{MessageTest, UserTest}
|
||||
import com.nutomic.ensichat.protocol.body.CryptoData
|
||||
|
@ -12,7 +13,6 @@ import com.nutomic.ensichat.protocol.header.ContentHeaderTest
|
|||
import com.nutomic.ensichat.protocol.header.ContentHeader
|
||||
import ContentHeaderTest._
|
||||
import MessageTest._
|
||||
import com.nutomic.ensichat.util.Database.OnContactsUpdatedListener
|
||||
import junit.framework.Assert._
|
||||
|
||||
object DatabaseTest {
|
||||
|
@ -29,6 +29,7 @@ object DatabaseTest {
|
|||
dbFile = file + "-test"
|
||||
context.openOrCreateDatabase(dbFile, mode, factory, errorHandler)
|
||||
}
|
||||
override def getApplicationContext = context
|
||||
def deleteDbFile() = context.deleteDatabase(dbFile)
|
||||
}
|
||||
|
||||
|
@ -94,11 +95,14 @@ class DatabaseTest extends AndroidTestCase {
|
|||
|
||||
def testAddContactCallback(): Unit = {
|
||||
val latch = new CountDownLatch(1)
|
||||
database.runOnContactsUpdated(new OnContactsUpdatedListener {
|
||||
override def onContactsUpdated() = latch.countDown()
|
||||
})
|
||||
val lbm = LocalBroadcastManager.getInstance(context)
|
||||
val receiver = new BroadcastReceiver {
|
||||
override def onReceive(context: Context, intent: Intent): Unit = latch.countDown()
|
||||
}
|
||||
lbm.registerReceiver(receiver, new IntentFilter(Database.ActionContactsUpdated))
|
||||
database.addContact(UserTest.u1)
|
||||
latch.await()
|
||||
lbm.unregisterReceiver(receiver)
|
||||
}
|
||||
|
||||
def testGetContact(): Unit = {
|
||||
|
|
|
@ -1,22 +1,22 @@
|
|||
package com.nutomic.ensichat.activities
|
||||
|
||||
import android.content.Intent
|
||||
import android.content.{IntentFilter, Intent, Context, BroadcastReceiver}
|
||||
import android.os.Bundle
|
||||
import android.support.v4.app.NavUtils
|
||||
import android.support.v4.content.LocalBroadcastManager
|
||||
import android.view._
|
||||
import android.widget.AdapterView.OnItemClickListener
|
||||
import android.widget._
|
||||
import com.nutomic.ensichat.R
|
||||
import com.nutomic.ensichat.protocol.ChatService
|
||||
import com.nutomic.ensichat.protocol.body.RequestAddContact
|
||||
import com.nutomic.ensichat.util.Database.OnContactsUpdatedListener
|
||||
import com.nutomic.ensichat.util.{Database, UsersAdapter}
|
||||
|
||||
/**
|
||||
* Lists all nearby, connected devices and allows adding them to be added as contacts.
|
||||
*/
|
||||
class AddContactsActivity extends EnsichatActivity with ChatService.OnConnectionsChangedListener
|
||||
with OnItemClickListener with OnContactsUpdatedListener {
|
||||
with OnItemClickListener {
|
||||
|
||||
private val Tag = "AddContactsActivity"
|
||||
|
||||
|
@ -39,11 +39,17 @@ class AddContactsActivity extends EnsichatActivity with ChatService.OnConnection
|
|||
|
||||
runOnServiceConnected(() => {
|
||||
service.registerConnectionListener(AddContactsActivity.this)
|
||||
database.runOnContactsUpdated(this)
|
||||
})
|
||||
LocalBroadcastManager.getInstance(this)
|
||||
.registerReceiver(onContactsUpdatedListener, new IntentFilter(Database.ActionContactsUpdated))
|
||||
}
|
||||
|
||||
override def onConnectionsChanged() = onContactsUpdated()
|
||||
override def onDestroy(): Unit = {
|
||||
super.onDestroy()
|
||||
LocalBroadcastManager.getInstance(this).unregisterReceiver(onContactsUpdatedListener)
|
||||
}
|
||||
|
||||
override def onConnectionsChanged() = onContactsUpdatedListener.onReceive(null, null)
|
||||
|
||||
/**
|
||||
* Initiates adding the device as contact if it hasn't been added yet.
|
||||
|
@ -67,14 +73,16 @@ class AddContactsActivity extends EnsichatActivity with ChatService.OnConnection
|
|||
/**
|
||||
* Fetches connections and displays them (excluding contacts).
|
||||
*/
|
||||
override def onContactsUpdated(): Unit ={
|
||||
runOnUiThread(new Runnable {
|
||||
override def run(): Unit = {
|
||||
adapter.clear()
|
||||
(service.connections().map(a => service.getUser(a)) -- database.getContacts)
|
||||
.foreach(adapter.add)
|
||||
}
|
||||
})
|
||||
private val onContactsUpdatedListener = new BroadcastReceiver() {
|
||||
override def onReceive(context: Context, intent: Intent): Unit = {
|
||||
runOnUiThread(new Runnable {
|
||||
override def run(): Unit = {
|
||||
adapter.clear()
|
||||
(service.connections().map(a => service.getUser(a)) -- database.getContacts)
|
||||
.foreach(adapter.add)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -3,22 +3,22 @@ package com.nutomic.ensichat.fragments
|
|||
import java.io.File
|
||||
|
||||
import android.app.ListFragment
|
||||
import android.content.Intent
|
||||
import android.content.{IntentFilter, Context, BroadcastReceiver, Intent}
|
||||
import android.net.Uri
|
||||
import android.os.Bundle
|
||||
import android.support.v4.content.LocalBroadcastManager
|
||||
import android.view._
|
||||
import android.widget.ListView
|
||||
import com.nutomic.ensichat.R
|
||||
import com.nutomic.ensichat.activities.{AddContactsActivity, EnsichatActivity, MainActivity, SettingsActivity}
|
||||
import com.nutomic.ensichat.protocol.ChatService
|
||||
import com.nutomic.ensichat.util.Database.OnContactsUpdatedListener
|
||||
import com.nutomic.ensichat.util.{Database, UsersAdapter}
|
||||
import scala.collection.JavaConversions._
|
||||
|
||||
/**
|
||||
* Lists all nearby, connected devices.
|
||||
*/
|
||||
class ContactsFragment extends ListFragment with OnContactsUpdatedListener {
|
||||
class ContactsFragment extends ListFragment {
|
||||
|
||||
private lazy val adapter = new UsersAdapter(getActivity)
|
||||
|
||||
|
@ -32,8 +32,14 @@ class ContactsFragment extends ListFragment with OnContactsUpdatedListener {
|
|||
|
||||
getActivity.asInstanceOf[EnsichatActivity].runOnServiceConnected(() => {
|
||||
database.getContacts.foreach(adapter.add)
|
||||
database.runOnContactsUpdated(this)
|
||||
})
|
||||
LocalBroadcastManager.getInstance(getActivity)
|
||||
.registerReceiver(onContactsUpdatedListener, new IntentFilter(Database.ActionContactsUpdated))
|
||||
}
|
||||
|
||||
override def onDestroy(): Unit = {
|
||||
super.onDestroy()
|
||||
LocalBroadcastManager.getInstance(getActivity).unregisterReceiver(onContactsUpdatedListener)
|
||||
}
|
||||
|
||||
override def onCreateView(inflater: LayoutInflater, container: ViewGroup,
|
||||
|
@ -75,13 +81,15 @@ class ContactsFragment extends ListFragment with OnContactsUpdatedListener {
|
|||
override def onListItemClick(l: ListView, v: View, position: Int, id: Long): Unit =
|
||||
getActivity.asInstanceOf[MainActivity].openChat(adapter.getItem(position).address)
|
||||
|
||||
override def onContactsUpdated(): Unit = {
|
||||
getActivity.runOnUiThread(new Runnable {
|
||||
override def run(): Unit = {
|
||||
adapter.clear()
|
||||
database.getContacts.foreach(adapter.add)
|
||||
}
|
||||
})
|
||||
private val onContactsUpdatedListener = new BroadcastReceiver() {
|
||||
override def onReceive(context: Context, intent: Intent): Unit = {
|
||||
getActivity.runOnUiThread(new Runnable {
|
||||
override def run(): Unit = {
|
||||
adapter.clear()
|
||||
database.getContacts.foreach(adapter.add)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
|
|
@ -2,19 +2,21 @@ package com.nutomic.ensichat.util
|
|||
|
||||
import java.util.Date
|
||||
|
||||
import android.content.{ContentValues, Context}
|
||||
import android.content.{Intent, ContentValues, Context}
|
||||
import android.database.sqlite.{SQLiteDatabase, SQLiteOpenHelper}
|
||||
import android.support.v4.content.LocalBroadcastManager
|
||||
import com.nutomic.ensichat.protocol.ChatService.OnMessageReceivedListener
|
||||
import com.nutomic.ensichat.protocol._
|
||||
import com.nutomic.ensichat.protocol.body.{Text, ResultAddContact, RequestAddContact}
|
||||
import com.nutomic.ensichat.protocol.header.ContentHeader
|
||||
import com.nutomic.ensichat.util.Database.OnContactsUpdatedListener
|
||||
|
||||
import scala.collection.immutable.TreeSet
|
||||
import scala.collection.{SortedSet, mutable}
|
||||
|
||||
object Database {
|
||||
|
||||
val ActionContactsUpdated = "Contacts_Updated"
|
||||
|
||||
private val DatabaseName = "message_store.db"
|
||||
|
||||
private val DatabaseVersion = 1
|
||||
|
@ -35,13 +37,6 @@ object Database {
|
|||
"address TEXT NOT NULL," +
|
||||
"name TEXT NOT NULL)"
|
||||
|
||||
trait OnContactsUpdatedListener {
|
||||
def onContactsUpdated()
|
||||
}
|
||||
|
||||
private var contactsUpdatedListeners =
|
||||
new mutable.WeakHashMap[OnContactsUpdatedListener, Unit].keySet
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
@ -139,21 +134,20 @@ class Database(context: Context)
|
|||
cv.put("address", contact.address.toString)
|
||||
cv.put("name", contact.name.toString)
|
||||
getWritableDatabase.insert("contacts", null, cv)
|
||||
Database.contactsUpdatedListeners.foreach(_.onContactsUpdated() )
|
||||
contactsUpdated()
|
||||
}
|
||||
|
||||
def changeContactName(contact: User): Unit = {
|
||||
val cv = new ContentValues()
|
||||
cv.put("name", contact.name.toString)
|
||||
getWritableDatabase.update("contacts", cv, "address = ?", Array(contact.address.toString))
|
||||
Database.contactsUpdatedListeners.foreach(_.onContactsUpdated())
|
||||
contactsUpdated()
|
||||
}
|
||||
|
||||
/**
|
||||
* Pass a callback that is called whenever a new contact is added.
|
||||
*/
|
||||
def runOnContactsUpdated(listener: OnContactsUpdatedListener) =
|
||||
Database.contactsUpdatedListeners += listener
|
||||
private def contactsUpdated(): Unit = {
|
||||
LocalBroadcastManager.getInstance(context)
|
||||
.sendBroadcast(new Intent(Database.ActionContactsUpdated))
|
||||
}
|
||||
|
||||
override def onUpgrade(db: SQLiteDatabase, oldVersion: Int, newVersion: Int): Unit = {
|
||||
}
|
||||
|
|
Reference in a new issue