1

如果我在这里完全忽略了明显的问题,请原谅我,但我似乎无法从代码中找出如何区分 smartWatch 1 和 smartWatch 2。硬件和显示尺寸似乎有些差异,我想要考虑到这一点。Soo ...如果有人知道如何获取当前手表的显示尺寸,或者确定当前手表是 SmartWatch 1 还是 SmartWatch 2,我将不胜感激!!!

这是我尝试过的,但对于这两款手表来说,它似乎总是返回 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);
}
4

3 回答 3

1

查看 SampleControlExtension 项目,看看它是如何使用的:

DeviceInfoHelper.isSmartWatch2ApiAndScreenDetected()

但是,如果您愿意,您可以从任何地方调用它。

这就是 SampleExtensionService 决定 SW1 或 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);
    }
}

就个人而言,我觉得没有必要使用资源,所以我选择这样做。我有一个enum定义,我使用与上述查询类似的代码,isSmartWatch2ApiAndScreenDetected然后传递正确的枚举值。

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;
    }

}

编辑您必须告诉系统您要支持智能手表 2。

在你的RegistrationInformation课堂上:

@Override
public int getTargetControlApiVersion() {
    return 2;
}

如果那是1,你只会得到 false isSmartWatch2ApiAndScreenDetected

编辑第 2 部分如何使用枚举

@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);
    }
}

大部分与第一个示例相同,但看看我如何使用相同的控件类SampleControlSmartWatch并将其传递ScreenConfiguration enum给它,因此它可以知道宽度和高度。

于 2013-10-21T07:20:57.430 回答
0

要获得 SmartWatch 1 的屏幕尺寸,您需要使用R.dimen.smart_watch_control_width宽度和R.dimen.smart_watch_control_height高度。

于 2013-10-21T07:25:05.047 回答
0

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.

于 2013-11-02T16:26:20.797 回答