diff --git a/app/src/main/java/net/diffengine/romandigitalclock/AppClass.java b/app/src/main/java/net/diffengine/romandigitalclock/AppClass.java index 44f3986..63098c2 100644 --- a/app/src/main/java/net/diffengine/romandigitalclock/AppClass.java +++ b/app/src/main/java/net/diffengine/romandigitalclock/AppClass.java @@ -1,3 +1,23 @@ +/* + * AppClass.java + * - This file is part of the Android app RomanDigital + * + * Copyright 2024 David Yockey + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + package net.diffengine.romandigitalclock; import android.app.Application; diff --git a/app/src/main/java/net/diffengine/romandigitalclock/SettingsActivity.java b/app/src/main/java/net/diffengine/romandigitalclock/SettingsActivity.java index 16f15fa..5207974 100644 --- a/app/src/main/java/net/diffengine/romandigitalclock/SettingsActivity.java +++ b/app/src/main/java/net/diffengine/romandigitalclock/SettingsActivity.java @@ -26,6 +26,7 @@ import androidx.annotation.NonNull; import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; import androidx.preference.Preference; import androidx.preference.PreferenceFragmentCompat; import androidx.preference.SwitchPreferenceCompat; @@ -39,8 +40,10 @@ protected void onCreate(Bundle savedInstanceState) { if (savedInstanceState == null) { getSupportFragmentManager() .beginTransaction() - .replace(R.id.app_settings_frame, new SettingsFragment()) - .replace(R.id.screen_settings_frame, new ScreenSettingsFragment()) + .setReorderingAllowed(true) + .add(R.id.app_settings_frame, new SettingsFragment()) + .add(R.id.screen_settings_frame, new ScreenSettingsFragment()) + .add(R.id.button_bar_2, new SettingsButtonBarFragment()) .commit(); } ActionBar actionBar = getSupportActionBar(); diff --git a/app/src/main/java/net/diffengine/romandigitalclock/SettingsButtonBarFragment.java b/app/src/main/java/net/diffengine/romandigitalclock/SettingsButtonBarFragment.java new file mode 100644 index 0000000..946d553 --- /dev/null +++ b/app/src/main/java/net/diffengine/romandigitalclock/SettingsButtonBarFragment.java @@ -0,0 +1,114 @@ +/* + * SettingsButtonBarFragment.java + * - This file is part of the Android app RomanDigital + * + * Copyright 2024 David Yockey + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +package net.diffengine.romandigitalclock; + +import static android.app.Activity.RESULT_OK; + +import android.app.Activity; +import android.appwidget.AppWidgetManager; +import android.content.Intent; +import android.content.SharedPreferences; +import android.os.Bundle; + +import androidx.fragment.app.Fragment; +import androidx.preference.PreferenceManager; + +import android.view.LayoutInflater; +import android.view.View; +import android.view.ViewGroup; +import android.widget.Button; + +import java.util.Map; + +public class SettingsButtonBarFragment extends Fragment implements View.OnClickListener { + Activity parentActivity; + Button btnCancel; + Button btnSave; + + Map origprefs; // Storage for backup of original preference values + SharedPreferences prefs; + + public SettingsButtonBarFragment() { + // If the layout isn't provided to the superclass, it's necessary to use a form of + // fragmentTransaction.add or .replace that takes the fragment class rather than an + // instance thereof to instantiate the fragment in an activity's onCreate method. + // Otherwise, onCreateView will never be called. + super(R.layout.fragment_settings_button_bar); + } + + @Override + public void onCreate(Bundle savedInstanceState) { + super.onCreate(savedInstanceState); + + parentActivity = requireActivity(); + prefs = PreferenceManager.getDefaultSharedPreferences(requireContext()); + origprefs = prefs.getAll(); + } + + public void onClick (View v) { + + // Get the appWidgetId if we're in a widget config activity + // + //noinspection ReassignedVariable + int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID; + String cls = parentActivity.getComponentName().getClassName(); + if (cls.equals("net.diffengine.romandigitalclock.TimeDisplayWidgetConfigActivity")) { + TimeDisplayWidgetConfigActivity widgetConfigActivity = (TimeDisplayWidgetConfigActivity) parentActivity; + appWidgetId = widgetConfigActivity.appWidgetId; + } + + if (v == btnCancel) { + // Set preferences back to original values + SharedPreferences.Editor spEditor = prefs.edit(); + for (String key : origprefs.keySet()) { + spEditor.putBoolean(key, (boolean) origprefs.get(key)); + } + spEditor.commit(); + } else if (v == btnSave) { + if (appWidgetId != AppWidgetManager.INVALID_APPWIDGET_ID) { + // Provision of appwidget id in the extra data should prevent crash of some UIs + // (e.g. TouchWiz on old Samsung devices) at activity destruction. + // See https://stackoverflow.com/a/40709721 + Intent result = new Intent().putExtra(AppWidgetManager.EXTRA_APPWIDGET_ID, appWidgetId); + parentActivity.setResult(RESULT_OK, result); + } + } else { + // In case of some shortsighted modification... :) + return; + } + + parentActivity.finish(); + } + + @Override + public View onCreateView(LayoutInflater inflater, ViewGroup container, + Bundle savedInstanceState) { + View v = inflater.inflate(R.layout.fragment_settings_button_bar, container, false); + + btnCancel = v.findViewById(R.id.buttonCancel); + btnCancel.setOnClickListener(this); + + btnSave = v.findViewById(R.id.buttonSave); + btnSave.setOnClickListener(this); + + return v; + } +} \ No newline at end of file diff --git a/app/src/main/java/net/diffengine/romandigitalclock/TimeDisplayWidgetConfigActivity.java b/app/src/main/java/net/diffengine/romandigitalclock/TimeDisplayWidgetConfigActivity.java index 9eb7886..828499f 100644 --- a/app/src/main/java/net/diffengine/romandigitalclock/TimeDisplayWidgetConfigActivity.java +++ b/app/src/main/java/net/diffengine/romandigitalclock/TimeDisplayWidgetConfigActivity.java @@ -1,5 +1,5 @@ /* - * TimeDisplayWidgetConfigActivity + * TimeDisplayWidgetConfigActivity.java * - This file is part of the Android app RomanDigital * * Copyright 2024 David Yockey @@ -22,6 +22,7 @@ import androidx.appcompat.app.ActionBar; import androidx.appcompat.app.AppCompatActivity; +import androidx.fragment.app.Fragment; import androidx.fragment.app.FragmentTransaction; import androidx.preference.PreferenceFragmentCompat; @@ -65,7 +66,8 @@ protected void onCreate(Bundle savedInstanceState) { .beginTransaction() .setReorderingAllowed(true); - fragmentTransaction.replace(R.id.widget_settings, new SettingsActivity.SettingsFragment()).commit(); + fragmentTransaction.add(R.id.widget_settings, new SettingsActivity.SettingsFragment()); + fragmentTransaction.add(R.id.button_bar, new SettingsButtonBarFragment()).commit(); } ActionBar actionBar = getSupportActionBar(); @@ -92,6 +94,7 @@ public boolean onOptionsItemSelected(MenuItem item) { } } + // This method is unused and should be removed at some point public static class SettingsFragment extends PreferenceFragmentCompat { @Override public void onCreatePreferences(Bundle savedInstanceState, String rootKey) { diff --git a/app/src/main/res/layout/activity_time_display_widget_config.xml b/app/src/main/res/layout/activity_time_display_widget_config.xml index bca5d68..931f74e 100644 --- a/app/src/main/res/layout/activity_time_display_widget_config.xml +++ b/app/src/main/res/layout/activity_time_display_widget_config.xml @@ -32,12 +32,14 @@ + app:layout_constraintTop_toTopOf="parent" + app:layout_constraintVertical_bias="0.0" + app:layout_constrainedHeight="true"> + app:layout_constraintTop_toTopOf="parent" /> + + diff --git a/app/src/main/res/layout/fragment_settings_button_bar.xml b/app/src/main/res/layout/fragment_settings_button_bar.xml new file mode 100644 index 0000000..a835243 --- /dev/null +++ b/app/src/main/res/layout/fragment_settings_button_bar.xml @@ -0,0 +1,57 @@ + + + + + + +