Reputation: 21
Please forgive me if I have completely overlooked the obvious here, but I can't seem to find out how to from the code distinguish between the smartWatch 1 and smartWatch 2. There seem to be some differences in the hardware and display size and I want to account for that. Soo... if someone knows how to either get the current watch display size, or determine if the current watch is the SmartWatch 1 or 2 I would really appreciate it!!!
Here is what I have tried, but for both watches it seems to always return 220x176
public static int getSupportedControlWidth(Context context) {
return context.getResources().getDimensionPixelSize(R.dimen.smart_watch_2_control_width);
}
public static int getSupportedControlHeight(Context context) {
return context.getResources().getDimensionPixelSize(R.dimen.smart_watch_2_control_height);
}
Upvotes: 1
Views: 335
Reputation: 54781
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.
Upvotes: 1
Reputation: 21
Ok I just figured out a way to make this work!! I opened up my SampleExtensionService.java file and captured the hotAppPackageName from the CreateControlExtension function. Basically I updated SampleExtensionService.java to work like this:
public static String hostPackage = "";
@Override
public ControlExtension createControlExtension(String hostAppPackageName) {
hostPackage = hostAppPackageName;
return new SampleSensorControl(hostAppPackageName, this);
}
Now I just call hostPackage from SampleExtenionService like this:
String pkg = SampleExtensionService.hostAppPackageName
And now this returns either com.sonymobile.smartwatch2 or com.sonyericsson.extras.smartwatch
Seems to work great for me!! Hope that helps someone else!
------------ Edit -------------
Ok so I feel like a total idiot now!!! I am creating a control extension and right in the initialization step is the hostAppPackageName
SampleSensorControl(final String hostAppPackageName, final Context context) {
:-0!!!!! I will leave up my initial answer though in case it is useful to someone else.
Upvotes: 0
Reputation: 43
For getting screen size of SmartWatch 1 you need to use R.dimen.smart_watch_control_width
for width and R.dimen.smart_watch_control_height
for height.
Upvotes: 0