Convert to java

This commit is contained in:
Thomas 2022-12-13 10:48:06 +01:00
parent 5f977d0422
commit 770eef7629
5 changed files with 235 additions and 194 deletions

View file

@ -0,0 +1,57 @@
package app.fedilab.android.activities;
/* Copyright 2022 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import static androidx.navigation.ui.NavigationUI.setupActionBarWithNavController;
import android.os.Bundle;
import android.view.MenuItem;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.navigation.ui.AppBarConfiguration;
import app.fedilab.android.R;
import app.fedilab.android.databinding.ActivitySettingsBinding;
public class SettingsActivity extends BaseBarActivity {
@Override
protected void onCreate(@Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
app.fedilab.android.databinding.ActivitySettingsBinding binding = ActivitySettingsBinding.inflate(getLayoutInflater());
setContentView(binding.getRoot());
NavController navController = Navigation.findNavController(this, R.id.fragment_container);
AppBarConfiguration appBarConfiguration = new AppBarConfiguration.Builder().build();
setupActionBarWithNavController(this, navController, appBarConfiguration);
}
@Override
public boolean onSupportNavigateUp() {
NavController navController = Navigation.findNavController(this, R.id.fragment_container);
return navController.navigateUp() || super.onSupportNavigateUp();
}
@Override
public boolean onOptionsItemSelected(@NonNull MenuItem item) {
NavController navController = Navigation.findNavController(this, R.id.fragment_container);
if (item.getItemId() == android.R.id.home && navController.getCurrentDestination() != null && navController.getCurrentDestination().getId() == R.id.FragmentSettingsCategories) {
finish();
}
return super.onOptionsItemSelected(item);
}
}

View file

@ -1,56 +0,0 @@
package app.fedilab.android.activities
/* Copyright 2022 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import android.os.Bundle
import android.view.MenuItem
import androidx.navigation.findNavController
import androidx.navigation.ui.AppBarConfiguration
import androidx.navigation.ui.navigateUp
import androidx.navigation.ui.setupActionBarWithNavController
import app.fedilab.android.R
import app.fedilab.android.databinding.ActivitySettingsBinding
class SettingsActivity : BaseBarActivity() {
private lateinit var binding: ActivitySettingsBinding
private lateinit var appBarConfiguration: AppBarConfiguration
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
binding = ActivitySettingsBinding.inflate(layoutInflater)
setContentView(binding.root)
val navController = findNavController(R.id.fragment_container)
appBarConfiguration = AppBarConfiguration.Builder().build()
setupActionBarWithNavController(navController, appBarConfiguration)
}
override fun onSupportNavigateUp(): Boolean {
val navController = findNavController(R.id.fragment_container)
return navController.navigateUp(appBarConfiguration) || super.onSupportNavigateUp()
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
val navController = findNavController(R.id.fragment_container)
if (item.itemId == android.R.id.home && navController.currentDestination?.id == R.id.FragmentSettingsCategories) {
finish()
}
return super.onOptionsItemSelected(item)
}
}

View file

@ -0,0 +1,176 @@
package app.fedilab.android.ui.fragment.settings;
/* Copyright 2022 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import android.Manifest;
import android.app.Activity;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.os.Bundle;
import androidx.activity.result.ActivityResultLauncher;
import androidx.activity.result.contract.ActivityResultContracts;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.app.ActivityCompat;
import androidx.navigation.NavController;
import androidx.navigation.Navigation;
import androidx.preference.Preference;
import androidx.preference.PreferenceFragmentCompat;
import app.fedilab.android.R;
import app.fedilab.android.helper.SettingsStorage;
import es.dmoral.toasty.Toasty;
public class FragmentSettingsCategories extends PreferenceFragmentCompat {
private static final int REQUEST_CODE = 5412;
private static final int PICKUP_FILE = 452;
@Override
public void onCreatePreferences(@Nullable Bundle savedInstanceState, @Nullable String rootKey) {
addPreferencesFromResource(R.xml.pref_categories);
Preference pref_category_key_account = findPreference(getString(R.string.pref_category_key_account));
if (pref_category_key_account != null) {
pref_category_key_account.setOnPreferenceClickListener(preference -> {
NavController navController = Navigation.findNavController(requireActivity(), R.id.fragment_container);
navController.navigate(FragmentSettingsCategoriesDirections.Companion.categoriesToAccount());
return false;
});
}
Preference pref_category_key_timeline = findPreference(getString(R.string.pref_category_key_timeline));
if (pref_category_key_timeline != null) {
pref_category_key_timeline.setOnPreferenceClickListener(preference -> {
NavController navController = Navigation.findNavController(requireActivity(), R.id.fragment_container);
navController.navigate(FragmentSettingsCategoriesDirections.Companion.categoriesToTimelines());
return false;
});
}
Preference pref_category_key_notifications = findPreference(getString(R.string.pref_category_key_notifications));
if (pref_category_key_notifications != null) {
pref_category_key_notifications.setOnPreferenceClickListener(preference -> {
NavController navController = Navigation.findNavController(requireActivity(), R.id.fragment_container);
navController.navigate(FragmentSettingsCategoriesDirections.Companion.categoriesToNotifications());
return false;
});
}
Preference pref_category_key_interface = findPreference(getString(R.string.pref_category_key_interface));
if (pref_category_key_interface != null) {
pref_category_key_interface.setOnPreferenceClickListener(preference -> {
NavController navController = Navigation.findNavController(requireActivity(), R.id.fragment_container);
navController.navigate(FragmentSettingsCategoriesDirections.Companion.categoriesToInterface());
return false;
});
}
Preference pref_category_key_compose = findPreference(getString(R.string.pref_category_key_compose));
if (pref_category_key_compose != null) {
pref_category_key_compose.setOnPreferenceClickListener(preference -> {
NavController navController = Navigation.findNavController(requireActivity(), R.id.fragment_container);
navController.navigate(FragmentSettingsCategoriesDirections.Companion.categoriesToCompose());
return false;
});
}
Preference pref_category_key_languages = findPreference(getString(R.string.pref_category_key_languages));
if (pref_category_key_languages != null) {
pref_category_key_languages.setOnPreferenceClickListener(preference -> {
NavController navController = Navigation.findNavController(requireActivity(), R.id.fragment_container);
navController.navigate(FragmentSettingsCategoriesDirections.Companion.categoriesToLanguage());
return false;
});
}
Preference pref_category_key_privacy = findPreference(getString(R.string.pref_category_key_privacy));
if (pref_category_key_privacy != null) {
pref_category_key_privacy.setOnPreferenceClickListener(preference -> {
NavController navController = Navigation.findNavController(requireActivity(), R.id.fragment_container);
navController.navigate(FragmentSettingsCategoriesDirections.Companion.categoriesToPrivacy());
return false;
});
}
Preference pref_category_key_theming = findPreference(getString(R.string.pref_category_key_theming));
if (pref_category_key_theming != null) {
pref_category_key_theming.setOnPreferenceClickListener(preference -> {
NavController navController = Navigation.findNavController(requireActivity(), R.id.fragment_container);
navController.navigate(FragmentSettingsCategoriesDirections.Companion.categoriesToTheming());
return false;
});
}
ActivityResultLauncher<String> permissionLauncher = registerForActivityResult(new ActivityResultContracts.RequestPermission(), isGranted -> {
if (isGranted) {
SettingsStorage.saveSharedPreferencesToFile(requireActivity());
} else {
ActivityCompat.requestPermissions(requireActivity(), new String[]{Manifest.permission.WRITE_EXTERNAL_STORAGE}, REQUEST_CODE);
}
});
Preference pref_export_settings = findPreference(getString(R.string.pref_export_settings));
if (pref_export_settings != null) {
pref_export_settings.setOnPreferenceClickListener(preference -> {
permissionLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE);
return false;
});
}
Preference pref_import_settings = findPreference(getString(R.string.pref_import_settings));
if (pref_import_settings != null) {
pref_import_settings.setOnPreferenceClickListener(preference -> {
Intent openFileIntent = new Intent(Intent.ACTION_OPEN_DOCUMENT);
openFileIntent.addCategory(Intent.CATEGORY_OPENABLE);
openFileIntent.setType("text/plain");
String[] mimeTypes = new String[]{"text/plain"};
openFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes);
startActivityForResult(
Intent.createChooser(
openFileIntent,
getString(R.string.load_settings)), PICKUP_FILE);
return false;
});
}
}
@SuppressWarnings("deprecation")
@Override
public void onActivityResult(int requestCode, int resultCode, @Nullable Intent data) {
if (resultCode == Activity.RESULT_OK && requestCode == PICKUP_FILE) {
boolean result = data != null && SettingsStorage.loadSharedPreferencesFromFile(requireActivity(), data.getData());
if (result) {
Toasty.success(requireActivity(), getString(R.string.data_import_settings_success), Toasty.LENGTH_LONG).show();
} else {
Toasty.error(requireActivity(), getString(R.string.toast_error), Toasty.LENGTH_LONG).show();
}
}
}
@SuppressWarnings("deprecation")
@Override
public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
if (requestCode == REQUEST_CODE) {
if (grantResults.length > 0
&& grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SettingsStorage.saveSharedPreferencesToFile(requireActivity());
} else {
Toasty.error(requireActivity(), getString(R.string.permission_missing), Toasty.LENGTH_SHORT).show();
}
}
}
}

View file

@ -1,138 +0,0 @@
package app.fedilab.android.ui.fragment.settings
/* Copyright 2022 Thomas Schneider
*
* This file is a part of Fedilab
*
* This program is free software; you can redistribute it and/or modify it under the terms of the
* GNU General Public License as published by the Free Software Foundation; either version 3 of the
* License, or (at your option) any later version.
*
* Fedilab is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even
* the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU General
* Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Fedilab; if not,
* see <http://www.gnu.org/licenses>. */
import android.Manifest
import android.app.Activity
import android.content.Intent
import android.content.pm.PackageManager
import android.os.Bundle
import android.widget.Toast
import androidx.activity.result.contract.ActivityResultContracts
import androidx.navigation.fragment.findNavController
import androidx.preference.Preference
import androidx.preference.PreferenceFragmentCompat
import app.fedilab.android.BaseMainActivity.currentAccount
import app.fedilab.android.R
import app.fedilab.android.helper.SettingsStorage
import es.dmoral.toasty.Toasty
class FragmentSettingsCategories : PreferenceFragmentCompat() {
private val REQUEST_CODE = 5412
private val PICKUP_FILE = 452
override fun onCreatePreferences(savedInstanceState: Bundle?, rootKey: String?) {
setPreferencesFromResource(R.xml.pref_categories, rootKey)
findPreference<Preference>(getString(R.string.pref_category_key_account))?.setOnPreferenceClickListener {
findNavController().navigate(FragmentSettingsCategoriesDirections.categoriesToAccount())
false
}
findPreference<Preference>(getString(R.string.pref_category_key_timeline))?.setOnPreferenceClickListener {
findNavController().navigate(FragmentSettingsCategoriesDirections.categoriesToTimelines())
false
}
findPreference<Preference>(getString(R.string.pref_category_key_notifications))?.setOnPreferenceClickListener {
findNavController().navigate(FragmentSettingsCategoriesDirections.categoriesToNotifications())
false
}
findPreference<Preference>(getString(R.string.pref_category_key_interface))?.setOnPreferenceClickListener {
findNavController().navigate(FragmentSettingsCategoriesDirections.categoriesToInterface())
false
}
findPreference<Preference>(getString(R.string.pref_category_key_compose))?.setOnPreferenceClickListener {
findNavController().navigate(FragmentSettingsCategoriesDirections.categoriesToCompose())
false
}
findPreference<Preference>(getString(R.string.pref_category_key_privacy))?.setOnPreferenceClickListener {
findNavController().navigate(FragmentSettingsCategoriesDirections.categoriesToPrivacy())
false
}
findPreference<Preference>(getString(R.string.pref_category_key_theming))?.setOnPreferenceClickListener {
findNavController().navigate(FragmentSettingsCategoriesDirections.categoriesToTheming())
false
}
@Suppress("DEPRECATION") val permissionLauncher = registerForActivityResult(
ActivityResultContracts.RequestPermission()
) { isGranted ->
if (isGranted) {
SettingsStorage.saveSharedPreferencesToFile(context)
} else {
requestPermissions(arrayOf(Manifest.permission.WRITE_EXTERNAL_STORAGE), REQUEST_CODE)
}
}
findPreference<Preference>(getString(R.string.pref_export_settings))?.setOnPreferenceClickListener {
permissionLauncher.launch(Manifest.permission.WRITE_EXTERNAL_STORAGE)
false
}
findPreference<Preference>(getString(R.string.pref_import_settings))?.setOnPreferenceClickListener {
val openFileIntent = Intent(Intent.ACTION_OPEN_DOCUMENT)
openFileIntent.addCategory(Intent.CATEGORY_OPENABLE)
openFileIntent.type = "text/plain"
val mimeTypes = arrayOf("text/plain")
openFileIntent.putExtra(Intent.EXTRA_MIME_TYPES, mimeTypes)
startActivityForResult(
Intent.createChooser(
openFileIntent,
getString(R.string.load_settings)), PICKUP_FILE)
false
}
val adminPreference = findPreference<Preference>(getString(R.string.pref_category_key_administration))
adminPreference?.isVisible = currentAccount != null && currentAccount.admin
adminPreference?.setOnPreferenceClickListener { false }
findPreference<Preference>(getString(R.string.pref_category_key_languages))?.setOnPreferenceClickListener {
findNavController().navigate(FragmentSettingsCategoriesDirections.categoriesToLanguage())
false
}
}
@Deprecated("Deprecated in Java")
override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
if (resultCode == Activity.RESULT_OK && requestCode == PICKUP_FILE) {
val result = SettingsStorage.loadSharedPreferencesFromFile(context, data?.data)
if (result) {
activity?.let { Toasty.success(it, getString(R.string.data_import_settings_success), Toasty.LENGTH_LONG).show() }
} else {
activity?.let { Toasty.error(it, getString(R.string.toast_error), Toasty.LENGTH_LONG).show() }
}
}
}
@Deprecated("Deprecated in Java")
override fun onRequestPermissionsResult(requestCode: Int, permissions: Array<out String>, grantResults: IntArray) {
when (requestCode) {
REQUEST_CODE -> if (grantResults.isNotEmpty() && grantResults[0] == PackageManager.PERMISSION_GRANTED) {
SettingsStorage.saveSharedPreferencesToFile(context)
} else {
Toast.makeText(context, getString(R.string.permission_missing), Toast.LENGTH_SHORT).show()
}
else -> {}
}
}
}

View file

@ -53,12 +53,14 @@
app:icon="@drawable/ic_theming"
app:key="@string/pref_category_key_theming" />
<!--
<Preference
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:title="@string/administration"
app:icon="@drawable/ic_admin"
app:key="@string/pref_category_key_administration" />
-->
<Preference
android:layout_width="match_parent"