Skip to content

Commit

Permalink
Picard Addition to the app!
Browse files Browse the repository at this point in the history
  • Loading branch information
akshaaatt committed Jun 23, 2021
1 parent 95e2495 commit 82ea69c
Show file tree
Hide file tree
Showing 8 changed files with 136 additions and 6 deletions.
5 changes: 3 additions & 2 deletions app/build.gradle
Expand Up @@ -15,6 +15,7 @@ android {

versionName "2.2.9"
multiDexEnabled true
useLibrary 'org.apache.http.legacy'
testInstrumentationRunner "androidx.test.runner.AndroidJUnitRunner"
}

Expand Down Expand Up @@ -62,8 +63,8 @@ android {

dependencies {
implementation 'androidx.appcompat:appcompat:1.4.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha01'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0-alpha01'
implementation 'androidx.lifecycle:lifecycle-runtime-ktx:2.4.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-viewmodel-ktx:2.4.0-alpha02'
implementation 'androidx.lifecycle:lifecycle-extensions:2.2.0'
implementation 'androidx.core:core-ktx:1.5.0'
implementation 'androidx.activity:activity-ktx:1.2.3'
Expand Down
1 change: 1 addition & 0 deletions app/src/main/java/org/metabrainz/mobile/App.kt
Expand Up @@ -65,6 +65,7 @@ class App : Application() {
companion object {
val TAGGER_ROOT_DIRECTORY = Environment.getRootDirectory().toString() + "/Picard/"
const val WEBSITE_BASE_URL = "https://musicbrainz.org/"
const val PICARD_OPENALBUM_URL = "http://%s:%s/openalbum?id=%s"
var context: App? = null
var robotoLight: Typeface? = null
private set
Expand Down
Expand Up @@ -7,8 +7,9 @@ import org.metabrainz.mobile.presentation.features.login.LoginSharedPreferences
object UserPreferences {

const val PREFERENCE_CLEAR_SUGGESTIONS = "clear_suggestions"
const val PREFERENCE_TAGGER_DIRECTORY = "tagger_directory"
const val PREFERENCE_LISTENBRAINZ_TOKEN = "listenbrainz_user_token"
const val PREFERENCE_PICARD_PORT = "picard_port"
const val PREFERENCE_IP_ADDRESS = "ip_address"
const val PREFERENCE_LISTENING_ENABLED = "listening_enabled"
private const val PREFERENCE_LISTENING_SPOTIFY = "listening_spotify_enabled"
private const val PREFERENCE_GET_PRIVATE_COLLECTIONS = "private_collections"
Expand All @@ -26,9 +27,11 @@ object UserPreferences {
val onBoardingStatus = preferences.getBoolean(PREFERENCE_ONBOARDING, false)
val privateCollectionsPreference = preferences.getBoolean(PREFERENCE_GET_PRIVATE_COLLECTIONS, false)
val ratingsTagsPreference = preferences.getBoolean(PREFERENCE_RATINGS_TAGS, false)
val taggerDirectoryPreference = preferences.getString(PREFERENCE_TAGGER_DIRECTORY, App.TAGGER_ROOT_DIRECTORY)
val systemLanguagePreference = preferences.getBoolean(PREFERENCE_SYSTEM_LANGUAGE, false)

val preferencePicardPort = preferences.getString(PREFERENCE_PICARD_PORT, "8000")
val preferenceIpAddress = preferences.getString(PREFERENCE_IP_ADDRESS, null)

val preferenceListenBrainzToken = preferences.getString(PREFERENCE_LISTENBRAINZ_TOKEN, null)
var preferenceListeningEnabled: Boolean
get() = preferences.getBoolean(PREFERENCE_LISTENING_ENABLED, false)
Expand Down
Expand Up @@ -17,6 +17,7 @@ import org.metabrainz.mobile.presentation.features.suggestion.SuggestionProvider

class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceClickListener {
private var preferenceChangeListener: Preference.OnPreferenceChangeListener? = null

fun setPreferenceChangeListener(preferenceChangeListener: Preference.OnPreferenceChangeListener?) {
this.preferenceChangeListener = preferenceChangeListener
}
Expand Down Expand Up @@ -47,8 +48,7 @@ class SettingsFragment : PreferenceFragmentCompat(), Preference.OnPreferenceClic
}

private fun clearSuggestionHistory() {
val suggestions = SearchRecentSuggestions(activity, SuggestionProvider.AUTHORITY,
SuggestionProvider.MODE)
val suggestions = SearchRecentSuggestions(activity, SuggestionProvider.AUTHORITY, SuggestionProvider.MODE)
suggestions.clearHistory()
Toast.makeText(activity, R.string.toast_search_cleared, Toast.LENGTH_SHORT).show()
}
Expand Down
@@ -1,7 +1,11 @@
package org.metabrainz.mobile.presentation.features.tagger

import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.os.StrictMode
import android.os.StrictMode.ThreadPolicy
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
Expand All @@ -10,13 +14,22 @@ import android.widget.Toast.LENGTH_SHORT
import androidx.fragment.app.Fragment
import androidx.fragment.app.activityViewModels
import com.bumptech.glide.Glide
import org.metabrainz.mobile.App.Companion.PICARD_OPENALBUM_URL
import org.metabrainz.mobile.data.sources.Constants
import org.metabrainz.mobile.databinding.FragmentTaggerBinding
import org.metabrainz.mobile.presentation.UserPreferences
import org.metabrainz.mobile.presentation.features.recording.RecordingActivity
import org.metabrainz.mobile.util.Log.d
import org.metabrainz.mobile.util.Resource
import java.io.IOException
import java.io.UnsupportedEncodingException
import java.net.HttpURLConnection
import java.net.MalformedURLException
import java.net.URL
import java.net.URLEncoder
import java.util.concurrent.TimeUnit


class TaggerFragment : Fragment() {

private lateinit var binding: FragmentTaggerBinding
Expand Down Expand Up @@ -73,8 +86,51 @@ class TaggerFragment : Fragment() {
context?.startActivity(intent)
}

binding.picard.setOnClickListener {
val ipAddress = UserPreferences.preferenceIpAddress
if(ipAddress==null){
Toast.makeText(context,"Add your IP Address in the settings, matched according to your Picard network", Toast.LENGTH_LONG).show()
return@setOnClickListener
}
//To allow http requests specially
val policy = ThreadPolicy.Builder().permitAll().build()
StrictMode.setThreadPolicy(policy)

val url = String.format(
PICARD_OPENALBUM_URL, ipAddress,
UserPreferences.preferencePicardPort, uriEncode(releaseMBID!!)
)
var connection: HttpURLConnection? = null
try {
val u = URL(url)
connection = u.openConnection() as HttpURLConnection?
connection!!.requestMethod = "GET"
val code = connection.responseCode
if(code==200){
(context as Activity).runOnUiThread {
Toast.makeText(context, "Release sent to your Picard!", LENGTH_SHORT).show()
}
}
} catch (e: MalformedURLException) {
e.printStackTrace()
} catch (e: IOException) {
e.printStackTrace()
} finally {
connection?.disconnect()
}
}

return binding.root
}
private fun uriEncode(releaseId: String): String {
return try {
URLEncoder.encode(releaseId, "UTF-8")
}
catch (e: UnsupportedEncodingException) {
Log.e(this.javaClass.name, e.message, e)
URLEncoder.encode(releaseId)
}
}

private fun setTaglibFetchedMetadata(metadata: AudioFile?) {
binding.loadingAnimation.root.visibility = View.VISIBLE
Expand All @@ -90,6 +146,7 @@ class TaggerFragment : Fragment() {
binding.albumArtLocal.visibility = View.GONE
binding.albumArtServer.visibility = View.GONE
binding.recordingButton.visibility = View.GONE
binding.picard.visibility = View.GONE

reset()

Expand Down Expand Up @@ -129,6 +186,7 @@ class TaggerFragment : Fragment() {
binding.albumArtLocal.visibility = View.VISIBLE
binding.albumArtServer.visibility = View.VISIBLE
binding.recordingButton.visibility = View.VISIBLE
binding.picard.visibility = View.VISIBLE

for (tags in tagsList) {
if (tags.newValue.isEmpty()) {
Expand Down

0 comments on commit 82ea69c

Please sign in to comment.