weaken core parameter types to the actually required type

When possible use CharSequence, Collection and Iterable for method parameters and function parameters instead of more specific types.
This commit is contained in:
Thomas Kuehne
2025-06-14 23:02:34 +02:00
committed by José Rebelo
parent 57f98422cb
commit 2a38a9e9b7
19 changed files with 52 additions and 49 deletions
@@ -2002,7 +2002,7 @@ public class GBApplication extends Application {
editor.apply();
}
public static SharedPreferences getDeviceSpecificSharedPrefs(String deviceIdentifier) {
public static SharedPreferences getDeviceSpecificSharedPrefs(CharSequence deviceIdentifier) {
if (deviceIdentifier == null || deviceIdentifier.isEmpty()) {
return null;
}
@@ -2013,7 +2013,7 @@ public class GBApplication extends Application {
return new DevicePrefs(getDeviceSpecificSharedPrefs(gbDevice.getAddress()), gbDevice);
}
public static void deleteDeviceSpecificSharedPrefs(String deviceIdentifier) {
public static void deleteDeviceSpecificSharedPrefs(CharSequence deviceIdentifier) {
if (deviceIdentifier == null || deviceIdentifier.isEmpty()) {
return;
}
@@ -53,7 +53,6 @@ import nodomain.freeyourgadget.gadgetbridge.impl.GBDevice;
import nodomain.freeyourgadget.gadgetbridge.model.DeviceType;
import nodomain.freeyourgadget.gadgetbridge.model.GenericItem;
import nodomain.freeyourgadget.gadgetbridge.model.ItemWithDetails;
import nodomain.freeyourgadget.gadgetbridge.util.DeviceHelper;
import nodomain.freeyourgadget.gadgetbridge.util.GB;
@@ -151,7 +150,7 @@ public class FwAppInstallerActivity extends AbstractGBActivity implements Instal
progressBar.setProgress(progress);
}
public void setProgressText(String text) {
public void setProgressText(CharSequence text) {
progressText.setVisibility(View.VISIBLE);
progressText.setText(text);
}
@@ -56,6 +56,7 @@ import org.slf4j.LoggerFactory;
import java.io.File;
import java.io.IOException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
@@ -261,7 +262,7 @@ public abstract class AbstractAppManagerFragment extends Fragment {
}
};
protected Map<UUID, GBDeviceApp> getCachedAppsMap(final List<UUID> uuids) {
protected Map<UUID, GBDeviceApp> getCachedAppsMap(final Collection<UUID> uuids) {
final List<GBDeviceApp> cachedApps = getCachedApps(uuids);
final Map<UUID, GBDeviceApp> cachedAppsMap = new HashMap<>();
for (GBDeviceApp cachedApp : cachedApps) {
@@ -270,7 +271,7 @@ public abstract class AbstractAppManagerFragment extends Fragment {
return cachedAppsMap;
}
protected List<GBDeviceApp> getCachedApps(List<UUID> uuids) {
protected List<GBDeviceApp> getCachedApps(Collection<UUID> uuids) {
List<GBDeviceApp> cachedAppList = new ArrayList<>();
File cachePath;
try {
@@ -172,7 +172,7 @@ public class AppManagerActivity extends AbstractGBFragmentActivity {
}
static synchronized void rewriteAppOrderFile(String filename, List<UUID> uuids) {
static synchronized void rewriteAppOrderFile(String filename, Iterable<UUID> uuids) {
try (BufferedWriter out = new BufferedWriter(new FileWriter(FileUtils.getExternalFilesDir() + "/" + filename))) {
for (UUID uuid : uuids) {
out.write(uuid.toString());
@@ -21,7 +21,6 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.HashMap;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityAmount;
import nodomain.freeyourgadget.gadgetbridge.model.ActivityAmounts;
@@ -36,7 +35,7 @@ public class ActivityAnalysis {
// max speed determined from samples
private int maxSpeed = 0;
public ActivityAmounts calculateActivityAmounts(List<? extends ActivitySample> samples) {
public ActivityAmounts calculateActivityAmounts(Iterable<? extends ActivitySample> samples) {
ActivityAmount deepSleep = new ActivityAmount(ActivityKind.DEEP_SLEEP);
ActivityAmount lightSleep = new ActivityAmount(ActivityKind.LIGHT_SLEEP);
ActivityAmount remSleep = new ActivityAmount(ActivityKind.REM_SLEEP);
@@ -144,7 +143,7 @@ public class ActivityAnalysis {
return result;
}
int calculateTotalSteps(List<? extends ActivitySample> samples) {
int calculateTotalSteps(Iterable<? extends ActivitySample> samples) {
int totalSteps = 0;
for (ActivitySample sample : samples) {
int steps = sample.getSteps();
@@ -41,7 +41,7 @@ public class ShowDurationDialog extends Dialog {
setDuration(mDuration);
}
public void setDuration(final String duration) {
public void setDuration(final CharSequence duration) {
if (mDuration != null) {
durationLabel.setText(duration);
} else {
@@ -28,7 +28,7 @@ public class SleepAnalysis {
public static final long MIN_SESSION_LENGTH = 5 * 60;
public static final long MAX_WAKE_PHASE_LENGTH = 2 * 60 * 60;
public List<SleepSession> calculateSleepSessions(List<? extends ActivitySample> samples) {
public List<SleepSession> calculateSleepSessions(Iterable<? extends ActivitySample> samples) {
List<SleepSession> result = new ArrayList<>();
ActivitySample previousSample = null;
@@ -20,6 +20,7 @@ import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Date;
import java.util.List;
@@ -180,7 +181,7 @@ public class StepAnalysis {
return result;
}
public ActivitySession calculateSummary(List<ActivitySession> sessions, boolean empty) {
public ActivitySession calculateSummary(Collection<ActivitySession> sessions, boolean empty) {
Date startTime = null;
Date endTime = null;
@@ -221,7 +222,7 @@ public class StepAnalysis {
return stepSessionSummary;
}
public ActivitySession getOngoingSessions(List<ActivitySession> sessions) {
public ActivitySession getOngoingSessions(Iterable<ActivitySession> sessions) {
for (ActivitySession session : sessions) {
if (session.getSessionType() == ActivitySession.SESSION_ONGOING) {
@@ -231,7 +232,7 @@ public class StepAnalysis {
return null;
}
private int calculateSumOfInts(List<Integer> samples) {
private int calculateSumOfInts(Iterable<Integer> samples) {
int result = 0;
for (Integer sample : samples) {
result += sample;
@@ -77,7 +77,7 @@ abstract class StressFragment<D extends ChartsData> extends AbstractChartFragmen
return legendEntries;
}
protected Map<StressType, Integer> calculateStressTotals(List<? extends StressSample> samples, int[] stressRanges, int sampleRate) {
protected Map<StressType, Integer> calculateStressTotals(Iterable<? extends StressSample> samples, int[] stressRanges, int sampleRate) {
Map<StressType, Integer> result = new HashMap<>();
for (StressType type : StressType.values()) {
result.put(type, 0);
@@ -101,7 +101,7 @@ abstract class StressFragment<D extends ChartsData> extends AbstractChartFragmen
return result;
}
protected int calculateAverageStress(List<? extends StressSample> samples) {
protected int calculateAverageStress(Iterable<? extends StressSample> samples) {
long sum = 0;
int count = 0;
@@ -48,28 +48,28 @@ public final class DeviceSettingsUtils {
/**
* Returns the preference key where to save the list of possible value for a preference, comma-separated.
*/
public static String getPrefPossibleValuesKey(final String key) {
public static String getPrefPossibleValuesKey(final CharSequence key) {
return String.format(Locale.ROOT, "%s_possible_values", key);
}
/**
* Returns the preference key where to save the list of entry labels for a preference, comma-separated.
*/
public static String getPrefPossibleValueLabelsKey(final String key) {
public static String getPrefPossibleValueLabelsKey(final CharSequence key) {
return String.format(Locale.ROOT, "%s_possible_value_labels", key);
}
/**
* Returns the preference key where to that a config was reported as supported (boolean).
*/
public static String getPrefKnownConfig(final String key) {
public static String getPrefKnownConfig(final CharSequence key) {
return String.format(Locale.ROOT, "%s_is_known", key);
}
/**
* Populates a list preference, or hides it if no known supported values are known.
*/
public static void populateOrHideListPreference(final String prefKey,
public static void populateOrHideListPreference(final CharSequence prefKey,
final DeviceSpecificSettingsHandler handler,
final Prefs prefs) {
final Preference pref = handler.findPreference(prefKey);
@@ -156,7 +156,7 @@ public final class DeviceSettingsUtils {
*/
public static void hidePrefIfNoneVisible(final DeviceSpecificSettingsHandler handler,
final String prefToHide,
final List<String> subPrefs) {
final Iterable<String> subPrefs) {
final Preference pref = handler.findPreference(prefToHide);
if (pref == null) {
return;
@@ -26,6 +26,7 @@ import androidx.recyclerview.widget.RecyclerView;
import java.util.ArrayList;
import java.util.BitSet;
import java.util.Collection;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.R;
@@ -126,7 +127,7 @@ public abstract class AbstractActivityListingAdapter<T> extends RecyclerView.Ada
public void loadItems() {
}
public void setItems(List<T> items, boolean notify) {
public void setItems(Collection<T> items, boolean notify) {
this.items.clear();
this.items.addAll(items);
this.selectedItems.clear();
@@ -29,6 +29,7 @@ import android.widget.TextView;
import androidx.annotation.DrawableRes;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.R;
@@ -128,7 +129,7 @@ public abstract class AbstractItemAdapter<T> extends ArrayAdapter<T> {
public void loadItems() {
}
public void setItems(List<T> items, boolean notify) {
public void setItems(Collection<T> items, boolean notify) {
this.items.clear();
this.items.addAll(items);
if (notify) {
@@ -136,7 +136,7 @@ public class GPXExporter implements ActivityTrackExporter {
ser.endTag(NS_GPX_URI, "trk");
}
private boolean exportTrackPoint(XmlSerializer ser, ActivityPoint point, List<ActivityPoint> trackPoints) throws IOException {
private boolean exportTrackPoint(XmlSerializer ser, ActivityPoint point, Iterable<ActivityPoint> trackPoints) throws IOException {
GPSCoordinate location = point.getLocation();
if (location == null) {
return false; // skip invalid points, that just contain hr data, for example
@@ -171,7 +171,7 @@ public class GPXExporter implements ActivityTrackExporter {
return true;
}
private void exportTrackpointExtensions(XmlSerializer ser, ActivityPoint point, List<ActivityPoint> trackPoints) throws IOException {
private void exportTrackpointExtensions(XmlSerializer ser, ActivityPoint point, Iterable<ActivityPoint> trackPoints) throws IOException {
if (!includeHeartRate) {
return;
}
@@ -211,7 +211,7 @@ public class GPXExporter implements ActivityTrackExporter {
ser.endTag(NS_GPX_URI, "extensions");
}
private @Nullable ActivityPoint findClosestSensibleActivityPoint(Date time, List<ActivityPoint> trackPoints) {
private @Nullable ActivityPoint findClosestSensibleActivityPoint(Date time, Iterable<ActivityPoint> trackPoints) {
ActivityPoint closestPointItem = null;
HeartRateUtils heartRateUtilsInstance = HeartRateUtils.getInstance();
@@ -145,7 +145,7 @@ public class CalendarReceiver extends ContentObserver {
syncCalendar(eventList);
}
public void syncCalendar(List<CalendarEvent> eventList) {
public void syncCalendar(Iterable<CalendarEvent> eventList) {
try (DBHandler dbHandler = GBApplication.acquireDB()) {
DaoSession session = dbHandler.getDaoSession();
syncCalendar(eventList, session);
@@ -154,7 +154,7 @@ public class CalendarReceiver extends ContentObserver {
}
}
public void syncCalendar(List<CalendarEvent> eventList, DaoSession session) {
public void syncCalendar(Iterable<CalendarEvent> eventList, DaoSession session) {
LOG.info("Syncing with calendar.");
Hashtable<Long, CalendarEvent> eventTable = new Hashtable<>();
Long deviceId = DBHelper.getDevice(mGBDevice, session).getId();
@@ -35,6 +35,7 @@ import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.ObjectStreamException;
import java.util.ArrayList;
import java.util.Collection;
import java.util.List;
import nodomain.freeyourgadget.gadgetbridge.R;
@@ -65,7 +66,7 @@ public class Weather {
return weatherSpecs;
}
public void setWeatherSpec(final List<WeatherSpec> newWeatherSpecs) {
public void setWeatherSpec(final Collection<WeatherSpec> newWeatherSpecs) {
weatherSpecs.clear();
weatherSpecs.addAll(newWeatherSpecs);
saveToCache();
@@ -180,7 +180,7 @@ public class BLEScanService extends Service {
);
}
private void updateNotification(String content) {
private void updateNotification(CharSequence content) {
notificationManager.notify(
GB.NOTIFICATION_ID_SCAN,
createNotification(content, R.drawable.ic_bluetooth)
@@ -204,7 +204,7 @@ public class BLEScanService extends Service {
return createNotification(content, icon);
}
private Notification createNotification(String content, int icon) {
private Notification createNotification(CharSequence content, int icon) {
return new NotificationCompat
.Builder(this, GB.NOTIFICATION_CHANNEL_ID_SCAN_SERVICE)
@@ -307,7 +307,7 @@ public class GB {
return sb.toString();
}
public static Notification createNotification(String text, Context context) {
public static Notification createNotification(CharSequence text, Context context) {
NotificationCompat.Builder builder = new NotificationCompat.Builder(context, NOTIFICATION_CHANNEL_ID_CONNECTION_STATUS);
builder.setTicker(text)
.setContentText(text)
@@ -389,7 +389,7 @@ public class GB {
/**
* https://stackoverflow.com/a/140861/4636860
*/
public static byte[] hexStringToByteArray(String s) {
public static byte[] hexStringToByteArray(CharSequence s) {
int len = s.length();
byte[] data = new byte[len / 2];
for (int i = 0; i < len; i += 2) {
@@ -505,7 +505,7 @@ public class GB {
}
}
private static Notification createTransferNotification(String title, String text, boolean ongoing,
private static Notification createTransferNotification(CharSequence title, CharSequence text, boolean ongoing,
int percentage, Context context) {
Intent notificationIntent = new Intent(context, ControlCenterv2.class);
notificationIntent.setPackage(BuildConfig.APPLICATION_ID);
@@ -535,7 +535,7 @@ public class GB {
return nb.build();
}
public static void updateTransferNotification(String title, String text, boolean ongoing, int percentage, Context context) {
public static void updateTransferNotification(CharSequence title, CharSequence text, boolean ongoing, int percentage, Context context) {
if (percentage == 100) {
removeNotification(NOTIFICATION_ID_TRANSFER, context);
} else {
@@ -544,7 +544,7 @@ public class GB {
}
}
private static Notification createInstallNotification(String text, boolean ongoing,
private static Notification createInstallNotification(CharSequence text, boolean ongoing,
int percentage, Context context) {
Intent notificationIntent = new Intent(context, ControlCenterv2.class);
notificationIntent.setPackage(BuildConfig.APPLICATION_ID);
@@ -571,12 +571,12 @@ public class GB {
return nb.build();
}
public static void updateInstallNotification(String text, boolean ongoing, int percentage, Context context) {
public static void updateInstallNotification(CharSequence text, boolean ongoing, int percentage, Context context) {
Notification notification = createInstallNotification(text, ongoing, percentage, context);
notify(NOTIFICATION_ID_INSTALL, notification, context);
}
private static Notification createBatteryLowNotification(String text, String bigText, Context context) {
private static Notification createBatteryLowNotification(CharSequence text, CharSequence bigText, Context context) {
Intent notificationIntent = new Intent(context, ControlCenterv2.class);
notificationIntent.setPackage(BuildConfig.APPLICATION_ID);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
@@ -599,7 +599,7 @@ public class GB {
return nb.build();
}
private static Notification createBatteryFullNotification(String text, String bigText, Context context) {
private static Notification createBatteryFullNotification(CharSequence text, CharSequence bigText, Context context) {
Intent notificationIntent = new Intent(context, ControlCenterv2.class);
notificationIntent.setPackage(BuildConfig.APPLICATION_ID);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
@@ -622,7 +622,7 @@ public class GB {
return nb.build();
}
public static void updateBatteryLowNotification(String text, String bigText, Context context) {
public static void updateBatteryLowNotification(CharSequence text, CharSequence bigText, Context context) {
if (GBEnvironment.env().isLocalTest()) {
return;
}
@@ -634,7 +634,7 @@ public class GB {
removeNotification(NOTIFICATION_ID_LOW_BATTERY, context);
}
public static void updateBatteryFullNotification(String text, String bigText, Context context) {
public static void updateBatteryFullNotification(CharSequence text, CharSequence bigText, Context context) {
if (GBEnvironment.env().isLocalTest()) {
return;
}
@@ -646,7 +646,7 @@ public class GB {
removeNotification(NOTIFICATION_ID_FULL_BATTERY, context);
}
public static Notification createExportFailedNotification(String text, Context context) {
public static Notification createExportFailedNotification(CharSequence text, Context context) {
Intent notificationIntent = new Intent(context, SettingsActivity.class);
notificationIntent.setPackage(BuildConfig.APPLICATION_ID);
notificationIntent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK
@@ -665,7 +665,7 @@ public class GB {
return nb.build();
}
public static void updateExportFailedNotification(String text, Context context) {
public static void updateExportFailedNotification(CharSequence text, Context context) {
if (GBEnvironment.env().isLocalTest()) {
return;
}
@@ -48,14 +48,14 @@ public class GBTextToSpeech {
});
}
public void speak(String text) {
public void speak(CharSequence text) {
Bundle params = new Bundle();
// Put the audio stream type into the Bundle
params.putInt(TextToSpeech.Engine.KEY_PARAM_STREAM, AudioManager.STREAM_RING);
textToSpeech.speak(text, TextToSpeech.QUEUE_FLUSH, params, "call");
}
public void speakNotification(String text) {
public void speakNotification(CharSequence text) {
int result = audioManager.requestAudioFocus(null, AudioManager.STREAM_MUSIC, this.audioFocus);
if (AudioManager.AUDIOFOCUS_REQUEST_GRANTED != result)
LOG.warn("AudioManager did not grant us the requested focus");
@@ -59,7 +59,7 @@ public class SilentMode {
}
}
public static void setPhoneSilentMode(final String deviceAddress, final boolean enabled) {
public static void setPhoneSilentMode(final CharSequence deviceAddress, final boolean enabled) {
final RingerMode[] phoneSilentMode = getPhoneSilentMode(deviceAddress);
final RingerMode ringerMode = phoneSilentMode[enabled ? 1 : 0];
@@ -68,7 +68,7 @@ public class SilentMode {
setRingerMode(ringerMode);
}
public static boolean isPhoneInSilenceMode(final String deviceAddress) {
public static boolean isPhoneInSilenceMode(final CharSequence deviceAddress) {
final AudioManager audioManager = (AudioManager) getContext().getSystemService(Context.AUDIO_SERVICE);
final RingerMode currentRingerMode = RingerMode.fromCode(audioManager.getRingerMode());
final RingerMode[] phoneSilentMode = getPhoneSilentMode(deviceAddress);
@@ -77,7 +77,7 @@ public class SilentMode {
return currentRingerMode.getCode() < phoneSilentMode[0].getCode();
}
public static RingerMode[] getPhoneSilentMode(final String deviceAddress) {
public static RingerMode[] getPhoneSilentMode(final CharSequence deviceAddress) {
final Prefs prefs = new Prefs(GBApplication.getDeviceSpecificSharedPrefs(deviceAddress));
final String phoneSilentModePref = prefs.getString(DeviceSettingsPreferenceConst.PREF_PHONE_SILENT_MODE, "normal_silent").toUpperCase(Locale.ROOT);
final String[] prefSplit = phoneSilentModePref.split("_");