diff --git a/app/src/main/AndroidManifest.xml b/app/src/main/AndroidManifest.xml
index bac9b509..4355842d 100644
--- a/app/src/main/AndroidManifest.xml
+++ b/app/src/main/AndroidManifest.xml
@@ -28,7 +28,7 @@
true
+ R.id.action_settings -> {
+ startActivity(Intent(this, SettingsActivity::class.java))
+ return true
+ }
else -> super.onOptionsItemSelected(item)
}
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.nav_host_fragment_content_main)
- return navController.navigateUp(appBarConfiguration)
- || super.onSupportNavigateUp()
+ return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
}
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/SettingsActivity.kt b/app/src/main/java/de/szalkowski/activitylauncher/SettingsActivity.kt
index bfb2d96d..2cad65fa 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/SettingsActivity.kt
+++ b/app/src/main/java/de/szalkowski/activitylauncher/SettingsActivity.kt
@@ -1,31 +1,22 @@
-package de.szalkowski.activitylauncher.todo;
-
-import android.os.Bundle;
-
-import androidx.appcompat.app.AppCompatActivity;
-
-import java.util.Objects;
-
-import de.szalkowski.activitylauncher.R;
-
-public class SettingsActivity extends AppCompatActivity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_settings);
- Objects.requireNonNull(this.getSupportActionBar()).setDisplayHomeAsUpEnabled(true);
-
- setTitle(R.string.activity_settings);
- getSupportFragmentManager()
- .beginTransaction()
- .replace(R.id.settings_container, new SettingsFragment())
- .commit();
- }
-
- @Override
- public boolean onSupportNavigateUp() {
- finish();
- return true;
+package de.szalkowski.activitylauncher
+
+import android.os.Bundle
+import androidx.appcompat.app.AppCompatActivity
+import dagger.hilt.android.AndroidEntryPoint
+import de.szalkowski.activitylauncher.services.SettingsService
+import de.szalkowski.activitylauncher.ui.SettingsFragment
+import javax.inject.Inject
+
+@AndroidEntryPoint
+class SettingsActivity : AppCompatActivity() {
+ override fun onCreate(savedInstanceState: Bundle?) {
+ super.onCreate(savedInstanceState)
+
+ setContentView(R.layout.activity_settings)
+ this.supportActionBar?.setDisplayHomeAsUpEnabled(true)
+ setTitle(R.string.activity_settings)
+
+ supportFragmentManager.beginTransaction()
+ .replace(R.id.settings_container, SettingsFragment()).commit()
}
}
\ No newline at end of file
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/services/ActivityListService.kt b/app/src/main/java/de/szalkowski/activitylauncher/services/ActivityListService.kt
index 91a54ce1..ad877da1 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/services/ActivityListService.kt
+++ b/app/src/main/java/de/szalkowski/activitylauncher/services/ActivityListService.kt
@@ -20,11 +20,11 @@ interface ActivityListService {
): MyActivityInfo
}
-class ActivityListServiceImpl @Inject constructor(@ActivityContext context: Context) :
+class ActivityListServiceImpl @Inject constructor(@ActivityContext context: Context, settingsService: SettingsService) :
ActivityListService {
+ private val config: Configuration = settingsService.getLocaleConfiguration()
private val packageManager = context.packageManager
- private val config: Configuration = Configuration() // FIXME
override fun getActivities(packageName: String): List {
val info = try {
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/services/Bindings.kt b/app/src/main/java/de/szalkowski/activitylauncher/services/Bindings.kt
index ce656cac..61f6db4c 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/services/Bindings.kt
+++ b/app/src/main/java/de/szalkowski/activitylauncher/services/Bindings.kt
@@ -4,7 +4,8 @@ import dagger.Binds
import dagger.Module
import dagger.hilt.InstallIn
import dagger.hilt.android.components.ActivityComponent
-import dagger.hilt.android.components.FragmentComponent
+import dagger.hilt.components.SingletonComponent
+import javax.inject.Singleton
@Module
@InstallIn(ActivityComponent::class)
@@ -28,4 +29,20 @@ abstract class ServicesModule {
abstract fun bindIconCreatorService(
iconCreatorServiceImpl: IconCreatorServiceImpl
): IconCreatorService
+}
+
+@Module
+@InstallIn(SingletonComponent::class)
+abstract class ApplicationServicesModule {
+ @Singleton
+ @Binds
+ abstract fun bindRootDetectionService(
+ rootDetectionServiceImpl: RootDetectionServiceImpl
+ ): RootDetectionService
+
+ @Singleton
+ @Binds
+ abstract fun bindSettingsService(
+ settingsServiceImpl: SettingsServiceImpl
+ ): SettingsService
}
\ No newline at end of file
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/services/PackageListService.kt b/app/src/main/java/de/szalkowski/activitylauncher/services/PackageListService.kt
index 4edc6dd4..872d11c4 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/services/PackageListService.kt
+++ b/app/src/main/java/de/szalkowski/activitylauncher/services/PackageListService.kt
@@ -13,9 +13,10 @@ interface PackageListService {
val packages: List
}
-class PackageListServiceImpl @Inject constructor(@ApplicationContext context: Context) :
+class PackageListServiceImpl @Inject constructor(@ApplicationContext context: Context, settingsService: SettingsService) :
PackageListService {
- private val config: Configuration = Configuration() // FIXME
+
+ private val config: Configuration = settingsService.getLocaleConfiguration()
private val packageManager: PackageManager = context.packageManager
override val packages: List
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/services/RootDetectionService.kt b/app/src/main/java/de/szalkowski/activitylauncher/services/RootDetectionService.kt
index ba7a9753..43007078 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/services/RootDetectionService.kt
+++ b/app/src/main/java/de/szalkowski/activitylauncher/services/RootDetectionService.kt
@@ -1,25 +1,17 @@
-package de.szalkowski.activitylauncher.todo;
+package de.szalkowski.activitylauncher.services
-import java.io.File;
-import java.util.Objects;
-import java.util.Vector;
+import java.io.File
+import javax.inject.Inject
-public class RootDetection {
- public static boolean detectSU() {
- Vector paths = new Vector<>();
- String[] dirs = Objects.requireNonNull(System.getenv("PATH")).split(":");
- for (String dir : dirs) {
- paths.add(new File(dir, "su"));
- }
- for (File path : paths) {
- if (path.exists() && path.canExecute() && path.isFile()) {
- return true;
- }
- }
-
- return false;
- }
+interface RootDetectionService {
+ fun detectSU(): Boolean
+}
+class RootDetectionServiceImpl @Inject constructor() : RootDetectionService {
+ override fun detectSU(): Boolean {
+ val dirs = System.getenv("PATH").orEmpty().split(":").map { dir -> File(dir, "su") }
+ return dirs.any { path -> path.exists() && path.canExecute() && path.isFile }
+ }
}
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/services/SettingsService.kt b/app/src/main/java/de/szalkowski/activitylauncher/services/SettingsService.kt
index c967bcf8..82ef353e 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/services/SettingsService.kt
+++ b/app/src/main/java/de/szalkowski/activitylauncher/services/SettingsService.kt
@@ -1,50 +1,96 @@
-package de.szalkowski.activitylauncher.todo;
+package de.szalkowski.activitylauncher.services
-import android.content.res.Configuration;
+import android.content.Context
+import android.content.res.Configuration
+import android.content.res.Resources
+import androidx.appcompat.app.AppCompatDelegate
+import androidx.preference.PreferenceManager
+import dagger.hilt.android.qualifiers.ApplicationContext
+import java.util.Locale
+import javax.inject.Inject
-import androidx.appcompat.app.AppCompatActivity;
-import androidx.appcompat.app.AppCompatDelegate;
+const val THEME_DEFAULT = "0"
+const val THEME_LIGHT = "1"
+const val THEME_DARK = "2"
-import java.util.Locale;
+interface SettingsService {
+ fun getLocaleConfiguration(): Configuration
+ fun getCountryName(name: String): String
+ fun setTheme(theme: String?)
-public class SettingsUtils extends AppCompatActivity {
- public final static String THEME_DEFAULT = "0";
- public final static String THEME_LIGHT = "1";
- public final static String THEME_DARK = "2";
+ fun init()
- public static Configuration createLocaleConfiguration(String language) {
- Configuration config = new Configuration();
+ fun applyLocaleConfiguration(context: Context)
+}
+
+class SettingsServiceImpl @Inject constructor(@ApplicationContext val context: Context) : SettingsService {
+ private val prefs = PreferenceManager.getDefaultSharedPreferences(context);
+
+ @Inject
+ internal lateinit var rootDetectionService: RootDetectionService
+
+ override fun init() {
+ setTheme(prefs.getString("theme", "0"))
+
+ if (!prefs.contains("allow_root")) {
+ val hasSU = rootDetectionService.detectSU()
+ prefs.edit().putBoolean("allow_root", hasSU).apply()
+ }
+
+ if (!prefs.contains("hide_hide_private")) {
+ prefs.edit().putBoolean("hide_hide_private", false).apply()
+ }
+
+ if (!prefs.contains("language")) {
+ prefs.edit().putString("language", "System Default").apply()
+ }
+ }
+
+ override fun applyLocaleConfiguration(context: Context) {
+ val config = getLocaleConfiguration()
+ Locale.setDefault(config.locale)
+ context.resources.updateConfiguration(config,
+ context.resources.displayMetrics);
+ }
+
+ override fun getLocaleConfiguration(): Configuration {
+ val settingsLanguage = prefs.getString("language", "System Default")!!
+
+ val language = if (settingsLanguage == "System Default") {
+ Resources.getSystem().configuration.locale.toString()
+ } else {
+ settingsLanguage
+ }
+
+ val config = Configuration()
if (language.contains("_")) {
- String[] parts = language.split("_");
- Locale locale = new Locale(parts[0], parts[1]);
- Locale.setDefault(locale);
- config.locale = locale;
+ val parts = language.split("_".toRegex()).dropLastWhile { it.isEmpty() }.toTypedArray()
+ val locale = Locale(parts[0], parts[1])
+ config.locale = locale // FIXME
}
- return config;
+
+ return config
}
- public static String getCountryName(String name) {
- for (Locale locale : Locale.getAvailableLocales()) {
- if (name.equals(locale.getLanguage() + '_' + locale.getCountry())) {
- String language = locale.getDisplayName(locale);
- return language.substring(0, 1).toUpperCase() + language.substring(1);
+ override fun getCountryName(name: String): String {
+ for (locale in Locale.getAvailableLocales()) {
+ if (name == locale.language + '_' + locale.country) {
+ val language = locale.getDisplayName(locale)
+ return language.substring(0, 1).uppercase(Locale.getDefault()) + language.substring(
+ 1
+ )
}
}
- return name;
+ return name
}
- public static void setTheme(String theme) {
- switch (theme) {
- default:
- case THEME_DEFAULT:
- AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM);
- break;
- case THEME_LIGHT:
- AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO);
- break;
- case THEME_DARK:
- AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES);
- break;
+ override fun setTheme(theme: String?) {
+ when (theme) {
+ THEME_DEFAULT -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
+ THEME_LIGHT -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_NO)
+ THEME_DARK -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_YES)
+ else -> AppCompatDelegate.setDefaultNightMode(AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM)
}
}
}
+
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/todo/AsyncProvider.java b/app/src/main/java/de/szalkowski/activitylauncher/todo/AsyncProvider.java
index e2eb933d..aa09d6cc 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/todo/AsyncProvider.java
+++ b/app/src/main/java/de/szalkowski/activitylauncher/todo/AsyncProvider.java
@@ -45,9 +45,7 @@ protected void onProgressUpdate(Integer... values) {
}
this.binding.progress.setProgress(value);
- this.binding.progressNumber.setText(
- String.format(Locale.getDefault(), "%1d/%2d", value, this.max)
- );
+ this.binding.progressNumber.setText(String.format(Locale.getDefault(), "%1d/%2d", value, this.max));
double percent = (double) value / (double) this.max;
this.binding.progressPercent.setText(this.progressPercentFormat.format(percent));
}
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/todo/DisclaimerDialogFragment.java b/app/src/main/java/de/szalkowski/activitylauncher/todo/DisclaimerDialogFragment.java
index 426ed959..21668be7 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/todo/DisclaimerDialogFragment.java
+++ b/app/src/main/java/de/szalkowski/activitylauncher/todo/DisclaimerDialogFragment.java
@@ -16,17 +16,14 @@ public class DisclaimerDialogFragment extends DialogFragment {
@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();
- });
+ 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();
}
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/todo/IconListAdapter.java b/app/src/main/java/de/szalkowski/activitylauncher/todo/IconListAdapter.java
index 1083f031..9006695e 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/todo/IconListAdapter.java
+++ b/app/src/main/java/de/szalkowski/activitylauncher/todo/IconListAdapter.java
@@ -38,7 +38,10 @@ public class IconListAdapter extends BaseAdapter {
void resolve(IconListAsyncProvider.Updater updater) {
TreeSet icons = new TreeSet<>();
List all_packages = this.pm.getInstalledPackages(0);
- Configuration locale = SettingsUtils.createLocaleConfiguration(prefs.getString("language", "System Default"));
+
+ //Configuration locale = SettingsUtils.createLocaleConfiguration(prefs.getString("language", "System Default"));
+ Configuration locale = new Configuration(); // FIXME
+
updater.updateMax(all_packages.size());
updater.update(0);
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/todo/IconPickerDialogFragment.java b/app/src/main/java/de/szalkowski/activitylauncher/todo/IconPickerDialogFragment.java
index 5562ba4a..44f2e433 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/todo/IconPickerDialogFragment.java
+++ b/app/src/main/java/de/szalkowski/activitylauncher/todo/IconPickerDialogFragment.java
@@ -45,16 +45,13 @@ public Dialog onCreateDialog(Bundle savedInstanceState) {
}
});
- builder.setTitle(R.string.title_dialog_icon_picker)
- .setView(view)
- .setNegativeButton(android.R.string.cancel, (dialog, which) -> IconPickerDialogFragment.this.requireDialog().cancel());
+ builder.setTitle(R.string.title_dialog_icon_picker).setView(view).setNegativeButton(android.R.string.cancel, (dialog, which) -> IconPickerDialogFragment.this.requireDialog().cancel());
return builder.create();
}
@Override
- public void onProviderFinished(AsyncProvider task,
- IconListAdapter value) {
+ public void onProviderFinished(AsyncProvider task, IconListAdapter value) {
try {
this.grid.setAdapter(value);
} catch (Exception e) {
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/todo/RootLauncherActivity.java b/app/src/main/java/de/szalkowski/activitylauncher/todo/RootLauncherActivity.java
index 15edfb97..8216bd88 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/todo/RootLauncherActivity.java
+++ b/app/src/main/java/de/szalkowski/activitylauncher/todo/RootLauncherActivity.java
@@ -33,7 +33,7 @@ protected void onCreate(Bundle savedInstanceState) {
}
} catch (Exception e) {
e.printStackTrace();
- Toast.makeText(getApplicationContext(), getText(R.string.error).toString() + ": " + e, Toast.LENGTH_LONG).show();
+ Toast.makeText(getApplicationContext(), getText(R.string.error) + ": " + e, Toast.LENGTH_LONG).show();
} finally {
finish();
}
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/todo/RootLauncherIconCreator.java b/app/src/main/java/de/szalkowski/activitylauncher/todo/RootLauncherIconCreator.java
index 4b592777..42f06dbe 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/todo/RootLauncherIconCreator.java
+++ b/app/src/main/java/de/szalkowski/activitylauncher/todo/RootLauncherIconCreator.java
@@ -19,7 +19,7 @@ public static void createLauncherIcon(Context context, MyActivityInfo activity)
signature = signer.signComponentName(comp);
} catch (Exception e) {
e.printStackTrace();
- Toast.makeText(context, context.getText(R.string.error).toString() + ": " + e, Toast.LENGTH_LONG).show();
+ Toast.makeText(context, context.getText(R.string.error) + ": " + e, Toast.LENGTH_LONG).show();
return;
}
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/ui/ActivityDetailsFragment.kt b/app/src/main/java/de/szalkowski/activitylauncher/ui/ActivityDetailsFragment.kt
index 104a3039..2ca882f1 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/ui/ActivityDetailsFragment.kt
+++ b/app/src/main/java/de/szalkowski/activitylauncher/ui/ActivityDetailsFragment.kt
@@ -13,7 +13,6 @@ import de.szalkowski.activitylauncher.services.ActivityLauncherService
import de.szalkowski.activitylauncher.services.ActivityListService
import de.szalkowski.activitylauncher.services.IconCreatorService
import de.szalkowski.activitylauncher.services.MyActivityInfo
-import de.szalkowski.activitylauncher.services.MyPackageInfo
import javax.inject.Inject
@AndroidEntryPoint
@@ -41,6 +40,7 @@ class ActivityDetailsFragment : Fragment() {
activityInfo = activityListService.getActivity(args.activityComponentName)
}
+
override fun onCreateView(
inflater: LayoutInflater, container: ViewGroup?, savedInstanceState: Bundle?
): View {
@@ -64,9 +64,8 @@ class ActivityDetailsFragment : Fragment() {
}
binding.btLaunch.setOnClickListener {
- activityLauncherService.launchActivity(editedActivityInfo.componentName,
- asRoot = false,
- showToast = true
+ activityLauncherService.launchActivity(
+ editedActivityInfo.componentName, asRoot = false, showToast = true
)
}
}
@@ -78,7 +77,8 @@ class ActivityDetailsFragment : Fragment() {
private val editedActivityInfo: MyActivityInfo
get() {
- val componentName = ComponentName(binding.etPackage.text.toString(), binding.etClass.text.toString())
+ val componentName =
+ ComponentName(binding.etPackage.text.toString(), binding.etClass.text.toString())
val iconResourceName = binding.etIcon.text.toString()
return MyActivityInfo(
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/ui/ActivityListAdapter.kt b/app/src/main/java/de/szalkowski/activitylauncher/ui/ActivityListAdapter.kt
index 835741ac..81636495 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/ui/ActivityListAdapter.kt
+++ b/app/src/main/java/de/szalkowski/activitylauncher/ui/ActivityListAdapter.kt
@@ -12,12 +12,11 @@ import dagger.assisted.AssistedInject
import de.szalkowski.activitylauncher.R
import de.szalkowski.activitylauncher.services.ActivityListService
import de.szalkowski.activitylauncher.services.MyActivityInfo
-import de.szalkowski.activitylauncher.services.MyPackageInfo
-import de.szalkowski.activitylauncher.services.PackageListService
-import javax.inject.Inject
-class ActivityListAdapter @AssistedInject constructor(activityListService: ActivityListService, @Assisted private val packageName: String) :
- RecyclerView.Adapter() {
+class ActivityListAdapter @AssistedInject constructor(
+ activityListService: ActivityListService,
+ @Assisted private val packageName: String
+) : RecyclerView.Adapter() {
@AssistedFactory
interface ActivityListAdapterFactory {
fun create(packageName: String): ActivityListAdapter
@@ -54,7 +53,11 @@ class ActivityListAdapter @AssistedInject constructor(activityListService: Activ
val item = activities[position]
holder.item = item
- tvName.text = if (item.isPrivate) { "(${item.name})" } else { item.name}
+ tvName.text = if (item.isPrivate) {
+ "(${item.name})"
+ } else {
+ item.name
+ }
tvPackage.text = item.componentName.shortClassName
ivIcon.setImageDrawable(item.icon)
}
diff --git a/app/src/main/java/de/szalkowski/activitylauncher/ui/SettingsFragment.kt b/app/src/main/java/de/szalkowski/activitylauncher/ui/SettingsFragment.kt
index 0b13119f..fe7bae55 100644
--- a/app/src/main/java/de/szalkowski/activitylauncher/ui/SettingsFragment.kt
+++ b/app/src/main/java/de/szalkowski/activitylauncher/ui/SettingsFragment.kt
@@ -1,92 +1,96 @@
-package de.szalkowski.activitylauncher.todo;
-
-import android.content.SharedPreferences;
-import android.content.res.Configuration;
-import android.content.res.Resources;
-import android.os.Bundle;
-import android.widget.Toast;
-
-import androidx.preference.ListPreference;
-import androidx.preference.PreferenceFragmentCompat;
-import androidx.preference.PreferenceManager;
-import androidx.preference.SwitchPreference;
-
-import java.util.ArrayList;
-import java.util.Objects;
-
-import de.szalkowski.activitylauncher.R;
-
-
-public class SettingsFragment extends PreferenceFragmentCompat {
- private SharedPreferences prefs;
-
- @Override
- public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
- setPreferencesFromResource(R.xml.preferences, rootKey);
- this.prefs = PreferenceManager.getDefaultSharedPreferences(requireActivity().getBaseContext());
-
- SwitchPreference hidePrivate = Objects.requireNonNull(findPreference("hide_private"));
- SwitchPreference allowRoot = Objects.requireNonNull(findPreference("allow_root"));
- ListPreference theme = Objects.requireNonNull(findPreference("theme"));
- ListPreference languages = Objects.requireNonNull(findPreference("language"));
+package de.szalkowski.activitylauncher.ui
+
+import android.content.SharedPreferences
+import android.content.res.Resources
+import android.os.Bundle
+import android.widget.Toast
+import androidx.preference.ListPreference
+import androidx.preference.PreferenceFragmentCompat
+import androidx.preference.PreferenceManager
+import androidx.preference.SwitchPreference
+import dagger.hilt.android.AndroidEntryPoint
+import de.szalkowski.activitylauncher.R
+import de.szalkowski.activitylauncher.services.RootDetectionService
+import de.szalkowski.activitylauncher.services.SettingsService
+import java.util.Objects
+import javax.inject.Inject
+
+
+@AndroidEntryPoint
+class SettingsFragment : PreferenceFragmentCompat() {
+ private lateinit var prefs: SharedPreferences
+
+ @Inject
+ internal lateinit var rootDetectionService: RootDetectionService
+
+ @Inject
+ internal lateinit var settingsService: SettingsService
+
+ override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
+ setPreferencesFromResource(R.xml.preferences, rootKey)
+ prefs = PreferenceManager.getDefaultSharedPreferences(requireActivity().baseContext)
+
+ val hidePrivate: SwitchPreference = Objects.requireNonNull(findPreference("hide_private"))
+ val allowRoot: SwitchPreference = Objects.requireNonNull(findPreference("allow_root"))
+ val theme: ListPreference = Objects.requireNonNull(findPreference("theme"))
+ val languages: ListPreference = Objects.requireNonNull(findPreference("language"))
+
+ languages.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance())
+ populateLanguages(languages)
+ languages.setOnPreferenceChangeListener { _, newValue ->
+ onLanguageUpdated(
+ newValue as String
+ )
+ }
- theme.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance());
- languages.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance());
+ hidePrivate.setOnPreferenceChangeListener { _, newValue ->
+ onHidePrivateUpdated(
+ newValue as Boolean
+ )
+ }
- populateLanguages(languages);
+ allowRoot.setOnPreferenceChangeListener { _, newValue ->
+ onAllowRootUpdated(
+ newValue as Boolean
+ )
+ }
- hidePrivate.setOnPreferenceChangeListener((preference, newValue) -> onHidePrivateUpdated((Boolean) newValue));
- allowRoot.setOnPreferenceChangeListener((preference, newValue) -> onAllowRootUpdated((Boolean) newValue));
- theme.setOnPreferenceChangeListener((preference, newValue) -> onThemeUpdated((String) newValue));
- languages.setOnPreferenceChangeListener((preference, newValue) -> onLanguageUpdated((String) newValue));
+ theme.setSummaryProvider(ListPreference.SimpleSummaryProvider.getInstance())
+ theme.setOnPreferenceChangeListener { _, newValue -> onThemeUpdated(newValue as String) }
}
- private void populateLanguages(ListPreference languages) {
- String[] locales = getResources().getStringArray(R.array.locales);
- ArrayList language = new ArrayList<>();
- for (String locale : locales) {
- language.add(SettingsUtils.getCountryName(locale));
- }
- String[] languageValue = language.toArray(new String[0]);
- languages.setEntries(languageValue);
+ private fun populateLanguages(languages: ListPreference) {
+ val languageValues = resources.getStringArray(R.array.locales)
+ .map { locale -> settingsService.getCountryName(locale) }
+ languages.entries = languageValues.toTypedArray()
}
- private boolean onAllowRootUpdated(boolean newValue) {
- boolean hasSU = RootDetection.detectSU();
-
+ private fun onAllowRootUpdated(newValue: Boolean): Boolean {
+ val hasSU = rootDetectionService.detectSU()
if (newValue && !hasSU) {
- Toast.makeText(getActivity(), getText(R.string.warning_root_check), Toast.LENGTH_LONG).show();
+ Toast.makeText(activity, getText(R.string.warning_root_check), Toast.LENGTH_LONG).show()
}
-
- prefs.edit().putBoolean("allow_root", newValue).apply();
- return true;
+ prefs.edit().putBoolean("allow_root", newValue).apply()
+ return true
}
- private boolean onThemeUpdated(String newValue) {
- this.prefs.edit().putString("theme", newValue).apply();
- SettingsUtils.setTheme(newValue);
- return true;
+ private fun onThemeUpdated(newValue: String): Boolean {
+ prefs.edit().putString("theme", newValue).apply()
+ settingsService.setTheme(newValue)
+ return true
}
- private boolean onHidePrivateUpdated(boolean newValue) {
- this.prefs.edit().putBoolean("hide_hide_private", newValue).apply();
- return true;
+ private fun onHidePrivateUpdated(newValue: Boolean): Boolean {
+ prefs.edit().putBoolean("hide_hide_private", newValue).apply()
+ return true
}
- private boolean onLanguageUpdated(String newValue) {
- this.prefs.edit().putString("language", newValue).apply();
- Configuration config;
- if (newValue.equals("System Default")) {
- config = SettingsUtils.createLocaleConfiguration(Resources.getSystem().getConfiguration().locale.toString());
- } else {
- config = SettingsUtils.createLocaleConfiguration(newValue);
- }
+ private fun onLanguageUpdated(newValue: String): Boolean {
+ prefs.edit().putString("language", newValue).apply()
- Resources resources = requireActivity().getBaseContext().getResources();
- resources.updateConfiguration(config, resources.getDisplayMetrics());
- // FIXME
- //PackageManagerCache.resetPackageManagerCache();
- requireActivity().recreate();
- return true;
+ settingsService.applyLocaleConfiguration(requireActivity().baseContext)
+ requireActivity().recreate()
+ return true
}
}
+