<commit_before>package org.mtransit.android.commons.provider;

import org.mtransit.android.commons.MTLog;
import org.mtransit.android.commons.PreferenceUtils;
import org.mtransit.android.commons.R;
import org.mtransit.android.commons.TimeUtils;
import org.mtransit.android.commons.receiver.DataChange;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class GTFSCurrentNextProvider implements MTLog.Loggable {

	private static final String LOG_TAG = GTFSCurrentNextProvider.class.getSimpleName();

	@Override
	public String getLogTag() {
		return LOG_TAG;
	}

	@Nullable
	private static Integer nextFirstDepartureInSec = null;

	/**
	 * Override if multiple {@link GTFSProvider} implementations in same app.
	 */
	@NonNull
	private static Integer getNEXT_FIRST_DEPARTURE_IN_SEC(@NonNull Context context) {
		if (nextFirstDepartureInSec == null) {
			nextFirstDepartureInSec = context.getResources().getInteger(R.integer.next_gtfs_rts_first_departure_in_sec);
		}
		return nextFirstDepartureInSec;
	}

	@Nullable
	private static Integer nextLastDepartureInSec = null;

	/**
	 * Override if multiple {@link GTFSProvider} implementations in same app.
	 */
	@NonNull
	private static Integer getNEXT_LAST_DEPARTURE_IN_SEC(@NonNull Context context) {
		if (nextLastDepartureInSec == null) {
			nextLastDepartureInSec = context.getResources().getInteger(R.integer.next_gtfs_rts_last_departure_in_sec);
		}
		return nextLastDepartureInSec;
	}

	@Nullable
	private static Integer currentFirstDepartureInSec = null;

	/**
	 * Override if multiple {@link GTFSProvider} implementations in same app.
	 */
	@NonNull
	private static Integer getCURRENT_FIRST_DEPARTURE_IN_SEC(@NonNull Context context) {
		if (currentFirstDepartureInSec == null) {
			currentFirstDepartureInSec = context.getResources().getInteger(R.integer.current_gtfs_rts_first_departure_in_sec);
		}
		return currentFirstDepartureInSec;
	}

	@Nullable
	private static Integer currentLastDepartureInSec = null;

	/**
	 * Override if multiple {@link GTFSProvider} implementations in same app.
	 */
	@NonNull
	private static Integer getCURRENT_LAST_DEPARTURE_IN_SEC(@NonNull Context context) {
		if (currentLastDepartureInSec == null) {
			currentLastDepartureInSec = context.getResources().getInteger(R.integer.current_gtfs_rts_last_departure_in_sec);
		}
		return currentLastDepartureInSec;
	}

	public static boolean isCurrentData(@NonNull Context context) {
		return hasCurrentData(context) //
				&& !isNextData(context);
	}

	@Nullable
	private static String currentNextData = null;

	private static final String PREF_KEY_CURRENT_NEXT_DATA = "pGTFSCurrentNextData4";

	private static final String CURRENT_NEXT_DATA_UNKNOWN = "unknown";
	private static final String CURRENT_NEXT_DATA_CURRENT = "current";
	private static final String CURRENT_NEXT_DATA_NEXT = "next";
	private static final String CURRENT_NEXT_DATA_DEFAULT = CURRENT_NEXT_DATA_UNKNOWN;

	@NonNull
	public static String getCurrentNextData(@NonNull Context context) {
		if (currentNextData == null) {
			currentNextData = PreferenceUtils.getPrefLcl(context, PREF_KEY_CURRENT_NEXT_DATA, CURRENT_NEXT_DATA_DEFAULT);
		}
		return currentNextData;
	}

	public static void setCurrentNextData(@NonNull Context context, @NonNull String newCurrentNextData) {
		if (newCurrentNextData.equals(currentNextData)) {
			return; // skip (same value)
		}
		currentNextData = newCurrentNextData;
		PreferenceUtils.savePrefLcl(context, PREF_KEY_CURRENT_NEXT_DATA, newCurrentNextData, false);
	}

	public static boolean isNextData(@NonNull Context context) {
		checkForNextData(context);
		return CURRENT_NEXT_DATA_NEXT.equals(getCurrentNextData(context));
	}

	public static void checkForNextData(@NonNull Context context) {
		boolean isNextDataNew = getCURRENT_LAST_DEPARTURE_IN_SEC(context) < TimeUtils.currentTimeSec(); // now AFTER current last departure
		String newCurrentNextData = isNextDataNew ? CURRENT_NEXT_DATA_NEXT : CURRENT_NEXT_DATA_CURRENT;
		if (CURRENT_NEXT_DATA_UNKNOWN.equals(getCurrentNextData(context))) {
			setCurrentNextData(context, newCurrentNextData); // 1st
		} else if (!getCurrentNextData(context).equals(newCurrentNextData)) {
			setCurrentNextData(context, newCurrentNextData); // 1st
			broadcastNextDataChange(context); // 2nd
		}
	}

	private static void broadcastNextDataChange(@NonNull Context context) {
		GTFSProvider.onCurrentNextDataChange(context);
		GTFSStatusProvider.onCurrentNextDataChange();
		DataChange.broadcastDataChange(context, GTFSProvider.getAUTHORITY(context), context.getPackageName(), true);
	}

	@SuppressWarnings("WeakerAccess")
	public static boolean hasNextData(@NonNull Context context) {
		return getNEXT_FIRST_DEPARTURE_IN_SEC(context) > 0 && getNEXT_LAST_DEPARTURE_IN_SEC(context) > 0;
	}

	public static boolean hasCurrentData(@NonNull Context context) {
		return getCURRENT_FIRST_DEPARTURE_IN_SEC(context) > 0 && getCURRENT_LAST_DEPARTURE_IN_SEC(context) > 0;
	}
}<commit_msg>Fix bug<commit_after>package org.mtransit.android.commons.provider;

