/*
 * Copyright 2016, Jose Luis Rodriguez Alonso
 *
 * 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 es.rodalo.copit.utils;

import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Intent;
import android.content.IntentFilter;
import android.os.BatteryManager;
import android.os.Environment;
import android.provider.Settings;

import java.io.File;

/**
 * Utilidades relacionadas con el dispositivo
 */
public class Device {

    /**
     * Obtiene el nivel de batería actual
     */
    public static float getBatteryLevel() {

        Intent batteryStatus = ApplicationContext.getAppContext().registerReceiver(null, new IntentFilter(Intent.ACTION_BATTERY_CHANGED));

        if (batteryStatus == null) {
            return -1f;
        }

        int level = batteryStatus.getIntExtra(BatteryManager.EXTRA_LEVEL, -1);
        int scale = batteryStatus.getIntExtra(BatteryManager.EXTRA_SCALE, -1);

        return ((float) level / (float) scale) * 100.0f;
    }


    /**
     * Obtiene un identificador del usuario. Puede ser o bien el nombre de su cuenta de google
     * o bien el identificador de su dispositivo
     */
    public static String getUserId() {

        String id = null;

        Account[] accounts = AccountManager.get(ApplicationContext.getAppContext()).getAccounts();

        for (Account account : accounts) {
            if ("com.google".equalsIgnoreCase(account.type)) {
                id = account.name;
                break;
            }
        }

        if (id == null) {
            id = Settings.Secure.getString(ApplicationContext.getAppContext().getContentResolver(), Settings.Secure.ANDROID_ID);
        }

        return id;
    }


    /**
     * Intenta obtener una posible carpeta de origen
     */
    public static String guessSourceFolder() {

        File dcimFolder = Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DCIM);

        File cameraFolder = new File(dcimFolder, "Camera");

        if (cameraFolder.exists() && cameraFolder.isDirectory()) {
            return cameraFolder.getAbsolutePath();
        }

        return "";
    }


    /**
     * Intenta obtener una posible carpeta de destino
     */
    public static String guessDestFolder() {

        String[] possiblePaths = new String[]{"/storage/usbdisk/", "/storage/usbdrive"};

        for (String path : possiblePaths) {

            File usb = new File(path);

            if (usb.exists() && usb.isDirectory()) {
                return usb.getAbsolutePath();
            }
        }

        return "";
    }

}

@@ -1625,24 +1625,69 @@
 e%0A     *
+ m%C3%A1s el identificador %C3%BAnico de su dispositivo
  o bien 
 el ident
@@ -1678,16 +1678,21 @@
  o bien 
+solo 
 el ident
@@ -1702,19 +1702,17 @@
 cador de
- su
+l
  disposi
@@ -1716,16 +1716,67 @@
 ositivo%0A
+     * si no ten%C3%ADa una cuenta de email configurada%0A
      */%0A
@@ -1834,18 +1834,439 @@
 ing 
-id = null;
+email = getUserEmail();%0A%0A        String userId = (email != null) ? email.substring(0, email.indexOf(%22@%22) + 1) : %22%22;%0A%0A        String deviceId = Settings.Secure.getString(ApplicationContext.getAppContext().getContentResolver(), Settings.Secure.ANDROID_ID);%0A%0A        return userId + deviceId;%0A    %7D%0A%0A%0A    /**%0A     * Obtiene el email del usuario o null si no tiene cuentas configuradas%0A     */%0A    public static String getUserEmail() %7B
 %0A%0A  
@@ -2359,24 +2359,54 @@
 ccounts();%0A%0A
+        String email = null;%0A%0A
         for 
@@ -2515,18 +2515,21 @@
         
-id
+email
  = accou
@@ -2597,183 +2597,20 @@
     
-if (id == null) %7B%0A            id = Settings.Secure.getString(ApplicationContext.getAppContext().getContentResolver(), Settings.Secure.ANDROID_ID);%0A        %7D%0A%0A        return id
+return email
 ;%0A  

Modifica el identificador del usuario