DeviceProxy.js 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596
  1. /*
  2. *
  3. * Licensed to the Apache Software Foundation (ASF) under one
  4. * or more contributor license agreements. See the NOTICE file
  5. * distributed with this work for additional information
  6. * regarding copyright ownership. The ASF licenses this file
  7. * to you under the Apache License, Version 2.0 (the
  8. * "License"); you may not use this file except in compliance
  9. * with the License. You may obtain a copy of the License at
  10. *
  11. * http://www.apache.org/licenses/LICENSE-2.0
  12. *
  13. * Unless required by applicable law or agreed to in writing,
  14. * software distributed under the License is distributed on an
  15. * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
  16. * KIND, either express or implied. See the License for the
  17. * specific language governing permissions and limitations
  18. * under the License.
  19. *
  20. */
  21. /* global Windows, createUUID */
  22. var ROOT_CONTAINER = '{00000000-0000-0000-FFFF-FFFFFFFFFFFF}';
  23. var DEVICE_CLASS_KEY = '{A45C254E-DF1C-4EFD-8020-67D146A850E0},10';
  24. var DEVICE_CLASS_KEY_NO_SEMICOLON = '{A45C254E-DF1C-4EFD-8020-67D146A850E0}10';
  25. var ROOT_CONTAINER_QUERY = 'System.Devices.ContainerId:="' + ROOT_CONTAINER + '"';
  26. var HAL_DEVICE_CLASS = '4d36e966-e325-11ce-bfc1-08002be10318';
  27. var DEVICE_DRIVER_VERSION_KEY = '{A8B865DD-2E3D-4094-AD97-E593A70C75D6},3';
  28. module.exports = {
  29. getDeviceInfo: function (win, fail, args) {
  30. // deviceId aka uuid, stored in Windows.Storage.ApplicationData.current.localSettings.values.deviceId
  31. var deviceId;
  32. // get deviceId, or create and store one
  33. var localSettings = Windows.Storage.ApplicationData.current.localSettings;
  34. if (localSettings.values.deviceId) {
  35. deviceId = localSettings.values.deviceId;
  36. } else {
  37. // App-specific hardware id could be used as uuid, but it changes if the hardware changes...
  38. try {
  39. var ASHWID = Windows.System.Profile.HardwareIdentification.getPackageSpecificToken(null).id;
  40. deviceId = Windows.Storage.Streams.DataReader.fromBuffer(ASHWID).readGuid();
  41. } catch (e) {
  42. // Couldn't get the hardware UUID
  43. deviceId = createUUID();
  44. }
  45. // ...so cache it per-install
  46. localSettings.values.deviceId = deviceId;
  47. }
  48. var userAgent = window.clientInformation.userAgent;
  49. // this will report "windows" in windows8.1 and windows phone 8.1 apps
  50. // and "windows8" in windows 8.0 apps similar to cordova.js
  51. // See https://github.com/apache/cordova-js/blob/master/src/windows/platform.js#L25
  52. var devicePlatform = userAgent.indexOf('MSAppHost/1.0') === -1 ? 'windows' : 'windows8';
  53. var versionString = userAgent.match(/Windows (?:Phone |NT )?([0-9.]+)/)[1];
  54. var deviceInfo = new Windows.Security.ExchangeActiveSyncProvisioning.EasClientDeviceInformation();
  55. // Running in the Windows Simulator is a remote session.
  56. // Running in the Windows Phone Emulator has the systemProductName set to "Virtual"
  57. var isVirtual = Windows.System.RemoteDesktop.InteractiveSession.isRemote || deviceInfo.systemProductName === 'Virtual';
  58. var manufacturer = deviceInfo.systemManufacturer;
  59. var model = deviceInfo.systemProductName;
  60. var Pnp = Windows.Devices.Enumeration.Pnp;
  61. Pnp.PnpObject.findAllAsync(Pnp.PnpObjectType.device,
  62. [DEVICE_DRIVER_VERSION_KEY, DEVICE_CLASS_KEY],
  63. ROOT_CONTAINER_QUERY)
  64. .then(function (rootDevices) {
  65. for (var i = 0; i < rootDevices.length; i++) {
  66. var rootDevice = rootDevices[i];
  67. if (!rootDevice.properties) continue;
  68. if (rootDevice.properties[DEVICE_CLASS_KEY_NO_SEMICOLON] === HAL_DEVICE_CLASS) {
  69. versionString = rootDevice.properties[DEVICE_DRIVER_VERSION_KEY];
  70. break;
  71. }
  72. }
  73. setTimeout(function () {
  74. win({ platform: devicePlatform,
  75. version: versionString,
  76. uuid: deviceId,
  77. isVirtual: isVirtual,
  78. model: model,
  79. manufacturer: manufacturer});
  80. }, 0);
  81. });
  82. }
  83. }; // exports
  84. require('cordova/exec/proxy').add('Device', module.exports);