Last active
September 24, 2017 14:26
-
-
Save ithebk/5fcefa1f14fce6eeab9ccfaaf9955ec8 to your computer and use it in GitHub Desktop.
Firebase update check provider
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
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