<commit_before>package com.garethevans.church.opensongtablet;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Handler;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.collection.SimpleArrayMap;

import com.google.android.gms.nearby.Nearby;
import com.google.android.gms.nearby.connection.AdvertisingOptions;
import com.google.android.gms.nearby.connection.ConnectionInfo;
import com.google.android.gms.nearby.connection.ConnectionLifecycleCallback;
import com.google.android.gms.nearby.connection.ConnectionResolution;
import com.google.android.gms.nearby.connection.ConnectionsStatusCodes;
import com.google.android.gms.nearby.connection.DiscoveredEndpointInfo;
import com.google.android.gms.nearby.connection.DiscoveryOptions;
import com.google.android.gms.nearby.connection.EndpointDiscoveryCallback;
import com.google.android.gms.nearby.connection.Payload;
import com.google.android.gms.nearby.connection.PayloadCallback;
import com.google.android.gms.nearby.connection.PayloadTransferUpdate;
import com.google.android.gms.nearby.connection.Strategy;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Objects;
import java.util.UUID;

public class NearbyConnections implements NearbyInterface {

    Context context;
    OptionMenuListeners optionMenuListeners;
    Preferences preferences;
    StorageAccess storageAccess;
    ProcessSong processSong;
    SQLiteHelper sqLiteHelper;

    NearbyReturnActionsInterface nearbyReturnActionsInterface;

    private final ArrayList<String> connectedEndPoints;
    private final ArrayList<String> connectedEndPointsNames;
    private final ArrayList<String> connectedDeviceIds;

    NearbyConnections(Context context, Preferences preferences, StorageAccess storageAccess,
                      ProcessSong processSong, OptionMenuListeners optionMenuListeners,
                      SQLiteHelper sqLiteHelper) {
        this.context = context;
        this.optionMenuListeners = optionMenuListeners;
        this.preferences = preferences;
        this.storageAccess = storageAccess;
        this.processSong = processSong;
        this.sqLiteHelper = sqLiteHelper;
        connectedEndPoints = new ArrayList<>();
        connectedEndPointsNames = new ArrayList<>();
        connectedDeviceIds = new ArrayList<>();
        nearbyReturnActionsInterface = (NearbyReturnActionsInterface) context;
    }

