Skip to content

Commit

Permalink
feat: port settings activity
Browse files Browse the repository at this point in the history
  • Loading branch information
butzist committed Feb 17, 2024
1 parent e65ee09 commit 3ad105f
Show file tree
Hide file tree
Showing 18 changed files with 265 additions and 223 deletions.
2 changes: 1 addition & 1 deletion app/src/main/AndroidManifest.xml
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@
</intent-filter>
</activity>
<activity
android:name=".todo.SettingsActivity"
android:name=".SettingsActivity"
android:label="@string/activity_settings"
android:exported="false" />
<activity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,33 +1,18 @@
package de.szalkowski.activitylauncher

import android.app.Application
import androidx.preference.PreferenceManager
import dagger.hilt.android.HiltAndroidApp
import de.szalkowski.activitylauncher.todo.RootDetection
import de.szalkowski.activitylauncher.todo.SettingsUtils
import de.szalkowski.activitylauncher.services.SettingsService
import javax.inject.Inject

@HiltAndroidApp
class ActivityLauncherApp : Application() {
@Inject
internal lateinit var settingsService: SettingsService

override fun onCreate() {
super.onCreate()

val prefs = PreferenceManager.getDefaultSharedPreferences(
baseContext
)

SettingsUtils.setTheme(prefs.getString("theme", "0"))

if (!prefs.contains("allow_root")) {
val hasSU = RootDetection.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()
}
settingsService.init()
}
}
22 changes: 15 additions & 7 deletions app/src/main/java/de/szalkowski/activitylauncher/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,31 +1,37 @@
package de.szalkowski.activitylauncher

import android.content.Intent
import android.os.Bundle
import com.google.android.material.snackbar.Snackbar
import android.view.Menu
import android.view.MenuItem
import androidx.appcompat.app.AppCompatActivity
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import android.view.Menu
import android.view.MenuItem
import dagger.hilt.android.AndroidEntryPoint
import de.szalkowski.activitylauncher.databinding.ActivityMainBinding
import de.szalkowski.activitylauncher.services.SettingsService
import javax.inject.Inject

@AndroidEntryPoint
class MainActivity : AppCompatActivity() {

private lateinit var appBarConfiguration: AppBarConfiguration
private lateinit var binding: ActivityMainBinding

@Inject
internal lateinit var settingsService: SettingsService

override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

binding = ActivityMainBinding.inflate(layoutInflater)
setContentView(binding.root)

setSupportActionBar(binding.toolbar)

settingsService.applyLocaleConfiguration(baseContext)

val navController = findNavController(R.id.nav_host_fragment_content_main)
appBarConfiguration = AppBarConfiguration(navController.graph)
setupActionBarWithNavController(navController, appBarConfiguration)
Expand All @@ -42,14 +48,16 @@ class MainActivity : AppCompatActivity() {
// automatically handle clicks on the Home/Up button, so long
// as you specify a parent activity in AndroidManifest.xml.
return when (item.itemId) {
R.id.action_settings -> 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()
}
}
Original file line number Diff line number Diff line change
@@ -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()
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -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<MyActivityInfo> {
val info = try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
}
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,10 @@ interface PackageListService {
val packages: List<MyPackageInfo>
}

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<MyPackageInfo>
Expand Down
Original file line number Diff line number Diff line change
@@ -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<File> 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 }
}
}
Original file line number Diff line number Diff line change
@@ -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)
}
}
}

Loading

0 comments on commit 3ad105f

Please sign in to comment.