Created
September 10, 2016 12:34
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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