Skip to content

Commit

Permalink
Added web search from details activity
Browse files Browse the repository at this point in the history
  • Loading branch information
izderadicka committed Jun 2, 2018
1 parent a59785d commit 3b30a76
Show file tree
Hide file tree
Showing 7 changed files with 89 additions and 4 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,8 @@ android {
applicationId "eu.zderadicka.audioserve"
minSdkVersion 21
targetSdkVersion 27
versionCode 14
versionName "0.6.7"
versionCode 15
versionName "0.6.8"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
Expand Down
48 changes: 46 additions & 2 deletions app/src/main/java/eu/zderadicka/audioserve/DetailsActivity.kt
Original file line number Diff line number Diff line change
@@ -1,28 +1,72 @@
package eu.zderadicka.audioserve

import android.content.Intent
import android.net.Uri
import android.support.v7.app.AppCompatActivity
import android.os.Bundle
import android.preference.PreferenceManager
import android.view.Menu
import android.view.MenuItem
import eu.zderadicka.audioserve.data.pathFromFolderId
import eu.zderadicka.audioserve.fragments.ARG_FOLDER_DETAILS
import eu.zderadicka.audioserve.fragments.ARG_FOLDER_NAME
import eu.zderadicka.audioserve.fragments.ARG_FOLDER_PATH
import eu.zderadicka.audioserve.fragments.DetailsFragment

private val searchUri = Uri.parse("https://www.google.com/search")
private val toReplace = Regex("""[_\-/,]""")
private val bracketed = Regex("""[(\[{].*[)\]}]""")



class DetailsActivity : AppCompatActivity() {

lateinit var folderName: String
lateinit var folderPath:String

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_details)
supportActionBar?.setDisplayHomeAsUpEnabled(true)

val folderName = intent.getStringExtra(ARG_FOLDER_NAME)?:""
folderName = intent.getStringExtra(ARG_FOLDER_NAME)?:""
val folderId = intent.getStringExtra(ARG_FOLDER_PATH)!!
val folderPath = pathFromFolderId(folderId)
folderPath = pathFromFolderId(folderId)
val details = intent.getBundleExtra(ARG_FOLDER_DETAILS)
val t = supportFragmentManager.beginTransaction()
t.add(R.id.detailsFragment, DetailsFragment.newInstance(folderPath,folderName,details))
t.commit()


}

override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.info_menu, menu)
return true
}

private val searchString:String
get() {
val searchPrefix = PreferenceManager.getDefaultSharedPreferences(this)
.getString("pref_web_search_prefix", "")
var query = searchPrefix+ " "+folderName + " " + folderPath
query = toReplace.replace(query, " ")
query = bracketed.replace(query, "")
query = Regex("""\s{2,}""").replace(query, " ")
query = query.trim()
return query
}

override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
R.id.action_web_search -> {
val uri = searchUri.buildUpon().appendQueryParameter("q", searchString).build()
val intent = Intent(Intent.ACTION_VIEW)
intent.data = uri
startActivity(intent)
}
}

return super.onOptionsItemSelected(item)
}
}
10 changes: 10 additions & 0 deletions app/src/main/java/eu/zderadicka/audioserve/SettingsActivity.kt
Original file line number Diff line number Diff line change
Expand Up @@ -225,6 +225,16 @@ class SettingsFragment: PreferenceFragment(), SharedPreferences.OnSharedPreferen

}

"pref_web_search_prefix" -> {
if (pref !is EditTextPreference) return
val prefix = sps.getString("pref_web_search_prefix", null)
if (prefix.isNullOrBlank()) {
pref.summary = getString(R.string.pref_web_search_prefix_summary_empty)
} else {
pref.summary = getString(R.string.pref_web_search_prefix_summary, prefix)
}
}


}
}
Expand Down
5 changes: 5 additions & 0 deletions app/src/main/res/drawable/ic_web.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
<vector android:height="24dp" android:tint="#FFFFFF"
android:viewportHeight="24.0" android:viewportWidth="24.0"
android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M11.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,2zM18.92,8h-2.95c-0.32,-1.25 -0.78,-2.45 -1.38,-3.56 1.84,0.63 3.37,1.91 4.33,3.56zM12,4.04c0.83,1.2 1.48,2.53 1.91,3.96h-3.82c0.43,-1.43 1.08,-2.76 1.91,-3.96zM4.26,14C4.1,13.36 4,12.69 4,12s0.1,-1.36 0.26,-2h3.38c-0.08,0.66 -0.14,1.32 -0.14,2 0,0.68 0.06,1.34 0.14,2L4.26,14zM5.08,16h2.95c0.32,1.25 0.78,2.45 1.38,3.56 -1.84,-0.63 -3.37,-1.9 -4.33,-3.56zM8.03,8L5.08,8c0.96,-1.66 2.49,-2.93 4.33,-3.56C8.81,5.55 8.35,6.75 8.03,8zM12,19.96c-0.83,-1.2 -1.48,-2.53 -1.91,-3.96h3.82c-0.43,1.43 -1.08,2.76 -1.91,3.96zM14.34,14L9.66,14c-0.09,-0.66 -0.16,-1.32 -0.16,-2 0,-0.68 0.07,-1.35 0.16,-2h4.68c0.09,0.65 0.16,1.32 0.16,2 0,0.68 -0.07,1.34 -0.16,2zM14.59,19.56c0.6,-1.11 1.06,-2.31 1.38,-3.56h2.95c-0.96,1.65 -2.49,2.93 -4.33,3.56zM16.36,14c0.08,-0.66 0.14,-1.32 0.14,-2 0,-0.68 -0.06,-1.34 -0.14,-2h3.38c0.16,0.64 0.26,1.31 0.26,2s-0.1,1.36 -0.26,2h-3.38z"/>
</vector>
11 changes: 11 additions & 0 deletions app/src/main/res/menu/info_menu.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto">
<item android:id="@+id/action_web_search"
android:title="Web search"
android:icon="@drawable/ic_web"
app:showAsAction="always"
/>


</menu>
3 changes: 3 additions & 0 deletions app/src/main/res/values/strings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,7 @@
<string name="pref_autorewind_selected_title">Before continuing playback it\'\'ll be rewind by a small amount (2s-30s), depending how long it was paused</string>
<string name="label_rewind">Rewind</string>
<string name="label_forward">Forward</string>
<string name="pref_web_search_prefix_title">Web search prefix</string>
<string name="pref_web_search_prefix_summary_empty">No prefix. You can set one to limit web search for folder details</string>
<string name="pref_web_search_prefix_summary">Prefix %1$s is used to search web for folder details</string>
</resources>
12 changes: 12 additions & 0 deletions app/src/main/res/xml/settings.xml
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,18 @@

</PreferenceCategory>

<PreferenceCategory
android:key="prefs_other"
android:title="OTHER"
>
<EditTextPreference
android:key="pref_web_search_prefix"
android:title="@string/pref_web_search_prefix_title"
android:selectAllOnFocus="true"
android:singleLine="true"
/>
</PreferenceCategory>



</PreferenceScreen>

0 comments on commit 3b30a76

Please sign in to comment.