Skip to content

Commit e2b27c1

Browse files
yash1200Egor
authored andcommitted
[device_info_platform_interface] Introduce package (flutter#2929)
This is the platform interface package of the federated device_info plugin.
1 parent 82f2b3b commit e2b27c1

File tree

9 files changed

+505
-0
lines changed

9 files changed

+505
-0
lines changed
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
## 1.0.0
2+
3+
- Initial open-source release.
Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
//
3+
// Redistribution and use in source and binary forms, with or without
4+
// modification, are permitted provided that the following conditions are
5+
// met:
6+
//
7+
// * Redistributions of source code must retain the above copyright
8+
// notice, this list of conditions and the following disclaimer.
9+
// * Redistributions in binary form must reproduce the above
10+
// copyright notice, this list of conditions and the following disclaimer
11+
// in the documentation and/or other materials provided with the
12+
// distribution.
13+
// * Neither the name of Google Inc. nor the names of its
14+
// contributors may be used to endorse or promote products derived from
15+
// this software without specific prior written permission.
16+
//
17+
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
18+
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
19+
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
20+
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
21+
// OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
22+
// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
23+
// LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
24+
// DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
25+
// THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
26+
// (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
27+
// OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
# device_info_platform_interface
2+
3+
A common platform interface for the [`device_info`][1] plugin.
4+
5+
This interface allows platform-specific implementations of the `device_info`
6+
plugin, as well as the plugin itself, to ensure they are supporting the
7+
same interface.
8+
9+
# Usage
10+
11+
To implement a new platform-specific implementation of `device_info`, extend
12+
[`DeviceInfoPlatform`][2] with an implementation that performs the
13+
platform-specific behavior, and when you register your plugin, set the default
14+
`DeviceInfoPlatform` by calling
15+
`DeviceInfoPlatform.instance = MyPlatformDeviceInfo()`.
16+
17+
# Note on breaking changes
18+
19+
Strongly prefer non-breaking changes (such as adding a method to the interface)
20+
over breaking changes for this package.
21+
22+
See https://flutter.dev/go/platform-interface-breaking-changes for a discussion
23+
on why a less-clean interface is preferable to a breaking change.
24+
25+
[1]: ../device_info
26+
[2]: lib/device_info_platform_interface.dart
Lines changed: 55 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,55 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
import 'dart:async';
6+
7+
import 'package:plugin_platform_interface/plugin_platform_interface.dart';
8+
9+
import 'method_channel/method_channel_device_info.dart';
10+
11+
import 'model/android_device_info.dart';
12+
import 'model/ios_device_info.dart';
13+
14+
export 'model/android_device_info.dart';
15+
export 'model/ios_device_info.dart';
16+
17+
/// The interface that implementations of device_info must implement.
18+
///
19+
/// Platform implementations should extend this class rather than implement it as `device_info`
20+
/// does not consider newly added methods to be breaking changes. Extending this class
21+
/// (using `extends`) ensures that the subclass will get the default implementation, while
22+
/// platform implementations that `implements` this interface will be broken by newly added
23+
/// [DeviceInfoPlatform] methods.
24+
abstract class DeviceInfoPlatform extends PlatformInterface {
25+
/// Constructs a UrlLauncherPlatform.
26+
DeviceInfoPlatform() : super(token: _token);
27+
28+
static final Object _token = Object();
29+
30+
static DeviceInfoPlatform _instance = MethodChannelDeviceInfo();
31+
32+
/// The default instance of [DeviceInfoPlatform] to use.
33+
///
34+
/// Defaults to [MethodChannelDeviceInfo].
35+
static DeviceInfoPlatform get instance => _instance;
36+
37+
/// Platform-specific plugins should set this with their own platform-specific
38+
/// class that extends [DeviceInfoPlatform] when they register themselves.
39+
static set instance(DeviceInfoPlatform instance) {
40+
PlatformInterface.verifyToken(instance, _token);
41+
_instance = instance;
42+
}
43+
44+
// Gets the Android device information.
45+
// ignore: public_member_api_docs
46+
Future<AndroidDeviceInfo> androidInfo() {
47+
throw UnimplementedError('androidInfo() has not been implemented.');
48+
}
49+
50+
// Gets the iOS device information.
51+
// ignore: public_member_api_docs
52+
Future<IosDeviceInfo> iosInfo() {
53+
throw UnimplementedError('iosInfo() has not been implemented.');
54+
}
55+
}
Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
import 'dart:async';
2+
3+
import 'package:flutter/services.dart';
4+
import 'package:meta/meta.dart';
5+
6+
import 'package:device_info_platform_interface/device_info_platform_interface.dart';
7+
8+
/// An implementation of [DeviceInfoPlatform] that uses method channels.
9+
class MethodChannelDeviceInfo extends DeviceInfoPlatform {
10+
/// The method channel used to interact with the native platform.
11+
@visibleForTesting
12+
MethodChannel channel = MethodChannel('plugins.flutter.io/device_info');
13+
14+
// Method channel for Android devices
15+
Future<AndroidDeviceInfo> androidInfo() async {
16+
return AndroidDeviceInfo.fromMap(
17+
(await channel.invokeMethod('getAndroidDeviceInfo'))
18+
.cast<String, dynamic>(),
19+
);
20+
}
21+
22+
// Method channel for iOS devices
23+
Future<IosDeviceInfo> iosInfo() async {
24+
return IosDeviceInfo.fromMap(
25+
(await channel.invokeMethod('getIosDeviceInfo')).cast<String, dynamic>(),
26+
);
27+
}
28+
}
Lines changed: 198 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,198 @@
1+
// Copyright 2017 The Chromium Authors. All rights reserved.
2+
// Use of this source code is governed by a BSD-style license that can be
3+
// found in the LICENSE file.
4+
5+
/// Information derived from `android.os.Build`.
6+
///
7+
/// See: https://developer.android.com/reference/android/os/Build.html
8+
class AndroidDeviceInfo {
9+
/// Android device Info class.
10+
AndroidDeviceInfo({
11+
this.version,
12+
this.board,
13+
this.bootloader,
14+
this.brand,
15+
this.device,
16+
this.display,
17+
this.fingerprint,
18+
this.hardware,
19+
this.host,
20+
this.id,
21+
this.manufacturer,
22+
this.model,
23+
this.product,
24+
List<String> supported32BitAbis,
25+
List<String> supported64BitAbis,
26+
List<String> supportedAbis,
27+
this.tags,
28+
this.type,
29+
this.isPhysicalDevice,
30+
this.androidId,
31+
List<String> systemFeatures,
32+
}) : supported32BitAbis = List<String>.unmodifiable(supported32BitAbis),
33+
supported64BitAbis = List<String>.unmodifiable(supported64BitAbis),
34+
supportedAbis = List<String>.unmodifiable(supportedAbis),
35+
systemFeatures = List<String>.unmodifiable(systemFeatures);
36+
37+
/// Android operating system version values derived from `android.os.Build.VERSION`.
38+
final AndroidBuildVersion version;
39+
40+
/// The name of the underlying board, like "goldfish".
41+
final String board;
42+
43+
/// The system bootloader version number.
44+
final String bootloader;
45+
46+
/// The consumer-visible brand with which the product/hardware will be associated, if any.
47+
final String brand;
48+
49+
/// The name of the industrial design.
50+
final String device;
51+
52+
/// A build ID string meant for displaying to the user.
53+
final String display;
54+
55+
/// A string that uniquely identifies this build.
56+
final String fingerprint;
57+
58+
/// The name of the hardware (from the kernel command line or /proc).
59+
final String hardware;
60+
61+
/// Hostname.
62+
final String host;
63+
64+
/// Either a changelist number, or a label like "M4-rc20".
65+
final String id;
66+
67+
/// The manufacturer of the product/hardware.
68+
final String manufacturer;
69+
70+
/// The end-user-visible name for the end product.
71+
final String model;
72+
73+
/// The name of the overall product.
74+
final String product;
75+
76+
/// An ordered list of 32 bit ABIs supported by this device.
77+
final List<String> supported32BitAbis;
78+
79+
/// An ordered list of 64 bit ABIs supported by this device.
80+
final List<String> supported64BitAbis;
81+
82+
/// An ordered list of ABIs supported by this device.
83+
final List<String> supportedAbis;
84+
85+
/// Comma-separated tags describing the build, like "unsigned,debug".
86+
final String tags;
87+
88+
/// The type of build, like "user" or "eng".
89+
final String type;
90+
91+
/// `false` if the application is running in an emulator, `true` otherwise.
92+
final bool isPhysicalDevice;
93+
94+
/// The Android hardware device ID that is unique between the device + user and app signing.
95+
final String androidId;
96+
97+
/// Describes what features are available on the current device.
98+
///
99+
/// This can be used to check if the device has, for example, a front-facing
100+
/// camera, or a touchscreen. However, in many cases this is not the best
101+
/// API to use. For example, if you are interested in bluetooth, this API
102+
/// can tell you if the device has a bluetooth radio, but it cannot tell you
103+
/// if bluetooth is currently enabled, or if you have been granted the
104+
/// necessary permissions to use it. Please *only* use this if there is no
105+
/// other way to determine if a feature is supported.
106+
///
107+
/// This data comes from Android's PackageManager.getSystemAvailableFeatures,
108+
/// and many of the common feature strings to look for are available in
109+
/// PackageManager's public documentation:
110+
/// https://developer.android.com/reference/android/content/pm/PackageManager
111+
final List<String> systemFeatures;
112+
113+
/// Deserializes from the message received from [_kChannel].
114+
static AndroidDeviceInfo fromMap(Map<String, dynamic> map) {
115+
return AndroidDeviceInfo(
116+
version: AndroidBuildVersion._fromMap(
117+
map['version']?.cast<String, dynamic>() ?? {}),
118+
board: map['board'],
119+
bootloader: map['bootloader'],
120+
brand: map['brand'],
121+
device: map['device'],
122+
display: map['display'],
123+
fingerprint: map['fingerprint'],
124+
hardware: map['hardware'],
125+
host: map['host'],
126+
id: map['id'],
127+
manufacturer: map['manufacturer'],
128+
model: map['model'],
129+
product: map['product'],
130+
supported32BitAbis: _fromList(map['supported32BitAbis'] ?? []),
131+
supported64BitAbis: _fromList(map['supported64BitAbis'] ?? []),
132+
supportedAbis: _fromList(map['supportedAbis'] ?? []),
133+
tags: map['tags'],
134+
type: map['type'],
135+
isPhysicalDevice: map['isPhysicalDevice'],
136+
androidId: map['androidId'],
137+
systemFeatures: _fromList(map['systemFeatures'] ?? []),
138+
);
139+
}
140+
141+
/// Deserializes message as List<String>
142+
static List<String> _fromList(dynamic message) {
143+
final List<dynamic> list = message;
144+
return List<String>.from(list);
145+
}
146+
}
147+
148+
/// Version values of the current Android operating system build derived from
149+
/// `android.os.Build.VERSION`.
150+
///
151+
/// See: https://developer.android.com/reference/android/os/Build.VERSION.html
152+
class AndroidBuildVersion {
153+
AndroidBuildVersion._({
154+
this.baseOS,
155+
this.codename,
156+
this.incremental,
157+
this.previewSdkInt,
158+
this.release,
159+
this.sdkInt,
160+
this.securityPatch,
161+
});
162+
163+
/// The base OS build the product is based on.
164+
final String baseOS;
165+
166+
/// The current development codename, or the string "REL" if this is a release build.
167+
final String codename;
168+
169+
/// The internal value used by the underlying source control to represent this build.
170+
final String incremental;
171+
172+
/// The developer preview revision of a prerelease SDK.
173+
final int previewSdkInt;
174+
175+
/// The user-visible version string.
176+
final String release;
177+
178+
/// The user-visible SDK version of the framework.
179+
///
180+
/// Possible values are defined in: https://developer.android.com/reference/android/os/Build.VERSION_CODES.html
181+
final int sdkInt;
182+
183+
/// The user-visible security patch level.
184+
final String securityPatch;
185+
186+
/// Deserializes from the map message received from [_kChannel].
187+
static AndroidBuildVersion _fromMap(Map<String, dynamic> map) {
188+
return AndroidBuildVersion._(
189+
baseOS: map['baseOS'],
190+
codename: map['codename'],
191+
incremental: map['incremental'],
192+
previewSdkInt: map['previewSdkInt'],
193+
release: map['release'],
194+
sdkInt: map['sdkInt'],
195+
securityPatch: map['securityPatch'],
196+
);
197+
}
198+
}

0 commit comments

Comments
 (0)