// Path: opfpush/src/main/java/org/onepf/opfpush/ConnectivityChangeReceiver.java
// public final class ConnectivityChangeReceiver extends BroadcastReceiver {
// 
//     @Override
//     public void onReceive(@NonNull final Context context, @NonNull final Intent intent) {
//         OPFLog.logMethod(context, OPFUtils.toString(intent));
// 
//         final Set<Pair<String, String>> retryProvidersActions = RetryManager.getInstance()
//                 .getRetryProvidersActions();
// 
//         final OPFPushHelper helper = OPFPush.getHelper();
//         for (Pair<String, String> retryProviderAction : retryProvidersActions) {
//             final String providerName = retryProviderAction.first;
//             final String action = retryProviderAction.second;
//             switch (action) {
//                 case ACTION_RETRY_REGISTER:
//                     helper.register(providerName);
//                     break;
//                 case ACTION_RETRY_UNREGISTER:
//                     helper.unregister(providerName);
//                     break;
//             }
//         }
//     }
// }
// 
// Path: opfpush/src/main/java/org/onepf/opfpush/RetryBroadcastReceiver.java
// public final class RetryBroadcastReceiver extends BroadcastReceiver {
// 
//     @Override
//     public void onReceive(@NonNull final Context context, @NonNull final Intent intent) {
//         OPFLog.logMethod(context, OPFUtils.toString(intent));
// 
//         final OPFPushHelper helper = OPFPush.getHelper();
//         if (helper.isInitDone()) {
//             OPFLog.d("Initialisation is done");
// 
//             final String action = intent.getAction();
//             final String providerName = intent.getStringExtra(EXTRA_PROVIDER_NAME);
//             switch (action) {
//                 case ACTION_RETRY_REGISTER:
//                     helper.register(providerName);
//                     break;
//                 case ACTION_RETRY_UNREGISTER:
//                     helper.unregister(providerName);
//                     break;
//                 case ACTION_CHECK_REGISTERING_TIMEOUT:
//                     checkRegistering(context, helper, providerName);
//                     break;
//                 default:
//                     throw new IllegalStateException(String.format(Locale.US, "Unknown action '%s'.", action));
//             }
//         } else {
//             OPFLog.w("OPFPush must be initialized");
//         }
//     }
// 
//     private void checkRegistering(@NonNull final Context context,
//                                   @NonNull final OPFPushHelper helper,
//                                   @NonNull final String providerName) {
//         OPFLog.logMethod(context, helper, providerName);
//         if (helper.isRegistering()) {
//             Settings.getInstance(context).saveState(State.UNREGISTERED);
//             helper.registerNextAvailableProvider(providerName);
//         }
//     }
// }
// 
// Path: opfpush/src/main/java/org/onepf/opfpush/model/Operation.java
// public enum Operation {
//     REGISTER,
//     UNREGISTER
// }
// 
// Path: opfpush/src/main/java/org/onepf/opfpush/OPFConstants.java
// public static final String ACTION_RETRY_REGISTER = BuildConfig.APPLICATION_ID + "intent.RETRY_REGISTER";
// 
// Path: opfpush/src/main/java/org/onepf/opfpush/OPFConstants.java
// public static final String ACTION_RETRY_UNREGISTER = BuildConfig.APPLICATION_ID + "intent.RETRY_UNREGISTER";
// 
// Path: opfpush/src/main/java/org/onepf/opfpush/OPFConstants.java
// public static final String EXTRA_PROVIDER_NAME = "org.onepf.opfpush.intent.EXTRA_PROVIDER_NAME";


import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Pair;
import org.onepf.opfpush.ConnectivityChangeReceiver;
import org.onepf.opfpush.RetryBroadcastReceiver;
import org.onepf.opfpush.model.Operation;
import org.onepf.opfutils.OPFChecks;
import org.onepf.opfutils.OPFLog;
import org.onepf.opfutils.exception.InitException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import static android.content.Context.ALARM_SERVICE;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static org.onepf.opfpush.OPFConstants.ACTION_RETRY_REGISTER;
import static org.onepf.opfpush.OPFConstants.ACTION_RETRY_UNREGISTER;
import static org.onepf.opfpush.OPFConstants.EXTRA_PROVIDER_NAME;
import static org.onepf.opfpush.model.Operation.REGISTER;
import static org.onepf.opfpush.model.Operation.UNREGISTER;
/*
 * Copyright 2012-2015 One Platform Foundation
 *
 * 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.
 */

package org.onepf.opfpush.backoff;





/**
 * @author Roman Savin
 * @since 06.02.2015
 */
public final class RetryManager implements BackoffManager {

    private static volatile RetryManager instance;

    @NonNull
    private final Context appContext;

    @NonNull
    private final BackoffManager backoffManager;

    @NonNull
    private final AlarmManager alarmManager;

    private final Set<Pair<String, String>> retryProvidersActions;

