/
Navigator.kt
233 lines (198 loc) · 8.12 KB
/
Navigator.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
/*
* 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 common
import android.app.Activity
import android.app.ActivityOptions
import android.content.ActivityNotFoundException
import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Build
import android.provider.ContactsContract
import android.provider.Settings
import android.provider.Telephony
import android.view.View
import com.moez.QKSMS.BuildConfig
import feature.blocked.BlockedActivity
import feature.compose.ComposeActivity
import feature.conversationinfo.ConversationInfoActivity
import feature.gallery.GalleryActivity
import feature.notificationprefs.NotificationPrefsActivity
import feature.plus.PlusActivity
import feature.settings.SettingsActivity
import feature.settings.about.AboutActivity
import feature.themepicker.ThemePickerActivity
import manager.NotificationManager
import javax.inject.Inject
import javax.inject.Singleton
@Singleton
class Navigator @Inject constructor(private val context: Context, private val notificationManager: NotificationManager) {
private fun startActivity(intent: Intent) {
intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK)
context.startActivity(intent)
}
private fun startActivityExternal(intent: Intent) {
if (intent.resolveActivity(context.packageManager) != null) {
startActivity(intent)
} else {
startActivity(Intent.createChooser(intent, null))
}
}
fun showQksmsPlusActivity() {
val intent = Intent(context, PlusActivity::class.java)
startActivity(intent)
}
fun showDefaultSmsDialog() {
val intent = Intent(Telephony.Sms.Intents.ACTION_CHANGE_DEFAULT)
if (Telephony.Sms.getDefaultSmsPackage(context) != context.packageName) {
intent.putExtra(Telephony.Sms.Intents.EXTRA_PACKAGE_NAME, context.packageName)
}
startActivity(intent)
}
fun showCompose(body: String? = null, images: List<Uri>? = null) {
val intent = Intent(context, ComposeActivity::class.java)
intent.putExtra(Intent.EXTRA_TEXT, body)
images?.takeIf { it.isNotEmpty() }?.let {
intent.putParcelableArrayListExtra(Intent.EXTRA_STREAM, ArrayList(images))
}
startActivity(intent)
}
fun showConversation(threadId: Long, query: String? = null) {
val intent = Intent(context, ComposeActivity::class.java)
.putExtra("threadId", threadId)
.putExtra("query", query)
startActivity(intent)
}
fun showConversationInfo(threadId: Long) {
val intent = Intent(context, ConversationInfoActivity::class.java)
intent.putExtra("threadId", threadId)
startActivity(intent)
}
fun showMedia(partId: Long) {
val intent = Intent(context, GalleryActivity::class.java)
intent.putExtra("partId", partId)
startActivity(intent)
}
/**
* Shows the attachment full-screen
* The transitionName for the view should be the id of the image being displayed
*/
fun showImageAnimated(from: Activity, view: View) {
val intent = Intent(context, GalleryActivity::class.java)
intent.putExtra("partId", view.transitionName.toLong())
val options = ActivityOptions.makeSceneTransitionAnimation(from, view, view.transitionName)
from.startActivity(intent, options.toBundle())
}
fun showSettings() {
val intent = Intent(context, SettingsActivity::class.java)
startActivity(intent)
}
fun showAbout() {
val intent = Intent(context, AboutActivity::class.java)
startActivity(intent)
}
fun showDeveloper() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/moezbhatti"))
startActivity(intent)
}
fun showSourceCode() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/moezbhatti/qksms"))
startActivity(intent)
}
fun showChangelog() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/moezbhatti/qksms/releases"))
startActivity(intent)
}
fun showLicense() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://github.com/moezbhatti/qksms/blob/master/LICENSE"))
startActivity(intent)
}
fun showBlockedConversations() {
val intent = Intent(context, BlockedActivity::class.java)
startActivity(intent)
}
fun showThemePicker(threadId: Long = 0) {
val intent = Intent(context, ThemePickerActivity::class.java)
intent.putExtra("threadId", threadId)
startActivity(intent)
}
fun makePhoneCall(address: String) {
val intent = Intent(Intent.ACTION_DIAL, Uri.parse("tel:$address"))
startActivityExternal(intent)
}
fun showDonation() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("https://bit.ly/QKSMSDonation"))
startActivity(intent)
}
fun showRating() {
val intent = Intent(Intent.ACTION_VIEW, Uri.parse("market://details?id=${context.packageName}"))
.addFlags(Intent.FLAG_ACTIVITY_NO_HISTORY
or Intent.FLAG_ACTIVITY_NEW_DOCUMENT
or Intent.FLAG_ACTIVITY_MULTIPLE_TASK)
try {
startActivity(intent)
} catch (e: ActivityNotFoundException) {
startActivity(Intent(Intent.ACTION_VIEW, Uri.parse("http://play.google.com/store/apps/details?id=" + context.packageName)))
}
}
/**
* Launch the Play Store and display the Should I Answer? listing
*/
fun showSia() {
val url = "https://play.google.com/store/apps/details?id=org.mistergroup.shouldianswerpersonal"
val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url))
startActivity(intent)
}
fun showSupport() {
val intent = Intent(Intent.ACTION_SENDTO)
intent.data = Uri.parse("mailto:")
intent.putExtra(Intent.EXTRA_EMAIL, arrayOf("team@qklabs.com"))
intent.putExtra(Intent.EXTRA_SUBJECT, "QKSMS Support")
intent.putExtra(Intent.EXTRA_TEXT, StringBuilder("\n\n")
.append("--- Please write your message above this line ---\n\n")
.append("Version: ${BuildConfig.VERSION_NAME}\n")
.append("Device: ${Build.BRAND} ${Build.MODEL}\n")
.append("SDK: ${Build.VERSION.SDK_INT}")
.toString())
startActivityExternal(intent)
}
fun addContact(address: String) {
val intent = Intent(Intent.ACTION_INSERT)
.setType(ContactsContract.Contacts.CONTENT_TYPE)
.putExtra(ContactsContract.Intents.Insert.PHONE, address)
startActivityExternal(intent)
}
fun showNotificationSettings(threadId: Long = 0) {
val intent = Intent(context, NotificationPrefsActivity::class.java)
intent.putExtra("threadId", threadId)
startActivity(intent)
}
fun showNotificationChannel(threadId: Long = 0) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
if (threadId != 0L) {
notificationManager.createNotificationChannel(threadId)
}
val channelId = notificationManager.buildNotificationChannelId(threadId)
val intent = Intent(Settings.ACTION_CHANNEL_NOTIFICATION_SETTINGS)
intent.putExtra(Settings.EXTRA_CHANNEL_ID, channelId)
intent.putExtra(Settings.EXTRA_APP_PACKAGE, context.packageName)
startActivity(intent)
}
}
}