Look at the SampleControlExtension project and see how it uses:
DeviceInfoHelper.isSmartWatch2ApiAndScreenDetected()
But you can call that from anywhere if you like.
This is how the SampleExtensionService decides SW1 or SW2:
@Override
public ControlExtension createControlExtension(String hostAppPackageName) {
// First we check if the API level and screen size required for
// SampleControlSmartWatch2 is supported
boolean advancedFeaturesSupported = DeviceInfoHelper.isSmartWatch2ApiAndScreenDetected(
this, hostAppPackageName);
if (advancedFeaturesSupported) {
return new SampleControlSmartWatch2(hostAppPackageName, this, new Handler());
} else {
// If not we return an API level 1 control based on screen size
final int controlSWWidth = SampleControlSmartWatch.getSupportedControlWidth(this);
final int controlSWHeight = SampleControlSmartWatch.getSupportedControlHeight(this);
final int controlSWHPWidth = SampleControlSmartWirelessHeadsetPro
.getSupportedControlWidth(this);
final int controlSWHPHeight = SampleControlSmartWirelessHeadsetPro
.getSupportedControlHeight(this);
for (DeviceInfo device : RegistrationAdapter.getHostApplication(this,
hostAppPackageName)
.getDevices()) {
for (DisplayInfo display : device.getDisplays()) {
if (display.sizeEquals(controlSWWidth, controlSWHeight)) {
return new SampleControlSmartWatch(hostAppPackageName, this, new Handler());
} else if (display.sizeEquals(controlSWHPWidth, controlSWHPHeight)) {
return new SampleControlSmartWirelessHeadsetPro(hostAppPackageName, this,
new Handler());
}
}
}
throw new IllegalArgumentException("No control for: " + hostAppPackageName);
}
}
Personally, I find the use of resources unnecessary, so this is how I choose to do it. I have an enum
defined, I use the similar code to the above querying isSmartWatch2ApiAndScreenDetected
then I pass the correct enum value around.
import android.graphics.Bitmap.Config;
public enum ScreenConfiguration {
SMARTWATCH1(128, 128, Config.RGB_565), SMARTWATCH2(220, 176, Config.RGB_565);
private final int mWidth;
private final int mHeight;
private final Config mBitmapConfig;
private ScreenConfiguration(int width, int height, Config bitmapConfig) {
mWidth = width;
mHeight = height;
mBitmapConfig = bitmapConfig;
}
public int getWidth() {
return mWidth;
}
public int getHeight() {
return mHeight;
}
public Config getBitmapConfig() {
return mBitmapConfig;
}
}
EDIT You must tell the system you want to support smartwatch 2.
In your RegistrationInformation
class:
@Override
public int getTargetControlApiVersion() {
return 2;
}
If that's 1
, you'll only get false for isSmartWatch2ApiAndScreenDetected
.
EDIT Part 2 How to use the enum
@Override
public ControlExtension createControlExtension(String hostAppPackageName) {
// First we check if the API level and screen size required for
// SampleControlSmartWatch2 is supported
boolean advancedFeaturesSupported = DeviceInfoHelper.isSmartWatch2ApiAndScreenDetected(
this, hostAppPackageName);
if (advancedFeaturesSupported) {
return new SampleControlSmartWatch(ScreenConfiguration.SMARTWATCH2, hostAppPackageName, this, new Handler());
} else {
// If not we return an API level 1 control based on screen size
final int controlSWWidth = SampleControlSmartWatch.getSupportedControlWidth(this);
final int controlSWHeight = SampleControlSmartWatch.getSupportedControlHeight(this);
final int controlSWHPWidth = SampleControlSmartWirelessHeadsetPro
.getSupportedControlWidth(this);
final int controlSWHPHeight = SampleControlSmartWirelessHeadsetPro
.getSupportedControlHeight(this);
for (DeviceInfo device : RegistrationAdapter.getHostApplication(this,
hostAppPackageName)
.getDevices()) {
for (DisplayInfo display : device.getDisplays()) {
if (display.sizeEquals(controlSWWidth, controlSWHeight)) {
return new SampleControlSmartWatch(ScreenConfiguration.SMARTWATCH1, hostAppPackageName, this, new Handler());
} else if (display.sizeEquals(controlSWHPWidth, controlSWHPHeight)) {
return new SampleControlSmartWirelessHeadsetPro(hostAppPackageName, this,
new Handler());
}
}
}
throw new IllegalArgumentException("No control for: " + hostAppPackageName);
}
}
Mostly the same as the first example, but see how I can use the same control class SampleControlSmartWatch
and I pass the ScreenConfiguration
enum
to it, so it can know the width and height.