Skip to content

Commit 3b68fd6

Browse files
authored
Merge pull request #31 from it5prasoon/dev/permission-helpers
Feature Enhancements
2 parents f9c74ca + 9a69172 commit 3b68fd6

File tree

13 files changed

+390
-198
lines changed

13 files changed

+390
-198
lines changed

app/src/main/java/com/matrix/autoreply/constants/Constants.kt

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,9 @@ object Constants {
2121

2222
const val MIN_DAYS = 0
2323
const val MAX_DAYS = 7
24+
25+
// Notification Listener Service
26+
const val PERMISSION_DENIED = "Permission Denied"
27+
const val PERMISSION_GRANTED = "Permission Granted"
28+
const val RESTART_SERVICE_TOAST = "Service not enabled! Please restart the service from setting."
2429
}

app/src/main/java/com/matrix/autoreply/preferences/PreferencesManager.kt

Lines changed: 24 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,14 @@ import java.util.*
1414
class PreferencesManager private constructor(private val thisAppContext: Context) {
1515

1616
private val KEY_SERVICE_ENABLED = "pref_service_enabled"
17+
private val KEY_AUTO_REPLY_ENABLED = "pref_auto_reply_enabled"
18+
private val KEY_MESSAGE_LOGS_ENABLED = "pref_message_logs_enabled"
1719
private val KEY_GROUP_REPLY_ENABLED = "pref_group_reply_enabled"
1820
private val KEY_AUTO_REPLY_THROTTLE_TIME_MS = "pref_auto_reply_throttle_time_ms"
1921
private val KEY_SELECTED_APPS_ARR = "pref_selected_apps_arr"
20-
private val KEY_IS_APPEND_WATOMATIC_ATTRIBUTION = "pref_is_append_watomatic_attribution"
21-
private val KEY_GITHUB_RELEASE_NOTES_ID = "pref_github_release_notes_id"
22+
private val KEY_IS_APPEND_AUTOREPLY_ATTRIBUTION = "pref_is_append_watomatic_attribution"
2223
private val KEY_PURGE_MESSAGE_LOGS_LAST_TIME = "pref_purge_message_logs_last_time"
2324
private val KEY_PLAY_STORE_RATING_STATUS = "pref_play_store_rating_status"
24-
private val KEY_PLAY_STORE_RATING_LAST_TIME = "pref_play_store_rating_last_time"
2525
private var KEY_IS_SHOW_NOTIFICATIONS_ENABLED: String? = null
2626
private var KEY_SELECTED_APP_LANGUAGE: String? = null
2727
private val _sharedPrefs: SharedPreferences = PreferenceManager.getDefaultSharedPreferences(thisAppContext)
@@ -39,7 +39,7 @@ class PreferencesManager private constructor(private val thisAppContext: Context
3939
setShowNotificationPref(true)
4040
}
4141
if (isFirstInstall(thisAppContext)) {
42-
if (!_sharedPrefs.contains(KEY_IS_APPEND_WATOMATIC_ATTRIBUTION)) {
42+
if (!_sharedPrefs.contains(KEY_IS_APPEND_AUTOREPLY_ATTRIBUTION)) {
4343
setAppendAutoreplyAttribution(true)
4444
}
4545
} else {
@@ -56,6 +56,24 @@ class PreferencesManager private constructor(private val thisAppContext: Context
5656
editor.apply()
5757
}
5858

59+
val isAutoReplyEnabled: Boolean
60+
get() = _sharedPrefs.getBoolean(KEY_AUTO_REPLY_ENABLED, false)
61+
62+
fun setAutoReplyPref(enabled: Boolean) {
63+
val editor = _sharedPrefs.edit()
64+
editor.putBoolean(KEY_AUTO_REPLY_ENABLED, enabled)
65+
editor.apply()
66+
}
67+
68+
val isMessageLogsEnabled: Boolean
69+
get() = _sharedPrefs.getBoolean(KEY_MESSAGE_LOGS_ENABLED, false)
70+
71+
fun setMessageLogsPref(enabled: Boolean) {
72+
val editor = _sharedPrefs.edit()
73+
editor.putBoolean(KEY_MESSAGE_LOGS_ENABLED, enabled)
74+
editor.apply()
75+
}
76+
5977
val isGroupReplyEnabled: Boolean
6078
get() = _sharedPrefs.getBoolean(KEY_GROUP_REPLY_ENABLED, false)
6179

@@ -119,12 +137,12 @@ class PreferencesManager private constructor(private val thisAppContext: Context
119137

120138
fun setAppendAutoreplyAttribution(enabled: Boolean) {
121139
val editor = _sharedPrefs.edit()
122-
editor.putBoolean(KEY_IS_APPEND_WATOMATIC_ATTRIBUTION, enabled)
140+
editor.putBoolean(KEY_IS_APPEND_AUTOREPLY_ATTRIBUTION, enabled)
123141
editor.apply()
124142
}
125143

126144
val isAppendAutoreplyAttributionEnabled: Boolean
127-
get() = _sharedPrefs.getBoolean(KEY_IS_APPEND_WATOMATIC_ATTRIBUTION, false)
145+
get() = _sharedPrefs.getBoolean(KEY_IS_APPEND_AUTOREPLY_ATTRIBUTION, false)
128146

129147
fun getSelectedLanguageStr(defaultLangStr: String?): String? {
130148
return _sharedPrefs.getString(KEY_SELECTED_APP_LANGUAGE, defaultLangStr)

app/src/main/java/com/matrix/autoreply/services/ForegroundNotificationService.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,15 @@ class ForegroundNotificationService : NotificationListenerService() {
4040

4141
private fun canSaveLogs(sbn: StatusBarNotification): Boolean {
4242
return isServiceEnabled &&
43+
isMessageLogsEnabled &&
4344
isSupportedPackage(sbn) &&
4445
NotificationUtils.isNewNotification(sbn) &&
4546
isGroupMessageAndReplyAllowed(sbn)
4647
}
4748

4849
private fun canReply(sbn: StatusBarNotification): Boolean {
4950
return isServiceEnabled &&
51+
isAutoReplyEnabled &&
5052
isSupportedPackage(sbn) &&
5153
NotificationUtils.isNewNotification(sbn) &&
5254
isGroupMessageAndReplyAllowed(sbn) &&
@@ -60,6 +62,9 @@ class ForegroundNotificationService : NotificationListenerService() {
6062
}
6163

6264
private fun saveLogs(sbn: StatusBarNotification) {
65+
if (dbUtils == null) {
66+
dbUtils = DbUtils(applicationContext)
67+
}
6368
dbUtils!!.saveLogs(sbn, NotificationUtils.getTitle(sbn), NotificationUtils.getMessage(sbn))
6469
}
6570

@@ -162,4 +167,10 @@ class ForegroundNotificationService : NotificationListenerService() {
162167

163168
private val isServiceEnabled: Boolean
164169
get() = PreferencesManager.getPreferencesInstance(this)!!.isServiceEnabled
170+
171+
private val isAutoReplyEnabled: Boolean
172+
get() = PreferencesManager.getPreferencesInstance(this)!!.isAutoReplyEnabled
173+
174+
private val isMessageLogsEnabled: Boolean
175+
get() = PreferencesManager.getPreferencesInstance(this)!!.isMessageLogsEnabled
165176
}

app/src/main/java/com/matrix/autoreply/ui/activity/TabbedActivity.kt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -112,7 +112,7 @@ class TabbedActivity : AppCompatActivity() {
112112
AlertDialogHelper.showDialog(
113113
this,
114114
"Help",
115-
"1. Give the required permissions one for auto reply and other for message logs.\n" +
115+
"1. Give the required permissions.\n" +
116116
"2. Set custom text for auto reply.\n" +
117117
"3. Select the applications for which you want auto reply.\n" +
118118
"4. If you want group chat reply then turn on that option.\n" +
Lines changed: 58 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,89 @@
11
package com.matrix.autoreply.ui.fragment
22

3+
import android.app.Activity
34
import android.content.Intent
45
import android.os.Bundle
56
import android.view.LayoutInflater
67
import android.view.View
78
import android.view.ViewGroup
8-
import android.widget.Button
9-
import android.widget.TextView
9+
import android.widget.CompoundButton
10+
import android.widget.Toast
1011
import androidx.fragment.app.Fragment
1112
import com.matrix.autoreply.R
13+
import com.matrix.autoreply.constants.Constants
14+
import com.matrix.autoreply.databinding.FragmentDeletedMessageBinding
15+
import com.matrix.autoreply.preferences.PreferencesManager
1216
import com.matrix.autoreply.ui.activity.logsViewer.MsgLogViewerActivity
17+
import com.matrix.autoreply.utils.NotificationListenerUtil
1318

1419

1520
open class DeletedMessageFragment : Fragment() {
1621

22+
private var _binding: FragmentDeletedMessageBinding? = null
23+
private val binding get() = _binding!!
1724
private val checkEmoji = String(Character.toChars(0x2714))
25+
private var preferencesManager: PreferencesManager? = null
26+
private var mActivity: Activity? = null
27+
private lateinit var notificationListenerUtil: NotificationListenerUtil
1828

1929
companion object {
2030
private const val WHATSAPP = "whatsapp";
2131
private const val APP = "app";
2232
}
2333

2434
override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View {
25-
val view: View = inflater.inflate(R.layout.fragment_deleted_message, container, false)
35+
_binding = FragmentDeletedMessageBinding.inflate(inflater, container, false)
36+
mActivity = activity
37+
return binding.root
38+
}
2639

27-
// Widgets
28-
val msgLogStatus = view.findViewById<TextView>(R.id.msg_log_status)
29-
val viewWALogBtn = view.findViewById<Button>(R.id.view_wa_log_btn)
40+
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
41+
preferencesManager = PreferencesManager.getPreferencesInstance(mActivity!!)
42+
binding.msgLogStatus.text = getString(R.string.msg_log_status_str, checkEmoji)
3043

31-
msgLogStatus.text = getString(R.string.msg_log_status_str, checkEmoji)
44+
notificationListenerUtil = NotificationListenerUtil(mActivity!!)
3245

3346
// WhatsApp Message Logs viewer
34-
viewWALogBtn.setOnClickListener {
47+
binding.viewWaLogBtn.setOnClickListener {
3548
val intent = Intent(requireActivity(), MsgLogViewerActivity::class.java)
3649
intent.putExtra(APP, WHATSAPP)
3750
startActivity(intent)
3851
}
39-
return view
52+
53+
handleEnableMessageLogsSwitch()
4054
}
41-
}
55+
56+
override fun onResume() {
57+
super.onResume()
58+
//If user directly goes to Settings and removes notifications permission
59+
//when app is launched check for permission and set appropriate app state
60+
if (!notificationListenerUtil.isNotificationServiceEnabled()) {
61+
preferencesManager!!.setMessageLogsPref(false)
62+
}
63+
setSwitchState()
64+
}
65+
66+
private fun handleEnableMessageLogsSwitch() {
67+
binding.enableMessageLogsSwitch.setOnCheckedChangeListener { buttonView: CompoundButton?, isChecked: Boolean ->
68+
if (isChecked && !notificationListenerUtil.isNotificationServiceEnabled()) {
69+
Toast.makeText(mActivity, Constants.RESTART_SERVICE_TOAST, Toast.LENGTH_LONG).show()
70+
} else {
71+
preferencesManager!!.setMessageLogsPref(isChecked)
72+
binding.enableMessageLogsSwitch.setText(
73+
if (isChecked) R.string.mainAutoReplySwitchOnLabel else R.string.mainAutoReplySwitchOffLabel
74+
)
75+
setSwitchState()
76+
}
77+
}
78+
}
79+
80+
private fun setSwitchState() {
81+
binding.enableMessageLogsSwitch.isChecked = preferencesManager!!.isMessageLogsEnabled
82+
}
83+
84+
override fun onDestroyView() {
85+
super.onDestroyView()
86+
_binding = null
87+
}
88+
89+
}

0 commit comments

Comments
 (0)