Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixed crashing when selecting folders #3

Merged
merged 1 commit into from
Jul 21, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -10,8 +10,8 @@ android {
applicationId "com.sc.gtradio"
minSdkVersion 24
targetSdkVersion 30
versionCode 1
versionName "1.0"
versionCode 3
versionName "1.0.1"

testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}
Expand Down
2 changes: 1 addition & 1 deletion app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@

# Uncomment this to preserve the line number information for
# debugging stack traces.
#-keepattributes SourceFile,LineNumberTable
-keepattributes SourceFile,LineNumberTable

# If you keep the line number information, uncomment this to
# hide the original source file name.
Expand Down
30 changes: 28 additions & 2 deletions app/src/main/java/com/sc/gtradio/GroupList.kt
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,19 @@ class GroupList : Fragment() {

private val subscriptionCallback = object : MediaBrowserCompat.SubscriptionCallback() {
override fun onChildrenLoaded(parentId: String, children: List<MediaBrowserCompat.MediaItem>) {
if (_binding != null) {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(requireActivity().applicationContext)
val radioUri = sharedPref?.getString(getString(R.string.radio_folders_uri_key), "")

if (children.isEmpty() && activity?.contentResolver?.persistedUriPermissions?.any { x -> x.uri == Uri.parse(radioUri) && x.isReadPermission } == true) {
//No stations listed and we have already selected a library, so show our nothing-found message
binding.textviewNogroups.visibility = View.VISIBLE
binding.sadFaceIcon.visibility = View.VISIBLE
} else {
binding.textviewNogroups.visibility = View.INVISIBLE
binding.sadFaceIcon.visibility = View.INVISIBLE
}
}
listAdapter.submitList(children)
}
}
Expand All @@ -53,7 +66,10 @@ class GroupList : Fragment() {
}

private val permissionResultLauncher = registerForActivityResult(contract)
{ result: Uri ->
{ result: Uri? ->
if (result == null) {
return@registerForActivityResult
}
activity?.contentResolver?.takePersistableUriPermission(result, Intent.FLAG_GRANT_READ_URI_PERMISSION)
val sharedPref = PreferenceManager.getDefaultSharedPreferences(activity?.applicationContext)
with (sharedPref?.edit()) {
Expand Down Expand Up @@ -118,9 +134,17 @@ class GroupList : Fragment() {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.requireActivity().applicationContext)
val radioUri = sharedPref?.getString(getString(R.string.radio_folders_uri_key), "")
if (radioUri.isNullOrEmpty() || activity?.contentResolver?.persistedUriPermissions?.any { x -> x.uri == Uri.parse(radioUri) && x.isReadPermission } != true) {
//User has no library selected, allow them to select something
//User has no library selected, allow them to select something and possibly reset our pref value
if (!radioUri.isNullOrEmpty()) {
with (sharedPref?.edit()) {
this?.putString(getString(R.string.radio_folders_uri_key), "")
this?.apply()
}
}
binding.buttonSelect.visibility = View.VISIBLE
binding.textviewNofolder.visibility = View.VISIBLE
binding.textviewNogroups.visibility = View.INVISIBLE
binding.sadFaceIcon.visibility = View.INVISIBLE
} else {
binding.buttonSelect.visibility = View.INVISIBLE
binding.textviewNofolder.visibility = View.INVISIBLE
Expand All @@ -129,6 +153,8 @@ class GroupList : Fragment() {

override fun onDestroyView() {
super.onDestroyView()
val sharedPref = PreferenceManager.getDefaultSharedPreferences(this.requireActivity().applicationContext)
sharedPref.unregisterOnSharedPreferenceChangeListener(sharedPrefListener)
_binding = null
}
}
Expand Down
35 changes: 35 additions & 0 deletions app/src/main/java/com/sc/gtradio/SettingsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,8 +1,14 @@
package com.sc.gtradio

import android.content.Context
import android.content.Intent
import android.net.Uri
import android.os.Bundle
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import androidx.preference.PreferenceManager

class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
Expand All @@ -18,9 +24,38 @@ class SettingsActivity : AppCompatActivity() {
}

class SettingsFragment : PreferenceFragmentCompat() {
private val contract = object : ActivityResultContracts.OpenDocumentTree() {
override fun createIntent(context: Context, input: Uri?): Intent {
val intent = super.createIntent(context, input)
intent.addFlags(Intent.FLAG_GRANT_PERSISTABLE_URI_PERMISSION)
intent.addFlags(Intent.FLAG_GRANT_READ_URI_PERMISSION)
intent.addFlags(Intent.FLAG_GRANT_WRITE_URI_PERMISSION)
return intent
}
}

private val permissionResultLauncher = registerForActivityResult(contract)
{ result: Uri? ->
if (result == null) {
return@registerForActivityResult
}
activity?.contentResolver?.takePersistableUriPermission(result, Intent.FLAG_GRANT_READ_URI_PERMISSION)
val sharedPref = PreferenceManager.getDefaultSharedPreferences(activity?.applicationContext)
with (sharedPref?.edit()) {
this?.putString(getString(R.string.radio_folders_uri_key), result.toString())
this?.apply()
}

}

override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.root_preferences, rootKey)

val pathPref = findPreference<Preference>(getString(R.string.radio_folders_uri_key))
pathPref?.setOnPreferenceClickListener {
permissionResultLauncher.launch(null)
true
}
}
}
}
16 changes: 16 additions & 0 deletions app/src/main/res/drawable/ic_sad_face.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="24dp"
android:height="24dp"
android:viewportWidth="24"
android:viewportHeight="24"
android:tint="?attr/colorControlNormal">
<path
android:fillColor="@android:color/white"
android:pathData="M15.5,9.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"/>
<path
android:fillColor="@android:color/white"
android:pathData="M8.5,9.5m-1.5,0a1.5,1.5 0,1 1,3 0a1.5,1.5 0,1 1,-3 0"/>
<path
android:fillColor="@android:color/white"
android:pathData="M12,14c-2.33,0 -4.32,1.45 -5.12,3.5h1.67c0.69,-1.19 1.97,-2 3.45,-2s2.75,0.81 3.45,2h1.67c-0.8,-2.05 -2.79,-3.5 -5.12,-3.5zM11.99,2C6.47,2 2,6.48 2,12s4.47,10 9.99,10C17.52,22 22,17.52 22,12S17.52,2 11.99,2zM12,20c-4.42,0 -8,-3.58 -8,-8s3.58,-8 8,-8 8,3.58 8,8 -3.58,8 -8,8z"/>
</vector>
32 changes: 30 additions & 2 deletions app/src/main/res/layout/fragment_grouplist.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/textview_nofolder"
tools:visibility="invisible" />
android:visibility="invisible" />

<TextView
android:id="@+id/textview_nofolder"
Expand All @@ -28,7 +28,35 @@
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"
tools:visibility="invisible" />
android:visibility="invisible" />

<androidx.constraintlayout.widget.ConstraintLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:padding="32dp">

<ImageView
android:id="@+id/sadFaceIcon"
android:layout_width="64dp"
android:layout_height="64dp"
android:contentDescription="@string/sad_face"
android:src="@drawable/ic_sad_face"
android:visibility="invisible"
app:layout_constraintBottom_toTopOf="@id/textview_nogroups"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"/>

<TextView
android:id="@+id/textview_nogroups"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="@string/no_groups_available"
android:visibility="invisible"
app:layout_constraintBottom_toBottomOf="parent"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
</androidx.constraintlayout.widget.ConstraintLayout>

<androidx.recyclerview.widget.RecyclerView
android:id="@+id/station_list"
Expand Down
2 changes: 2 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -34,5 +34,7 @@
<string name="news_reports_summary">Note: This setting only affects gen 3 stations.</string>

<string name="nothing_playing">Nothing Playing</string>
<string name="no_groups_available">No groups available. Double check the selected folder and your folder structure.</string>
<string name="sad_face">Sad face</string>

</resources>
Original file line number Diff line number Diff line change
Expand Up @@ -331,7 +331,11 @@ open class GTRadioMusicService : MediaBrowserServiceCompat() {
val list = ArrayList<StationGroup>()
val subDirs = (dir?.listFiles() ?: emptyArray()).filter { x -> x.isDirectory }
for (subDir in subDirs) {
list.add(StationGroup(subDir, applicationContext))
val group = StationGroup(subDir, applicationContext)
if (group.generation in 1..3) {
//Valid group.
list.add(group)
}
}
list.sortBy { x -> x.groupName }
return list
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,8 @@ class StationGroup(val folderDoc: DocumentFile, private val context: Context) {

var mediaItem: MediaBrowserCompat.MediaItem
var stationList: ArrayList<MediaBrowserCompat.MediaItem>
var generation: Int = 0
lateinit var groupName: String
var generation: Int = -1
var groupName: String = ""

init {
setupDataFromJson(folderDoc)
Expand Down Expand Up @@ -71,7 +71,7 @@ class StationGroup(val folderDoc: DocumentFile, private val context: Context) {
jsonObj.getInt("generation")
} catch (e: Exception) {
//No valid generation
0
-1
}
groupName = try {
jsonObj.getString("name")
Expand Down