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

Apply theme to licenses page #201

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from
Open
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
15 changes: 15 additions & 0 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ apply plugin: 'com.google.android.gms.oss-licenses-plugin'
apply plugin: 'com.google.gms.google-services'
apply plugin: 'com.google.firebase.crashlytics'
apply plugin: 'com.facebook.testing.screenshot'
apply plugin: 'com.jaredsburrows.license'


allOpen {
annotation 'journal.gratitude.com.gratitudejournal.util.OpenClass'
Expand Down Expand Up @@ -66,6 +68,16 @@ android {
}

kotlinOptions { jvmTarget = "1.8" }
licenseReport {
generateCsvReport = false
generateHtmlReport = true
generateJsonReport = true

// These options are ignored for Java projects
copyHtmlReportToAssets = false
copyHtmlReportToAssets = true
copyJsonReportToAssets = false
}
}

dependencies {
Expand Down Expand Up @@ -151,6 +163,9 @@ dependencies {
androidTestImplementation 'androidx.test.uiautomator:uiautomator:2.2.0'
androidTestImplementation 'org.jetbrains.kotlinx:kotlinx-coroutines-test:1.3.2'
androidTestImplementation "androidx.work:work-testing:2.4.0"
// androidTestImplementation "androidx.test:core-ktx:'1.2.0"



kaptAndroidTest "com.google.dagger:dagger-compiler:$dagger_version"
kaptTest "com.google.dagger:dagger-compiler:$dagger_version"
Expand Down
1 change: 1 addition & 0 deletions app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
<category android:name="android.intent.category.DEFAULT" />
</intent-filter>
</activity>
<activity android:name=".ui.licenses.OssLicensesActivity"></activity>

<receiver android:name=".util.reminders.ReminderReceiver" />

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,17 @@ import android.app.Activity
import android.app.NotificationChannel
import android.app.NotificationManager
import android.content.Context
import android.graphics.Color
import android.os.Build
import android.os.Bundle
import android.view.View
import androidx.appcompat.app.AppCompatActivity
import androidx.core.view.WindowCompat
import androidx.navigation.findNavController
import androidx.preference.PreferenceManager
import com.google.android.gms.common.ConnectionResult
import com.google.android.gms.common.GoogleApiAvailability
import com.google.android.gms.oss.licenses.OssLicensesMenuActivity
import com.google.android.play.core.splitcompat.SplitCompat
import com.google.firebase.analytics.FirebaseAnalytics
import journal.gratitude.com.gratitudejournal.model.CAME_FROM_NOTIFICATION
Expand Down Expand Up @@ -76,7 +79,6 @@ class ContainerActivity : AppCompatActivity() {

override fun onStart() {
super.onStart()

val sharedPref = PreferenceManager.getDefaultSharedPreferences(this)
val fingerprintLock = sharedPref.getBoolean(FINGERPRINT, false)
if (fingerprintLock) {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
package journal.gratitude.com.gratitudejournal.ui.licenses

import android.app.Activity
import android.os.Bundle
import android.util.Log
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.TextView
import androidx.fragment.app.Fragment
import androidx.lifecycle.Observer
import journal.gratitude.com.gratitudejournal.R
import java.lang.Exception
import java.lang.IndexOutOfBoundsException
import java.util.*

class OssLicenseFragment(val license: OssLicensesActivity.License): Fragment() {
private val TAG = "OssLicenseFragment"
private lateinit var viewModel: OssLicensesViewModel

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = OssLicensesViewModel(activity?.application!!)

}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val root = inflater.inflate(R.layout.fragment_license, container, false)
val content = root.findViewById<TextView>(R.id.content)

viewModel.loadLicense(license)
viewModel.licenseLD.observe(viewLifecycleOwner, object : Observer<OssLicensesActivity.License?> {
override fun onChanged(t: OssLicensesActivity.License?) {
content.text = t?.licenseContent
}
})

return root
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
package journal.gratitude.com.gratitudejournal.ui.licenses

import android.content.Context
import android.graphics.Color
import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import android.widget.AdapterView
import android.widget.ArrayAdapter
import android.widget.ListView
import android.widget.TextView
import androidx.core.view.get
import androidx.fragment.app.Fragment
import androidx.fragment.app.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import androidx.recyclerview.widget.RecyclerView
import journal.gratitude.com.gratitudejournal.R
import java.lang.Exception
import java.lang.IndexOutOfBoundsException
import java.util.*

class OssLicenseMenuFragment: Fragment() {
private lateinit var viewModel: OssLicensesViewModel
private val TAG = "OssLicensesMenuActivity";
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
viewModel = OssLicensesViewModel(activity?.application!!)
}

override fun onCreateView(inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?): View? {
val view = inflater.inflate(R.layout.fragment_license_menu, container, false)
val list = view.findViewById<ListView>(R.id.listView)
viewModel.loadMenu()
viewModel.menuLD.observe(viewLifecycleOwner, androidx.lifecycle.Observer {
val adapter = LicenseMenuAdapter(requireContext(), android.R.layout.simple_list_item_1, it.toMutableList())

list.setOnItemClickListener(AdapterView.OnItemClickListener(function ={ parent, view, position, id ->
run {
val license = adapter.getItem(position)
if(license != null)
activity?.supportFragmentManager?.beginTransaction()?.addToBackStack(null)?.replace(R.id.fragment, OssLicenseFragment(license))?.commit()
}
}))
list.adapter = adapter
})

return view
}

private class LicenseMenuAdapter(context: Context, resource: Int, licenses: List<OssLicensesActivity.License>) :
ArrayAdapter<OssLicensesActivity.License>(context, resource, licenses)
{
val licenses = licenses
override fun getView(position: Int, convertView: View?, parent: ViewGroup): View {
val textView = super.getView(position, convertView, parent) as TextView
textView?.text = licenses[position].libName
return textView
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package journal.gratitude.com.gratitudejournal.ui.licenses

import android.os.Bundle
import android.util.Log
import androidx.appcompat.app.AppCompatActivity
import androidx.preference.PreferenceManager
import journal.gratitude.com.gratitudejournal.R
import journal.gratitude.com.gratitudejournal.ui.settings.SettingsFragment
import java.lang.Exception
import java.lang.IndexOutOfBoundsException
import java.util.*

class OssLicensesActivity: AppCompatActivity() {
data class License(val start: Int, val length: Int, val libName: String, var licenseContent: String = "") {}
private val TAG = "OssLicensesMenuActivity";
override fun onCreate(savedInstanceState: Bundle?) {
val sharedPref = PreferenceManager.getDefaultSharedPreferences(this)
val currentTheme = sharedPref.getString(SettingsFragment.THEME_PREF, "original") ?: "original"
setAppTheme(currentTheme)
super.onCreate(savedInstanceState)
setContentView(R.layout.activity_licenses)
supportFragmentManager.beginTransaction().replace(R.id.fragment, OssLicenseMenuFragment()).commit()
}

private fun setAppTheme(currentTheme: String) {
when (currentTheme) {
"Sunset" -> setTheme(R.style.AppTheme_SUNSET)
"Moonlight" -> setTheme(R.style.AppTheme_MOONLIGHT)
"Midnight" -> setTheme(R.style.AppTheme_MIDNIGHT)
"Ivy" -> setTheme(R.style.AppTheme_IVY)
"Dawn" -> setTheme(R.style.AppTheme_DAWN)
"Wesley" -> setTheme(R.style.AppTheme_WESLEY)
"Moss" -> setTheme(R.style.AppTheme_MOSS)
"Clean" -> setTheme(R.style.AppTheme_CLEAN)
"Glacier" -> setTheme(R.style.AppTheme_GLACIER)
"Gelato" -> setTheme(R.style.AppTheme_GELATO)
"Waves" -> setTheme(R.style.AppTheme_WAVES)
"Beach" -> setTheme(R.style.AppTheme_BEACH)
"Field" -> setTheme(R.style.AppTheme_FIELD)
"Western" -> setTheme(R.style.AppTheme_WESTERN)
"Sunlight" -> setTheme(R.style.AppTheme_SUNLIGHT)
"Tulip" -> setTheme(R.style.AppTheme_TULIP)
"Rosie" -> setTheme(R.style.AppTheme_ROSIE)
"Daisy" -> setTheme(R.style.AppTheme_DAISY)
"Matisse" -> setTheme(R.style.AppTheme_MATISSE)
"Clouds" -> setTheme(R.style.AppTheme_CLOUDS)
"Monstera" -> setTheme(R.style.AppTheme_MONSTERA)
"Lotus" -> setTheme(R.style.AppTheme_LOTUS)
"Katie" -> setTheme(R.style.AppTheme_KATIE)
"Brittany" -> setTheme(R.style.AppTheme_BRITTANY)
"Jungle" -> setTheme(R.style.AppTheme_JUNGLE)
"Julie" -> setTheme(R.style.AppTheme_JULIE)
"Ellen" -> setTheme(R.style.AppTheme_ELLEN)
"Danah" -> setTheme(R.style.AppTheme_DANAH)
"Ahalya" -> setTheme(R.style.AppTheme_AHALYA)
else -> setTheme(R.style.Base_AppTheme)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
package journal.gratitude.com.gratitudejournal.ui.licenses

import android.app.Application
import androidx.lifecycle.AndroidViewModel
import androidx.lifecycle.LiveData
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import journal.gratitude.com.gratitudejournal.R
import kotlinx.coroutines.launch
import java.io.InputStreamReader
import java.util.*

class OssLicensesViewModel(application: Application): AndroidViewModel(application) {
private val menuMLD: MutableLiveData<List<OssLicensesActivity.License>> = MutableLiveData()
val menuLD: LiveData<List<OssLicensesActivity.License>> = menuMLD

private val licenseMLD: MutableLiveData<OssLicensesActivity.License> = MutableLiveData()
val licenseLD = licenseMLD

private lateinit var licensesInputStream: InputStreamReader

fun loadMenu() {
val inStream = getApplication<Application>().resources.openRawResource(R.raw.third_party_license_metadata)
val scanner = Scanner(inStream)
viewModelScope.launch {
val libs = mutableListOf<OssLicensesActivity.License>()
while (scanner.hasNextLine()) {
val line = scanner.nextLine()
var firstDelimIndex = line.indexOf(":")
val startString = line.subSequence(0, firstDelimIndex).toString()
var secondDelimIndex = line.indexOf(" ")
val lengthString = line.subSequence(firstDelimIndex + 1, secondDelimIndex).toString()
val library = line.subSequence(secondDelimIndex, line.length).toString()
libs.add(OssLicensesActivity.License(Integer.parseInt(startString), Integer.parseInt(lengthString), libName = library))
}
libs.sortBy {
it.libName.toLowerCase()
}
menuMLD.postValue(libs)
}
}

fun loadLicense(license: OssLicensesActivity.License) {
var charArray :CharArray = CharArray(license.length)

licensesInputStream = InputStreamReader(getApplication<Application>().resources.openRawResource(R.raw.third_party_licenses))
licensesInputStream.skip((license.start - 1).toLong())
licensesInputStream.read(charArray, 0, license.length)
license.licenseContent = charArray.concatToString()
licenseMLD.postValue(license)
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,7 @@ import dagger.android.support.AndroidSupportInjection
import journal.gratitude.com.gratitudejournal.BuildConfig
import journal.gratitude.com.gratitudejournal.R
import journal.gratitude.com.gratitudejournal.model.*
import journal.gratitude.com.gratitudejournal.ui.licenses.OssLicensesActivity
import journal.gratitude.com.gratitudejournal.ui.timeline.TimelineFragment
import journal.gratitude.com.gratitudejournal.util.backups.LocalExporter.convertCsvToEntries
import journal.gratitude.com.gratitudejournal.util.backups.LocalExporter.exportEntriesToCsvFile
Expand Down Expand Up @@ -141,7 +142,7 @@ class SettingsFragment : PreferenceFragmentCompat(),
}
val oss = findPreference<Preference>(getString(R.string.key_open_source))
oss?.setOnPreferenceClickListener {
startActivity(Intent(context, OssLicensesMenuActivity::class.java))
startActivity(Intent(context, OssLicensesActivity::class.java))
true
}
val version = findPreference<Preference>(VERSION_PREF)
Expand Down Expand Up @@ -631,3 +632,10 @@ interface ExportCallback {

fun onFailure(exception: Exception)
}


fun OssLicensesMenuActivity.setBackgroundColor(color: Int) {
val content = this.findViewById<View>(R.id.content)
content.setBackgroundColor(Color.CYAN)

}
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
package journal.gratitude.com.gratitudejournal.ui.settings

import androidx.lifecycle.ViewModel
import androidx.lifecycle.viewModelScope
import journal.gratitude.com.gratitudejournal.model.Entry
import journal.gratitude.com.gratitudejournal.repository.EntryRepository
import kotlinx.coroutines.*
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.withContext
import javax.inject.Inject
import kotlin.coroutines.CoroutineContext

class SettingsViewModel @Inject constructor(private val repository: EntryRepository) : ViewModel() {

private var parentJob = Job()
private val coroutineContext: CoroutineContext
get() = parentJob + Dispatchers.Main
private val scope = CoroutineScope(coroutineContext)

suspend fun getEntries(): List<Entry> = withContext(Dispatchers.IO) {
repository.getEntries()
}

fun addEntries(entries: List<Entry>) = scope.launch(Dispatchers.IO) {
fun addEntries(entries: List<Entry>) = viewModelScope.launch(Dispatchers.IO) {
repository.addEntries(entries)
}

Expand Down
Loading