diff options
| author | Tyler Nijmeh <tylernij@gmail.com> | 2021-03-31 00:57:45 -0700 |
|---|---|---|
| committer | Tyler Nijmeh <tylernij@gmail.com> | 2021-03-31 00:57:45 -0700 |
| commit | b8efe157978c2e648e2d1ba1408122bad0365a30 (patch) | |
| tree | 90832e540ae974236b8b5a4ca58d9c670327c931 /app/src/main/java | |
| parent | 9ce17e0dc3e17c3a640363247667409e67ca68ae (diff) | |
Migrate to preference screen
Signed-off-by: Tyler Nijmeh <tylernij@gmail.com>
Diffstat (limited to 'app/src/main/java')
3 files changed, 81 insertions, 96 deletions
diff --git a/app/src/main/java/com/draco/buoy/fragments/MainPreferenceFragment.kt b/app/src/main/java/com/draco/buoy/fragments/MainPreferenceFragment.kt new file mode 100644 index 0000000..e0be6d3 --- /dev/null +++ b/app/src/main/java/com/draco/buoy/fragments/MainPreferenceFragment.kt @@ -0,0 +1,71 @@ +package com.draco.buoy.fragments + +import android.content.Context +import android.content.Intent +import android.net.Uri +import android.os.Bundle +import androidx.preference.Preference +import androidx.preference.PreferenceFragmentCompat +import com.draco.buoy.R +import com.draco.buoy.models.BatterySaverConstantsConfig +import com.draco.buoy.repositories.BatterySaverConstantsConfigProfiles +import com.draco.buoy.utils.BatterySaverManager +import com.google.android.gms.oss.licenses.OssLicensesMenuActivity +import com.google.android.material.snackbar.Snackbar + +class MainPreferenceFragment : PreferenceFragmentCompat() { + private lateinit var batterySaverManager: BatterySaverManager + + override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) { + setPreferencesFromResource(R.xml.main, rootKey) + } + + override fun onAttach(context: Context) { + super.onAttach(context) + batterySaverManager = BatterySaverManager(context.contentResolver) + } + + override fun onPreferenceTreeClick(preference: Preference): Boolean { + when (preference.key) { + getString(R.string.profile_key_reset) -> resetProfile() + getString(R.string.profile_key_light) -> applyProfile(BatterySaverConstantsConfigProfiles.LIGHT) + getString(R.string.profile_key_moderate) -> applyProfile(BatterySaverConstantsConfigProfiles.MODERATE) + getString(R.string.profile_key_high) -> applyProfile(BatterySaverConstantsConfigProfiles.HIGH) + getString(R.string.profile_key_extreme) -> applyProfile(BatterySaverConstantsConfigProfiles.EXTREME) + + getString(R.string.pref_developer_key) -> openURL(getString(R.string.developer_url)) + getString(R.string.pref_source_key) -> openURL(getString(R.string.source_url)) + getString(R.string.pref_contact_key) -> openURL(getString(R.string.contact_url)) + getString(R.string.pref_licenses_key) -> { + val intent = Intent(requireContext(), OssLicensesMenuActivity::class.java) + startActivity(intent) + } + else -> return super.onPreferenceTreeClick(preference) + } + return true + } + + private fun applyProfile(profile: BatterySaverConstantsConfig) { + batterySaverManager.setConstantsConfig(profile) + batterySaverManager.setLowPower(true) + batterySaverManager.setLowPowerSticky(true) + batterySaverManager.setLowPowerStickyAutoDisableEnabled(false) + } + + private fun resetProfile() { + batterySaverManager.resetConstants() + batterySaverManager.setLowPower(false) + batterySaverManager.setLowPowerSticky(false) + batterySaverManager.setLowPowerStickyAutoDisableEnabled(true) + } + + private fun openURL(url: String) { + val intent = Intent(Intent.ACTION_VIEW, Uri.parse(url)) + try { + startActivity(intent) + } catch (e: Exception) { + e.printStackTrace() + Snackbar.make(requireView(), getString(R.string.snackbar_intent_failed), Snackbar.LENGTH_SHORT).show() + } + } +}
\ No newline at end of file diff --git a/app/src/main/java/com/draco/buoy/recyclers/BatterySaverProfileRecyclerAdapter.kt b/app/src/main/java/com/draco/buoy/recyclers/BatterySaverProfileRecyclerAdapter.kt deleted file mode 100644 index fe22e4c..0000000 --- a/app/src/main/java/com/draco/buoy/recyclers/BatterySaverProfileRecyclerAdapter.kt +++ /dev/null @@ -1,85 +0,0 @@ -package com.draco.buoy.recyclers - -import android.content.ContentResolver -import android.content.Context -import android.view.LayoutInflater -import android.view.View -import android.view.ViewGroup -import android.view.animation.AnimationUtils -import android.widget.TextView -import androidx.recyclerview.widget.RecyclerView -import com.draco.buoy.R -import com.draco.buoy.repositories.BatterySaverConstantsConfigProfiles -import com.draco.buoy.utils.BatterySaverManager - -class BatterySaverProfileRecyclerAdapter( - private val context: Context -) : RecyclerView.Adapter<BatterySaverProfileRecyclerAdapter.ViewHolder>() { - private val batterySaverProfiles = arrayOf( - null, /* Reset */ - BatterySaverConstantsConfigProfiles.LIGHT, - BatterySaverConstantsConfigProfiles.MODERATE, - BatterySaverConstantsConfigProfiles.HIGH, - BatterySaverConstantsConfigProfiles.EXTREME, - ) - - private val batterySaverManager = BatterySaverManager(context.contentResolver) - - class ViewHolder(itemView: View): RecyclerView.ViewHolder(itemView) { - val title = itemView.findViewById<TextView>(R.id.title) - val description = itemView.findViewById<TextView>(R.id.description) - } - - override fun onCreateViewHolder(parent: ViewGroup, viewType: Int): ViewHolder { - val view = LayoutInflater.from(parent.context).inflate(R.layout.recycler_item_profile, parent, false) - return ViewHolder(view) - } - - override fun onBindViewHolder(holder: ViewHolder, position: Int) { - val profile = batterySaverProfiles[position] - - when (profile) { - null -> { - holder.title.setText(R.string.profile_title_reset) - holder.description.setText(R.string.profile_description_reset) - } - - BatterySaverConstantsConfigProfiles.LIGHT -> { - holder.title.setText(R.string.profile_title_light) - holder.description.setText(R.string.profile_description_light) - } - - BatterySaverConstantsConfigProfiles.MODERATE -> { - holder.title.setText(R.string.profile_title_moderate) - holder.description.setText(R.string.profile_description_moderate) - } - - BatterySaverConstantsConfigProfiles.HIGH -> { - holder.title.setText(R.string.profile_title_high) - holder.description.setText(R.string.profile_description_high) - } - - BatterySaverConstantsConfigProfiles.EXTREME -> { - holder.title.setText(R.string.profile_title_extreme) - holder.description.setText(R.string.profile_description_extreme) - } - } - - holder.itemView.setOnClickListener { - it.startAnimation(AnimationUtils.loadAnimation(context, R.anim.press)) - if (profile == null) { - batterySaverManager.resetConstants() - batterySaverManager.setLowPower(false) - batterySaverManager.setLowPowerSticky(false) - batterySaverManager.setLowPowerStickyAutoDisableEnabled(true) - } else { - batterySaverManager.setConstantsConfig(profile) - batterySaverManager.setLowPower(true) - batterySaverManager.setLowPowerSticky(true) - batterySaverManager.setLowPowerStickyAutoDisableEnabled(false) - } - } - } - - override fun getItemCount(): Int = batterySaverProfiles.size -}
\ No newline at end of file diff --git a/app/src/main/java/com/draco/buoy/views/MainActivity.kt b/app/src/main/java/com/draco/buoy/views/MainActivity.kt index 90e06e8..a28ab7a 100644 --- a/app/src/main/java/com/draco/buoy/views/MainActivity.kt +++ b/app/src/main/java/com/draco/buoy/views/MainActivity.kt @@ -1,33 +1,32 @@ package com.draco.buoy.views import android.content.Intent -import androidx.appcompat.app.AppCompatActivity import android.os.Bundle import androidx.activity.viewModels -import androidx.recyclerview.widget.LinearLayoutManager -import androidx.recyclerview.widget.RecyclerView +import androidx.appcompat.app.AppCompatActivity +import androidx.fragment.app.FragmentContainerView import com.draco.buoy.R -import com.draco.buoy.recyclers.BatterySaverProfileRecyclerAdapter +import com.draco.buoy.fragments.MainPreferenceFragment import com.draco.buoy.utils.PermissionUtils import com.draco.buoy.viewmodels.MainActivityViewModel class MainActivity : AppCompatActivity() { private val viewModel: MainActivityViewModel by viewModels() - private lateinit var recycler: RecyclerView + private lateinit var preferences: FragmentContainerView override fun onCreate(savedInstanceState: Bundle?) { super.onCreate(savedInstanceState) setContentView(R.layout.activity_main) - recycler = findViewById(R.id.recycler_profile) - - recycler.apply { - adapter = BatterySaverProfileRecyclerAdapter(this@MainActivity) - layoutManager = LinearLayoutManager(this@MainActivity) - } + preferences = findViewById(R.id.preferences) if (!PermissionUtils.isPermissionsGranted(this, android.Manifest.permission.WRITE_SECURE_SETTINGS)) goToPermissionActivity() + + supportFragmentManager + .beginTransaction() + .replace(R.id.preferences, MainPreferenceFragment()) + .commit() } private fun goToPermissionActivity() { |
