Skip to content

Instantly share code, notes, and snippets.

@ithebk
Last active September 24, 2017 14:26
Show Gist options
  • Save ithebk/5fcefa1f14fce6eeab9ccfaaf9955ec8 to your computer and use it in GitHub Desktop.
Save ithebk/5fcefa1f14fce6eeab9ccfaaf9955ec8 to your computer and use it in GitHub Desktop.
Firebase update check provider
package easydo.ithebk.listener;
import android.content.Context;
import android.support.annotation.NonNull;
import com.google.firebase.remoteconfig.FirebaseRemoteConfig;
import easydo.ithebk.BuildConfig;
/**
* Created by bharath on 8/5/17.
*/
public class ForceUpdateChecker {
public static final String KEY_UPDATE_REQUIRED = "force_update_required";
public static final String KEY_APP_VERSION_CODE = "google_play_app_version_code";
public static final String KEY_PLAY_WHATS_NEW_TEXT = "play_whats_new_text";
public static final String PLAY_WHATS_NEW_TEXT_DEFAULT = "Bug fixes";
private static final String TAG = ForceUpdateChecker.class.getSimpleName();
private OnUpdateNeededListener onUpdateNeededListener;
private Context context;
public ForceUpdateChecker(@NonNull Context context,
OnUpdateNeededListener onUpdateNeededListener) {
this.context = context;
this.onUpdateNeededListener = onUpdateNeededListener;
}
public static Builder with(@NonNull Context context) {
return new Builder(context);
}
public void check() {
final FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance();
if (remoteConfig.getBoolean(KEY_UPDATE_REQUIRED)) {
String playStoreVersionConfig = remoteConfig.getString(KEY_APP_VERSION_CODE);
int appVersion = BuildConfig.VERSION_CODE;
String whatsNew = remoteConfig.getString(KEY_PLAY_WHATS_NEW_TEXT);
;
int playStoreVersion;
try {
playStoreVersion = Integer.parseInt(playStoreVersionConfig);
} catch (NumberFormatException e) {
playStoreVersion = 0;
}
System.out.println("playStoreVersion version:" + playStoreVersion);
System.out.println("App version:" + appVersion);
if (playStoreVersion != 0 && playStoreVersion > appVersion
&& onUpdateNeededListener != null) {
onUpdateNeededListener.onUpdateNeeded(whatsNew);
}
}
}
public interface OnUpdateNeededListener {
void onUpdateNeeded(String updateUrl);
}
public static class Builder {
private Context context;
private OnUpdateNeededListener onUpdateNeededListener;
public Builder(Context context) {
this.context = context;
}
public Builder onUpdateNeeded(OnUpdateNeededListener onUpdateNeededListener) {
this.onUpdateNeededListener = onUpdateNeededListener;
return this;
}
public ForceUpdateChecker build() {
return new ForceUpdateChecker(context, onUpdateNeededListener);
}
public ForceUpdateChecker check() {
ForceUpdateChecker forceUpdateChecker = build();
forceUpdateChecker.check();
return forceUpdateChecker;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment