Rewrite 'settings' to Kotlin
This commit is contained in:
@@ -31,7 +31,7 @@ class InitializationActivity : AppCompatActivity() {
|
|||||||
super.onCreate(savedInstanceState)
|
super.onCreate(savedInstanceState)
|
||||||
Log.init(this)
|
Log.init(this)
|
||||||
val theme = PreferenceManager.getDefaultSharedPreferences(this)
|
val theme = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
.getString(getString(R.string.setting_key_theme), getString(R.string.theme_default))
|
.getString(getString(R.string.setting_key_theme), getString(R.string.theme_default))!!
|
||||||
ThemeHelper.setTheme(this, theme)
|
ThemeHelper.setTheme(this, theme)
|
||||||
|
|
||||||
setContentView(R.layout.splash)
|
setContentView(R.layout.splash)
|
||||||
|
|||||||
@@ -1,96 +0,0 @@
|
|||||||
package com.github.gotify.settings;
|
|
||||||
|
|
||||||
import android.app.AlertDialog;
|
|
||||||
import android.content.ComponentName;
|
|
||||||
import android.content.Intent;
|
|
||||||
import android.content.SharedPreferences;
|
|
||||||
import android.content.pm.PackageManager;
|
|
||||||
import android.os.Bundle;
|
|
||||||
import android.view.MenuItem;
|
|
||||||
import android.view.View;
|
|
||||||
import androidx.annotation.NonNull;
|
|
||||||
import androidx.annotation.Nullable;
|
|
||||||
import androidx.appcompat.app.ActionBar;
|
|
||||||
import androidx.appcompat.app.AppCompatActivity;
|
|
||||||
import androidx.preference.ListPreference;
|
|
||||||
import androidx.preference.PreferenceFragmentCompat;
|
|
||||||
import androidx.preference.PreferenceManager;
|
|
||||||
import com.github.gotify.R;
|
|
||||||
|
|
||||||
public class SettingsActivity extends AppCompatActivity
|
|
||||||
implements SharedPreferences.OnSharedPreferenceChangeListener {
|
|
||||||
|
|
||||||
@Override
|
|
||||||
protected void onCreate(Bundle savedInstanceState) {
|
|
||||||
super.onCreate(savedInstanceState);
|
|
||||||
setContentView(R.layout.settings_activity);
|
|
||||||
getSupportFragmentManager()
|
|
||||||
.beginTransaction()
|
|
||||||
.replace(R.id.settings, new SettingsFragment())
|
|
||||||
.commit();
|
|
||||||
setSupportActionBar(findViewById(R.id.toolbar));
|
|
||||||
ActionBar actionBar = getSupportActionBar();
|
|
||||||
if (actionBar != null) {
|
|
||||||
actionBar.setDisplayHomeAsUpEnabled(true);
|
|
||||||
actionBar.setDisplayShowCustomEnabled(true);
|
|
||||||
}
|
|
||||||
SharedPreferences sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this);
|
|
||||||
sharedPreferences.registerOnSharedPreferenceChangeListener(this);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
|
|
||||||
if (item.getItemId() == android.R.id.home) {
|
|
||||||
finish();
|
|
||||||
}
|
|
||||||
return super.onOptionsItemSelected(item);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onSharedPreferenceChanged(SharedPreferences sharedPreferences, String key) {
|
|
||||||
if (getString(R.string.setting_key_theme).equals(key)) {
|
|
||||||
ThemeHelper.setTheme(
|
|
||||||
this, sharedPreferences.getString(key, getString(R.string.theme_default)));
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
public static class SettingsFragment extends PreferenceFragmentCompat {
|
|
||||||
@Override
|
|
||||||
public void onCreatePreferences(Bundle savedInstanceState, String rootKey) {
|
|
||||||
setPreferencesFromResource(R.xml.root_preferences, rootKey);
|
|
||||||
}
|
|
||||||
|
|
||||||
@Override
|
|
||||||
public void onViewCreated(@NonNull View view, @Nullable Bundle savedInstanceState) {
|
|
||||||
super.onViewCreated(view, savedInstanceState);
|
|
||||||
ListPreference message_layout =
|
|
||||||
findPreference(getString(R.string.setting_key_message_layout));
|
|
||||||
message_layout.setOnPreferenceChangeListener(
|
|
||||||
(ignored, ignored2) -> {
|
|
||||||
new AlertDialog.Builder(getContext())
|
|
||||||
.setTitle(R.string.setting_message_layout_dialog_title)
|
|
||||||
.setMessage(R.string.setting_message_layout_dialog_message)
|
|
||||||
.setPositiveButton(
|
|
||||||
getString(R.string.setting_message_layout_dialog_button1),
|
|
||||||
(ignored3, ignored4) -> {
|
|
||||||
restartApp();
|
|
||||||
})
|
|
||||||
.setNegativeButton(
|
|
||||||
getString(R.string.setting_message_layout_dialog_button2),
|
|
||||||
(ignore3, ignored4) -> {})
|
|
||||||
.show();
|
|
||||||
return true;
|
|
||||||
});
|
|
||||||
}
|
|
||||||
|
|
||||||
private void restartApp() {
|
|
||||||
PackageManager packageManager = getContext().getPackageManager();
|
|
||||||
String packageName = getContext().getPackageName();
|
|
||||||
Intent intent = packageManager.getLaunchIntentForPackage(packageName);
|
|
||||||
ComponentName componentName = intent.getComponent();
|
|
||||||
Intent mainIntent = Intent.makeRestartActivityTask(componentName);
|
|
||||||
startActivity(mainIntent);
|
|
||||||
Runtime.getRuntime().exit(0);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,92 @@
|
|||||||
|
package com.github.gotify.settings
|
||||||
|
|
||||||
|
import android.app.AlertDialog
|
||||||
|
import android.content.Intent
|
||||||
|
import android.content.SharedPreferences
|
||||||
|
import android.content.SharedPreferences.OnSharedPreferenceChangeListener
|
||||||
|
import android.os.Bundle
|
||||||
|
import android.view.MenuItem
|
||||||
|
import android.view.View
|
||||||
|
import androidx.appcompat.app.AppCompatActivity
|
||||||
|
import androidx.preference.ListPreference
|
||||||
|
import androidx.preference.Preference
|
||||||
|
import androidx.preference.PreferenceFragmentCompat
|
||||||
|
import androidx.preference.PreferenceManager
|
||||||
|
import com.github.gotify.R
|
||||||
|
import com.github.gotify.databinding.SettingsActivityBinding
|
||||||
|
|
||||||
|
class SettingsActivity : AppCompatActivity(), OnSharedPreferenceChangeListener {
|
||||||
|
private lateinit var binding: SettingsActivityBinding
|
||||||
|
|
||||||
|
override fun onCreate(savedInstanceState: Bundle?) {
|
||||||
|
super.onCreate(savedInstanceState)
|
||||||
|
binding = SettingsActivityBinding.inflate(layoutInflater)
|
||||||
|
setContentView(binding.root)
|
||||||
|
supportFragmentManager
|
||||||
|
.beginTransaction()
|
||||||
|
.replace(R.id.settings, SettingsFragment())
|
||||||
|
.commit()
|
||||||
|
setSupportActionBar(binding.appBarDrawer.toolbar)
|
||||||
|
val actionBar = supportActionBar
|
||||||
|
if (actionBar != null) {
|
||||||
|
actionBar.setDisplayHomeAsUpEnabled(true)
|
||||||
|
actionBar.setDisplayShowCustomEnabled(true)
|
||||||
|
}
|
||||||
|
val sharedPreferences = PreferenceManager.getDefaultSharedPreferences(this)
|
||||||
|
sharedPreferences.registerOnSharedPreferenceChangeListener(this)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onOptionsItemSelected(item: MenuItem): Boolean {
|
||||||
|
if (item.itemId == android.R.id.home) {
|
||||||
|
finish()
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onSharedPreferenceChanged(sharedPreferences: SharedPreferences, key: String) {
|
||||||
|
if (getString(R.string.setting_key_theme) == key) {
|
||||||
|
ThemeHelper.setTheme(
|
||||||
|
this, sharedPreferences.getString(key, getString(R.string.theme_default))!!)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class SettingsFragment : PreferenceFragmentCompat() {
|
||||||
|
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
|
||||||
|
setPreferencesFromResource(R.xml.root_preferences, rootKey)
|
||||||
|
}
|
||||||
|
|
||||||
|
override fun onViewCreated(view: View, savedInstanceState: Bundle?) {
|
||||||
|
super.onViewCreated(view, savedInstanceState)
|
||||||
|
val messageLayout: ListPreference? =
|
||||||
|
findPreference(getString(R.string.setting_key_message_layout))
|
||||||
|
messageLayout?.onPreferenceChangeListener =
|
||||||
|
Preference.OnPreferenceChangeListener { _, _ ->
|
||||||
|
AlertDialog.Builder(context)
|
||||||
|
.setTitle(R.string.setting_message_layout_dialog_title)
|
||||||
|
.setMessage(R.string.setting_message_layout_dialog_message)
|
||||||
|
.setPositiveButton(
|
||||||
|
getString(R.string.setting_message_layout_dialog_button1)
|
||||||
|
) { _, _ ->
|
||||||
|
restartApp()
|
||||||
|
}
|
||||||
|
.setNegativeButton(
|
||||||
|
getString(R.string.setting_message_layout_dialog_button2),
|
||||||
|
null
|
||||||
|
)
|
||||||
|
.show()
|
||||||
|
true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun restartApp() {
|
||||||
|
val packageManager = requireContext().packageManager
|
||||||
|
val packageName = requireContext().packageName
|
||||||
|
val intent = packageManager.getLaunchIntentForPackage(packageName)
|
||||||
|
val componentName = intent!!.component
|
||||||
|
val mainIntent = Intent.makeRestartActivityTask(componentName)
|
||||||
|
startActivity(mainIntent)
|
||||||
|
Runtime.getRuntime().exit(0)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,27 +0,0 @@
|
|||||||
package com.github.gotify.settings;
|
|
||||||
|
|
||||||
import android.content.Context;
|
|
||||||
import android.os.Build;
|
|
||||||
import androidx.appcompat.app.AppCompatDelegate;
|
|
||||||
import com.github.gotify.R;
|
|
||||||
|
|
||||||
public final class ThemeHelper {
|
|
||||||
private ThemeHelper() {}
|
|
||||||
|
|
||||||
public static void setTheme(Context context, String newTheme) {
|
|
||||||
AppCompatDelegate.setDefaultNightMode(ofKey(context, newTheme));
|
|
||||||
}
|
|
||||||
|
|
||||||
private static int ofKey(Context context, String newTheme) {
|
|
||||||
if (context.getString(R.string.theme_dark).equals(newTheme)) {
|
|
||||||
return AppCompatDelegate.MODE_NIGHT_YES;
|
|
||||||
}
|
|
||||||
if (context.getString(R.string.theme_light).equals(newTheme)) {
|
|
||||||
return AppCompatDelegate.MODE_NIGHT_NO;
|
|
||||||
}
|
|
||||||
if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
|
|
||||||
return AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY;
|
|
||||||
}
|
|
||||||
return AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
22
app/src/main/java/com/github/gotify/settings/ThemeHelper.kt
Normal file
22
app/src/main/java/com/github/gotify/settings/ThemeHelper.kt
Normal file
@@ -0,0 +1,22 @@
|
|||||||
|
package com.github.gotify.settings
|
||||||
|
|
||||||
|
import android.content.Context
|
||||||
|
import android.os.Build
|
||||||
|
import androidx.appcompat.app.AppCompatDelegate
|
||||||
|
import com.github.gotify.R
|
||||||
|
|
||||||
|
object ThemeHelper {
|
||||||
|
fun setTheme(context: Context, newTheme: String) {
|
||||||
|
AppCompatDelegate.setDefaultNightMode(ofKey(context, newTheme))
|
||||||
|
}
|
||||||
|
|
||||||
|
private fun ofKey(context: Context, newTheme: String): Int {
|
||||||
|
return if (context.getString(R.string.theme_dark) == newTheme) {
|
||||||
|
AppCompatDelegate.MODE_NIGHT_YES
|
||||||
|
} else if (context.getString(R.string.theme_light) == newTheme) {
|
||||||
|
AppCompatDelegate.MODE_NIGHT_NO
|
||||||
|
} else if (Build.VERSION.SDK_INT <= Build.VERSION_CODES.P) {
|
||||||
|
AppCompatDelegate.MODE_NIGHT_AUTO_BATTERY
|
||||||
|
} else AppCompatDelegate.MODE_NIGHT_FOLLOW_SYSTEM
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -12,7 +12,7 @@
|
|||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
|
|
||||||
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
|
<LinearLayout
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
app:layout_behavior="@string/appbar_scrolling_view_behavior"
|
app:layout_behavior="@string/appbar_scrolling_view_behavior"
|
||||||
android:layout_height="match_parent">
|
android:layout_height="match_parent">
|
||||||
@@ -24,6 +24,7 @@
|
|||||||
</LinearLayout>
|
</LinearLayout>
|
||||||
|
|
||||||
<include
|
<include
|
||||||
|
android:id="@+id/app_bar_drawer"
|
||||||
layout="@layout/app_bar_drawer"
|
layout="@layout/app_bar_drawer"
|
||||||
android:layout_width="match_parent"
|
android:layout_width="match_parent"
|
||||||
android:layout_height="wrap_content" />
|
android:layout_height="wrap_content" />
|
||||||
|
|||||||
Reference in New Issue
Block a user