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 Dec 25, 2023
1 parent c4c074b commit 3e66427
Show file tree
Hide file tree
Showing 16 changed files with 219 additions and 194 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
Expand Up @@ -3,22 +3,29 @@ 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.RootDetectionService
import de.szalkowski.activitylauncher.services.SettingsService
import javax.inject.Inject

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

@Inject
internal lateinit var rootDetectionService: RootDetectionService

override fun onCreate() {
super.onCreate()

val prefs = PreferenceManager.getDefaultSharedPreferences(
baseContext
)

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

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

Expand Down
14 changes: 8 additions & 6 deletions app/src/main/java/de/szalkowski/activitylauncher/MainActivity.kt
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
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

Expand Down Expand Up @@ -42,14 +42,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,25 @@
package de.szalkowski.activitylauncher.todo;
package de.szalkowski.activitylauncher

import android.os.Bundle;
import android.os.Bundle
import androidx.appcompat.app.AppCompatActivity
import dagger.hilt.android.AndroidEntryPoint
import de.szalkowski.activitylauncher.ui.SettingsFragment

import androidx.appcompat.app.AppCompatActivity;
@AndroidEntryPoint
class SettingsActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)

import java.util.Objects;
setContentView(R.layout.activity_settings)
this.supportActionBar?.setDisplayHomeAsUpEnabled(true)
setTitle(R.string.activity_settings)

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();
supportFragmentManager.beginTransaction()
.replace(R.id.settings_container, SettingsFragment()).commit()
}

@Override
public boolean onSupportNavigateUp() {
finish();
return true;
override fun onSupportNavigateUp(): Boolean {
finish()
return true
}
}
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
@@ -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,52 @@
package de.szalkowski.activitylauncher.todo;
package de.szalkowski.activitylauncher.services

import android.content.res.Configuration;
import android.content.res.Configuration
import androidx.appcompat.app.AppCompatDelegate
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;

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";
interface SettingsService {
fun createLocaleConfiguration(language: String): Configuration
fun getCountryName(name: String): String
fun setTheme(theme: String?)
}

public static Configuration createLocaleConfiguration(String language) {
Configuration config = new Configuration();
class SettingsServiceImpl @Inject constructor() : SettingsService {
override fun createLocaleConfiguration(language: String): Configuration {
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])
Locale.setDefault(locale)
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)
}
}
}

Original file line number Diff line number Diff line change
Expand Up @@ -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));
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,10 @@ public class IconListAdapter extends BaseAdapter {
void resolve(IconListAsyncProvider.Updater updater) {
TreeSet<String> icons = new TreeSet<>();
List<PackageInfo> 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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<IconListAdapter> task,
IconListAdapter value) {
public void onProviderFinished(AsyncProvider<IconListAdapter> task, IconListAdapter value) {
try {
this.grid.setAdapter(value);
} catch (Exception e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down
Loading

0 comments on commit 3e66427

Please sign in to comment.