2
0
mirror of https://github.com/KDE/kdeconnect-android synced 2025-09-05 16:45:08 +00:00
Files
kdeconnect-android/src/org/kde/kdeconnect/Helpers/DeviceHelper.java

91 lines
3.6 KiB
Java
Raw Normal View History

2014-11-16 23:14:06 -08:00
/*
2020-08-17 16:17:20 +02:00
* SPDX-FileCopyrightText: 2014 Albert Vaca Cintora <albertvaka@gmail.com>
2014-11-16 23:14:06 -08:00
*
2020-08-17 16:17:20 +02:00
* SPDX-License-Identifier: GPL-2.0-only OR GPL-3.0-only OR LicenseRef-KDE-Accepted-GPL
2014-11-16 23:14:06 -08:00
*/
package org.kde.kdeconnect.Helpers;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.preference.PreferenceManager;
import android.provider.Settings;
import android.util.Log;
import com.jaredrummler.android.device.DeviceName;
import org.kde.kdeconnect.Device;
public class DeviceHelper {
2020-07-18 20:01:23 +02:00
public static final int ProtocolVersion = 7;
public static final String KEY_DEVICE_NAME_PREFERENCE = "device_name_preference";
private static boolean fetchingName = false;
private static boolean isTablet() {
Configuration config = Resources.getSystem().getConfiguration();
//This assumes that the values for the screen sizes are consecutive, so XXLARGE > XLARGE > LARGE
return ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);
}
private static boolean isTv(Context context) {
int uiMode = context.getResources().getConfiguration().uiMode;
return (uiMode & Configuration.UI_MODE_TYPE_MASK) == Configuration.UI_MODE_TYPE_TELEVISION;
}
public static Device.DeviceType getDeviceType(Context context) {
if (isTv(context)) {
return Device.DeviceType.Tv;
} else if (isTablet()) {
return Device.DeviceType.Tablet;
} else {
return Device.DeviceType.Phone;
}
}
//It returns getAndroidDeviceName() if no user-defined name has been set with setDeviceName().
public static String getDeviceName(Context context) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
2019-07-20 13:32:58 +02:00
// Could use preferences.contains but would need to check for empty String anyway.
String deviceName = preferences.getString(KEY_DEVICE_NAME_PREFERENCE, "");
if (deviceName.isEmpty()) {
//DeviceName.init(context); // Needed in DeviceName 2.x +
if (!fetchingName) {
fetchingName = true;
DeviceHelper.backgroundFetchDeviceName(context); //Starts a background thread that will eventually update the shared pref
}
return DeviceName.getDeviceName(); //Temp name while we fetch it from the internet
}
return deviceName;
}
private static void backgroundFetchDeviceName(final Context context) {
DeviceName.with(context).request((info, error) -> {
fetchingName = false;
2019-07-20 13:32:58 +02:00
if (error != null) {
Log.e("DeviceHelper", "Error fetching device name");
error.printStackTrace();
}
if (info != null) {
String deviceName = info.getName();
Log.i("DeviceHelper", "Got device name: " + deviceName);
// Update the shared preference. Places that display the name should be listening to this change and update it
setDeviceName(context, deviceName);
}
});
}
public static void setDeviceName(Context context, String name) {
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
preferences.edit().putString(KEY_DEVICE_NAME_PREFERENCE, name).apply();
}
public static String getDeviceId(Context context) {
return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
}
}