Skip to content

Commit

Permalink
Merge branch 'main' into android-15
Browse files Browse the repository at this point in the history
  • Loading branch information
pcolby committed Aug 12, 2024
2 parents c5bb8f7 + 8a866f6 commit 8280790
Show file tree
Hide file tree
Showing 10 changed files with 109 additions and 12 deletions.
2 changes: 1 addition & 1 deletion .github/workflows/build.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ jobs:
distribution: 'temurin'
java-version: '17'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
uses: gradle/actions/setup-gradle@v4
- name: Setup Build ID
run: |
[[ "$MATRIX_OS" == ubuntu* ]] && osName=linux || osName="${MATRIX_OS:0:3}"
Expand Down
2 changes: 1 addition & 1 deletion .github/workflows/codeql.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ jobs:
distribution: 'temurin'
java-version: '17'
- name: Setup Gradle
uses: gradle/actions/setup-gradle@v3
uses: gradle/actions/setup-gradle@v4
- name: Initialize CodeQL
uses: github/codeql-action/init@v3
with:
Expand Down
7 changes: 6 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# Changelog[^1]

## [1.4.3][] (2024-08-12)

Redirected long-tap to NFC Settings when granted `WRITE_SECURE_SETTINGS` permission.

## [1.4.2][] (2024-07-30)

Handle a rare exception when unregistering the broadcast listener.
Expand Down Expand Up @@ -42,7 +46,8 @@ Added translations for 86 languages.

Initial release.

