T assertNotNull(T object, String name) {
if (object == null) {
throw new NullPointerException(name + " cannot be null");
}
return object;
}
/**
* Asserts that the {@code string} is not empty or {@code null}. It throws an
* {@link IllegalArgumentException} if it is.
*
* @param string The string to inspect
* @param name A name for the string to be used in the NPE message
*/
public static void assertNotEmpty(String string, String name) {
if (TextUtils.isEmpty(string)) {
throw new IllegalArgumentException(name + " cannot be null");
}
}
/**
* Builds an {@link com.google.android.gms.wearable.Asset} from a bitmap. The image that we get
* back from the camera in "data" is a thumbnail size. Typically, your image should not exceed
* 320x320 and if you want to have zoom and parallax effect in your app, limit the size of your
* image to 640x400. Resize your image before transferring to your wearable device.
*/
public static Asset toAsset(Bitmap bitmap) {
ByteArrayOutputStream byteStream = null;
try {
byteStream = new ByteArrayOutputStream();
bitmap.compress(Bitmap.CompressFormat.PNG, 100, byteStream);
return Asset.createFromBytes(byteStream.toByteArray());
} finally {
if (null != byteStream) {
try {
byteStream.close();
} catch (IOException e) {
// ignore
}
}
}
}
/**
* Determines if the wear device has a built-in speaker or not.
*
* Important: This method should only be called on a wear device; the return value on
* a non-wear device can be trusted if and only if the device is running android version M+.
*/
public static final boolean wearHasSpeaker(Context context) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.M) {
PackageManager packageManager = context.getPackageManager();
// The results from AudioManager.getDevices can't be trusted unless the device
// advertises FEATURE_AUDIO_OUTPUT.
if (!packageManager.hasSystemFeature(PackageManager.FEATURE_AUDIO_OUTPUT)) {
return false;
}
AudioManager audioManager = (AudioManager) context
.getSystemService(Context.AUDIO_SERVICE);
AudioDeviceInfo[] devices = audioManager.getDevices(AudioManager.GET_DEVICES_OUTPUTS);
for (AudioDeviceInfo device : devices) {
if (device.getType() == AudioDeviceInfo.TYPE_BUILTIN_SPEAKER) {
return true;
}
}
}
return false;
}
/**
* Returns a random UUID
*/
public static String buildUuid() {
return UUID.randomUUID().toString();
}
/**
* A simple wrapper around Log.d
*/
public static void LOGD(String tag, String message) {
if (DEBUG || Log.isLoggable(tag, Log.DEBUG)) {
Log.d(tag, "[v" + WearManager.getInstance().getVersion() + "] " + message);
}
}
}