/*
 * Copyright Google Inc. All Rights Reserved.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

/**
 * For more information on setting up and running this sample code, see
 * https://developers.google.com/firebase/docs/remote-config/android
 */

package com.google.samples.quickstart.config;

import com.google.android.gms.common.ConnectionResult;
import com.google.android.gms.common.api.GoogleApiClient;
import com.google.android.gms.common.api.ResultCallback;
import com.google.android.gms.config.Config;
import com.google.android.gms.config.ConfigApi;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.util.Log;
import android.widget.TextView;


public class MainActivity extends AppCompatActivity implements GoogleApiClient.ConnectionCallbacks,
        GoogleApiClient.OnConnectionFailedListener {

    private static final String TAG = "MyTestApp";
    private GoogleApiClient mClient;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mClient = new GoogleApiClient.Builder(this, this, this)
                .addApi(Config.API)
                .enableAutoManage(this, this)
                .build();
    }

    public void onConnected(Bundle connectionHint) {
        Log.i(TAG, "onConnected");
        // [START fetch_config_request]
        ConfigApi.FetchConfigRequest request = new ConfigApi.FetchConfigRequest.Builder()
                .addCustomVariable("build", "dev")
                .build();
        // [END fetch_config_request]
        // [START fetch_config_callback]
        Config.ConfigApi.fetchConfig(mClient, request)
                .setResultCallback(new ResultCallback<ConfigApi.FetchConfigResult>() {
                    @Override
                    public void onResult(ConfigApi.FetchConfigResult fetchConfigResult) {
                        Log.i(TAG, "onResult");
                        if (fetchConfigResult.getStatus().isSuccess()) {
                            long price = 100;
                            boolean isPromo =
                                    fetchConfigResult.getAsBoolean("is_promotion_on", false);
                            long discount = fetchConfigResult.getAsLong("discount", 0);
                            if (isPromo) {
                                price = (price / 100) * (price - discount);
                            }
                            String priceMsg = "Your price is $" + price;
                            TextView pv = (TextView) findViewById(R.id.priceView);
                            pv.setText(priceMsg);
                            boolean isDevBuild =
                                    fetchConfigResult.getAsBoolean("dev_features_on", false);
                            if (isDevBuild) {
                                String debugMsg = "Config set size: " +
                                        fetchConfigResult.getAllConfigKeys().size();
                                TextView dv = (TextView) findViewById(R.id.debugView);
                                dv.setText(debugMsg);
                            }
                        } else {
                            // There has been an error fetching the config
                            Log.w(TAG, "Error fetching config: " +
                                    fetchConfigResult.getStatus().toString());
                        }
                    }
                });
                // [END fetch_config_callback]
    }

    @Override
    public void onConnectionSuspended(int cause) {
        Log.w(TAG, "suspended: " + cause);
    }

    @Override
    public void onConnectionFailed(ConnectionResult status) {
        Log.w(TAG, "failed: " + status);
    }

}

@@ -1064,16 +1064,72 @@
 nfigApi;
+%0Aimport com.google.android.gms.config.ConfigStatusCodes;
 %0A%0Aimport
@@ -2110,16 +2110,392 @@
  %22dev%22)%0A
+                /**%0A                 Cache expiration defaults to 12 hours, uncomment the line below to change it.%0A                 If you fetch too frequently though, your requests will be throttled and%0A                 values read from the local cache. 20 minutes should prevent this from happening.%0A                 */%0A                //.setCacheExpirationSeconds(60 * 20)%0A
         
@@ -2929,53 +2929,463 @@
     
-if (fetchConfigResult.getStatus().isSuccess()
+TextView pv = (TextView) findViewById(R.id.priceView);%0A                        int statusCode = fetchConfigResult.getStatus().getStatusCode();%0A                        if (fetchConfigResult.getStatus().isSuccess() %7C%7C%0A                                // if throttled, values won't be fresh%0A                                statusCode == ConfigStatusCodes.FETCH_THROTTLED %7C%7C%0A                                statusCode == ConfigStatusCodes.FETCH_THROTTLED_STALE
 ) %7B%0A
@@ -3884,91 +3884,8 @@
 ce;%0A
-                            TextView pv = (TextView) findViewById(R.id.priceView);%0A
     
@@ -4618,24 +4618,24 @@
 config: %22 +%0A
-
             
@@ -4701,16 +4701,170 @@
 ing());%0A
+                            pv.setText(%22Error fetching config: %22 +%0A                                    fetchConfigResult.getStatus().getStatusMessage());%0A
         

Clarify caching and throttling for Config