[Unreleased]: https://github.com/pcolby/nfc-quick-settings/compare/v1.4.2...HEAD
[Unreleased]: https://github.com/pcolby/nfc-quick-settings/compare/v1.4.3...HEAD
[1.4.3]: https://github.com/pcolby/nfc-quick-settings/releases/tag/v1.4.3
[1.4.2]: https://github.com/pcolby/nfc-quick-settings/releases/tag/v1.4.2
[1.4.1]: https://github.com/pcolby/nfc-quick-settings/releases/tag/v1.4.1
[1.4.0]: https://github.com/pcolby/nfc-quick-settings/releases/tag/v1.4.0
Expand Down
9 changes: 7 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ android {
applicationId "au.id.colby.nfcquicksettings"
minSdk 24
targetSdk 35
versionCode 13
versionName "1.4.3-pre"
versionCode 14
versionName "1.4.4-pre"
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand All @@ -51,6 +51,11 @@ android {
kotlinOptions {
jvmTarget = '1.8'
}
packaging {
resources {
excludes += '/META-INF/{AL2.0,LGPL2.1}'
}
}
}

base {
Expand Down
12 changes: 12 additions & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,18 @@
android:theme="@style/Theme.NFCQuickSettingsTile"
tools:targetApi="31">

<activity
android:name=".NfcTilePreferencesActivity"
android:enabled="false"
android:excludeFromRecents="true"
android:exported="false"
android:label="@string/tile_prefs_label"
android:theme="@android:style/Theme.NoDisplay">
<intent-filter>
<action android:name="android.service.quicksettings.action.QS_TILE_PREFERENCES" />
</intent-filter>
</activity>

<service
android:name=".NfcTileService"
android:exported="true"
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package au.id.colby.nfcquicksettings

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import android.util.Log

private const val TAG = "NfcTilePrefsActivity"

/**
* A custom "preferences" activity for the NFC Quick Settings tile.
*
* The activity has no display, but simply starts the NFC Settings activity, then finishes. By
* default, this activity is not enabled (ie via the AndroidManifest.xml) however, if the
* `WRITE_SECURE_SETTINGS` permission has been granted, then the NfcTileService will enable this
* activity. See NfcTileService::onCreate().
*/
class NfcTilePreferencesActivity : Activity() {

/**
* Called when this activity is being created.
*
* This override simply starts the NFC Settings activity, then finishes this activity.
*/
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
Log.d(TAG, "onCreate")
Log.i(TAG, "Starting the ACTION_NFC_SETTINGS activity")
startActivity(
Intent(Settings.ACTION_NFC_SETTINGS).setFlags(
Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED
)
)
finish()
}
}
43 changes: 40 additions & 3 deletions app/src/main/java/au/id/colby/nfcquicksettings/NfcTileService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,11 @@ import android.Manifest.permission.WRITE_SECURE_SETTINGS
import android.app.PendingIntent
import android.app.PendingIntent.FLAG_IMMUTABLE
import android.content.BroadcastReceiver
import android.content.ComponentName
import android.content.Context
import android.content.Intent
import android.content.IntentFilter
import android.content.pm.PackageManager
import android.content.pm.PackageManager.PERMISSION_GRANTED
import android.nfc.NfcAdapter
import android.os.Build.VERSION.SDK_INT
Expand All @@ -21,7 +23,6 @@ import android.service.quicksettings.TileService
import android.util.Log
import androidx.core.content.ContextCompat
import au.id.colby.nfcquicksettings.R.string
import java.lang.IllegalArgumentException

private const val TAG = "NfcTileService"

Expand All @@ -34,6 +35,18 @@ private const val TAG = "NfcTileService"
class NfcTileService : TileService() {
private val nfcBroadcastReceiver = NfcBroadcastReceiver()

/**
* Called when the tile service is created.
*
* This updates the associated NFC Tile Preferences activity (ie to enable or disable that
* activity, based on whether or not the WRITE_SECURE_SETTINGS permission has been granted).
*/
override fun onCreate() {
super.onCreate()
Log.d(TAG, "onCreate")
updatePreferencesActivity()
}

/**
* Called when this tile moves into a listening state.
*
Expand Down Expand Up @@ -145,6 +158,28 @@ class NfcTileService : TileService() {
else startActivityAndCollapse(PendingIntent.getActivity(this, 0, intent, FLAG_IMMUTABLE))
}

/**
* Enables or disables the NFC tile's preferences activity based on whether or not the
* `WRITE_SECURE_SETTINGS` permission has been granted.
*
* Thus if the permission is granted, then NfcTilePreferencesActivity will be enabled to
* redirect users to the NFC Settings activity (`ACTION_NFC_SETTINGS`) on long-tapping the tile
* But if the the permissions is not granted, then NfcTilePreferencesActivity is disabled,
* and long-tapping the tile will result in the default OS behaviour (ie starting the
* Application Details activity (`ACTION_APPLICATION_DETAILS_SETTINGS`)).
*/
private fun updatePreferencesActivity() {
val newState =
if (permissionGranted(WRITE_SECURE_SETTINGS)) PackageManager.COMPONENT_ENABLED_STATE_ENABLED
else PackageManager.COMPONENT_ENABLED_STATE_DISABLED
Log.d(TAG, "Setting preferences activity enabled setting to $newState")
applicationContext.packageManager.setComponentEnabledSetting(
ComponentName(applicationContext, NfcTilePreferencesActivity::class.java),
newState,
PackageManager.DONT_KILL_APP
)
}

/**
* Updates the Quick Settings tile with the [newState] and (if supported) [newSubTitleResId].
*
Expand Down Expand Up @@ -178,8 +213,10 @@ class NfcTileService : TileService() {
* @param adapter The adapter to reflect the state of.
*/
private fun updateTile(adapter: NfcAdapter? = NfcAdapter.getDefaultAdapter(this)) {
adapter?.apply { updateTile(isEnabled) } ?:
updateTile(Tile.STATE_INACTIVE, string.tile_subtitle_unavailable)
adapter?.apply { updateTile(isEnabled) } ?: updateTile(
Tile.STATE_INACTIVE,
string.tile_subtitle_unavailable
)
}

/**
Expand Down
2 changes: 1 addition & 1 deletion app/src/main/play/release-notes/en-US/default.txt
Original file line number Diff line number Diff line change
@@ -1 +1 @@
- Added metadata for F-Droid
- Redirected long-tap to NFC Settings, when granted WRITE_SECURE_SETTINGS permission
1 change: 1 addition & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
<resources>
<string name="app_name" translatable="false">NFC Quick Settings</string>
<string name="tile_label" translatable="false">NFC</string>
<string name="tile_prefs_label" translatable="false">NFC Tile Preferences</string>
<string name="tile_subtitle_active">On</string>
<string name="tile_subtitle_inactive">Off</string>
<string name="tile_subtitle_turning_on">Turning on…</string>
Expand Down
6 changes: 3 additions & 3 deletions build.gradle
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Top-level build file where you can add configuration options common to all sub-projects/modules.
plugins {
id 'com.android.application' version '8.5.1' apply false
id 'com.android.library' version '8.5.1' apply false
id 'com.android.application' version '8.5.2' apply false
id 'com.android.library' version '8.5.2' apply false
id 'org.jetbrains.dokka' version '1.9.20' apply false
id 'org.jetbrains.kotlin.android' version '2.0.0' apply false
id 'org.jetbrains.kotlin.android' version '2.0.10' apply false
}

0 comments on commit 8280790

Please sign in to comment.