<commit_before>package edu.kit.isco.kitalumniapp.gcm;


/*
 * Copyright 2012 Google Inc.
 *
 * 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.
 */

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import edu.kit.isco.kitalumniapp.R;
import edu.kit.isco.kitalumniapp.dbObjects.DataAccessTag;
import edu.kit.isco.kitalumniapp.dbObjects.DataAccessUser;

/**
 * Binds in one place methods for register, unregister and updating for notifications.
 * Created by <NAME> on 14.1.2015 г..
 */
public class ServerUtilities {

    static final String SERVER_URL = "yourUrl";
    /**
     * Tag used on log messages.
     */
    static final String TAG = "GCMDemo";
    /**
     * Intent used to display a message in the screen.
     */
    static final String DISPLAY_MESSAGE_ACTION =
            "com.google.android.gcm.demo.app.DISPLAY_MESSAGE";
    /**
     * Intent's extra that contains the message to be displayed.
     */
    static final String EXTRA_MESSAGE = "message";
    private static final int MAX_ATTEMPTS = 5;
    private static final int BACKOFF_MILLI_SECONDS = 2000;
    private static final Random random = new Random();
    BigInteger bigInteger;
    private List<DataAccessTag> fullTagsList;

    /**
     * Notifies UI to display a message.
     * <p/>
     * This method is defined in the common helper because it's used both by
     * the UI and the background service.
     *
     * @param context application's context.
     * @param message message to be displayed.
     */
    static void displayMessage(Context context, String message) {
        Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
        intent.putExtra(EXTRA_MESSAGE, message);
        context.sendBroadcast(intent);
    }

    /**
     * Checks if the list has been created and if it hasn`t, then it puts all Tags in one single list.
     * @return fully populated List of DataAccessTags
     */

    private List<DataAccessTag> populateTagList() {
        if (fullTagsList == null) {
            fullTagsList = new ArrayList<>();
            fullTagsList.add(DataAccessTag.DATA_ADMINISTRATION);
            fullTagsList.add(DataAccessTag.TRAINEE);
            fullTagsList.add(DataAccessTag.CLERK);
            fullTagsList.add(DataAccessTag.GRADUAND);
            fullTagsList.add(DataAccessTag.DOCTORAND);
            fullTagsList.add(DataAccessTag.ENGINEER);
            fullTagsList.add(DataAccessTag.CLERK);
            fullTagsList.add(DataAccessTag.INDUSTRIAL);
            fullTagsList.add(DataAccessTag.SALES_OCCUPATION);
            fullTagsList.add(DataAccessTag.THRESHOLD_WORKER);
            fullTagsList.add(DataAccessTag.PROFESSOR);
            fullTagsList.add(DataAccessTag.TECHNICAL_EMPLOYEE);
            fullTagsList.add(DataAccessTag.STUDENT_RESEARCH_PROJECT);
            fullTagsList.add(DataAccessTag.ADMINISTRATION);
            fullTagsList.add(DataAccessTag.SCIENTIST);
            fullTagsList.add(DataAccessTag.OTHERS);
        }

        return fullTagsList;
    }

    /**
     * Generates password the first time and then returns it
     * every other time.
     * @return password
     */
    private BigInteger getPassword() {
        if (bigInteger == null) {
            SecureRandom password = new <PASSWORD>();
            bigInteger = new BigInteger(64, password);
        }
        return bigInteger;
    }

