Skip to content

Instantly share code, notes, and snippets.

@sembozdemir
Created July 21, 2016 15:59
Show Gist options
  • Star 2 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save sembozdemir/811ccbb8dd0b52f30603b44fc0e0c16c to your computer and use it in GitHub Desktop.
Save sembozdemir/811ccbb8dd0b52f30603b44fc0e0c16c to your computer and use it in GitHub Desktop.
SampleForceUpdate wrapper class
public class ForceUpdateChecker {
private static final String TAG = ForceUpdateChecker.class.getSimpleName();
public static final String KEY_UPDATE_REQUIRED = "force_update_required";
public static final String KEY_CURRENT_VERSION = "force_update_current_version";
public static final String KEY_UPDATE_URL = "force_update_store_url";
private OnUpdateNeededListener onUpdateNeededListener;
private Context context;
public interface OnUpdateNeededListener {
void onUpdateNeeded(String updateUrl);
}
public static Builder with(@NonNull Context context) {
return new Builder(context);
}
public ForceUpdateChecker(@NonNull Context context,
OnUpdateNeededListener onUpdateNeededListener) {
this.context = context;
this.onUpdateNeededListener = onUpdateNeededListener;
}
public void check() {
final FirebaseRemoteConfig remoteConfig = FirebaseRemoteConfig.getInstance();
if (remoteConfig.getBoolean(KEY_UPDATE_REQUIRED)) {
String currentVersion = remoteConfig.getString(KEY_CURRENT_VERSION);
String appVersion = getAppVersion(context);
String updateUrl = remoteConfig.getString(KEY_UPDATE_URL);
if (!TextUtils.equals(currentVersion, appVersion)
&& onUpdateNeededListener != null) {
onUpdateNeededListener.onUpdateNeeded(updateUrl);
}
}
}
private String getAppVersion(Context context) {
String result = "";
try {
result = context.getPackageManager()
.getPackageInfo(context.getPackageName(), 0)
.versionName;
result = result.replaceAll("[a-zA-Z]|-", "");
} catch (PackageManager.NameNotFoundException e) {
Log.e(TAG, e.getMessage());
}
return result;
}
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