Skip to content

Instantly share code, notes, and snippets.

@s0h4m
Created September 10, 2016 12:34
Show Gist options
  • Save s0h4m/2addf1cb4b4e701818bcadd38b657e76 to your computer and use it in GitHub Desktop.
Save s0h4m/2addf1cb4b4e701818bcadd38b657e76 to your computer and use it in GitHub Desktop.
A simple way to determine if the current app version is in alpha or beta using Firebase Remote Config
import android.content.Context;
import android.support.annotation.NonNull;
import com.google.android.gms.tasks.OnCompleteListener;
import com.google.android.gms.tasks.Task;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
public class RemoteConfigManager {
public static final String KEY_STABLE_VERSION = "stable_version";
public interface OnAlphaBetaDeterminedListener {
void onAlphaBetaDetermined(boolean inAlphaBeta);
}
public static void isVersionInAlphaBeta(final Context context, final OnAlphaBetaDeterminedListener onAlphaBetaDeterminedListener) {
FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance();
firebaseRemoteConfig.setDefaults(R.xml.remote_config_defaults);
firebaseRemoteConfig.fetch()
.addOnCompleteListener(new OnCompleteListener<Void>() {
@Override
public void onComplete(@NonNull Task<Void> task) {
firebaseRemoteConfig.activateFetched();
long stableVersion = firebaseRemoteConfig.getLong(KEY_STABLE_VERSION);
if (isInAlphaOrBeta(stableVersion, context)) {
onAlphaBetaDeterminedListener.onAlphaBetaDetermined(true);
} else {
onAlphaBetaDeterminedListener.onAlphaBetaDetermined(false);
}
}
});
}
private static boolean isInAlphaOrBeta(long stableVersion, Context context) {
return getCurrentAppVersion(context.getApplicationContext()) > stableVersion;
}
public static int getCurrentAppVersion(final Context context) {
int versionNumber = 0;
try {
versionNumber = context.getPackageManager().getPackageInfo(context.getPackageName(), 0).versionCode;
} catch (Exception e) {
ExceptionUtils.logException(e);
}
return versionNumber;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment