diff --git a/packages/flutter/lib/src/widgets/app.dart b/packages/flutter/lib/src/widgets/app.dart index f3d474b666..a505c5344f 100644 --- a/packages/flutter/lib/src/widgets/app.dart +++ b/packages/flutter/lib/src/widgets/app.dart @@ -179,6 +179,9 @@ class _WidgetsAppState extends State implements WidgetsBindingObserv @override void didChangeAppLifecycleState(AppLifecycleState state) { } + @override + void didHaveMemoryPressure() { } + @override Widget build(BuildContext context) { if (config.onLocaleChanged != null && _localeData == null) { diff --git a/packages/flutter/lib/src/widgets/binding.dart b/packages/flutter/lib/src/widgets/binding.dart index 6faa189b4c..29a818d1c3 100644 --- a/packages/flutter/lib/src/widgets/binding.dart +++ b/packages/flutter/lib/src/widgets/binding.dart @@ -48,6 +48,9 @@ abstract class WidgetsBindingObserver { /// Called when the system puts the app in the background or returns /// the app to the foreground. void didChangeAppLifecycleState(AppLifecycleState state) { } + + /// Called when the system is running low on memory. + void didHaveMemoryPressure() { } } /// The glue between the widgets layer and the Flutter engine. @@ -60,6 +63,7 @@ abstract class WidgetsBinding extends BindingBase implements GestureBinding, Ren ui.window.onLocaleChanged = handleLocaleChanged; PlatformMessages.setJSONMessageHandler('flutter/navigation', _handleNavigationMessage); PlatformMessages.setStringMessageHandler('flutter/lifecycle', _handleLifecycleMessage); + PlatformMessages.setJSONMessageHandler('flutter/system', _handleSystemMessage); } /// The current [WidgetsBinding], if one has been created. @@ -213,6 +217,15 @@ abstract class WidgetsBinding extends BindingBase implements GestureBinding, Ren return null; } + Future _handleSystemMessage(Map message) async { + final String type = message['type']; + if (type == 'memoryPressure') { + for (WidgetsBindingObserver observer in _observers) + observer.didHaveMemoryPressure(); + } + return null; + } + bool _needToReportFirstFrame = true; bool _thisFrameWasUseful = true; diff --git a/packages/flutter/test/widgets/binding_test.dart b/packages/flutter/test/widgets/binding_test.dart new file mode 100644 index 0000000000..2d4673035d --- /dev/null +++ b/packages/flutter/test/widgets/binding_test.dart @@ -0,0 +1,46 @@ +// Copyright 2017 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +import 'dart:typed_data'; + +import 'package:flutter_test/flutter_test.dart'; +import 'package:flutter/services.dart'; +import 'package:flutter/widgets.dart'; + +class MemoryPressureObserver implements WidgetsBindingObserver { + bool sawMemoryPressure = false; + + @override + void didHaveMemoryPressure() { + sawMemoryPressure = true; + } + + @override + Future didPopRoute() => new Future.value(false); + + @override + void didChangeMetrics() { } + + @override + void didChangeLocale(Locale locale) { } + + @override + void didChangeAppLifecycleState(AppLifecycleState state) { } +} + +void main() { + setUp(() { + WidgetsFlutterBinding.ensureInitialized(); + }); + + testWidgets('didHaveMemoryPressure callback', (WidgetTester tester) async { + final MemoryPressureObserver observer = new MemoryPressureObserver(); + WidgetsBinding.instance.addObserver(observer); + final ByteData message = const JSONMessageCodec().encodeMessage( + {'type': 'memoryPressure'}); + await PlatformMessages.handlePlatformMessage('flutter/system', message, (_) {}); + expect(observer.sawMemoryPressure, true); + WidgetsBinding.instance.removeObserver(observer); + }); +}