    /**
     * Register this account/device pair within the server.
     */
    public void register(final Context context, final String regId) {
        Log.i(TAG, "registering device (regId = " + regId + ")");
        DataAccessUser user = new DataAccessUser(regId, null, getPassword().toString(32));
        long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
        // Once GCM returns a registration id, we need to register it in the
        // app server. The registration will be repeated maximal 5 times.
        for (int i = 1; i <= MAX_ATTEMPTS; i++) {
            Log.d(TAG, "Attempt #" + i + " to register");
            try {
                displayMessage(context, context.getString(
                        R.string.server_registering, i, MAX_ATTEMPTS));
                Ion.with(context)
                        .load("POST", SERVER_URL)
                        .setJsonPojoBody(user)
                        .asJsonObject()
                        .setCallback(new FutureCallback<JsonObject>() {
                            @Override
                            public void onCompleted(Exception e, JsonObject result) {
                                Log.i(TAG, "Registration delivered to APP Server");
                            }
                        });
                String message = context.getString(R.string.server_registered);
                displayMessage(context, message);
                return;
            } catch (Exception e) {
                Log.e(TAG, "Failed to register on attempt " + i + ":" + e);
                if (i == MAX_ATTEMPTS) {<commit_msg>BugFix beim Tags Übertragung<commit_after>package edu.kit.isco.kitalumniapp.gcm;


/*
 * Copyright 2012 Google Inc.
 *
 * 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.
 */

import android.content.Context;
import android.content.Intent;
import android.util.Log;

import com.google.gson.JsonObject;
import com.koushikdutta.async.future.FutureCallback;
import com.koushikdutta.ion.Ion;

import java.math.BigInteger;
import java.security.SecureRandom;
import java.util.ArrayList;
import java.util.List;
import java.util.Random;

import edu.kit.isco.kitalumniapp.R;
import edu.kit.isco.kitalumniapp.dbObjects.DataAccessTag;
import edu.kit.isco.kitalumniapp.dbObjects.DataAccessUser;

/**
 * Binds in one place methods for register, unregister and updating for notifications.
 * Created by <NAME> on 14.1.2015 г..
 */
public class ServerUtilities {

    static final String SERVER_URL = "yourURL";
    /**
     * Tag used on log messages.
     */
    static final String TAG = "GCMDemo";
    /**
     * Intent used to display a message in the screen.
     */
    static final String DISPLAY_MESSAGE_ACTION =
            "com.google.android.gcm.demo.app.DISPLAY_MESSAGE";
    /**
     * Intent's extra that contains the message to be displayed.
     */
    static final String EXTRA_MESSAGE = "message";
    private static final int MAX_ATTEMPTS = 5;
    private static final int BACKOFF_MILLI_SECONDS = 2000;
    private static final Random random = new Random();
    BigInteger bigInteger;
    private List<DataAccessTag> fullTagsList;

    /**
     * Notifies UI to display a message.
     * <p/>
     * This method is defined in the common helper because it's used both by
     * the UI and the background service.
     *
     * @param context application's context.
     * @param message message to be displayed.
     */
    static void displayMessage(Context context, String message) {
        Intent intent = new Intent(DISPLAY_MESSAGE_ACTION);
        intent.putExtra(EXTRA_MESSAGE, message);
        context.sendBroadcast(intent);
    }

    /**
     * Checks if the list has been created and if it hasn`t, then it puts all Tags in one single list.
     * @return fully populated List of DataAccessTags
     */

    private List<DataAccessTag> populateTagList() {
        if (fullTagsList == null) {
            fullTagsList = new ArrayList<>();
            fullTagsList.add(DataAccessTag.DATA_ADMINISTRATION);
            fullTagsList.add(DataAccessTag.TRAINEE);
            fullTagsList.add(DataAccessTag.CLERK);
            fullTagsList.add(DataAccessTag.GRADUAND);
            fullTagsList.add(DataAccessTag.DOCTORAND);
            fullTagsList.add(DataAccessTag.ENGINEER);
            fullTagsList.add(DataAccessTag.INDUSTRIAL);
            fullTagsList.add(DataAccessTag.SALES_OCCUPATION);
            fullTagsList.add(DataAccessTag.THRESHOLD_WORKER);
            fullTagsList.add(DataAccessTag.PROFESSOR);
            fullTagsList.add(DataAccessTag.TECHNICAL_EMPLOYEE);
            fullTagsList.add(DataAccessTag.STUDENT_RESEARCH_PROJECT);
            fullTagsList.add(DataAccessTag.ADMINISTRATION);
            fullTagsList.add(DataAccessTag.SCIENTIST);
            fullTagsList.add(DataAccessTag.OTHERS);
        }

        return fullTagsList;
    }

    /**
     * Generates password the first time and then returns it
     * every other time.
     * @return password
     */
    private BigInteger getPassword() {
        if (bigInteger == null) {
            SecureRandom password = new SecureRandom();
            bigInteger = new BigInteger(64, password);
        }
        return bigInteger;
    }

    /**
     * Register this account/device pair within the server.
     */
    public void register(final Context context, final String regId) {
        Log.i(TAG, "registering device (regId = " + regId + ")");
        DataAccessUser user = new DataAccessUser(regId, populateTagList(), getPassword().toString(32));
        long backoff = BACKOFF_MILLI_SECONDS + random.nextInt(1000);
        // Once GCM returns a registration id, we need to register it in the
        // app server. The registration will be repeated maximal 5 times.
        for (int i = 1; i <= MAX_ATTEMPTS; i++) {
            Log.d(TAG, "Attempt #" + i + " to register");
            try {
                displayMessage(context, context.getString(
                        R.string.server_registering, i, MAX_ATTEMPTS));
                Ion.with(context)
                        .load("POST", SERVER_URL)
                        .setJsonPojoBody(user)
                        .asJsonObject()
                        .setCallback(new FutureCallback<JsonObject>() {
                            @Override
                            public void onCompleted(Exception e, JsonObject result) {
                                Log.i(TAG, "Registration delivered to APP Server");
                            }
                        });
                String message = context.getString(R.string.server_registered);
                displayMessage(context, message);
                return;
            } catch (Exception e) {
                Log.e(TAG, "Failed to register on attempt " + i + ":" + e);
                if (i == MAX_ATTEMPTS) {
{'fraction_non_alphanumeric': 0.0892934924645004, 'fraction_numerical': 0.005226936144263438, 'mean_word_length': 2.3655819407798298, 'pattern_counts': {'":': 2, '<': 18, '<?xml version=': 0, '>': 16, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '2653907', 'n_tokens_mistral': 3244, 'n_tokens_neox': 3116, 'n_words': 888}