<commit_before>/*
 * Copyright 2012-2014 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;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;

import org.onepf.opfpush.RetryBroadcastReceiver;
import org.onepf.opfpush.model.Operation;
import org.onepf.opfutils.OPFLog;

import java.text.SimpleDateFormat;
import java.util.Date;

import static android.content.Context.ALARM_SERVICE;
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;

/**
 * @author <NAME>
 * @since 06.02.2015
 */
public final class RetryManager implements BackoffManager {

    private static RetryManager instance = null;

    @NonNull
    private Context appContext;

    @NonNull
    private BackoffManager backoffManager;

    @NonNull
    private AlarmManager alarmManager;

    private RetryManager(@NonNull final Context context,
                         @NonNull final BackoffManager backoffManager) {
        this.appContext = context.getApplicationContext();
        this.backoffManager = backoffManager;
        this.alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    }

    @NonNull
    public static RetryManager getInstance(@NonNull final Context context,
                                           @NonNull final BackoffManager backoffManager) {
        if (instance == null) {
            instance = new RetryManager(context, backoffManager);
        }
        return instance;
    }

    @Override
    public boolean hasTries(@NonNull final String providerName,
                            @NonNull final Operation operation) {
        return backoffManager.hasTries(providerName, operation);
    }

    @Override
    public long getTryDelay(@NonNull final String providerName,
                            @NonNull final Operation operation) {
        return backoffManager.getTryDelay(providerName, operation);
    }

    @Override
    public void reset(@NonNull final String providerName, @NonNull final Operation operation) {
        backoffManager.reset(providerName, operation);
    }

    public void postRetryRegister(@NonNull final String providerName) {
        OPFLog.methodD(providerName);
        postRetry(providerName, REGISTER, ACTION_RETRY_REGISTER);
    }

    public void postRetryUnregister(@NonNull final String providerName) {
        OPFLog.methodD(providerName);
        postRetry(providerName, UNREGISTER, ACTION_RETRY_UNREGISTER);
    }

    public void cancelRetryAllOperations(@NonNull final String providerName) {
        cancelRetryRegister(providerName);
        cancelRetryUnregister(providerName);
    }

    public void cancelRetryRegister(@NonNull final String providerName) {
        OPFLog.methodD(providerName);
        cancelRetry(providerName, REGISTER, ACTION_RETRY_REGISTER);
    }

    public void cancelRetryUnregister(@NonNull final String providerName) {
        OPFLog.methodD(providerName);
        cancelRetry(providerName, UNREGISTER, ACTION_RETRY_UNREGISTER);
    }

    private void postRetry(@NonNull final String providerName,
                           @NonNull final Operation operation,
                           @NonNull final String action) {
        final long when = System.currentTimeMillis() + getTryDelay(providerName, operation);
        OPFLog.d("Post retry provider '%s' at %s", providerName,
                SimpleDateFormat.getDateTimeInstance().format(new Date(when)));

        final Intent intent = new Intent(appContext, RetryBroadcastReceiver.class);
        intent.setAction(action);
        intent.putExtra(EXTRA_PROVIDER_NAME, providerName);

        alarmManager.set(
                AlarmManager.RTC,
                when,
                //TODO: Use providerName.hashCode() as requestCode
                PendingIntent.getBroadcast(appContext, 0, intent, 0)
        );
    }

    private void cancelRetry(@NonNull final String providerName,
                             @NonNull final Operation operation,
                             @NonNull final String action) {
        reset(providerName, operation);

        final Intent intent = new Intent(appContext, RetryBroadcastReceiver.class);
        intent.setAction(action);
        alarmManager.cancel(
                PendingIntent.getBroadcast(appContext, providerName.hashCode(), intent, 0)
        );
    }
}<commit_msg>Use providerName.hashCode() as requestCode for PendingIntent.<commit_after>/*
 * Copyright 2012-2014 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;

import android.app.AlarmManager;
import android.app.PendingIntent;
import android.content.Context;
import android.content.Intent;
import android.support.annotation.NonNull;

import org.onepf.opfpush.RetryBroadcastReceiver;
import org.onepf.opfpush.model.Operation;
import org.onepf.opfutils.OPFLog;

import java.text.SimpleDateFormat;
import java.util.Date;

import static android.content.Context.ALARM_SERVICE;
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;

/**
 * @author <NAME>
 * @since 06.02.2015
 */
