@@ -7,9 +7,11 @@ import android.Manifest.permission.WRITE_SECURE_SETTINGS
7
7
import android.app.PendingIntent
8
8
import android.app.PendingIntent.FLAG_IMMUTABLE
9
9
import android.content.BroadcastReceiver
10
+ import android.content.ComponentName
10
11
import android.content.Context
11
12
import android.content.Intent
12
13
import android.content.IntentFilter
14
+ import android.content.pm.PackageManager
13
15
import android.content.pm.PackageManager.PERMISSION_GRANTED
14
16
import android.nfc.NfcAdapter
15
17
import android.os.Build.VERSION.SDK_INT
@@ -21,7 +23,6 @@ import android.service.quicksettings.TileService
21
23
import android.util.Log
22
24
import androidx.core.content.ContextCompat
23
25
import au.id.colby.nfcquicksettings.R.string
24
- import java.lang.IllegalArgumentException
25
26
26
27
private const val TAG = " NfcTileService"
27
28
@@ -34,6 +35,18 @@ private const val TAG = "NfcTileService"
34
35
class NfcTileService : TileService () {
35
36
private val nfcBroadcastReceiver = NfcBroadcastReceiver ()
36
37
38
+ /* *
39
+ * Called when the tile service is created.
40
+ *
41
+ * This updates the associated NFC Tile Preferences activity (ie to enable or disable that
42
+ * activity, based on whether or not the WRITE_SECURE_SETTINGS permission has been granted).
43
+ */
44
+ override fun onCreate () {
45
+ super .onCreate()
46
+ Log .d(TAG , " onCreate" )
47
+ updatePreferencesActivity()
48
+ }
49
+
37
50
/* *
38
51
* Called when this tile moves into a listening state.
39
52
*
@@ -145,6 +158,28 @@ class NfcTileService : TileService() {
145
158
else startActivityAndCollapse(PendingIntent .getActivity(this , 0 , intent, FLAG_IMMUTABLE ))
146
159
}
147
160
161
+ /* *
162
+ * Enables or disables the NFC tile's preferences activity based on whether or not the
163
+ * `WRITE_SECURE_SETTINGS` permission has been granted.
164
+ *
165
+ * Thus if the permission is granted, then NfcTilePreferencesActivity will be enabled to
166
+ * redirect users to the NFC Settings activity (`ACTION_NFC_SETTINGS`) on long-tapping the tile
167
+ * But if the the permissions is not granted, then NfcTilePreferencesActivity is disabled,
168
+ * and long-tapping the tile will result in the default OS behaviour (ie starting the
169
+ * Application Details activity (`ACTION_APPLICATION_DETAILS_SETTINGS`)).
170
+ */
171
+ private fun updatePreferencesActivity () {
172
+ val newState =
173
+ if (permissionGranted(WRITE_SECURE_SETTINGS )) PackageManager .COMPONENT_ENABLED_STATE_ENABLED
174
+ else PackageManager .COMPONENT_ENABLED_STATE_DISABLED
175
+ Log .d(TAG , " Setting preferences activity enabled setting to $newState " )
176
+ applicationContext.packageManager.setComponentEnabledSetting(
177
+ ComponentName (applicationContext, NfcTilePreferencesActivity ::class .java),
178
+ newState,
179
+ PackageManager .DONT_KILL_APP
180
+ )
181
+ }
182
+
148
183
/* *
149
184
* Updates the Quick Settings tile with the [newState] and (if supported) [newSubTitleResId].
150
185
*
@@ -178,8 +213,10 @@ class NfcTileService : TileService() {
178
213
* @param adapter The adapter to reflect the state of.
179
214
*/
180
215
private fun updateTile (adapter : NfcAdapter ? = NfcAdapter .getDefaultAdapter(this)) {
181
- adapter?.apply { updateTile(isEnabled) } ? :
182
- updateTile(Tile .STATE_INACTIVE , string.tile_subtitle_unavailable)
216
+ adapter?.apply { updateTile(isEnabled) } ? : updateTile(
217
+ Tile .STATE_INACTIVE ,
218
+ string.tile_subtitle_unavailable
219
+ )
183
220
}
184
221
185
222
/* *
0 commit comments