Skip to content

Commit

Permalink
feat: port disclaimer
Browse files Browse the repository at this point in the history
  • Loading branch information
butzist committed Feb 17, 2024
1 parent 44c2045 commit 3751d9e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 48 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import androidx.navigation.ui.setupActionBarWithNavController
import dagger.hilt.android.AndroidEntryPoint
import de.szalkowski.activitylauncher.databinding.ActivityMainBinding
import de.szalkowski.activitylauncher.services.SettingsService
import de.szalkowski.activitylauncher.ui.DisclaimerDialogFragment
import javax.inject.Inject

@AndroidEntryPoint
Expand All @@ -31,6 +32,9 @@ class MainActivity : AppCompatActivity() {
setSupportActionBar(binding.toolbar)

settingsService.applyLocaleConfiguration(baseContext)
if (!settingsService.disclaimerAccepted) {
DisclaimerDialogFragment().show(supportFragmentManager, "DisclaimerDialogFragment")
}

val navController = findNavController(R.id.nav_host_fragment_content_main)
appBarConfiguration = AppBarConfiguration(navController.graph)
Expand All @@ -52,6 +56,7 @@ class MainActivity : AppCompatActivity() {
startActivity(Intent(this, SettingsActivity::class.java))
return true
}

else -> super.onOptionsItemSelected(item)
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,57 +9,87 @@ import dagger.hilt.android.qualifiers.ApplicationContext
import java.util.Locale
import javax.inject.Inject

const val THEME_DEFAULT = "0"
const val THEME_LIGHT = "1"
const val THEME_DARK = "2"

interface SettingsService {
fun init()
fun getLocaleConfiguration(): Configuration
fun applyLocaleConfiguration(context: Context)
fun getCountryName(name: String): String
fun setTheme(theme: String?)

fun init()

fun applyLocaleConfiguration(context: Context)
var disclaimerAccepted: Boolean
val hideHidePrivate: Boolean
val language: String
val allowRoot: Boolean
val theme: String
}

class SettingsServiceImpl @Inject constructor(@ApplicationContext val context: Context) : SettingsService {
private val prefs = PreferenceManager.getDefaultSharedPreferences(context);
class SettingsServiceImpl @Inject constructor(@ApplicationContext val context: Context) :
SettingsService {
companion object {
const val THEME_DEFAULT = "0"
const val THEME_LIGHT = "1"
const val THEME_DARK = "2"

const val LANGUAGE_DEFAULT = "System Default"

const val PREF_ALLOW_ROOT = "allow_root"
const val PREF_THEME = "theme"
const val PREF_HIDE_HIDE_PRIVATE = "hide_hide_private"
const val PREF_LANGUAGE = "language"
const val PREF_DISCLAIMER_ACCEPTED = "disclaimer_accepted"
}

private val prefs = PreferenceManager.getDefaultSharedPreferences(context)

@Inject
internal lateinit var rootDetectionService: RootDetectionService

override fun init() {
setTheme(prefs.getString("theme", "0"))
setTheme(theme)

if (!prefs.contains("allow_root")) {
if (!prefs.contains(PREF_ALLOW_ROOT)) {
val hasSU = rootDetectionService.detectSU()
prefs.edit().putBoolean("allow_root", hasSU).apply()
prefs.edit().putBoolean(PREF_ALLOW_ROOT, hasSU).apply()
}

if (!prefs.contains("hide_hide_private")) {
prefs.edit().putBoolean("hide_hide_private", false).apply()
if (!prefs.contains(PREF_HIDE_HIDE_PRIVATE)) {
prefs.edit().putBoolean(PREF_HIDE_HIDE_PRIVATE, false).apply()
}

if (!prefs.contains("language")) {
prefs.edit().putString("language", "System Default").apply()
if (!prefs.contains(PREF_LANGUAGE)) {
prefs.edit().putString(PREF_LANGUAGE, LANGUAGE_DEFAULT).apply()
}
}

override var disclaimerAccepted: Boolean
get() = prefs.getBoolean(PREF_DISCLAIMER_ACCEPTED, false)
set(value) = prefs.edit().putBoolean(PREF_DISCLAIMER_ACCEPTED, value).apply()

override val theme: String
get() = prefs.getString(PREF_THEME, THEME_DEFAULT)!!

override val allowRoot: Boolean
get() = prefs.getBoolean(PREF_ALLOW_ROOT, false)

override val language: String
get() = prefs.getString(PREF_LANGUAGE, LANGUAGE_DEFAULT)!!

override val hideHidePrivate: Boolean
get() = prefs.getBoolean(PREF_HIDE_HIDE_PRIVATE, false)

override fun applyLocaleConfiguration(context: Context) {
val config = getLocaleConfiguration()
Locale.setDefault(config.locale)
context.resources.updateConfiguration(config,
context.resources.displayMetrics);
context.resources.updateConfiguration(
config, context.resources.displayMetrics
)
}

override fun getLocaleConfiguration(): Configuration {
val settingsLanguage = prefs.getString("language", "System Default")!!

val language = if (settingsLanguage == "System Default") {
val language = if (language == LANGUAGE_DEFAULT) {
Resources.getSystem().configuration.locale.toString()
} else {
settingsLanguage
language
}

val config = Configuration()
Expand Down
Original file line number Diff line number Diff line change
@@ -1,30 +1,28 @@
package de.szalkowski.activitylauncher.todo;
package de.szalkowski.activitylauncher.ui

import android.app.Dialog;
import android.content.SharedPreferences;
import android.os.Bundle;
import android.app.Dialog
import android.os.Bundle
import androidx.appcompat.app.AlertDialog
import androidx.fragment.app.DialogFragment
import dagger.hilt.android.AndroidEntryPoint
import de.szalkowski.activitylauncher.R
import de.szalkowski.activitylauncher.services.SettingsService
import javax.inject.Inject

import androidx.annotation.NonNull;
import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.DialogFragment;
import androidx.preference.PreferenceManager;
@AndroidEntryPoint
class DisclaimerDialogFragment : DialogFragment() {
@Inject
internal lateinit var settingsService: SettingsService

import de.szalkowski.activitylauncher.R;

public class DisclaimerDialogFragment extends DialogFragment {
@NonNull
@Override
public Dialog onCreateDialog(Bundle savedInstanceState) {
AlertDialog.Builder builder = new AlertDialog.Builder(getActivity());
builder.setTitle(R.string.title_dialog_disclaimer).setMessage(R.string.dialog_disclaimer).setPositiveButton(android.R.string.yes, (dialog, which) -> {
SharedPreferences editor = PreferenceManager.getDefaultSharedPreferences(requireActivity().getBaseContext());
editor.edit().putBoolean("disclaimer_accepted", true).apply();
}).setNegativeButton(android.R.string.cancel, (dialog, which) -> {
SharedPreferences editor = PreferenceManager.getDefaultSharedPreferences(requireActivity().getBaseContext());
editor.edit().putBoolean("disclaimer_accepted", false).apply();
requireActivity().finish();
});

return builder.create();
override fun onCreateDialog(savedInstanceState: Bundle?): Dialog {
val builder = AlertDialog.Builder(requireActivity())
builder.setTitle(R.string.title_dialog_disclaimer).setMessage(R.string.dialog_disclaimer)
.setPositiveButton(android.R.string.yes) { _, _ ->
settingsService.disclaimerAccepted = true
}.setNegativeButton(android.R.string.cancel) { _, _ ->
settingsService.disclaimerAccepted = false
requireActivity().finish()
}
return builder.create()
}
}
}

0 comments on commit 3751d9e

Please sign in to comment.