Created
July 21, 2016 15:59
-
-
Save sembozdemir/811ccbb8dd0b52f30603b44fc0e0c16c to your computer and use it in GitHub Desktop.
SampleForceUpdate wrapper class
This file contains hidden or 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
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