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
|
|
|
*/
|
|
|
|
|
2013-11-06 18:33:38 +01:00
|
|
|
package org.kde.kdeconnect.Helpers;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2015-09-03 03:04:36 -07:00
|
|
|
import android.content.Context;
|
|
|
|
import android.content.SharedPreferences;
|
2013-11-06 18:33:38 +01:00
|
|
|
import android.content.res.Configuration;
|
|
|
|
import android.content.res.Resources;
|
2015-09-03 03:04:36 -07:00
|
|
|
import android.preference.PreferenceManager;
|
2015-09-10 09:01:12 -07:00
|
|
|
import android.provider.Settings;
|
2015-06-14 00:57:21 -07:00
|
|
|
import android.util.Log;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2019-06-27 10:12:22 -04:00
|
|
|
import com.jaredrummler.android.device.DeviceName;
|
2018-05-10 12:34:28 +02:00
|
|
|
|
2019-06-27 10:12:22 -04:00
|
|
|
import org.kde.kdeconnect.Device;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2013-11-06 18:33:38 +01:00
|
|
|
public class DeviceHelper {
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2020-07-18 20:01:23 +02:00
|
|
|
public static final int ProtocolVersion = 7;
|
|
|
|
|
2019-01-29 09:26:42 +00:00
|
|
|
public static final String KEY_DEVICE_NAME_PREFERENCE = "device_name_preference";
|
2015-09-03 03:04:36 -07:00
|
|
|
|
2019-06-27 10:12:22 -04:00
|
|
|
private static boolean fetchingName = false;
|
2013-08-16 10:31:01 +02:00
|
|
|
|
2018-05-10 12:34:28 +02:00
|
|
|
private static boolean isTablet() {
|
2013-11-06 18:33:38 +01:00
|
|
|
Configuration config = Resources.getSystem().getConfiguration();
|
|
|
|
//This assumes that the values for the screen sizes are consecutive, so XXLARGE > XLARGE > LARGE
|
2018-03-03 15:51:35 +01:00
|
|
|
return ((config.screenLayout & Configuration.SCREENLAYOUT_SIZE_MASK) >= Configuration.SCREENLAYOUT_SIZE_LARGE);
|
2013-11-06 18:33:38 +01:00
|
|
|
}
|
|
|
|
|
2018-05-10 12:34:28 +02:00
|
|
|
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;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-09-03 03:04:36 -07:00
|
|
|
//It returns getAndroidDeviceName() if no user-defined name has been set with setDeviceName().
|
2018-03-03 16:06:52 +01:00
|
|
|
public static String getDeviceName(Context context) {
|
2015-09-03 03:04:36 -07:00
|
|
|
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.
|
2015-09-03 03:04:36 -07:00
|
|
|
String deviceName = preferences.getString(KEY_DEVICE_NAME_PREFERENCE, "");
|
2018-03-03 16:06:52 +01:00
|
|
|
if (deviceName.isEmpty()) {
|
2023-03-18 20:39:37 +01:00
|
|
|
//DeviceName.init(context); // Needed in DeviceName 2.x +
|
2019-06-27 10:12:22 -04:00
|
|
|
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
|
2015-09-03 03:04:36 -07:00
|
|
|
}
|
|
|
|
return deviceName;
|
|
|
|
}
|
|
|
|
|
2019-06-27 10:12:22 -04:00
|
|
|
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);
|
|
|
|
}
|
2019-06-27 10:12:22 -04:00
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2018-03-03 16:06:52 +01:00
|
|
|
public static void setDeviceName(Context context, String name) {
|
2015-09-03 03:04:36 -07:00
|
|
|
SharedPreferences preferences = PreferenceManager.getDefaultSharedPreferences(context);
|
|
|
|
preferences.edit().putString(KEY_DEVICE_NAME_PREFERENCE, name).apply();
|
|
|
|
}
|
|
|
|
|
2015-09-10 09:01:12 -07:00
|
|
|
public static String getDeviceId(Context context) {
|
|
|
|
return Settings.Secure.getString(context.getContentResolver(), Settings.Secure.ANDROID_ID);
|
|
|
|
}
|
2013-08-16 10:31:01 +02:00
|
|
|
}
|