import org.mtransit.android.commons.MTLog;
import org.mtransit.android.commons.PreferenceUtils;
import org.mtransit.android.commons.R;
import org.mtransit.android.commons.TimeUtils;
import org.mtransit.android.commons.receiver.DataChange;

import android.content.Context;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;

public class GTFSCurrentNextProvider implements MTLog.Loggable {

	private static final String LOG_TAG = GTFSCurrentNextProvider.class.getSimpleName();

	@Override
	public String getLogTag() {
		return LOG_TAG;
	}

	@Nullable
	private static Integer nextFirstDepartureInSec = null;

	/**
	 * Override if multiple {@link GTFSProvider} implementations in same app.
	 */
	@NonNull
	private static Integer getNEXT_FIRST_DEPARTURE_IN_SEC(@NonNull Context context) {
		MTLog.w(LOG_TAG, "#CurrentNext getNEXT_FIRST_DEPARTURE_IN_SEC()");
		if (nextFirstDepartureInSec == null) {
			nextFirstDepartureInSec = context.getResources().getInteger(R.integer.next_gtfs_rts_first_departure_in_sec);
			MTLog.w(LOG_TAG, "#CurrentNext getNEXT_FIRST_DEPARTURE_IN_SEC() > nextFirstDepartureInSec: '%s'.", nextFirstDepartureInSec);
		}
		return nextFirstDepartureInSec;
	}

	@Nullable
	private static Integer nextLastDepartureInSec = null;

	/**
	 * Override if multiple {@link GTFSProvider} implementations in same app.
	 */
	@NonNull
	private static Integer getNEXT_LAST_DEPARTURE_IN_SEC(@NonNull Context context) {
		MTLog.w(LOG_TAG, "#CurrentNext getNEXT_LAST_DEPARTURE_IN_SEC()");
		if (nextLastDepartureInSec == null) {
			nextLastDepartureInSec = context.getResources().getInteger(R.integer.next_gtfs_rts_last_departure_in_sec);
			MTLog.w(LOG_TAG, "#CurrentNext getNEXT_LAST_DEPARTURE_IN_SEC() > nextLastDepartureInSec: '%s'.", nextLastDepartureInSec);
		}
		return nextLastDepartureInSec;
	}

	@Nullable
	private static Integer currentFirstDepartureInSec = null;

	/**
	 * Override if multiple {@link GTFSProvider} implementations in same app.
	 */
	@NonNull
	private static Integer getCURRENT_FIRST_DEPARTURE_IN_SEC(@NonNull Context context) {
		MTLog.w(LOG_TAG, "#CurrentNext getCURRENT_FIRST_DEPARTURE_IN_SEC()");
		if (currentFirstDepartureInSec == null) {
			currentFirstDepartureInSec = context.getResources().getInteger(R.integer.current_gtfs_rts_first_departure_in_sec);
			MTLog.w(LOG_TAG, "#CurrentNext getCURRENT_FIRST_DEPARTURE_IN_SEC() > currentFirstDepartureInSec: '%s'.", currentFirstDepartureInSec);
		}
		return currentFirstDepartureInSec;
	}

	@Nullable
	private static Integer currentLastDepartureInSec = null;

	/**
	 * Override if multiple {@link GTFSProvider} implementations in same app.
	 */
	@NonNull
	private static Integer getCURRENT_LAST_DEPARTURE_IN_SEC(@NonNull Context context) {
		if (currentLastDepartureInSec == null) {
			currentLastDepartureInSec = context.getResources().getInteger(R.integer.current_gtfs_rts_last_departure_in_sec);
			MTLog.w(LOG_TAG, "#CurrentNext getCURRENT_LAST_DEPARTURE_IN_SEC() > currentLastDepartureInSec: '%s'.", currentLastDepartureInSec);
		}
		return currentLastDepartureInSec;
	}

	public static boolean isCurrentData(@NonNull Context context) {
		return hasCurrentData(context) //
				&& !isNextData(context);
	}

