Created
September 24, 2017 14:25
-
-
Save ithebk/d9e0947507b979218a779a005659e469 to your computer and use it in GitHub Desktop.
File to handle force update and update firebase config on start
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.fragment; | |
import android.databinding.DataBindingUtil; | |
import android.os.Bundle; | |
import android.support.annotation.NonNull; | |
import android.support.annotation.Nullable; | |
import android.support.v4.app.Fragment; | |
import android.view.LayoutInflater; | |
import android.view.View; | |
import android.view.ViewGroup; | |
import com.google.android.gms.tasks.OnCompleteListener; | |
import com.google.android.gms.tasks.Task; | |
import com.google.firebase.remoteconfig.FirebaseRemoteConfig; | |
import com.google.firebase.remoteconfig.FirebaseRemoteConfigSettings; | |
import java.util.HashMap; | |
import java.util.Map; | |
import easydo.ithebk.BuildConfig; | |
import easydo.ithebk.R; | |
import easydo.ithebk.databinding.FragmentHomeBinding; | |
import easydo.ithebk.listener.ForceUpdateChecker; | |
/** | |
* Created by bharath on 28/8/16. | |
*/ | |
public class HomeFragment extends Fragment implements ForceUpdateChecker.OnUpdateNeededListener { | |
private static final String TAG = HomeFragment.class.getSimpleName(); | |
FragmentHomeBinding binding; | |
@Nullable | |
@Override | |
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) { | |
binding = DataBindingUtil.inflate(inflater, R.layout.fragment_home, container, false); | |
updateConfig(); | |
ForceUpdateChecker.with(getContext()).onUpdateNeeded(HomeFragment.this).check(); | |
return binding.getRoot(); | |
} | |
private void updateConfig() { | |
final FirebaseRemoteConfig firebaseRemoteConfig = FirebaseRemoteConfig.getInstance(); | |
FirebaseRemoteConfigSettings configSettings = new FirebaseRemoteConfigSettings.Builder() | |
// .setDeveloperModeEnabled(BuildConfig.DEBUG) // Enable this on debugging | |
.build(); | |
firebaseRemoteConfig.setConfigSettings(configSettings); | |
// set in-app defaults | |
Map<String, Object> remoteConfigDefaults = new HashMap(); | |
remoteConfigDefaults.put(ForceUpdateChecker.KEY_APP_VERSION_CODE, BuildConfig.VERSION_CODE); | |
remoteConfigDefaults.put(ForceUpdateChecker.KEY_UPDATE_REQUIRED, false); | |
remoteConfigDefaults.put(ForceUpdateChecker.KEY_PLAY_WHATS_NEW_TEXT, ForceUpdateChecker.PLAY_WHATS_NEW_TEXT_DEFAULT); | |
firebaseRemoteConfig.setDefaults(remoteConfigDefaults); | |
long cacheExpiration = 3600; | |
//onDevelopment make cacheExpiration as zero second; | |
if (firebaseRemoteConfig.getInfo().getConfigSettings().isDeveloperModeEnabled()) { | |
cacheExpiration = 0; | |
} | |
firebaseRemoteConfig.fetch(cacheExpiration) | |
.addOnCompleteListener(new OnCompleteListener<Void>() { | |
@Override | |
public void onComplete(@NonNull Task<Void> task) { | |
if (task.isSuccessful()) { | |
System.out.println("remote config is fetched."); | |
firebaseRemoteConfig.activateFetched(); | |
} else { | |
System.out.println("fetch failed:" + task.getException()); | |
} | |
} | |
}); | |
} | |
@Override | |
public void onUpdateNeeded(final String whatsNew) { | |
System.out.println("Update need to be pushed"); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment