<commit_before>package com.githang.android.apnbb;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import org.androidpn.client.LogUtil;
import org.androidpn.client.NotificationIQ;
import org.androidpn.client.NotificationIQProvider;
import org.androidpn.client.XmppManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Registration;
import org.jivesoftware.smack.provider.ProviderManager;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * XMLPP连接的控制类，控制连接、注册、登录、重连、响应连接状态及回执等。
 * Created by msdx on 14-4-17.
 */
public class XmppConnectReceiver extends BroadcastReceiver {
    private static final String LOG_TAG = LogUtil.makeLogTag(XmppConnectReceiver.class);
    private static final Object lock = new Object();
    private static XmppConnectReceiver instance = null;
    private Context context;
    private Handler handler;
    private XmppManager xmppManager;
    private SharedPreferences sharedPrefs;
    private String xmppHost;
    private int xmppPort;
    private Runnable disconnectTask;
    private Runnable reconnectTask;
    private Runnable loginServerTask;
    private boolean isConnecting;


    private XmppConnectReceiver(Context context, XmppManager xmppManager) {
        this.context = context;
        this.xmppManager = xmppManager;
        LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
        IntentFilter filter = new IntentFilter();
        filter.addAction(BroadcastUtil.APN_ACTION_LOGIN);
        filter.addAction(BroadcastUtil.APN_ACTION_RECONNECT);
        filter.addAction(BroadcastUtil.APN_ACTION_REQUEST_STATUS);
        filter.addAction(BroadcastUtil.APN_ACTION_RECEIPT);
        lbm.registerReceiver(this, filter);

        BroadcastUtil.sendBroadcast(context, BroadcastUtil.ANDROIDPN_MSG_RECEIVER_READY);

        sharedPrefs = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
                Context.MODE_PRIVATE);

        xmppHost = sharedPrefs.getString(Constants.XMPP_HOST, "localhost");
        xmppPort = sharedPrefs.getInt(Constants.XMPP_PORT, 5222);


        HandlerThread thread = new HandlerThread(XmppConnectReceiver.class.getSimpleName());
        thread.start();
        handler = new Handler(thread.getLooper());
        disconnectTask = new DisconnectTask();
        reconnectTask = new ReconnectTask();

        loginServerTask = new LoginServer();

    }

    public static final void initInstance(Context context, XmppManager xmppManager) {
        if (instance == null) {
            synchronized (lock) {
                if (instance == null) {
                    instance = new XmppConnectReceiver(context, xmppManager);
                }
            }
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(LOG_TAG, action);
        if (BroadcastUtil.APN_ACTION_LOGIN.equals(action)) {
            doLogin();
        } else if (BroadcastUtil.APN_ACTION_RECONNECT.equals(action)) {
            doReconnect();
        } else if (BroadcastUtil.APN_ACTION_DISCONNECT.equals(action)) {
            doDisconnect();
        } else if (BroadcastUtil.APN_ACTION_REQUEST_STATUS.equals(action)) {
            if (xmppManager.isAuthenticated()) {
                BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_STATUS_CONNECTED);
            } else if (isConnecting) {
                BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_STATUS_CONNECTING);
            } else {
                BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_STATUS_DISCONNECT);
            }
        } else if (BroadcastUtil.APN_ACTION_RECEIPT.equals(action)) {
            doSendReceipt((IQ) intent.getSerializableExtra(Constants.INTENT_EXTRA_IQ));
        }
    }

    private void doSendReceipt(IQ iq) {
        IQ result = IQ.createResultIQ(iq);
        xmppManager.getConnection().sendPacket(result);
        Log.d(LOG_TAG, "receipt" + iq.toString());
    }

    private void doLogin() {
        if (xmppManager.isAuthenticated()) {
            return;
        }
        handler.removeCallbacks(loginServerTask);
        handler.post(loginServerTask);
    }

    private void doReconnect() {
        handler.post(reconnectTask);
    }

    private void doDisconnect() {
        handler.removeCallbacks(loginServerTask);
        handler.post(disconnectTask);
    }

    public static class DelayTime {
        private static final DelayTime delayTime = new DelayTime();
        private AtomicInteger times = new AtomicInteger(0);

        public static void resetTimes() {
            delayTime.times.set(0);
        }

        public static void increase() {
            delayTime.times.incrementAndGet();
        }

        public static int getWaitingTime() {
            int time = delayTime.times.get();
            if (time == 0) {
                return 0;
            }
            // 20秒一次
            if (time < 15) {
                return 20;
            }
            // 60秒一次
            if (time < 20) {
                return 60;
            }
            return time < 30 ? 120 : 300;
        }

    }

    /**
     * A runnable task to connect the server.
     */
    private class LoginServer implements Runnable {
        private static final int REGISTER_TIME_OUT = 60000;

        private LoginServer() {
        }

        public void run() {
            final String uuid = UUID.randomUUID().toString();
            // 密码也设成UUID，以使应用程序清除数据之后，再注册的用户username是一样的。
            final String newUsername = uuid;
            final String newPassword = uuid;
            isConnecting = true;
            boolean isSuccess = false;
            if(connect()) {
                if(register(newUsername, newPassword)) {
                    SharedPreferences.Editor editor = sharedPrefs.edit();
                    editor.putString(Constants.XMPP_USERNAME,
                            newUsername);
                    editor.putString(Constants.XMPP_PASSWORD,
                            newPassword);
                    editor.commit();
                    String username = sharedPrefs.getString(Constants.XMPP_USERNAME, "");
                    String password = sharedPrefs.getString(Constants.XMPP_PASSWORD, "");
                    if(login(username, password)) {
                        isSuccess = true;
                        BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_STATUS_LOGINED);
                    } else {
                    }
                }
            } else {
                BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_STATUS_CONNECT_FAILED);
            }
            isConnecting = false;
            if(!isSuccess) {
                BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_ACTION_RECONNECT);
            }<commit_msg>Make the name to same in same device.<commit_after>package com.githang.android.apnbb;

import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.SharedPreferences;
import android.os.Handler;
import android.os.HandlerThread;
import android.support.v4.content.LocalBroadcastManager;
import android.util.Log;

import org.androidpn.client.LogUtil;
import org.androidpn.client.NotificationIQ;
import org.androidpn.client.NotificationIQProvider;
import org.androidpn.client.XmppManager;
import org.jivesoftware.smack.ConnectionConfiguration;
import org.jivesoftware.smack.PacketCollector;
import org.jivesoftware.smack.PacketListener;
import org.jivesoftware.smack.XMPPConnection;
import org.jivesoftware.smack.XMPPException;
import org.jivesoftware.smack.filter.AndFilter;
import org.jivesoftware.smack.filter.PacketFilter;
import org.jivesoftware.smack.filter.PacketIDFilter;
import org.jivesoftware.smack.filter.PacketTypeFilter;
import org.jivesoftware.smack.packet.IQ;
import org.jivesoftware.smack.packet.Packet;
import org.jivesoftware.smack.packet.Registration;
import org.jivesoftware.smack.provider.ProviderManager;

import java.util.UUID;
import java.util.concurrent.atomic.AtomicInteger;


/**
 * XMLPP连接的控制类，控制连接、注册、登录、重连、响应连接状态及回执等。
 * Created by msdx on 14-4-17.
 */
public class XmppConnectReceiver extends BroadcastReceiver {
    private static final String LOG_TAG = LogUtil.makeLogTag(XmppConnectReceiver.class);
    private static final Object lock = new Object();
    private static XmppConnectReceiver instance = null;
    private Context context;
    private Handler handler;
    private XmppManager xmppManager;
    private SharedPreferences sharedPrefs;
    private String xmppHost;
    private int xmppPort;
    private Runnable disconnectTask;
    private Runnable reconnectTask;
    private Runnable loginServerTask;
    private boolean isConnecting;


    private XmppConnectReceiver(Context context, XmppManager xmppManager) {
        this.context = context;
        this.xmppManager = xmppManager;
        LocalBroadcastManager lbm = LocalBroadcastManager.getInstance(context);
        IntentFilter filter = new IntentFilter();
        filter.addAction(BroadcastUtil.APN_ACTION_LOGIN);
        filter.addAction(BroadcastUtil.APN_ACTION_RECONNECT);
        filter.addAction(BroadcastUtil.APN_ACTION_REQUEST_STATUS);
        filter.addAction(BroadcastUtil.APN_ACTION_RECEIPT);
        lbm.registerReceiver(this, filter);

        BroadcastUtil.sendBroadcast(context, BroadcastUtil.ANDROIDPN_MSG_RECEIVER_READY);

        sharedPrefs = context.getSharedPreferences(Constants.SHARED_PREFERENCE_NAME,
                Context.MODE_PRIVATE);

        xmppHost = sharedPrefs.getString(Constants.XMPP_HOST, "localhost");
        xmppPort = sharedPrefs.getInt(Constants.XMPP_PORT, 5222);


        HandlerThread thread = new HandlerThread(XmppConnectReceiver.class.getSimpleName());
        thread.start();
        handler = new Handler(thread.getLooper());
        disconnectTask = new DisconnectTask();
        reconnectTask = new ReconnectTask();

        loginServerTask = new LoginServer();

    }