	@Nullable
	private static String currentNextData = null;

	private static final String PREF_KEY_CURRENT_NEXT_DATA = "pGTFSCurrentNextData4";

	private static final String CURRENT_NEXT_DATA_UNKNOWN = "unknown";
	private static final String CURRENT_NEXT_DATA_CURRENT = "current";
	private static final String CURRENT_NEXT_DATA_NEXT = "next";
	private static final String CURRENT_NEXT_DATA_DEFAULT = CURRENT_NEXT_DATA_UNKNOWN;

	@NonNull
	private static String getCurrentNextData(@NonNull Context context) {
		if (currentNextData == null) {
			currentNextData = PreferenceUtils.getPrefLcl(context, PREF_KEY_CURRENT_NEXT_DATA, CURRENT_NEXT_DATA_DEFAULT);
		}
		return currentNextData;
	}

	public static void setCurrentNextData(@NonNull Context context, @NonNull String newCurrentNextData) {
		if (newCurrentNextData.equals(currentNextData)) {
			return; // skip (same value)
		}
		currentNextData = newCurrentNextData;
		PreferenceUtils.savePrefLcl(context, PREF_KEY_CURRENT_NEXT_DATA, newCurrentNextData, false);
	}

	public static boolean isNextData(@NonNull Context context) {
		MTLog.w(LOG_TAG, "#CurrentNext hasNextData()");
		checkForNextData(context);
		return CURRENT_NEXT_DATA_NEXT.equals(getCurrentNextData(context));
	}

	public static void checkForNextData(@NonNull Context context) {
		MTLog.w(LOG_TAG, "#CurrentNext checkForNextData()");
		MTLog.w(LOG_TAG, "#CurrentNext checkForNextData() > getCurrentNextData(): '%s'.", getCurrentNextData(context));
		MTLog.w(LOG_TAG, "#CurrentNext checkForNextData() > nextFirstDepartureInSec: '%s'.", nextFirstDepartureInSec);
		MTLog.w(LOG_TAG, "#CurrentNext checkForNextData() > nextLastDepartureInSec: '%s'.", nextLastDepartureInSec);
		MTLog.w(LOG_TAG, "#CurrentNext checkForNextData() > currentLastDepartureInSec: '%s'.", currentLastDepartureInSec);
		boolean isNextDataNew = getCURRENT_LAST_DEPARTURE_IN_SEC(context) < TimeUtils.currentTimeSec(); // now AFTER current last departure
		MTLog.w(LOG_TAG, "#CurrentNext checkForNextData() > isNextDataNew: '%s'.", isNextDataNew);
		String newCurrentNextData = isNextDataNew ? CURRENT_NEXT_DATA_NEXT : CURRENT_NEXT_DATA_CURRENT;
		if (CURRENT_NEXT_DATA_UNKNOWN.equals(getCurrentNextData(context))) {
			setCurrentNextData(context, newCurrentNextData); // 1st
			MTLog.w(LOG_TAG, "#CurrentNext checkForNextData() > isNextData: %s", getCurrentNextData(context));
		} else if (!getCurrentNextData(context).equals(newCurrentNextData)) {
			setCurrentNextData(context, newCurrentNextData); // 1st
			broadcastNextDataChange(context); // 2nd
		}
	}

	private static void broadcastNextDataChange(@NonNull Context context) {
		MTLog.w(LOG_TAG, "#CurrentNext broadcastNextDataChange()");
		MTLog.w(LOG_TAG, "#CurrentNext broadcastNextDataChange() ...");
		GTFSProvider.onCurrentNextDataChange(context);
		GTFSStatusProvider.onCurrentNextDataChange();
		DataChange.broadcastDataChange(context, GTFSProvider.getAUTHORITY(context), context.getPackageName(), true);
		MTLog.w(LOG_TAG, "#CurrentNext broadcastNextDataChange() ... DONE");
	}

	@SuppressWarnings("WeakerAccess")
	public static boolean hasNextData(@NonNull Context context) {
		MTLog.w(LOG_TAG, "#CurrentNext hasNextData()");
		return getNEXT_FIRST_DEPARTURE_IN_SEC(context) > 0 && getNEXT_LAST_DEPARTURE_IN_SEC(context) > 0;
	}

	public static boolean hasCurrentData(@NonNull Context context) {
		MTLog.w(LOG_TAG, "#CurrentNext hasCurrentData()");
		return getCURRENT_FIRST_DEPARTURE_IN_SEC(context) > 0 && getCURRENT_LAST_DEPARTURE_IN_SEC(context) > 0;
	}
}
{'fraction_non_alphanumeric': 0.09388996873151356, 'fraction_numerical': 0.0013521507648102764, 'mean_word_length': 6.629916183107673, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 21, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '2016513', 'n_tokens_mistral': 3915, 'n_tokens_neox': 3715, 'n_words': 712}