Skip to content

Commit

Permalink
App shortcuts
Browse files Browse the repository at this point in the history
  • Loading branch information
glodanif committed Oct 13, 2017
1 parent 4702bbf commit c8d4639
Show file tree
Hide file tree
Showing 17 changed files with 254 additions and 55 deletions.
30 changes: 16 additions & 14 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,21 @@
android:theme="@style/AppTheme"
tools:replace="android:allowBackup">

<activity
android:name=".activity.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
<intent-filter>
<action android:name="android.intent.action.SEND" />
<category android:name="android.intent.category.DEFAULT" />
<data android:mimeType="text/plain" />
</intent-filter>
</activity>

<activity
android:name=".activity.ConversationsActivity"
android:label="@string/app_name"
Expand All @@ -46,20 +61,7 @@
android:name=".activity.ProfileActivity"
android:label="@string/initial_setup__initial_setup"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">

</activity>

<activity
android:name=".activity.SplashActivity"
android:screenOrientation="portrait"
android:theme="@style/AppTheme">
<intent-filter>
<action android:name="android.intent.action.MAIN" />

<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
android:theme="@style/AppTheme"/>

<activity
android:name=".activity.SettingsActivity"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,8 @@ import android.bluetooth.BluetoothAdapter
import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.v7.app.AppCompatActivity
import android.support.v7.app.AppCompatDelegate
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.view.Menu
import android.view.MenuItem
import android.view.View
Expand Down Expand Up @@ -56,14 +53,14 @@ class ChatActivity : SkeletonActivity(), ChatView {
toolbar?.setTitleTextAppearance(this, R.style.ActionBar_TitleTextStyle)
toolbar?.setSubtitleTextAppearance(this, R.style.ActionBar_SubTitleTextStyle)

actions = findViewById<ActionView>(R.id.av_actions)
messageField = findViewById<EditText>(R.id.et_message)
actions = findViewById(R.id.av_actions)
messageField = findViewById(R.id.et_message)

findViewById<ImageButton>(R.id.ib_send).setOnClickListener {
presenter.sendMessage(messageField.text.toString().trim())
}

chatList = findViewById<RecyclerView>(R.id.rv_chat)
chatList = findViewById(R.id.rv_chat)
layoutManager.reverseLayout = true
chatList.layoutManager = layoutManager
chatList.adapter = adapter
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,8 @@ import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.support.design.widget.FloatingActionButton
import android.support.v7.app.AppCompatActivity
import android.support.v7.app.AppCompatDelegate
import android.support.v7.widget.LinearLayoutManager
import android.support.v7.widget.RecyclerView
import android.support.v7.widget.Toolbar
import android.view.View
import android.widget.Button
import android.widget.ImageView
Expand All @@ -22,21 +19,28 @@ import com.glodanif.bluetoothchat.R
import com.glodanif.bluetoothchat.adapter.ConversationsAdapter
import com.glodanif.bluetoothchat.entity.Conversation
import com.glodanif.bluetoothchat.extension.getFirstLetter
import com.glodanif.bluetoothchat.model.*
import com.glodanif.bluetoothchat.model.BluetoothConnectorImpl
import com.glodanif.bluetoothchat.model.ConversationsStorageImpl
import com.glodanif.bluetoothchat.model.SettingsManager
import com.glodanif.bluetoothchat.model.SettingsManagerImpl
import com.glodanif.bluetoothchat.presenter.ConversationsPresenter
import com.glodanif.bluetoothchat.view.ConversationsView
import com.glodanif.bluetoothchat.view.NotificationView
import com.glodanif.bluetoothchat.widget.ActionView
import com.glodanif.bluetoothchat.widget.SettingsPopup
import com.glodanif.bluetoothchat.widget.ShortcutManager
import com.glodanif.bluetoothchat.widget.ShortcutManagerImpl
import java.util.*

class ConversationsActivity : SkeletonActivity(), ConversationsView {

private val REQUEST_SCAN = 101

private lateinit var presenter: ConversationsPresenter
private lateinit var settings: SettingsManager
private val connection: BluetoothConnector = BluetoothConnectorImpl(this)
private val storage: ConversationsStorage = ConversationsStorageImpl(this)
private lateinit var shortcutsManager: ShortcutManager
private val connection = BluetoothConnectorImpl(this)
private val storage = ConversationsStorageImpl(this)

private lateinit var conversationsList: RecyclerView
private lateinit var noConversations: View
Expand All @@ -56,15 +60,16 @@ class ConversationsActivity : SkeletonActivity(), ConversationsView {
setContentView(R.layout.activity_conversations, ActivityType.CUSTOM_TOOLBAR_ACTIVITY)

settings = SettingsManagerImpl(this)
shortcutsManager = ShortcutManagerImpl(this)
presenter = ConversationsPresenter(this, connection, storage, settings)

actions = findViewById<ActionView>(R.id.av_actions)

userAvatar = findViewById<ImageView>(R.id.iv_avatar)
conversationsList = findViewById<RecyclerView>(R.id.rv_conversations)
userAvatar = findViewById(R.id.iv_avatar)
conversationsList = findViewById(R.id.rv_conversations)
noConversations = findViewById(R.id.ll_empty_holder)
optionsButton = findViewById(R.id.ll_options)
addButton = findViewById<FloatingActionButton>(R.id.fab_new_conversation)
addButton = findViewById(R.id.fab_new_conversation)

conversationsList.layoutManager = LinearLayoutManager(this)
conversationsList.adapter = adapter
Expand All @@ -76,8 +81,7 @@ class ConversationsActivity : SkeletonActivity(), ConversationsView {
)

adapter.clickListener = { ChatActivity.start(this, it.deviceAddress) }
adapter.longClickListener = {
conversation, isCurrent ->
adapter.longClickListener = { conversation, isCurrent ->
showContextMenu(conversation, isCurrent)
}

Expand All @@ -94,27 +98,42 @@ class ConversationsActivity : SkeletonActivity(), ConversationsView {

private fun showContextMenu(conversation: Conversation, isCurrent: Boolean) {

val labels = if (!isCurrent) {
arrayOf(getString(R.string.conversations__remove))
} else {
arrayOf(getString(R.string.conversations__remove), getString(R.string.general__disconnect))
val labels = ArrayList<String>()
labels.add(getString(R.string.conversations__remove))
if (isCurrent) {
labels.add(getString(R.string.general__disconnect))
}
if (shortcutsManager.isRequestPinShortcutSupported()) {
labels.add(getString(R.string.conversations__pin_to_home_screen))
}

val builder = AlertDialog.Builder(this)
builder.setTitle(getString(R.string.conversations__options))
.setItems(labels, { _, which ->
.setItems(labels.toTypedArray(), { _, which ->
when (which) {
0 -> {
confirmRemoval(conversation)
}
1 -> {
presenter.disconnect()
if (isCurrent) {
presenter.disconnect()
} else {
requestPinShortcut(conversation)
}
}
2 -> {
requestPinShortcut(conversation)
}
}
})
builder.create().show()
}

private fun requestPinShortcut(conversation: Conversation) {
shortcutsManager.requestPinConversationShortcut(
conversation.deviceAddress, conversation.displayName, conversation.color)
}

private fun confirmRemoval(conversation: Conversation) {

AlertDialog.Builder(this)
Expand Down Expand Up @@ -211,6 +230,10 @@ class ConversationsActivity : SkeletonActivity(), ConversationsView {
settingsPopup.populateData(name, color)
}

override fun removeFromShortcuts(address: String) {
shortcutsManager.removeConversationShortcut(address)
}

override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
super.onActivityResult(requestCode, resultCode, data)

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,25 @@ package com.glodanif.bluetoothchat.activity

import android.content.Context
import android.content.Intent
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.support.annotation.ColorInt
import android.support.v7.app.AppCompatDelegate
import android.support.v7.widget.Toolbar
import android.view.MenuItem
import android.view.View
import android.view.WindowManager
import android.widget.Button
import android.widget.EditText
import android.widget.ImageView
import android.widget.TextView

import com.amulyakhare.textdrawable.TextDrawable
import com.glodanif.bluetoothchat.R
import com.glodanif.bluetoothchat.model.SettingsManager
import com.glodanif.bluetoothchat.model.SettingsManagerImpl
import com.glodanif.bluetoothchat.presenter.ProfilePresenter
import com.glodanif.bluetoothchat.util.SimpleTextWatcher
import com.glodanif.bluetoothchat.view.ProfileView
import com.glodanif.bluetoothchat.widget.ShortcutManagerImpl
import me.priyesh.chroma.ChromaDialog
import me.priyesh.chroma.ColorMode
import me.priyesh.chroma.ColorSelectListener
import com.amulyakhare.textdrawable.TextDrawable
import com.glodanif.bluetoothchat.model.SettingsManager
import com.glodanif.bluetoothchat.util.SimpleTextWatcher

class ProfileActivity : SkeletonActivity(), ProfileView {

Expand Down Expand Up @@ -113,6 +109,10 @@ class ProfileActivity : SkeletonActivity(), ProfileView {
nameField.error = getString(R.string.profile__validation_error)
}

override fun addSearchShortcut() {
ShortcutManagerImpl(this).addSearchShortcut()
}

private val textWatcher = object : SimpleTextWatcher() {
override fun afterTextChanged(text: String) {
nameField.error = null
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import android.widget.TextView
import com.amulyakhare.textdrawable.TextDrawable
import com.glodanif.bluetoothchat.R
import com.glodanif.bluetoothchat.entity.Conversation
import com.glodanif.bluetoothchat.extension.getFirstLetter
import com.glodanif.bluetoothchat.extension.getRelativeTime

class ConversationsAdapter(private val context: Context) : RecyclerView.Adapter<ConversationsAdapter.ConversationViewHolder>() {
Expand Down Expand Up @@ -61,8 +62,7 @@ class ConversationsAdapter(private val context: Context) : RecyclerView.Adapter<
color = Color.LTGRAY
}

val symbol = conversation.displayName[0].toString().toUpperCase()
val drawable = TextDrawable.builder().buildRound(symbol, color)
val drawable = TextDrawable.builder().buildRound(conversation.displayName.getFirstLetter(), color)
holder.avatar.setImageDrawable(drawable)
}

Expand Down Expand Up @@ -91,12 +91,12 @@ class ConversationsAdapter(private val context: Context) : RecyclerView.Adapter<
}

class ConversationViewHolder(itemView: View) : RecyclerView.ViewHolder(itemView) {
val avatar: ImageView = itemView.findViewById<ImageView>(R.id.iv_avatar)
val name: TextView = itemView.findViewById<TextView>(R.id.tv_name)
val connected: ImageView = itemView.findViewById<ImageView>(R.id.iv_connected)
val lastMessage: TextView = itemView.findViewById<TextView>(R.id.tv_last_message)
val time: TextView = itemView.findViewById<TextView>(R.id.tv_time)
val notSeen: TextView = itemView.findViewById<TextView>(R.id.tv_not_seen)
val messageContainer: LinearLayout = itemView.findViewById<LinearLayout>(R.id.ll_message_info)
val avatar: ImageView = itemView.findViewById(R.id.iv_avatar)
val name: TextView = itemView.findViewById(R.id.tv_name)
val connected: ImageView = itemView.findViewById(R.id.iv_connected)
val lastMessage: TextView = itemView.findViewById(R.id.tv_last_message)
val time: TextView = itemView.findViewById(R.id.tv_time)
val notSeen: TextView = itemView.findViewById(R.id.tv_not_seen)
val messageContainer: LinearLayout = itemView.findViewById(R.id.ll_message_info)
}
}
Original file line number Diff line number Diff line change
@@ -1,10 +1,19 @@
package com.glodanif.bluetoothchat.extension

import android.content.Context
import android.graphics.Bitmap
import com.amulyakhare.textdrawable.TextDrawable
import com.glodanif.bluetoothchat.R
import java.text.DateFormat
import java.text.SimpleDateFormat
import java.util.*
import android.R.attr.bitmap
import android.graphics.Canvas
import android.graphics.drawable.Drawable
import android.opengl.ETC1.getHeight
import android.opengl.ETC1.getWidth



fun Date.getRelativeTime(context: Context): String {

Expand Down Expand Up @@ -57,3 +66,13 @@ fun Date.getRelativeTime(context: Context): String {
fun String.getFirstLetter(): String {
return if (this.isEmpty()) "?" else this[0].toString().toUpperCase()
}

fun TextDrawable.getBitmap(): Bitmap {

val bitmap = Bitmap.createBitmap(128, 128, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bitmap)
this.setBounds(0, 0, canvas.width, canvas.height)
this.draw(canvas)

return bitmap
}
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ class ConversationsPresenter(private val view: ConversationsView, private val co
fun removeConversation(conversation: Conversation) {
connection.sendDisconnectRequest()
storage.removeConversation(conversation)
view.removeFromShortcuts(conversation.deviceAddress)
loadConversations()
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ class ProfilePresenter(private val view: ProfileView, private val settings: Sett
if (!currentName.isEmpty() && currentName.length <= 25 && !currentName.contains("#")) {
settings.saveUserName(currentName.trim())
settings.saveUserColor(currentColor)
view.addSearchShortcut()
view.redirectToConversations()
} else {
view.showNotValidNameError()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
package com.glodanif.bluetoothchat.service

import android.app.NotificationManager
import android.app.Service
import android.bluetooth.BluetoothAdapter
import android.bluetooth.BluetoothDevice
Expand All @@ -26,6 +25,8 @@ import com.glodanif.bluetoothchat.entity.Message
import com.glodanif.bluetoothchat.model.*
import com.glodanif.bluetoothchat.view.NotificationView
import com.glodanif.bluetoothchat.view.NotificationViewImpl
import com.glodanif.bluetoothchat.widget.ShortcutManager
import com.glodanif.bluetoothchat.widget.ShortcutManagerImpl
import java.io.IOException
import java.io.InputStream
import java.io.OutputStream
Expand Down Expand Up @@ -67,6 +68,7 @@ class BluetoothConnectionService : Service() {

private lateinit var application: ChatApplication
private lateinit var notificationView: NotificationView
private lateinit var shortcutManager: ShortcutManager

override fun onBind(intent: Intent?): IBinder? {
return binder
Expand All @@ -86,6 +88,7 @@ class BluetoothConnectionService : Service() {
settings = SettingsManagerImpl(this)
preferences = UserPreferences(this)
notificationView = NotificationViewImpl(this)
shortcutManager = ShortcutManagerImpl(this)
isRunning = true
}

Expand Down Expand Up @@ -240,6 +243,10 @@ class BluetoothConnectionService : Service() {
thread {
db.messagesDao().insert(sentMessage)
handler.post { messageListener?.onMessageSent(sentMessage) }
if (currentConversation != null) {
shortcutManager.addConversationShortcut(sentMessage.deviceAddress,
currentConversation!!.displayName, currentConversation!!.color)
}
}
}
}
Expand Down Expand Up @@ -301,6 +308,10 @@ class BluetoothConnectionService : Service() {
thread {
db.messagesDao().insert(receivedMessage)
handler.post { messageListener?.onMessageReceived(receivedMessage) }
if (currentConversation != null) {
shortcutManager.addConversationShortcut(device.address,
currentConversation!!.displayName, currentConversation!!.color)
}
}
}

Expand Down
Loading

0 comments on commit c8d4639

Please sign in to comment.