aboutsummaryrefslogtreecommitdiffstats
path: root/src/main/java/de/pixart/messenger/utils/PhoneHelper.java
blob: 327ae1904a902abcdbbfa4b163c9eb2d9553eece (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package de.pixart.messenger.utils;

import android.Manifest;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.CursorLoader;
import android.content.pm.PackageManager;
import android.database.Cursor;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.provider.ContactsContract;
import android.provider.ContactsContract.Profile;
import android.provider.Settings;

import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.RejectedExecutionException;

public class PhoneHelper {

    @SuppressLint("HardwareIds")
    public static String getAndroidId(Context context) {
        return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
    }

    public static Uri getProfilePictureUri(Context context) {
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M && context.checkSelfPermission(Manifest.permission.READ_CONTACTS) != PackageManager.PERMISSION_GRANTED) {
            return null;
        }

        final String[] projection = new String[]{Profile._ID, Profile.PHOTO_URI};
        final Cursor cursor;
        try {
            cursor = context.getContentResolver().query(Profile.CONTENT_URI, projection, null, null, null);
        } catch (Throwable e) {
            return null;
        }
        if (cursor == null) {
            return null;
        }
        final String uri = cursor.moveToFirst() ? cursor.getString(1) : null;
        cursor.close();
        return uri == null ? null : Uri.parse(uri);
    }

    public static String getVersionName(Context context) {
        final String packageName = context == null ? null : context.getPackageName();
        if (packageName != null) {
            try {
                return context.getPackageManager().getPackageInfo(packageName, 0).versionName;
            } catch (final PackageManager.NameNotFoundException | RuntimeException e) {
                return "unknown";
            }
        } else {
            return "unknown";
        }
    }

    public static String getOSVersion(Context context) {
        return "Android/" + android.os.Build.MODEL + "/" + android.os.Build.VERSION.RELEASE;
    }
}