From 7b7fa2578d60a2befaca908159f02d492514ff85 Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Fri, 23 Aug 2019 16:04:08 -0700 Subject: [PATCH 01/17] (WIP) setting up setSystemGestureExclusionRects platform channel --- .../systemchannels/PlatformChannel.java | 38 ++++++++++++++++++- .../plugin/platform/PlatformPlugin.java | 22 +++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index 68eeb7c7506cc..13c4cc961c8f4 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -5,8 +5,10 @@ package io.flutter.embedding.engine.systemchannels; import android.content.pm.ActivityInfo; +import android.graphics.Rect; import android.support.annotation.NonNull; import android.support.annotation.Nullable; +import android.util.Log; import org.json.JSONArray; import org.json.JSONException; @@ -15,7 +17,6 @@ import java.util.ArrayList; import java.util.List; -import io.flutter.Log; import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.plugin.common.JSONMethodCodec; import io.flutter.plugin.common.MethodCall; @@ -118,6 +119,18 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result platformMessageHandler.popSystemNavigator(); result.success(null); break; + case "SystemGestures.setSystemGestureExclusionRects": + if (arguments instanceof JSONArray) { + Log.v(TAG, "passed check"); + JSONArray inputRects = (JSONArray) arguments; + Log.v(TAG, inputRects.getJSONObject(0).get("top").toString()); + ArrayList decodedRects = decodeRects(inputRects); + result.success(null); + } else { + String inputTypeError = "Input type is incorrect. Ensure that a List> is passed as the input."; + result.error("inputTypeError", inputTypeError, null); + } + break; case "Clipboard.getData": { String contentFormatName = (String) arguments; ClipboardContentFormat clipboardFormat = null; @@ -337,6 +350,26 @@ private SystemChromeStyle decodeSystemChromeStyle(@NonNull JSONObject encodedSty ); } + private ArrayList decodeRects(@NonNull JSONArray inputRects) { + ArrayList exclusionRects = new ArrayList(); + try { + for (int i = 0; i < inputRects.length(); i++) { + JSONObject rect = inputRects.getJSONObject(i); + + int top = rect.getInt("top"); + int right = rect.getInt("right"); + int bottom = rect.getInt("bottom"); + int left = rect.getInt("left"); + + Rect gestureRect = new Rect(left, top, right, bottom); + exclusionRects.add(gestureRect); + } + } catch (JSONException e) { + //some exception handler code. + } + return exclusionRects; + } + /** * Handler that receives platform messages sent from Flutter to Android * through a given {@link PlatformChannel}. @@ -420,6 +453,9 @@ public interface PlatformMessageHandler { * clipboard to the given {@code text}. */ void setClipboardData(@NonNull String text); + + /* TODO: Write docs */ + void setSystemGestureExclusionRects(@NonNull ArrayList rects); } /** diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java b/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java index 0a9718a81c0b1..b8f6639dd7067 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java @@ -9,6 +9,7 @@ import android.content.ClipData; import android.content.ClipboardManager; import android.content.Context; +import android.graphics.Rect; import android.os.Build; import android.support.annotation.NonNull; import android.support.annotation.Nullable; @@ -17,8 +18,12 @@ import android.view.View; import android.view.Window; +import java.util.ArrayList; +import java.util.HashMap; import java.util.List; +import android.util.Log; + import io.flutter.embedding.engine.systemchannels.PlatformChannel; import io.flutter.plugin.common.ActivityLifecycleListener; @@ -84,6 +89,12 @@ public CharSequence getClipboardData(@Nullable PlatformChannel.ClipboardContentF public void setClipboardData(@NonNull String text) { PlatformPlugin.this.setClipboardData(text); } + + @Override + public void setSystemGestureExclusionRects(@NonNull ArrayList rects) { + Log.v("TAG", "WHAT IS THIS"); + PlatformPlugin.this.setSystemGestureExclusionRects(rects); + } }; public PlatformPlugin(Activity activity, PlatformChannel platformChannel) { @@ -272,4 +283,15 @@ private void setClipboardData(String text) { ClipData clip = ClipData.newPlainText("text label?", text); clipboard.setPrimaryClip(clip); } + + private void setSystemGestureExclusionRects(ArrayList rects) { + if (Build.VERSION.SDK_INT >= 29) { + Window window = activity.getWindow(); + View view = window.getDecorView(); + view.setSystemGestureExclusionRects(rects); + + List resultingRects = view.getSystemGestureExclusionRects(); + Log.v("results", resultingRects.toString()); + } + } } From 5245ad3b0f7bd4806eef41a59ffb06198daa6bcf Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Mon, 26 Aug 2019 09:56:30 -0700 Subject: [PATCH 02/17] SystemGesture.setSystemGestureExclusionRects --- .../systemchannels/PlatformChannel.java | 38 +++++++------------ 1 file changed, 14 insertions(+), 24 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index 13c4cc961c8f4..d76c5e05a6b34 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -121,13 +121,23 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result break; case "SystemGestures.setSystemGestureExclusionRects": if (arguments instanceof JSONArray) { - Log.v(TAG, "passed check"); JSONArray inputRects = (JSONArray) arguments; - Log.v(TAG, inputRects.getJSONObject(0).get("top").toString()); - ArrayList decodedRects = decodeRects(inputRects); + ArrayList decodedRects = new ArrayList(); + for (int i = 0; i < inputRects.length(); i++) { + JSONObject rect = inputRects.getJSONObject(i); + + int top = rect.getInt("top"); + int right = rect.getInt("right"); + int bottom = rect.getInt("bottom"); + int left = rect.getInt("left"); + + Rect gestureRect = new Rect(left, top, right, bottom); + decodedRects.add(gestureRect); + } + platformMessageHandler.setSystemGestureExclusionRects(decodedRects); result.success(null); } else { - String inputTypeError = "Input type is incorrect. Ensure that a List> is passed as the input."; + String inputTypeError = "Input type is incorrect. Ensure that a List> is passed as the input for SystemGestureExclusionRects.setSystemGestureExclusionRects."; result.error("inputTypeError", inputTypeError, null); } break; @@ -350,26 +360,6 @@ private SystemChromeStyle decodeSystemChromeStyle(@NonNull JSONObject encodedSty ); } - private ArrayList decodeRects(@NonNull JSONArray inputRects) { - ArrayList exclusionRects = new ArrayList(); - try { - for (int i = 0; i < inputRects.length(); i++) { - JSONObject rect = inputRects.getJSONObject(i); - - int top = rect.getInt("top"); - int right = rect.getInt("right"); - int bottom = rect.getInt("bottom"); - int left = rect.getInt("left"); - - Rect gestureRect = new Rect(left, top, right, bottom); - exclusionRects.add(gestureRect); - } - } catch (JSONException e) { - //some exception handler code. - } - return exclusionRects; - } - /** * Handler that receives platform messages sent from Flutter to Android * through a given {@link PlatformChannel}. From b37fa16ccd802d4496bd0ead7df7497b6efc61bf Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Mon, 26 Aug 2019 09:59:03 -0700 Subject: [PATCH 03/17] Cleanup Log.v and unnecessary imports --- .../embedding/engine/systemchannels/PlatformChannel.java | 2 -- .../android/io/flutter/plugin/platform/PlatformPlugin.java | 6 ------ 2 files changed, 8 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index d76c5e05a6b34..ae475c0b5ab13 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -8,7 +8,6 @@ import android.graphics.Rect; import android.support.annotation.NonNull; import android.support.annotation.Nullable; -import android.util.Log; import org.json.JSONArray; import org.json.JSONException; @@ -45,7 +44,6 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result String method = call.method; Object arguments = call.arguments; - Log.v(TAG, "Received '" + method + "' message."); try { switch (method) { case "SystemSound.play": diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java b/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java index b8f6639dd7067..222ef8bd4be28 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java @@ -22,8 +22,6 @@ import java.util.HashMap; import java.util.List; -import android.util.Log; - import io.flutter.embedding.engine.systemchannels.PlatformChannel; import io.flutter.plugin.common.ActivityLifecycleListener; @@ -92,7 +90,6 @@ public void setClipboardData(@NonNull String text) { @Override public void setSystemGestureExclusionRects(@NonNull ArrayList rects) { - Log.v("TAG", "WHAT IS THIS"); PlatformPlugin.this.setSystemGestureExclusionRects(rects); } }; @@ -289,9 +286,6 @@ private void setSystemGestureExclusionRects(ArrayList rects) { Window window = activity.getWindow(); View view = window.getDecorView(); view.setSystemGestureExclusionRects(rects); - - List resultingRects = view.getSystemGestureExclusionRects(); - Log.v("results", resultingRects.toString()); } } } From c155aec66be98cdf74d06054a796228c443bff3e Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Mon, 26 Aug 2019 10:00:40 -0700 Subject: [PATCH 04/17] Add Javadoc --- .../embedding/engine/systemchannels/PlatformChannel.java | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index ae475c0b5ab13..e6fb20d628222 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -442,7 +442,10 @@ public interface PlatformMessageHandler { */ void setClipboardData(@NonNull String text); - /* TODO: Write docs */ + /** + * The Flutter application would like to set the system gesture exclusion + * rects through the given {@code rects}. + */ void setSystemGestureExclusionRects(@NonNull ArrayList rects); } From 53e858bd0d7f47d391c539a6b33504cd2595d459 Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Mon, 26 Aug 2019 11:37:04 -0700 Subject: [PATCH 05/17] Reinclude accidentally removed log --- .../embedding/engine/systemchannels/PlatformChannel.java | 2 ++ 1 file changed, 2 insertions(+) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index e6fb20d628222..f237a720689f5 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -16,6 +16,7 @@ import java.util.ArrayList; import java.util.List; +import io.flutter.Log; import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.plugin.common.JSONMethodCodec; import io.flutter.plugin.common.MethodCall; @@ -44,6 +45,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result String method = call.method; Object arguments = call.arguments; + Log.v(TAG, "Received '" + method + "' message."); try { switch (method) { case "SystemSound.play": From 009830fb7bce85523d5d082964c91fbe1d6fd64b Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Mon, 26 Aug 2019 12:12:55 -0700 Subject: [PATCH 06/17] View.getSystemGestureExclusionRects platform channel --- .../systemchannels/PlatformChannel.java | 41 ++++++++----------- .../plugin/platform/PlatformPlugin.java | 10 +++-- 2 files changed, 24 insertions(+), 27 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index f237a720689f5..58170097bdcf5 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -14,6 +14,7 @@ import org.json.JSONObject; import java.util.ArrayList; +import java.util.HashMap; import java.util.List; import io.flutter.Log; @@ -119,27 +120,21 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result platformMessageHandler.popSystemNavigator(); result.success(null); break; - case "SystemGestures.setSystemGestureExclusionRects": - if (arguments instanceof JSONArray) { - JSONArray inputRects = (JSONArray) arguments; - ArrayList decodedRects = new ArrayList(); - for (int i = 0; i < inputRects.length(); i++) { - JSONObject rect = inputRects.getJSONObject(i); - - int top = rect.getInt("top"); - int right = rect.getInt("right"); - int bottom = rect.getInt("bottom"); - int left = rect.getInt("left"); - - Rect gestureRect = new Rect(left, top, right, bottom); - decodedRects.add(gestureRect); - } - platformMessageHandler.setSystemGestureExclusionRects(decodedRects); - result.success(null); - } else { - String inputTypeError = "Input type is incorrect. Ensure that a List> is passed as the input for SystemGestureExclusionRects.setSystemGestureExclusionRects."; - result.error("inputTypeError", inputTypeError, null); + case "SystemGestures.getSystemGestureExclusionRects": + List exclusionRects = platformMessageHandler.getSystemGestureExclusionRects(); + Log.v(TAG, exclusionRects.toString()); + ArrayList> encodedExclusionRects = new ArrayList>(); + Log.v(TAG, encodedExclusionRects.toString()); + + for (Rect rect : exclusionRects) { + HashMap rectMap = new HashMap(); + rectMap.put("top", rect.top); + rectMap.put("right", rect.right); + rectMap.put("bottom", rect.bottom); + rectMap.put("left", rect.left); + encodedExclusionRects.add(rectMap); } + result.success(encodedExclusionRects); break; case "Clipboard.getData": { String contentFormatName = (String) arguments; @@ -445,10 +440,10 @@ public interface PlatformMessageHandler { void setClipboardData(@NonNull String text); /** - * The Flutter application would like to set the system gesture exclusion - * rects through the given {@code rects}. + * The Flutter application would like to get the system gesture exclusion + * rects. */ - void setSystemGestureExclusionRects(@NonNull ArrayList rects); + List getSystemGestureExclusionRects(); } /** diff --git a/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java b/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java index 222ef8bd4be28..b4c6c71e44c5d 100644 --- a/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java +++ b/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java @@ -89,8 +89,8 @@ public void setClipboardData(@NonNull String text) { } @Override - public void setSystemGestureExclusionRects(@NonNull ArrayList rects) { - PlatformPlugin.this.setSystemGestureExclusionRects(rects); + public List getSystemGestureExclusionRects() { + return PlatformPlugin.this.getSystemGestureExclusionRects(); } }; @@ -281,11 +281,13 @@ private void setClipboardData(String text) { clipboard.setPrimaryClip(clip); } - private void setSystemGestureExclusionRects(ArrayList rects) { + private List getSystemGestureExclusionRects() { if (Build.VERSION.SDK_INT >= 29) { Window window = activity.getWindow(); View view = window.getDecorView(); - view.setSystemGestureExclusionRects(rects); + return view.getSystemGestureExclusionRects(); } + + return null; } } From 9342b27b3eb7ea7b3cca3aa3e92754d8519e77d7 Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Mon, 26 Aug 2019 12:18:15 -0700 Subject: [PATCH 07/17] Handle error case for API level 28 and under --- .../systemchannels/PlatformChannel.java | 27 ++++++++++--------- 1 file changed, 15 insertions(+), 12 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index 58170097bdcf5..7b3dc60e8d5df 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -122,19 +122,22 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result break; case "SystemGestures.getSystemGestureExclusionRects": List exclusionRects = platformMessageHandler.getSystemGestureExclusionRects(); - Log.v(TAG, exclusionRects.toString()); - ArrayList> encodedExclusionRects = new ArrayList>(); - Log.v(TAG, encodedExclusionRects.toString()); - - for (Rect rect : exclusionRects) { - HashMap rectMap = new HashMap(); - rectMap.put("top", rect.top); - rectMap.put("right", rect.right); - rectMap.put("bottom", rect.bottom); - rectMap.put("left", rect.left); - encodedExclusionRects.add(rectMap); + if (exclusionRects != null) { + ArrayList> encodedExclusionRects = new ArrayList>(); + + for (Rect rect : exclusionRects) { + HashMap rectMap = new HashMap(); + rectMap.put("top", rect.top); + rectMap.put("right", rect.right); + rectMap.put("bottom", rect.bottom); + rectMap.put("left", rect.left); + encodedExclusionRects.add(rectMap); + } + result.success(encodedExclusionRects); + } else { + String incorrectApiLevel = "Exclusion rects only exist for Android API 29+."; + result.error("error", incorrectApiLevel, null); } - result.success(encodedExclusionRects); break; case "Clipboard.getData": { String contentFormatName = (String) arguments; From 991ad1b486235e6c7e99cc7ff2d2e21afb1c0c5b Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Thu, 29 Aug 2019 13:39:11 -0700 Subject: [PATCH 08/17] Add guard clause, split encoding logic into its own method --- .../systemchannels/PlatformChannel.java | 32 +++++++++++-------- 1 file changed, 19 insertions(+), 13 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index 7b3dc60e8d5df..b3621300a8c5d 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -122,22 +122,14 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result break; case "SystemGestures.getSystemGestureExclusionRects": List exclusionRects = platformMessageHandler.getSystemGestureExclusionRects(); - if (exclusionRects != null) { - ArrayList> encodedExclusionRects = new ArrayList>(); - - for (Rect rect : exclusionRects) { - HashMap rectMap = new HashMap(); - rectMap.put("top", rect.top); - rectMap.put("right", rect.right); - rectMap.put("bottom", rect.bottom); - rectMap.put("left", rect.left); - encodedExclusionRects.add(rectMap); - } - result.success(encodedExclusionRects); - } else { + if (exclusionRects == null) { String incorrectApiLevel = "Exclusion rects only exist for Android API 29+."; result.error("error", incorrectApiLevel, null); + return; } + + ArrayList> encodedExclusionRects = encodeExclusionRects(exclusionRects); + result.success(encodedExclusionRects); break; case "Clipboard.getData": { String contentFormatName = (String) arguments; @@ -559,6 +551,20 @@ static SystemUiOverlay fromValue(@NonNull String encodedName) throws NoSuchField } } + private encodeExclusionRects(List exclusionRects) { + ArrayList> encodedExclusionRects = new ArrayList>(); + for (Rect rect : exclusionRects) { + HashMap rectMap = new HashMap(); + rectMap.put("top", rect.top); + rectMap.put("right", rect.right); + rectMap.put("bottom", rect.bottom); + rectMap.put("left", rect.left); + encodedExclusionRects.add(rectMap); + } + + return encodedExclusionRects; + } + /** * The color and label of an application that appears in Android's app switcher, AKA * recents screen. From ef8fe9fb7f3798142a416360f06fc1c8d3ce8e5a Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Thu, 29 Aug 2019 14:27:47 -0700 Subject: [PATCH 09/17] Reintroduce accidentally removed getSystemExclusionRects case --- .../systemchannels/PlatformChannel.java | 38 ++++++++++++------- 1 file changed, 25 insertions(+), 13 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index 06b3b87812596..032916a29e04f 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -461,7 +461,7 @@ static SystemUiOverlay fromValue(@NonNull String encodedName) throws NoSuchField } } - private encodeExclusionRects(List exclusionRects) { + private ArrayList> encodeExclusionRects(List exclusionRects) { ArrayList> encodedExclusionRects = new ArrayList>(); for (Rect rect : exclusionRects) { HashMap rectMap = new HashMap(); @@ -615,18 +615,6 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result platformMessageHandler.popSystemNavigator(); result.success(null); break; - case "SystemGestures.setSystemGestureExclusionRects": - if (!(arguments instanceof JSONArray)) { - String inputTypeError = "Input type is incorrect. Ensure that a List> is passed as the input for SystemGestureExclusionRects.setSystemGestureExclusionRects."; - result.error("inputTypeError", inputTypeError, null); - break; - } - - JSONArray inputRects = (JSONArray) arguments; - ArrayList decodedRects = decodeRects(inputRects); - platformMessageHandler.setSystemGestureExclusionRects(decodedRects); - result.success(null); - break; case "Clipboard.getData": { String contentFormatName = (String) arguments; ClipboardContentFormat clipboardFormat = null; @@ -655,6 +643,30 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result result.success(null); break; } + case "SystemGestures.setSystemGestureExclusionRects": + if (!(arguments instanceof JSONArray)) { + String inputTypeError = "Input type is incorrect. Ensure that a List> is passed as the input for SystemGestureExclusionRects.setSystemGestureExclusionRects."; + result.error("inputTypeError", inputTypeError, null); + break; + } + + JSONArray inputRects = (JSONArray) arguments; + ArrayList decodedRects = decodeRects(inputRects); + platformMessageHandler.setSystemGestureExclusionRects(decodedRects); + result.success(null); + break; + case "SystemGestures.getSystemGestureExclusionRects": { + List exclusionRects = platformMessageHandler.getSystemGestureExclusionRects(); + if (exclusionRects == null) { + String incorrectApiLevel = "Exclusion rects only exist for Android API 29+."; + result.error("error", incorrectApiLevel, null); + break; + } + + ArrayList> encodedExclusionRects = encodeExclusionRects(exclusionRects); + result.success(encodedExclusionRects); + break; + } default: result.notImplemented(); break; From 50c6509c525a1ce7bdd9b47487b4cff49ec67cb6 Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Thu, 29 Aug 2019 14:28:00 -0700 Subject: [PATCH 10/17] Add getSystemExclsionRects unit tests --- .../systemchannels/PlatformChannelTest.java | 69 +++++++++++++++++-- 1 file changed, 63 insertions(+), 6 deletions(-) diff --git a/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java b/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java index 31d7d71c39fd8..05fb2381b981f 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java @@ -3,6 +3,7 @@ import android.graphics.Rect; import java.util.ArrayList; +import java.util.HashMap; import io.flutter.embedding.engine.dart.DartExecutor; import io.flutter.embedding.engine.systemchannels.PlatformChannel; @@ -21,6 +22,7 @@ import static org.mockito.Mockito.mock; import static org.mockito.Mockito.times; import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; @Config(manifest=Config.NONE) @RunWith(RobolectricTestRunner.class) @@ -50,12 +52,12 @@ public void setSystemExclusionRectsSendsSuccessMessageToFramework() throws JSONE Rect gestureRect = new Rect(left, top, right, bottom); expectedDecodedRects.add(gestureRect); - MethodCall callSystemGestureExclusionRects = new MethodCall( + MethodCall callSetSystemGestureExclusionRects = new MethodCall( "SystemGestures.setSystemGestureExclusionRects", inputRects ); - platformChannel.parsingMethodCallHandler.onMethodCall(callSystemGestureExclusionRects, resultsMock); + platformChannel.parsingMethodCallHandler.onMethodCall(callSetSystemGestureExclusionRects, resultsMock); verify(platformMessageHandler, times(1)).setSystemGestureExclusionRects(expectedDecodedRects); verify(resultsMock, times(1)).success(null); } @@ -69,11 +71,11 @@ public void setSystemExclusionRectsRequiresJSONArrayInput() { ResultsMock resultsMock = mock(ResultsMock.class); String nonJsonInput = "Non-JSON"; - MethodCall callSystemGestureExclusionRects = new MethodCall( + MethodCall callSetSystemGestureExclusionRects = new MethodCall( "SystemGestures.setSystemGestureExclusionRects", nonJsonInput ); - platformChannel.parsingMethodCallHandler.onMethodCall(callSystemGestureExclusionRects, resultsMock); + platformChannel.parsingMethodCallHandler.onMethodCall(callSetSystemGestureExclusionRects, resultsMock); String inputTypeError = "Input type is incorrect. Ensure that a List> is passed as the input for SystemGestureExclusionRects.setSystemGestureExclusionRects."; verify(resultsMock, times(1)).error( @@ -100,11 +102,11 @@ public void setSystemExclusionRectsSendsJSONExceptionOnIncorrectDataShape() thro JSONArray inputArray = new JSONArray(); inputArray.put(jsonObject); - MethodCall callSystemGestureExclusionRects = new MethodCall( + MethodCall callSetSystemGestureExclusionRects = new MethodCall( "SystemGestures.setSystemGestureExclusionRects", inputArray ); - platformChannel.parsingMethodCallHandler.onMethodCall(callSystemGestureExclusionRects, resultsMock); + platformChannel.parsingMethodCallHandler.onMethodCall(callSetSystemGestureExclusionRects, resultsMock); verify(resultsMock, times(1)).error( "error", "JSON error: Incorrect JSON data shape. To set system gesture exclusion rects, \n" + @@ -113,6 +115,61 @@ public void setSystemExclusionRectsSendsJSONExceptionOnIncorrectDataShape() thro ); } + @Test + public void getSystemExclusionRectsSendsExclusionRectsToFrameworkOnSuccess() throws JSONException { + DartExecutor dartExecutor = mock(DartExecutor.class); + PlatformChannel platformChannel = new PlatformChannel(dartExecutor); + PlatformMessageHandler platformMessageHandler = mock(PlatformMessageHandler.class); + platformChannel.setPlatformMessageHandler(platformMessageHandler); + + int top = 0; + int right = 500; + int bottom = 250; + int left = 0; + + ArrayList expectedExclusionRects = new ArrayList(); + Rect gestureRect = new Rect(left, top, right, bottom); + expectedExclusionRects.add(gestureRect); + when(platformMessageHandler.getSystemGestureExclusionRects()).thenReturn(expectedExclusionRects); + + MethodCall callGetSystemGestureExclusionRects = new MethodCall( + "SystemGestures.getSystemGestureExclusionRects", + null + ); + ResultsMock resultsMock = mock(ResultsMock.class); + platformChannel.parsingMethodCallHandler.onMethodCall(callGetSystemGestureExclusionRects, resultsMock); + + ArrayList> expectedEncodedOutputRects = new ArrayList>(); + HashMap rectMap = new HashMap(); + rectMap.put("top", top); + rectMap.put("right", right); + rectMap.put("bottom", bottom); + rectMap.put("left", left); + expectedEncodedOutputRects.add(rectMap); + verify(resultsMock, times(1)).success(expectedEncodedOutputRects); + } + + @Test + public void getSystemExclusionRectsSendsIncorrectAPILevelException() { + DartExecutor dartExecutor = mock(DartExecutor.class); + PlatformChannel platformChannel = new PlatformChannel(dartExecutor); + PlatformMessageHandler platformMessageHandler = mock(PlatformMessageHandler.class); + platformChannel.setPlatformMessageHandler(platformMessageHandler); + when(platformMessageHandler.getSystemGestureExclusionRects()).thenReturn(null); + + MethodCall callGetSystemGestureExclusionRects = new MethodCall( + "SystemGestures.getSystemGestureExclusionRects", + null + ); + ResultsMock resultsMock = mock(ResultsMock.class); + platformChannel.parsingMethodCallHandler.onMethodCall(callGetSystemGestureExclusionRects, resultsMock); + verify(resultsMock, times(1)).error( + "error", + "Exclusion rects only exist for Android API 29+.", + null + ); + } + private class ResultsMock implements Result { @Override public void success(Object result) {} From 4a230f852d4e5b0d5877dcdfd55229bdf0605e53 Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Thu, 29 Aug 2019 14:31:26 -0700 Subject: [PATCH 11/17] Add Javadocs, improve variable naming --- .../systemchannels/PlatformChannel.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index 032916a29e04f..5bc927ba7ceb4 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -174,6 +174,23 @@ private ArrayList decodeRects(@NonNull JSONArray inputRects) throws JSONEx return exclusionRects; } + /** + * Encodes a List provided by the host platform into an ArrayList>. + */ + private ArrayList> encodeExclusionRects(List exclusionRects) { + ArrayList> encodedExclusionRects = new ArrayList>(); + for (Rect rect : exclusionRects) { + HashMap rectMap = new HashMap(); + rectMap.put("top", rect.top); + rectMap.put("right", rect.right); + rectMap.put("bottom", rect.bottom); + rectMap.put("left", rect.left); + encodedExclusionRects.add(rectMap); + } + + return encodedExclusionRects; + } + @NonNull private AppSwitcherDescription decodeAppSwitcherDescription(@NonNull JSONObject encodedDescription) throws JSONException { int color = encodedDescription.getInt("primaryColor"); @@ -461,20 +478,6 @@ static SystemUiOverlay fromValue(@NonNull String encodedName) throws NoSuchField } } - private ArrayList> encodeExclusionRects(List exclusionRects) { - ArrayList> encodedExclusionRects = new ArrayList>(); - for (Rect rect : exclusionRects) { - HashMap rectMap = new HashMap(); - rectMap.put("top", rect.top); - rectMap.put("right", rect.right); - rectMap.put("bottom", rect.bottom); - rectMap.put("left", rect.left); - encodedExclusionRects.add(rectMap); - } - - return encodedExclusionRects; - } - /** * The color and label of an application that appears in Android's app switcher, AKA * recents screen. From 5537541210fc76cbcde0f717ec41c5e760da9813 Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Thu, 29 Aug 2019 14:31:26 -0700 Subject: [PATCH 12/17] Add Javadocs --- .../systemchannels/PlatformChannel.java | 31 ++++++++++--------- 1 file changed, 17 insertions(+), 14 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index 032916a29e04f..5bc927ba7ceb4 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -174,6 +174,23 @@ private ArrayList decodeRects(@NonNull JSONArray inputRects) throws JSONEx return exclusionRects; } + /** + * Encodes a List provided by the host platform into an ArrayList>. + */ + private ArrayList> encodeExclusionRects(List exclusionRects) { + ArrayList> encodedExclusionRects = new ArrayList>(); + for (Rect rect : exclusionRects) { + HashMap rectMap = new HashMap(); + rectMap.put("top", rect.top); + rectMap.put("right", rect.right); + rectMap.put("bottom", rect.bottom); + rectMap.put("left", rect.left); + encodedExclusionRects.add(rectMap); + } + + return encodedExclusionRects; + } + @NonNull private AppSwitcherDescription decodeAppSwitcherDescription(@NonNull JSONObject encodedDescription) throws JSONException { int color = encodedDescription.getInt("primaryColor"); @@ -461,20 +478,6 @@ static SystemUiOverlay fromValue(@NonNull String encodedName) throws NoSuchField } } - private ArrayList> encodeExclusionRects(List exclusionRects) { - ArrayList> encodedExclusionRects = new ArrayList>(); - for (Rect rect : exclusionRects) { - HashMap rectMap = new HashMap(); - rectMap.put("top", rect.top); - rectMap.put("right", rect.right); - rectMap.put("bottom", rect.bottom); - rectMap.put("left", rect.left); - encodedExclusionRects.add(rectMap); - } - - return encodedExclusionRects; - } - /** * The color and label of an application that appears in Android's app switcher, AKA * recents screen. From 54f01f7853ee642807b5a3fcd877efefa20db428 Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Thu, 29 Aug 2019 14:32:40 -0700 Subject: [PATCH 13/17] Improve helper function naming --- .../embedding/engine/systemchannels/PlatformChannel.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index 5bc927ba7ceb4..d5850b8823257 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -146,7 +146,7 @@ private int decodeOrientations(@NonNull JSONArray encodedOrientations) throws JS * @throws JSONException if {@code inputRects} does not contain expected keys and value types. */ @NonNull - private ArrayList decodeRects(@NonNull JSONArray inputRects) throws JSONException { + private ArrayList decodeExclusionRects(@NonNull JSONArray inputRects) throws JSONException { ArrayList exclusionRects = new ArrayList(); for (int i = 0; i < inputRects.length(); i++) { JSONObject rect = inputRects.getJSONObject(i); @@ -654,7 +654,7 @@ public void onMethodCall(@NonNull MethodCall call, @NonNull MethodChannel.Result } JSONArray inputRects = (JSONArray) arguments; - ArrayList decodedRects = decodeRects(inputRects); + ArrayList decodedRects = decodeExclusionRects(inputRects); platformMessageHandler.setSystemGestureExclusionRects(decodedRects); result.success(null); break; From 024aa3a7ecb2ea49b7ab211695ee683150f7732a Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Fri, 30 Aug 2019 23:09:18 -0700 Subject: [PATCH 14/17] Incorporate code review feedback --- .../systemchannels/PlatformChannelTest.java | 65 +++++++++---------- 1 file changed, 30 insertions(+), 35 deletions(-) diff --git a/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java b/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java index 05fb2381b981f..c1f6d99dbab3f 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java @@ -116,68 +116,63 @@ public void setSystemExclusionRectsSendsJSONExceptionOnIncorrectDataShape() thro } @Test - public void getSystemExclusionRectsSendsExclusionRectsToFrameworkOnSuccess() throws JSONException { + public void itSendsSuccessMessageToFrameworkWhenGettingSystemGestureExclusionRects() throws JSONException { + // --- Test Setup --- DartExecutor dartExecutor = mock(DartExecutor.class); PlatformChannel platformChannel = new PlatformChannel(dartExecutor); PlatformMessageHandler platformMessageHandler = mock(PlatformMessageHandler.class); platformChannel.setPlatformMessageHandler(platformMessageHandler); + Result result = mock(Result.class); - int top = 0; - int right = 500; - int bottom = 250; - int left = 0; - - ArrayList expectedExclusionRects = new ArrayList(); - Rect gestureRect = new Rect(left, top, right, bottom); - expectedExclusionRects.add(gestureRect); - when(platformMessageHandler.getSystemGestureExclusionRects()).thenReturn(expectedExclusionRects); + // Fake API output setup + ArrayList fakeExclusionRects = new ArrayList(); + Rect gestureRect = new Rect(0, 0, 500, 250); + fakeExclusionRects.add(gestureRect); + when(platformMessageHandler.getSystemGestureExclusionRects()).thenReturn(fakeExclusionRects); + // Parsed API output that should be passed to result.success() + ArrayList> expectedEncodedOutputRects = new ArrayList>(); + HashMap rectMap = new HashMap(); + rectMap.put("top", 0); + rectMap.put("right", 500); + rectMap.put("bottom", 250); + rectMap.put("left", 0); + expectedEncodedOutputRects.add(rectMap); MethodCall callGetSystemGestureExclusionRects = new MethodCall( "SystemGestures.getSystemGestureExclusionRects", null ); - ResultsMock resultsMock = mock(ResultsMock.class); - platformChannel.parsingMethodCallHandler.onMethodCall(callGetSystemGestureExclusionRects, resultsMock); - ArrayList> expectedEncodedOutputRects = new ArrayList>(); - HashMap rectMap = new HashMap(); - rectMap.put("top", top); - rectMap.put("right", right); - rectMap.put("bottom", bottom); - rectMap.put("left", left); - expectedEncodedOutputRects.add(rectMap); - verify(resultsMock, times(1)).success(expectedEncodedOutputRects); + // --- Execute Test --- + platformChannel.parsingMethodCallHandler.onMethodCall(callGetSystemGestureExclusionRects, result); + + // --- Verify Results --- + verify(result, times(1)).success(expectedEncodedOutputRects); } @Test - public void getSystemExclusionRectsSendsIncorrectAPILevelException() { + public void itSendsAPILevelErrorWhenAndroidVersionIsTooLowWhenGettingSystemGestureExclusionRects() { + // --- Test Setup --- DartExecutor dartExecutor = mock(DartExecutor.class); PlatformChannel platformChannel = new PlatformChannel(dartExecutor); PlatformMessageHandler platformMessageHandler = mock(PlatformMessageHandler.class); platformChannel.setPlatformMessageHandler(platformMessageHandler); when(platformMessageHandler.getSystemGestureExclusionRects()).thenReturn(null); + Result result = mock(Result.class); MethodCall callGetSystemGestureExclusionRects = new MethodCall( "SystemGestures.getSystemGestureExclusionRects", null ); - ResultsMock resultsMock = mock(ResultsMock.class); - platformChannel.parsingMethodCallHandler.onMethodCall(callGetSystemGestureExclusionRects, resultsMock); - verify(resultsMock, times(1)).error( + + // --- Execute Test --- + platformChannel.parsingMethodCallHandler.onMethodCall(callGetSystemGestureExclusionRects, result); + + // --- Verify Results --- + verify(result, times(1)).error( "error", "Exclusion rects only exist for Android API 29+.", null ); } - - private class ResultsMock implements Result { - @Override - public void success(Object result) {} - - @Override - public void error(String errorCode, String errorMessage, Object errorDetails) {} - - @Override - public void notImplemented() {} - } } From 815d0734848179d047090d117dc94bc3c2952db8 Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Fri, 30 Aug 2019 23:28:39 -0700 Subject: [PATCH 15/17] Add relevant context for the encodeExclusionRects javadoc --- .../engine/systemchannels/PlatformChannel.java | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java index d5850b8823257..11cf6c48225c6 100644 --- a/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java +++ b/shell/platform/android/io/flutter/embedding/engine/systemchannels/PlatformChannel.java @@ -175,7 +175,15 @@ private ArrayList decodeExclusionRects(@NonNull JSONArray inputRects) thro } /** - * Encodes a List provided by the host platform into an ArrayList>. + * Encodes a List provided by the Android host into an + * ArrayList>. + * + * Since View.getSystemGestureExclusionRects returns a list of Rects, these + * Rects need to be transformed into UTF-8 encoded JSON messages to be + * properly decoded by the Flutter framework. + * + * This method is used by the SystemGestures.getSystemGestureExclusionRects + * platform channel. */ private ArrayList> encodeExclusionRects(List exclusionRects) { ArrayList> encodedExclusionRects = new ArrayList>(); From eef30b89c64a80bf76cc1b04280a39737e3f626d Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Fri, 30 Aug 2019 23:37:39 -0700 Subject: [PATCH 16/17] Add ResultsMock back to make tests happy, will be removed in #11804 --- .../engine/systemchannels/PlatformChannelTest.java | 11 +++++++++++ 1 file changed, 11 insertions(+) diff --git a/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java b/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java index c1f6d99dbab3f..f047d024184d7 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java @@ -175,4 +175,15 @@ public void itSendsAPILevelErrorWhenAndroidVersionIsTooLowWhenGettingSystemGestu null ); } + + private class ResultsMock implements Result { + @Override + public void success(Object result) {} + + @Override + public void error(String errorCode, String errorMessage, Object errorDetails) {} + + @Override + public void notImplemented() {} + } } From 3c53063181541416a7e9e86c216881b5b1abd2ad Mon Sep 17 00:00:00 2001 From: Shi Hao Hong Date: Fri, 30 Aug 2019 23:39:03 -0700 Subject: [PATCH 17/17] Remove accidental spaces --- .../embedding/engine/systemchannels/PlatformChannelTest.java | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java b/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java index f047d024184d7..93b06c2e961ac 100644 --- a/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java +++ b/shell/platform/android/test/io/flutter/embedding/engine/systemchannels/PlatformChannelTest.java @@ -180,10 +180,10 @@ private class ResultsMock implements Result { @Override public void success(Object result) {} - @Override + @Override public void error(String errorCode, String errorMessage, Object errorDetails) {} - @Override + @Override public void notImplemented() {} } }