    @Nullable
// Path: opfpush/src/main/java/org/onepf/opfpush/ConnectivityChangeReceiver.java
// public final class ConnectivityChangeReceiver extends BroadcastReceiver {
// 
//     @Override
//     public void onReceive(@NonNull final Context context, @NonNull final Intent intent) {
//         OPFLog.logMethod(context, OPFUtils.toString(intent));
// 
//         final Set<Pair<String, String>> retryProvidersActions = RetryManager.getInstance()
//                 .getRetryProvidersActions();
// 
//         final OPFPushHelper helper = OPFPush.getHelper();
//         for (Pair<String, String> retryProviderAction : retryProvidersActions) {
//             final String providerName = retryProviderAction.first;
//             final String action = retryProviderAction.second;
//             switch (action) {
//                 case ACTION_RETRY_REGISTER:
//                     helper.register(providerName);
//                     break;
//                 case ACTION_RETRY_UNREGISTER:
//                     helper.unregister(providerName);
//                     break;
//             }
//         }
//     }
// }
// 
// Path: opfpush/src/main/java/org/onepf/opfpush/RetryBroadcastReceiver.java
// public final class RetryBroadcastReceiver extends BroadcastReceiver {
// 
//     @Override
//     public void onReceive(@NonNull final Context context, @NonNull final Intent intent) {
//         OPFLog.logMethod(context, OPFUtils.toString(intent));
// 
//         final OPFPushHelper helper = OPFPush.getHelper();
//         if (helper.isInitDone()) {
//             OPFLog.d("Initialisation is done");
// 
//             final String action = intent.getAction();
//             final String providerName = intent.getStringExtra(EXTRA_PROVIDER_NAME);
//             switch (action) {
//                 case ACTION_RETRY_REGISTER:
//                     helper.register(providerName);
//                     break;
//                 case ACTION_RETRY_UNREGISTER:
//                     helper.unregister(providerName);
//                     break;
//                 case ACTION_CHECK_REGISTERING_TIMEOUT:
//                     checkRegistering(context, helper, providerName);
//                     break;
//                 default:
//                     throw new IllegalStateException(String.format(Locale.US, "Unknown action '%s'.", action));
//             }
//         } else {
//             OPFLog.w("OPFPush must be initialized");
//         }
//     }
// 
//     private void checkRegistering(@NonNull final Context context,
//                                   @NonNull final OPFPushHelper helper,
//                                   @NonNull final String providerName) {
//         OPFLog.logMethod(context, helper, providerName);
//         if (helper.isRegistering()) {
//             Settings.getInstance(context).saveState(State.UNREGISTERED);
//             helper.registerNextAvailableProvider(providerName);
//         }
//     }
// }
// 
// Path: opfpush/src/main/java/org/onepf/opfpush/model/Operation.java
// public enum Operation {
//     REGISTER,
//     UNREGISTER
// }
// 
// Path: opfpush/src/main/java/org/onepf/opfpush/OPFConstants.java
// public static final String ACTION_RETRY_REGISTER = BuildConfig.APPLICATION_ID + "intent.RETRY_REGISTER";
// 
// Path: opfpush/src/main/java/org/onepf/opfpush/OPFConstants.java
// public static final String ACTION_RETRY_UNREGISTER = BuildConfig.APPLICATION_ID + "intent.RETRY_UNREGISTER";
// 
// Path: opfpush/src/main/java/org/onepf/opfpush/OPFConstants.java
// public static final String EXTRA_PROVIDER_NAME = "org.onepf.opfpush.intent.EXTRA_PROVIDER_NAME";


// Path: opfpush/src/main/java/org/onepf/opfpush/backoff/RetryManager.java
import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.support.annotation.NonNull;
import android.support.annotation.Nullable;
import android.util.Pair;
import org.onepf.opfpush.ConnectivityChangeReceiver;
import org.onepf.opfpush.RetryBroadcastReceiver;
import org.onepf.opfpush.model.Operation;
import org.onepf.opfutils.OPFChecks;
import org.onepf.opfutils.OPFLog;
import org.onepf.opfutils.exception.InitException;
import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashSet;
import java.util.Locale;
import java.util.Set;
import static android.content.Context.ALARM_SERVICE;
import static android.net.ConnectivityManager.CONNECTIVITY_ACTION;
import static org.onepf.opfpush.OPFConstants.ACTION_RETRY_REGISTER;
import static org.onepf.opfpush.OPFConstants.ACTION_RETRY_UNREGISTER;
import static org.onepf.opfpush.OPFConstants.EXTRA_PROVIDER_NAME;
import static org.onepf.opfpush.model.Operation.REGISTER;
import static org.onepf.opfpush.model.Operation.UNREGISTER;

/*
 * Copyright 2012-2015 One Platform Foundation
 *
 * 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.
 */

package org.onepf.opfpush.backoff;





/**
 * @author Roman Savin
 * @since 06.02.2015
 */
public final class RetryManager implements BackoffManager {

    private static volatile RetryManager instance;

    @NonNull
    private final Context appContext;

    @NonNull
    private final BackoffManager backoffManager;

    @NonNull
    private final AlarmManager alarmManager;

    private final Set<Pair<String, String>> retryProvidersActions;

    @Nullable
    private ConnectivityChangeReceiver connectivityChangeReceiver;