Skip to content

Commit

Permalink
improve(settings): implement part of btn functions
Browse files Browse the repository at this point in the history
- Implement finishing of the activity for both Cancel and Save buttons.
- If a widget is being configured, set the result returned when the
  activity is finished by clicking Save to RESULT_OK so as to signal
  that widget installation should proceed (if it's being installed).
- Include in the result a data intent including the widget id to prevent
  some UIs (e.g. TouchWiz) from crashing.
  • Loading branch information
dfyockey committed Oct 25, 2024
1 parent af0069b commit 85e62a6
Showing 1 changed file with 31 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@

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.os.Bundle;

import androidx.fragment.app.Fragment;
Expand Down Expand Up @@ -47,13 +52,35 @@ public void onCreate(Bundle savedInstanceState) {
}

public void onClick (View v) {

Activity activity = requireActivity();

// Get the appWidgetId if we're in a widget config activity
//
//noinspection ReassignedVariable
int appWidgetId = AppWidgetManager.INVALID_APPWIDGET_ID;
String cls = activity.getComponentName().getClassName();
if (cls.equals("net.diffengine.romandigitalclock.TimeDisplayWidgetConfigActivity")) {
TimeDisplayWidgetConfigActivity widgetConfigActivity = (TimeDisplayWidgetConfigActivity) activity;
appWidgetId = widgetConfigActivity.appWidgetId;
}

if (v == btnCancel) {
btnCancel.setText("Xyzzy");
btnSave.setText("Reset");
// Add code to reset settings here
} else if (v == btnSave) {
btnCancel.setText("Cancel");
btnSave.setText("Save");
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);
activity.setResult(RESULT_OK, result);
}
} else {
// In case of some utterly stupid modification... :)
return;
}

activity.finish();
}

@Override
Expand Down

0 comments on commit 85e62a6

Please sign in to comment.