public final class RetryManager implements BackoffManager {

    private static RetryManager instance = null;

    @NonNull
    private Context appContext;

    @NonNull
    private BackoffManager backoffManager;

    @NonNull
    private AlarmManager alarmManager;

    private RetryManager(@NonNull final Context context,
                         @NonNull final BackoffManager backoffManager) {
        this.appContext = context.getApplicationContext();
        this.backoffManager = backoffManager;
        this.alarmManager = (AlarmManager) context.getSystemService(ALARM_SERVICE);
    }

    @NonNull
    public static RetryManager getInstance(@NonNull final Context context,
                                           @NonNull final BackoffManager backoffManager) {
        if (instance == null) {
            instance = new RetryManager(context, backoffManager);
        }
        return instance;
    }

    @Override
    public boolean hasTries(@NonNull final String providerName,
                            @NonNull final Operation operation) {
        return backoffManager.hasTries(providerName, operation);
    }

    @Override
    public long getTryDelay(@NonNull final String providerName,
                            @NonNull final Operation operation) {
        return backoffManager.getTryDelay(providerName, operation);
    }

    @Override
    public void reset(@NonNull final String providerName, @NonNull final Operation operation) {
        backoffManager.reset(providerName, operation);
    }

    public void postRetryRegister(@NonNull final String providerName) {
        OPFLog.methodD(providerName);
        postRetry(providerName, REGISTER, ACTION_RETRY_REGISTER);
    }

    public void postRetryUnregister(@NonNull final String providerName) {
        OPFLog.methodD(providerName);
        postRetry(providerName, UNREGISTER, ACTION_RETRY_UNREGISTER);
    }

    public void cancelRetryAllOperations(@NonNull final String providerName) {
        cancelRetryRegister(providerName);
        cancelRetryUnregister(providerName);
    }

    public void cancelRetryRegister(@NonNull final String providerName) {
        OPFLog.methodD(providerName);
        cancelRetry(providerName, REGISTER, ACTION_RETRY_REGISTER);
    }

    public void cancelRetryUnregister(@NonNull final String providerName) {
        OPFLog.methodD(providerName);
        cancelRetry(providerName, UNREGISTER, ACTION_RETRY_UNREGISTER);
    }

    private void postRetry(@NonNull final String providerName,
                           @NonNull final Operation operation,
                           @NonNull final String action) {
        final long when = System.currentTimeMillis() + getTryDelay(providerName, operation);
        OPFLog.d("Post retry provider '%s' at %s", providerName,
                SimpleDateFormat.getDateTimeInstance().format(new Date(when)));

        final Intent intent = new Intent(appContext, RetryBroadcastReceiver.class);
        intent.setAction(action);
        intent.putExtra(EXTRA_PROVIDER_NAME, providerName);

        alarmManager.set(
                AlarmManager.RTC,
                when,
                PendingIntent.getBroadcast(appContext, providerName.hashCode(), intent, 0)
        );
    }

    private void cancelRetry(@NonNull final String providerName,
                             @NonNull final Operation operation,
                             @NonNull final String action) {
        reset(providerName, operation);

        final Intent intent = new Intent(appContext, RetryBroadcastReceiver.class);
        intent.setAction(action);

        final PendingIntent pendingIntent = PendingIntent
                .getBroadcast(appContext, providerName.hashCode(), intent, 0);
        alarmManager.cancel(pendingIntent);
        pendingIntent.cancel();
    }
}
{'fraction_non_alphanumeric': 0.07576332258676276, 'fraction_numerical': 0.004267020671344586, 'mean_word_length': 3.081656346749226, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 5, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5628722', 'n_tokens_mistral': 2711, 'n_tokens_neox': 2716, 'n_words': 715}