com.android.projectz.teamrocket.thebusapp.util.Device.java Source code

Java tutorial

Introduction

Here is the source code for com.android.projectz.teamrocket.thebusapp.util.Device.java

Source

package com.android.projectz.teamrocket.thebusapp.util;
/*
 Copyright (C) 2016-2017  TeamRocket
    
 This program is free software: you can redistribute it and/or modify
 it under the terms of the GNU General Public License as published by
 the Free Software Foundation, either version 3 of the License, or
 (at your option) any later version.
    
 This program is distributed in the hope that it will be useful,
 but WITHOUT ANY WARRANTY; without even the implied warranty of
 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 GNU General Public License for more details.
    
 You should have received a copy of the GNU General Public License
 along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

import android.Manifest;
import android.accounts.Account;
import android.accounts.AccountManager;
import android.content.Context;
import android.content.pm.PackageManager;
import android.os.Build;
import android.os.Bundle;
import android.provider.Settings;
import android.support.v4.app.ActivityCompat;
import android.telephony.TelephonyManager;

import com.android.projectz.teamrocket.thebusapp.activities.SplashScreenActivity;

import java.util.LinkedList;
import java.util.List;

/**
 * Device:
 * classe che permette di prendere informazioni sul device che esegue l'app
 * i metodi saranno statici visto che tali metodi vengono usati da altre parti
 * <p>
 * Created by simone98dm on 05/12/16.
 */

public class Device {

    /**
     * permette di prendere il codice IMEI del telefono
     *
     * @param context
     * @return
     */
    public static String getDeviceIMEI(Context context) {
        if (ActivityCompat.checkSelfPermission(context,
                Manifest.permission.READ_PHONE_STATE) != PackageManager.PERMISSION_GRANTED) {
            return "ERR_PERMISSION";
        }
        String identifier = null;
        TelephonyManager tm = (TelephonyManager) context.getSystemService(Context.TELEPHONY_SERVICE);
        if (tm != null) {
            identifier = tm.getDeviceId();
        }

        if (identifier == null || identifier.length() == 0) {
            identifier = Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
        }

        return identifier;
    }

    /**
     * permette di prendere il probabile indirizzo email del telefono
     *
     * @param context
     * @return
     */
    public static String getDeviceEMAIL(Context context) {
        AccountManager manager = AccountManager.get(context);
        if (ActivityCompat.checkSelfPermission(context,
                Manifest.permission.GET_ACCOUNTS) != PackageManager.PERMISSION_GRANTED) {
            return "ERR_PERMISSION";
        }
        Account[] accounts = manager.getAccountsByType("com.google");
        List<String> possibleEmails = new LinkedList<String>();

        for (Account account : accounts) {
            possibleEmails.add(account.name);
        }
        if (!possibleEmails.isEmpty() && possibleEmails.get(0) != null) {
            String email = possibleEmails.get(0);
            String[] parts = email.split("@");

            if (parts.length > 1)
                return parts[0];
        }
        return null;
    }

}