Skip to content

Commit

Permalink
Add @nonnull annotations
Browse files Browse the repository at this point in the history
  • Loading branch information
Stypox committed Dec 22, 2023
1 parent fb19270 commit 68c46be
Show file tree
Hide file tree
Showing 4 changed files with 50 additions and 37 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ public CommentRepliesFragment() {
super(UserAction.REQUESTED_COMMENT_REPLIES);
}

public CommentRepliesFragment(final CommentsInfoItem commentsInfoItem) {
public CommentRepliesFragment(@NonNull final CommentsInfoItem commentsInfoItem) {
this();
this.commentsInfoItem = commentsInfoItem;
// setting "" as title since the title will be properly set right after
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
import android.widget.RelativeLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.text.HtmlCompat;
import androidx.fragment.app.FragmentActivity;
Expand Down Expand Up @@ -166,12 +167,12 @@ public void updateFromItem(final InfoItem infoItem,
});
}

private void openCommentAuthor(final CommentsInfoItem item) {
private void openCommentAuthor(@NonNull final CommentsInfoItem item) {
NavigationHelper.openCommentAuthorIfPresent((FragmentActivity) itemBuilder.getContext(),
item);
}

private void openCommentReplies(final CommentsInfoItem item) {
private void openCommentReplies(@NonNull final CommentsInfoItem item) {
NavigationHelper.openCommentRepliesFragment((FragmentActivity) itemBuilder.getContext(),
item);
}
Expand Down
74 changes: 43 additions & 31 deletions app/src/main/java/org/schabi/newpipe/util/Localization.java
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ public static org.schabi.newpipe.extractor.localization.Localization getPreferre
.fromLocale(getPreferredLocale(context));
}

public static ContentCountry getPreferredContentCountry(final Context context) {
public static ContentCountry getPreferredContentCountry(@NonNull final Context context) {
final String contentCountry = PreferenceManager.getDefaultSharedPreferences(context)
.getString(context.getString(R.string.content_country_key),
context.getString(R.string.default_localization_key));
Expand All @@ -95,41 +95,43 @@ public static ContentCountry getPreferredContentCountry(final Context context) {
return new ContentCountry(contentCountry);
}

public static Locale getPreferredLocale(final Context context) {
public static Locale getPreferredLocale(@NonNull final Context context) {
return getLocaleFromPrefs(context, R.string.content_language_key);
}

public static Locale getAppLocale(final Context context) {
public static Locale getAppLocale(@NonNull final Context context) {
return getLocaleFromPrefs(context, R.string.app_language_key);
}

public static String localizeNumber(final Context context, final long number) {
public static String localizeNumber(@NonNull final Context context, final long number) {
return localizeNumber(context, (double) number);
}

public static String localizeNumber(final Context context, final double number) {
public static String localizeNumber(@NonNull final Context context, final double number) {
final NumberFormat nf = NumberFormat.getInstance(getAppLocale(context));
return nf.format(number);
}

public static String formatDate(final OffsetDateTime offsetDateTime, final Context context) {
public static String formatDate(@NonNull final Context context,
@NonNull final OffsetDateTime offsetDateTime) {
return DateTimeFormatter.ofLocalizedDate(FormatStyle.MEDIUM)
.withLocale(getAppLocale(context)).format(offsetDateTime
.atZoneSameInstant(ZoneId.systemDefault()));
}

@SuppressLint("StringFormatInvalid")
public static String localizeUploadDate(final Context context,
final OffsetDateTime offsetDateTime) {
return context.getString(R.string.upload_date_text, formatDate(offsetDateTime, context));
public static String localizeUploadDate(@NonNull final Context context,
@NonNull final OffsetDateTime offsetDateTime) {
return context.getString(R.string.upload_date_text, formatDate(context, offsetDateTime));
}

public static String localizeViewCount(final Context context, final long viewCount) {
public static String localizeViewCount(@NonNull final Context context, final long viewCount) {
return getQuantity(context, R.plurals.views, R.string.no_views, viewCount,
localizeNumber(context, viewCount));
}

public static String localizeStreamCount(final Context context, final long streamCount) {
public static String localizeStreamCount(@NonNull final Context context,
final long streamCount) {
switch ((int) streamCount) {
case (int) ListExtractor.ITEM_COUNT_UNKNOWN:
return "";
Expand All @@ -143,7 +145,8 @@ public static String localizeStreamCount(final Context context, final long strea
}
}

public static String localizeStreamCountMini(final Context context, final long streamCount) {
public static String localizeStreamCountMini(@NonNull final Context context,
final long streamCount) {
switch ((int) streamCount) {
case (int) ListExtractor.ITEM_COUNT_UNKNOWN:
return "";
Expand All @@ -156,12 +159,13 @@ public static String localizeStreamCountMini(final Context context, final long s
}
}

public static String localizeWatchingCount(final Context context, final long watchingCount) {
public static String localizeWatchingCount(@NonNull final Context context,
final long watchingCount) {
return getQuantity(context, R.plurals.watching, R.string.no_one_watching, watchingCount,
localizeNumber(context, watchingCount));
}

public static String shortCount(final Context context, final long count) {
public static String shortCount(@NonNull final Context context, final long count) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
return CompactDecimalFormat.getInstance(getAppLocale(context),
CompactDecimalFormat.CompactStyle.SHORT).format(count);
Expand All @@ -182,37 +186,40 @@ public static String shortCount(final Context context, final long count) {
}
}

public static String listeningCount(final Context context, final long listeningCount) {
public static String listeningCount(@NonNull final Context context, final long listeningCount) {
return getQuantity(context, R.plurals.listening, R.string.no_one_listening, listeningCount,
shortCount(context, listeningCount));
}

public static String shortWatchingCount(final Context context, final long watchingCount) {
public static String shortWatchingCount(@NonNull final Context context,
final long watchingCount) {
return getQuantity(context, R.plurals.watching, R.string.no_one_watching, watchingCount,
shortCount(context, watchingCount));
}

public static String shortViewCount(final Context context, final long viewCount) {
public static String shortViewCount(@NonNull final Context context, final long viewCount) {
return getQuantity(context, R.plurals.views, R.string.no_views, viewCount,
shortCount(context, viewCount));
}

public static String shortSubscriberCount(final Context context, final long subscriberCount) {
public static String shortSubscriberCount(@NonNull final Context context,
final long subscriberCount) {
return getQuantity(context, R.plurals.subscribers, R.string.no_subscribers, subscriberCount,
shortCount(context, subscriberCount));
}

public static String downloadCount(final Context context, final int downloadCount) {
public static String downloadCount(@NonNull final Context context, final int downloadCount) {
return getQuantity(context, R.plurals.download_finished_notification, 0,
downloadCount, shortCount(context, downloadCount));
}

public static String deletedDownloadCount(final Context context, final int deletedCount) {
public static String deletedDownloadCount(@NonNull final Context context,
final int deletedCount) {
return getQuantity(context, R.plurals.deleted_downloads_toast, 0,
deletedCount, shortCount(context, deletedCount));
}

public static String replyCount(final Context context, final int replyCount) {
public static String replyCount(@NonNull final Context context, final int replyCount) {
return getQuantity(context, R.plurals.replies, 0, replyCount,
String.valueOf(replyCount));
}
Expand All @@ -223,7 +230,7 @@ public static String replyCount(final Context context, final int replyCount) {
* @return if {@code likeCount} is smaller than {@code 0}, the string {@code "-"}, otherwise
* the result of calling {@link #shortCount(Context, long)} on the like count
*/
public static String likeCount(final Context context, final int likeCount) {
public static String likeCount(@NonNull final Context context, final int likeCount) {
if (likeCount < 0) {
return "-";
} else {
Expand Down Expand Up @@ -263,7 +270,8 @@ public static String getDurationString(final long duration) {
* @return duration in a human readable string.
*/
@NonNull
public static String localizeDuration(final Context context, final int durationInSecs) {
public static String localizeDuration(@NonNull final Context context,
final int durationInSecs) {
if (durationInSecs < 0) {
throw new IllegalArgumentException("duration can not be negative");
}
Expand Down Expand Up @@ -300,7 +308,7 @@ public static String localizeDuration(final Context context, final int durationI
* @param track an {@link AudioStream} of the track
* @return the localized name of the audio track
*/
public static String audioTrackName(final Context context, final AudioStream track) {
public static String audioTrackName(@NonNull final Context context, final AudioStream track) {
final String name;
if (track.getAudioLocale() != null) {
name = track.getAudioLocale().getDisplayLanguage(getAppLocale(context));
Expand All @@ -320,7 +328,8 @@ public static String audioTrackName(final Context context, final AudioStream tra
}

@Nullable
private static String audioTrackType(final Context context, final AudioTrackType trackType) {
private static String audioTrackType(@NonNull final Context context,
final AudioTrackType trackType) {
switch (trackType) {
case ORIGINAL:
return context.getString(R.string.audio_track_type_original);
Expand All @@ -336,17 +345,17 @@ private static String audioTrackType(final Context context, final AudioTrackType
// Pretty Time
//////////////////////////////////////////////////////////////////////////*/

public static void initPrettyTime(final PrettyTime time) {
public static void initPrettyTime(@NonNull final PrettyTime time) {
prettyTime = time;
// Do not use decades as YouTube doesn't either.
prettyTime.removeUnit(Decade.class);
}

public static PrettyTime resolvePrettyTime(final Context context) {
public static PrettyTime resolvePrettyTime(@NonNull final Context context) {
return new PrettyTime(getAppLocale(context));
}

public static String relativeTime(final OffsetDateTime offsetDateTime) {
public static String relativeTime(@NonNull final OffsetDateTime offsetDateTime) {
return prettyTime.formatUnrounded(offsetDateTime);
}

Expand Down Expand Up @@ -383,7 +392,8 @@ public static void assureCorrectAppLanguage(final Context c) {
res.updateConfiguration(conf, dm);
}

private static Locale getLocaleFromPrefs(final Context context, @StringRes final int prefKey) {
private static Locale getLocaleFromPrefs(@NonNull final Context context,
@StringRes final int prefKey) {
final SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(context);
final String defaultKey = context.getString(R.string.default_localization_key);
final String languageCode = sp.getString(context.getString(prefKey), defaultKey);
Expand All @@ -399,8 +409,10 @@ private static double round(final double value) {
return new BigDecimal(value).setScale(1, RoundingMode.HALF_UP).doubleValue();
}

private static String getQuantity(final Context context, @PluralsRes final int pluralId,
@StringRes final int zeroCaseStringId, final long count,
private static String getQuantity(@NonNull final Context context,
@PluralsRes final int pluralId,
@StringRes final int zeroCaseStringId,
final long count,
final String formattedCount) {
if (count == 0) {
return context.getString(zeroCaseStringId);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -489,7 +489,7 @@ public static void openChannelFragment(@NonNull final Fragment fragment,
* @param comment the comment whose uploader/author will be opened
*/
public static void openCommentAuthorIfPresent(@NonNull final FragmentActivity activity,
final CommentsInfoItem comment) {
@NonNull final CommentsInfoItem comment) {
if (isEmpty(comment.getUploaderUrl())) {
return;
}
Expand All @@ -502,9 +502,9 @@ public static void openCommentAuthorIfPresent(@NonNull final FragmentActivity ac
}

public static void openCommentRepliesFragment(@NonNull final FragmentActivity activity,
final CommentsInfoItem commentsInfoItem) {
@NonNull final CommentsInfoItem comment) {
defaultTransaction(activity.getSupportFragmentManager())
.replace(R.id.fragment_holder, new CommentRepliesFragment(commentsInfoItem),
.replace(R.id.fragment_holder, new CommentRepliesFragment(comment),
CommentRepliesFragment.TAG)
.addToBackStack(CommentRepliesFragment.TAG)
.commit();
Expand Down

0 comments on commit 68c46be

Please sign in to comment.