    // The stuff used for Google Nearby for connecting devices
    String serviceId = "com.garethevans.church.opensongtablet";
    private void updateConnectionLog(String newMessage) {
        if (newMessage!=null) {
            StaticVariables.connectionLog += newMessage + "\n";
            try {
                optionMenuListeners.updateConnectionsLog();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void startAdvertising() {
        Log.d("NearbyConnections","Nearby.getConnectionsClient(context)="+Nearby.getConnectionsClient(context));
        Log.d("d","startAdvertising()");
        if (!StaticVariables.isConnected) {
            AdvertisingOptions advertisingOptions =
                    new AdvertisingOptions.Builder().setStrategy(Strategy.P2P_CLUSTER).build();
            Nearby.getConnectionsClient(context)
                    .startAdvertising(
                            getUserNickname(), serviceId, connectionLifecycleCallback(), advertisingOptions)
                    .addOnSuccessListener(
                            (Void unused) -> {
                                // We're advertising!
                                updateConnectionLog(context.getResources().getString(R.string.connections_broadcast) + " " + getUserNickname());

                            })
                    .addOnFailureListener(
                            (Exception e) -> {
                                // We were unable to start advertising.
                                updateConnectionLog(context.getResources().getString(R.string.connections_failure) + " " + getUserNickname());
                                e.printStackTrace();
                            });
        }
    }
    @Override
    public void startDiscovery() {
        Log.d("d","startDiscovery()");
        if (!StaticVariables.isConnected) {
            DiscoveryOptions discoveryOptions = new DiscoveryOptions.Builder().setStrategy(Strategy.P2P_CLUSTER).build();
            Nearby.getConnectionsClient(context)
                    .startDiscovery(serviceId, endpointDiscoveryCallback(), discoveryOptions)
                    .addOnSuccessListener(
                            (Void unused) -> {
                                // We're discovering!
                                updateConnectionLog(context.getResources().getString(R.string.connections_discover));
                            })
                    .addOnFailureListener(
                            (Exception e) -> {
                                // We're unable to start discovering.
                                updateConnectionLog(context.getResources().getString(R.string.connections_discover_stop));
                                stopDiscovery();
                                e.printStackTrace();
                            });
        }
    }
    @Override
    public void stopAdvertising() {
        Nearby.getConnectionsClient(context).stopAdvertising();
        Log.d("d","stopAdvertising()");
    }
    @Override
    public void stopDiscovery() {
        Nearby.getConnectionsClient(context).stopDiscovery();
        Log.d("d","stopDiscovery()");
    }
    private String getDeviceNameFromId(String endpointId) {
        // When requesting connections, the proper device name is stored in an arraylist like endpointId__deviceName
        for (String s:connectedEndPointsNames) {
            if (s.startsWith(endpointId+"__")) {<commit_msg>Updated Nearby to allow previously connected devices to reconnect without permission request<commit_after>package com.garethevans.church.opensongtablet;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.net.Uri;
import android.os.Handler;
import android.os.ParcelFileDescriptor;
import android.provider.DocumentsContract;
import android.util.Log;

import androidx.annotation.NonNull;
import androidx.collection.SimpleArrayMap;

import com.google.android.gms.nearby.Nearby;
import com.google.android.gms.nearby.connection.AdvertisingOptions;
import com.google.android.gms.nearby.connection.ConnectionInfo;
import com.google.android.gms.nearby.connection.ConnectionLifecycleCallback;
import com.google.android.gms.nearby.connection.ConnectionResolution;
import com.google.android.gms.nearby.connection.ConnectionsStatusCodes;
import com.google.android.gms.nearby.connection.DiscoveredEndpointInfo;
import com.google.android.gms.nearby.connection.DiscoveryOptions;
import com.google.android.gms.nearby.connection.EndpointDiscoveryCallback;
import com.google.android.gms.nearby.connection.Payload;
import com.google.android.gms.nearby.connection.PayloadCallback;
import com.google.android.gms.nearby.connection.PayloadTransferUpdate;
import com.google.android.gms.nearby.connection.Strategy;

import java.io.FileInputStream;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.Objects;
import java.util.UUID;

public class NearbyConnections implements NearbyInterface {

    Context context;
    OptionMenuListeners optionMenuListeners;
    Preferences preferences;
    StorageAccess storageAccess;
    ProcessSong processSong;
    SQLiteHelper sqLiteHelper;

    NearbyReturnActionsInterface nearbyReturnActionsInterface;

    private final ArrayList<String> connectedEndPoints;
    private final ArrayList<String> connectedEndPointsNames;
    private final ArrayList<String> connectedDeviceIds;

    NearbyConnections(Context context, Preferences preferences, StorageAccess storageAccess,
                      ProcessSong processSong, OptionMenuListeners optionMenuListeners,
                      SQLiteHelper sqLiteHelper) {
        this.context = context;
        this.optionMenuListeners = optionMenuListeners;
        this.preferences = preferences;
        this.storageAccess = storageAccess;
        this.processSong = processSong;
        this.sqLiteHelper = sqLiteHelper;
        connectedEndPoints = new ArrayList<>();
        connectedEndPointsNames = new ArrayList<>();
        connectedDeviceIds = new ArrayList<>();
        nearbyReturnActionsInterface = (NearbyReturnActionsInterface) context;
    }

    // The stuff used for Google Nearby for connecting devices
    String serviceId = "com.garethevans.church.opensongtablet";
    private void updateConnectionLog(String newMessage) {
        if (newMessage!=null) {
            StaticVariables.connectionLog += newMessage + "\n";
            try {
                optionMenuListeners.updateConnectionsLog();
            } catch (Exception e) {
                e.printStackTrace();
            }
        }
    }
    @Override
    public void startAdvertising() {
        Log.d("NearbyConnections","Nearby.getConnectionsClient(context)="+Nearby.getConnectionsClient(context));
        Log.d("d","startAdvertising()");
        if (!StaticVariables.isConnected) {
            AdvertisingOptions advertisingOptions =
                    new AdvertisingOptions.Builder().setStrategy(Strategy.P2P_CLUSTER).build();
            Nearby.getConnectionsClient(context)
                    .startAdvertising(
                            getUserNickname(), serviceId, connectionLifecycleCallback(), advertisingOptions)
                    .addOnSuccessListener(
                            (Void unused) -> {
                                // We're advertising!
                                updateConnectionLog(context.getResources().getString(R.string.connections_broadcast) + " " + getUserNickname());

                            })
                    .addOnFailureListener(
                            (Exception e) -> {
                                // We were unable to start advertising.
                                updateConnectionLog(context.getResources().getString(R.string.connections_failure) + " " + getUserNickname());
                                e.printStackTrace();
                            });
        }
    }
    @Override
    public void startDiscovery() {
        Log.d("d","startDiscovery()");
        if (!StaticVariables.isConnected) {
            DiscoveryOptions discoveryOptions = new DiscoveryOptions.Builder().setStrategy(Strategy.P2P_CLUSTER).build();
            Nearby.getConnectionsClient(context)
                    .startDiscovery(serviceId, endpointDiscoveryCallback(), discoveryOptions)
                    .addOnSuccessListener(
                            (Void unused) -> {
                                // We're discovering!
                                updateConnectionLog(context.getResources().getString(R.string.connections_discover));
                            })
                    .addOnFailureListener(
                            (Exception e) -> {
                                // We're unable to start discovering.
                                updateConnectionLog(context.getResources().getString(R.string.connections_discover_stop));
                                stopDiscovery();
                                e.printStackTrace();
                            });
        }
    }
    @Override
    public void stopAdvertising() {
        Nearby.getConnectionsClient(context).stopAdvertising();
        updateConnectionLog(context.getResources().getString(R.string.connections_service_stop));
        Log.d("d","stopAdvertising()");
    }
    @Override
    public void stopDiscovery() {
        Nearby.getConnectionsClient(context).stopDiscovery();
        updateConnectionLog(context.getResources().getString(R.string.connections_discover_stop));
        Log.d("d","stopDiscovery()");
    }
    private String getDeviceNameFromId(String endpointId) {
        // When requesting connections, the proper device name is stored in an arraylist like endpointId__deviceName
        for (String s:connectedEndPointsNames) {
            if (s.startsWith(endpointId+"__")) {
{'fraction_non_alphanumeric': 0.08842239185750636, 'fraction_numerical': 0.0003180661577608143, 'mean_word_length': 2.515092230296255, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 8, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7153502', 'n_tokens_mistral': 3117, 'n_tokens_neox': 2992, 'n_words': 529}