// 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/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-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMConstants.java
// static final String ACCOUNT_TYPE = "com.amazon.account";
// 
// Path: opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMConstants.java
// public static final String PROVIDER_NAME = "Amazon Device Messaging";


import static org.onepf.opfpush.OPFConstants.EXTRA_PROVIDER_NAME;
import static org.onepf.opfpush.adm.ADMConstants.ACCOUNT_TYPE;
import static org.onepf.opfpush.adm.ADMConstants.PROVIDER_NAME;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import org.onepf.opfpush.RetryBroadcastReceiver;
import org.onepf.opfutils.OPFChecks;
import org.onepf.opfutils.OPFLog;
import org.onepf.opfutils.OPFUtils;
import static android.Manifest.permission.GET_ACCOUNTS;
import static org.onepf.opfpush.OPFConstants.ACTION_RETRY_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.adm;




/**
 * It's used to retry the unregister operation if there was an
 * {@link com.amazon.device.messaging.ADMConstants#ERROR_AUTHENTICATION_FAILED} error while unregistering.
 *
 * @author Roman Savin
 * @since 03.02.2015
 */
public class LoginAccountsChangedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(@NonNull final Context context, @NonNull final Intent intent) {
        OPFLog.logMethod(context, OPFUtils.toString(intent));
        if (!OPFChecks.hasPermission(context, GET_ACCOUNTS)) {
            OPFLog.w(GET_ACCOUNTS + " permission hasn't been declared");
            return;
        }

        final PreferencesProvider preferencesProvider = PreferencesProvider.getInstance(context);
// 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/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-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMConstants.java
// static final String ACCOUNT_TYPE = "com.amazon.account";
// 
// Path: opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/ADMConstants.java
// public static final String PROVIDER_NAME = "Amazon Device Messaging";


// Path: opfpush-providers/adm/src/main/java/org/onepf/opfpush/adm/LoginAccountsChangedReceiver.java
import static org.onepf.opfpush.OPFConstants.EXTRA_PROVIDER_NAME;
import static org.onepf.opfpush.adm.ADMConstants.ACCOUNT_TYPE;
import static org.onepf.opfpush.adm.ADMConstants.PROVIDER_NAME;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;
import org.onepf.opfpush.RetryBroadcastReceiver;
import org.onepf.opfutils.OPFChecks;
import org.onepf.opfutils.OPFLog;
import org.onepf.opfutils.OPFUtils;
import static android.Manifest.permission.GET_ACCOUNTS;
import static org.onepf.opfpush.OPFConstants.ACTION_RETRY_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.adm;




/**
 * It's used to retry the unregister operation if there was an
 * {@link com.amazon.device.messaging.ADMConstants#ERROR_AUTHENTICATION_FAILED} error while unregistering.
 *
 * @author Roman Savin
 * @since 03.02.2015
 */
public class LoginAccountsChangedReceiver extends BroadcastReceiver {

    @Override
    public void onReceive(@NonNull final Context context, @NonNull final Intent intent) {
        OPFLog.logMethod(context, OPFUtils.toString(intent));
        if (!OPFChecks.hasPermission(context, GET_ACCOUNTS)) {
            OPFLog.w(GET_ACCOUNTS + " permission hasn't been declared");
            return;
        }

        final PreferencesProvider preferencesProvider = PreferencesProvider.getInstance(context);
        final Account[] amazonAccounts = AccountManager.get(context).getAccountsByType(ACCOUNT_TYPE);