    public static final void initInstance(Context context, XmppManager xmppManager) {
        if (instance == null) {
            synchronized (lock) {
                if (instance == null) {
                    instance = new XmppConnectReceiver(context, xmppManager);
                }
            }
        }
    }

    @Override
    public void onReceive(Context context, Intent intent) {
        String action = intent.getAction();
        Log.d(LOG_TAG, action);
        if (BroadcastUtil.APN_ACTION_LOGIN.equals(action)) {
            doLogin();
        } else if (BroadcastUtil.APN_ACTION_RECONNECT.equals(action)) {
            doReconnect();
        } else if (BroadcastUtil.APN_ACTION_DISCONNECT.equals(action)) {
            doDisconnect();
        } else if (BroadcastUtil.APN_ACTION_REQUEST_STATUS.equals(action)) {
            if (xmppManager.isAuthenticated()) {
                BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_STATUS_CONNECTED);
            } else if (isConnecting) {
                BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_STATUS_CONNECTING);
            } else {
                BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_STATUS_DISCONNECT);
            }
        } else if (BroadcastUtil.APN_ACTION_RECEIPT.equals(action)) {
            doSendReceipt((IQ) intent.getSerializableExtra(Constants.INTENT_EXTRA_IQ));
        }
    }

    private void doSendReceipt(IQ iq) {
        IQ result = IQ.createResultIQ(iq);
        xmppManager.getConnection().sendPacket(result);
        Log.d(LOG_TAG, "receipt" + iq.toString());
    }

    private void doLogin() {
        if (xmppManager.isAuthenticated()) {
            return;
        }
        handler.removeCallbacks(loginServerTask);
        handler.post(loginServerTask);
    }

    private void doReconnect() {
        handler.post(reconnectTask);
    }

    private void doDisconnect() {
        handler.removeCallbacks(loginServerTask);
        handler.post(disconnectTask);
    }

    public static class DelayTime {
        private static final DelayTime delayTime = new DelayTime();
        private AtomicInteger times = new AtomicInteger(0);

        public static void resetTimes() {
            delayTime.times.set(0);
        }

        public static void increase() {
            delayTime.times.incrementAndGet();
        }

        public static int getWaitingTime() {
            int time = delayTime.times.get();
            if (time == 0) {
                return 0;
            }
            // 20秒一次
            if (time < 15) {
                return 20;
            }
            // 60秒一次
            if (time < 20) {
                return 60;
            }
            return time < 30 ? 120 : 300;
        }

    }

    /**
     * A runnable task to connect the server.
     */
    private class LoginServer implements Runnable {
        private static final int REGISTER_TIME_OUT = 60000;

        private LoginServer() {
        }

        public void run() {
            final String uuid = UUIDUtil.getID(context);
            // 密码也设成UUID，以使应用程序清除数据之后，再注册的用户username是一样的。
            final String newUsername = uuid;
            final String newPassword = <PASSWORD>;
            isConnecting = true;
            boolean isSuccess = false;
            if(connect()) {
                if(register(newUsername, newPassword)) {
                    SharedPreferences.Editor editor = sharedPrefs.edit();
                    editor.putString(Constants.XMPP_USERNAME,
                            newUsername);
                    editor.putString(Constants.XMPP_PASSWORD,
                            newPassword);
                    editor.commit();
                    String username = sharedPrefs.getString(Constants.XMPP_USERNAME, "");
                    String password = sharedPrefs.getString(Constants.XMPP_PASSWORD, "");
                    if(login(username, password)) {
                        isSuccess = true;
                        BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_STATUS_LOGINED);
                    } else {
                    }
                }
            } else {
                BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_STATUS_CONNECT_FAILED);
            }
            isConnecting = false;
            if(!isSuccess) {
                BroadcastUtil.sendBroadcast(context, BroadcastUtil.APN_ACTION_RECONNECT);
            }
{'fraction_non_alphanumeric': 0.0860116515022583, 'fraction_numerical': 0.005105714472736794, 'mean_word_length': 2.4628286491387126, 'pattern_counts': {'":': 0, '<': 10, '<?xml version=': 0, '>': 4, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6989529', 'n_tokens_mistral': 4431, 'n_tokens_neox': 4283, 'n_words': 779}