mirror of
https://github.com/quik-sms/quik.git
synced 2026-11-17 19:38:52 +01:00
Merge pull request #214 from gavine99/widget_conversations_162
Main widget launch appropriate app activities
This commit is contained in:
@@ -0,0 +1,60 @@
|
||||
/*
|
||||
* Copyright (C) 2017 Moez Bhatti <moez.bhatti@gmail.com>
|
||||
*
|
||||
* This file is part of QKSMS.
|
||||
*
|
||||
* QKSMS 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.
|
||||
*
|
||||
* QKSMS 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.
|
||||
*
|
||||
* You should have received a copy of the GNU General Public License
|
||||
* along with QKSMS. If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
package dev.octoshrimpy.quik.receiver
|
||||
|
||||
import android.content.BroadcastReceiver
|
||||
import android.content.Context
|
||||
import android.content.Intent
|
||||
import android.content.Intent.FLAG_ACTIVITY_NEW_TASK
|
||||
import dagger.android.AndroidInjection
|
||||
|
||||
class StartActivityFromWidgetReceiver : BroadcastReceiver() {
|
||||
// why does this shim class exist rather than the widget launching activities more directly?
|
||||
// the main widget listview items all need to launch the compose activity at an existing
|
||||
// threadId *except* the 'view more conversations' footer item, which needs to launch
|
||||
// the main activity. a listview in a widget *must* use a single pending intent template
|
||||
// for all items which doesn't allow for more than one component/class. this shim class
|
||||
// allows a single component/class for the widget listview pending intent template that, based
|
||||
// on the activityToStart value, launches the appropriate activity
|
||||
|
||||
companion object {
|
||||
const val COMPOSE_ACTIVITY = ".feature.compose.ComposeActivity"
|
||||
const val MAIN_ACTIVITY = ".feature.main.MainActivity"
|
||||
}
|
||||
|
||||
override fun onReceive(context: Context, intent: Intent) {
|
||||
AndroidInjection.inject(this, context)
|
||||
|
||||
var activityToStartName = intent.getStringExtra("activityToStart")
|
||||
|
||||
// protect against invalid launch activity
|
||||
activityToStartName = when (activityToStartName) {
|
||||
COMPOSE_ACTIVITY -> activityToStartName
|
||||
MAIN_ACTIVITY -> activityToStartName
|
||||
else -> return
|
||||
}
|
||||
|
||||
context.startActivity(
|
||||
Intent(context, Class.forName(context.packageName + activityToStartName))
|
||||
.setFlags(FLAG_ACTIVITY_NEW_TASK)
|
||||
.putExtra("threadId", intent.getLongExtra("threadId", 0L))
|
||||
)
|
||||
}
|
||||
|
||||
}
|
||||
@@ -219,6 +219,7 @@
|
||||
android:resource="@xml/widget_info" />
|
||||
</receiver>
|
||||
<receiver android:name=".receiver.SendSmsReceiver" />
|
||||
<receiver android:name=".receiver.StartActivityFromWidgetReceiver" />
|
||||
|
||||
<service android:name="com.android.mms.transaction.TransactionService" />
|
||||
<service android:name=".feature.backup.RestoreBackupService" />
|
||||
|
||||
@@ -37,6 +37,7 @@ import dev.octoshrimpy.quik.injection.appComponent
|
||||
import dev.octoshrimpy.quik.model.Contact
|
||||
import dev.octoshrimpy.quik.model.Conversation
|
||||
import dev.octoshrimpy.quik.model.PhoneNumber
|
||||
import dev.octoshrimpy.quik.receiver.StartActivityFromWidgetReceiver
|
||||
import dev.octoshrimpy.quik.repository.ConversationRepository
|
||||
import dev.octoshrimpy.quik.util.GlideApp
|
||||
import dev.octoshrimpy.quik.util.Preferences
|
||||
@@ -160,11 +161,13 @@ class WidgetAdapter(intent: Intent) : RemoteViewsService.RemoteViewsFactory {
|
||||
remoteViews.setTextColor(R.id.snippet, if (conversation.unread) textPrimary else textTertiary)
|
||||
remoteViews.setTextViewText(R.id.snippet, boldText(snippet, conversation.unread))
|
||||
|
||||
// Launch conversation on click
|
||||
val clickIntent = Intent()
|
||||
.putExtra("screen", "compose")
|
||||
// set fill-in intent to be used for current item
|
||||
remoteViews.setOnClickFillInIntent(
|
||||
R.id.conversation,
|
||||
Intent()
|
||||
.putExtra("activityToStart", StartActivityFromWidgetReceiver.COMPOSE_ACTIVITY)
|
||||
.putExtra("threadId", conversation.id)
|
||||
remoteViews.setOnClickFillInIntent(R.id.conversation, clickIntent)
|
||||
)
|
||||
|
||||
return remoteViews
|
||||
}
|
||||
@@ -173,7 +176,11 @@ class WidgetAdapter(intent: Intent) : RemoteViewsService.RemoteViewsFactory {
|
||||
val view = RemoteViews(context.packageName, R.layout.widget_loading)
|
||||
view.setTextColor(R.id.loadingText, textSecondary)
|
||||
view.setTextViewText(R.id.loadingText, context.getString(R.string.widget_more))
|
||||
view.setOnClickFillInIntent(R.id.loading, Intent())
|
||||
view.setOnClickFillInIntent(
|
||||
R.id.loadingText,
|
||||
Intent()
|
||||
.putExtra("activityToStart", StartActivityFromWidgetReceiver.MAIN_ACTIVITY)
|
||||
)
|
||||
return view
|
||||
}
|
||||
|
||||
|
||||
@@ -35,6 +35,7 @@ import dev.octoshrimpy.quik.common.util.extensions.getColorCompat
|
||||
import dev.octoshrimpy.quik.feature.compose.ComposeActivity
|
||||
import dev.octoshrimpy.quik.feature.main.MainActivity
|
||||
import dev.octoshrimpy.quik.manager.WidgetManager
|
||||
import dev.octoshrimpy.quik.receiver.StartActivityFromWidgetReceiver
|
||||
import dev.octoshrimpy.quik.util.Preferences
|
||||
import timber.log.Timber
|
||||
import javax.inject.Inject
|
||||
@@ -129,8 +130,6 @@ class WidgetProvider : AppWidgetProvider() {
|
||||
false -> R.color.textPrimary
|
||||
}))
|
||||
|
||||
remoteViews.setInt(R.id.compose, "setColorFilter", colors.theme().theme)
|
||||
|
||||
// Set adapter for conversations
|
||||
val intent = Intent(context, WidgetService::class.java)
|
||||
.putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId)
|
||||
@@ -138,20 +137,40 @@ class WidgetProvider : AppWidgetProvider() {
|
||||
intent.data = Uri.parse(intent.toUri(Intent.URI_INTENT_SCHEME))
|
||||
remoteViews.setRemoteAdapter(R.id.conversations, intent)
|
||||
|
||||
// Main intent
|
||||
val mainIntent = Intent(context, MainActivity::class.java)
|
||||
val mainPI = PendingIntent.getActivity(context, 0, mainIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
|
||||
remoteViews.setOnClickPendingIntent(R.id.title, mainPI)
|
||||
// compose new message image color and on click intent
|
||||
remoteViews.setInt(R.id.compose, "setColorFilter", colors.theme().theme)
|
||||
remoteViews.setOnClickPendingIntent(
|
||||
R.id.compose,
|
||||
PendingIntent.getActivity(
|
||||
context,
|
||||
0,
|
||||
Intent(context, ComposeActivity::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
)
|
||||
|
||||
// Compose intent
|
||||
val composeIntent = Intent(context, ComposeActivity::class.java)
|
||||
val composePI = PendingIntent.getActivity(context, 0, composeIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
|
||||
remoteViews.setOnClickPendingIntent(R.id.compose, composePI)
|
||||
// use a single pending intent for launching main activity because they
|
||||
// can sometimes be in short supply in the OS
|
||||
val mainActivityPendingIntent = PendingIntent.getActivity(
|
||||
context,
|
||||
99,
|
||||
Intent(context, MainActivity::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE
|
||||
)
|
||||
|
||||
// Conversation intent
|
||||
val startActivityIntent = Intent(context, MainActivity::class.java)
|
||||
val startActivityPendingIntent = PendingIntent.getActivity(context, 0, startActivityIntent, PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_IMMUTABLE)
|
||||
remoteViews.setPendingIntentTemplate(R.id.conversations, startActivityPendingIntent)
|
||||
// title click to load app main activity / conversation view
|
||||
remoteViews.setOnClickPendingIntent(R.id.title, mainActivityPendingIntent)
|
||||
|
||||
// pending intent template to be used for all list items
|
||||
remoteViews.setPendingIntentTemplate(
|
||||
R.id.conversations,
|
||||
PendingIntent.getBroadcast(
|
||||
context,
|
||||
1,
|
||||
Intent(context, StartActivityFromWidgetReceiver::class.java),
|
||||
PendingIntent.FLAG_UPDATE_CURRENT or PendingIntent.FLAG_MUTABLE
|
||||
)
|
||||
)
|
||||
|
||||
AppWidgetManager.getInstance(context).updateAppWidget(appWidgetId, remoteViews)
|
||||
}
|
||||
|
||||
+5
@@ -41,6 +41,7 @@ import dev.octoshrimpy.quik.receiver.SmsSentReceiver
|
||||
import dagger.Module
|
||||
import dagger.android.ContributesAndroidInjector
|
||||
import dev.octoshrimpy.quik.receiver.SpeakThreadsReceiver
|
||||
import dev.octoshrimpy.quik.receiver.StartActivityFromWidgetReceiver
|
||||
|
||||
@Module
|
||||
abstract class BroadcastReceiverBuilderModule {
|
||||
@@ -73,6 +74,10 @@ abstract class BroadcastReceiverBuilderModule {
|
||||
@ContributesAndroidInjector()
|
||||
abstract fun bindSpeakThreadsReceiver(): SpeakThreadsReceiver
|
||||
|
||||
@ActivityScope
|
||||
@ContributesAndroidInjector()
|
||||
abstract fun bindStartActivityFromWidgetReceiver(): StartActivityFromWidgetReceiver
|
||||
|
||||
@ActivityScope
|
||||
@ContributesAndroidInjector()
|
||||
abstract fun bindMarkSeenReceiver(): MarkSeenReceiver
|
||||
|
||||
@@ -24,6 +24,7 @@
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content"
|
||||
android:background="@drawable/ripple"
|
||||
android:clickable="false"
|
||||
android:gravity="center_vertical|start"
|
||||
android:orientation="horizontal"
|
||||
android:paddingLeft="16dp"
|
||||
@@ -36,12 +37,14 @@
|
||||
android:layout_width="48dp"
|
||||
android:layout_height="48dp"
|
||||
android:layout_marginEnd="16dp"
|
||||
android:clickable="false"
|
||||
tools:background="@color/tools_theme">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/initial"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:clickable="false"
|
||||
android:gravity="center"
|
||||
android:textAllCaps="true"
|
||||
android:textSize="20sp"
|
||||
@@ -53,6 +56,7 @@
|
||||
android:layout_width="24dp"
|
||||
android:layout_height="24dp"
|
||||
android:layout_gravity="center"
|
||||
android:clickable="false"
|
||||
android:src="@drawable/ic_person_black_24dp"
|
||||
tools:tint="@color/textPrimaryDark" />
|
||||
|
||||
@@ -60,19 +64,22 @@
|
||||
android:id="@+id/photo"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:clickable="false"
|
||||
tools:src="@tools:sample/avatars" />
|
||||
|
||||
<ImageView
|
||||
android:id="@+id/avatarMask"
|
||||
android:layout_width="match_parent"
|
||||
android:layout_height="match_parent"
|
||||
android:clickable="false"
|
||||
android:src="@drawable/circle_mask" />
|
||||
|
||||
</FrameLayout>
|
||||
|
||||
<RelativeLayout
|
||||
android:layout_width="wrap_content"
|
||||
android:layout_height="wrap_content">
|
||||
android:layout_height="wrap_content"
|
||||
android:clickable="false">
|
||||
|
||||
<TextView
|
||||
android:id="@+id/name"
|
||||
@@ -80,6 +87,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_alignParentStart="true"
|
||||
android:layout_toStartOf="@+id/date"
|
||||
android:clickable="false"
|
||||
android:singleLine="true"
|
||||
android:textSize="16sp"
|
||||
tools:text="@tools:sample/full_names" />
|
||||
@@ -91,6 +99,7 @@
|
||||
android:layout_alignParentTop="true"
|
||||
android:layout_alignParentEnd="true"
|
||||
android:layout_marginStart="16dp"
|
||||
android:clickable="false"
|
||||
android:textSize="12sp"
|
||||
tools:text="@tools:sample/date/day_of_week" />
|
||||
|
||||
@@ -100,6 +109,7 @@
|
||||
android:layout_height="wrap_content"
|
||||
android:layout_below="@id/name"
|
||||
android:layout_alignParentStart="true"
|
||||
android:clickable="false"
|
||||
android:lines="1"
|
||||
android:singleLine="true"
|
||||
android:textSize="14sp"
|
||||
|
||||
Reference in New Issue
Block a user