From 35e0fa18185830900b01bd4a9008e52eb0a426f0 Mon Sep 17 00:00:00 2001 From: Adam Barth Date: Wed, 12 Oct 2016 14:09:18 -0700 Subject: [PATCH] Add SystemChrome support to PlatformPlugin.java (flutter/engine#3122) --- .../plugin/platform/PlatformPlugin.java | 119 ++++++++++++++++-- .../android/io/flutter/view/FlutterView.java | 5 +- 2 files changed, 112 insertions(+), 12 deletions(-) diff --git a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java index f487d7ccae..2a1d91d82a 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/plugin/platform/PlatformPlugin.java @@ -5,41 +5,138 @@ package io.flutter.plugin.platform; import android.app.Activity; +import android.content.pm.ActivityInfo; +import android.os.Build; import android.view.SoundEffectConstants; import android.view.View; -import android.util.Log; - -import org.json.JSONException; -import org.json.JSONObject; import io.flutter.plugin.common.JSONMessageListener; import io.flutter.view.FlutterView; +import org.domokit.common.ActivityLifecycleListener; +import org.json.JSONArray; +import org.json.JSONException; +import org.json.JSONObject; + /** * Android implementation of the platform plugin. */ -public class PlatformPlugin extends JSONMessageListener { +public class PlatformPlugin extends JSONMessageListener implements ActivityLifecycleListener { private final Activity mActivity; + private static final int DEFAULT_OVERLAYS = View.SYSTEM_UI_FLAG_LAYOUT_STABLE + | View.SYSTEM_UI_FLAG_LAYOUT_FULLSCREEN + | View.SYSTEM_UI_FLAG_FULLSCREEN + | View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION + | View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; public PlatformPlugin(Activity activity) { mActivity = activity; + mEnabledOverlays = DEFAULT_OVERLAYS; } @Override public JSONObject onJSONMessage(FlutterView view, JSONObject message) throws JSONException { - if (message.getString("method").equals("SystemSound.play")) { - playSystemSound(message.getJSONArray("args").getString(0)); - return null; + String method = message.getString("method"); + JSONArray args = message.getJSONArray("args"); + if (method.equals("SystemSound.play")) { + playSystemSound(args.getString(0)); + } else if (method.equals("SystemChrome.setPreferredOrientations")) { + setSystemChromePreferredOrientatations(args.getJSONArray(0)); + } else if (method.equals("SystemChrome.setApplicationSwitcherDescription")) { + setSystemChromeApplicationSwitcherDescription(args.getJSONObject(0)); + } else if (method.equals("SystemChrome.setEnabledSystemUIOverlays")) { + setSystemChromeEnabledSystemUIOverlays(args.getJSONArray(0)); + } else if (method.equals("SystemChrome.setSystemUIOverlayStyle")) { + setSystemChromeSystemUIOverlayStyle(args.getString(0)); + } else { + // TODO(abarth): We should throw an exception here that gets + // transmitted back to Dart. } - // TODO(abarth): We should throw an exception here that gets - // transmitted back to Dart. return null; } - void playSystemSound(String soundType) { + private void playSystemSound(String soundType) { if (soundType.equals("SystemSoundType.click")) { View view = mActivity.getWindow().getDecorView(); view.playSoundEffect(SoundEffectConstants.CLICK); } } + + private void setSystemChromePreferredOrientatations(JSONArray orientatations) throws JSONException { + // Currently the Android implementation only supports masks with zero or one + // selected device orientations. + int androidOrientation; + if (orientatations.length() == 0) { + androidOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED; + } else if (orientatations.getString(0).equals("DeviceOrientation.portraitUp")) { + androidOrientation = ActivityInfo.SCREEN_ORIENTATION_PORTRAIT; + } else if (orientatations.getString(0).equals("DeviceOrientation.landscapeLeft")) { + androidOrientation = ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE; + } else if (orientatations.getString(0).equals("DeviceOrientation.portraitDown")) { + androidOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_PORTRAIT; + } else if (orientatations.getString(0).equals("DeviceOrientation.landscapeRight")) { + androidOrientation = ActivityInfo.SCREEN_ORIENTATION_REVERSE_LANDSCAPE; + } else { + return; + } + + mActivity.setRequestedOrientation(androidOrientation); + } + + private void setSystemChromeApplicationSwitcherDescription(JSONObject description) throws JSONException { + if (Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP) { + return; + } + + int color = description.getInt("primaryColor"); + if (color != 0) { // 0 means color isn't set, use system default + color = color | 0xFF000000; // color must be opaque if set + } + + mActivity.setTaskDescription( + new android.app.ActivityManager.TaskDescription( + description.getString("label"), + null, + color + ) + ); + } + + private int mEnabledOverlays; + + private void setSystemChromeEnabledSystemUIOverlays(JSONArray overlays) throws JSONException { + int enabledOverlays = DEFAULT_OVERLAYS; + + if (overlays.length() == 0) { + enabledOverlays |= View.SYSTEM_UI_FLAG_IMMERSIVE_STICKY; + } + + for (int i = 0; i < overlays.length(); ++i) { + String overlay = overlays.getString(i); + if (overlay.equals("SystemUiOverlay.top")) { + enabledOverlays &= ~View.SYSTEM_UI_FLAG_FULLSCREEN; + } else if (overlay.equals("SystemUiOverlay.bottom")) { + enabledOverlays &= ~View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION; + enabledOverlays &= ~View.SYSTEM_UI_FLAG_HIDE_NAVIGATION; + } + } + + mEnabledOverlays = enabledOverlays; + updateSystemUiOverlays(); + } + + private void updateSystemUiOverlays() { + mActivity.getWindow().getDecorView().setSystemUiVisibility(mEnabledOverlays); + } + + private void setSystemChromeSystemUIOverlayStyle(String style) { + // You can change the navigation bar color (including translucent colors) + // in Android, but you can't change the color of the navigation buttons, + // so LIGHT vs DARK effectively isn't supported in Android. + } + + @Override + public void onPostResume() { + updateSystemUiOverlays(); + } } diff --git a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java index 9b6e7fa3b8..8b102db243 100644 --- a/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java +++ b/engine/src/flutter/shell/platform/android/io/flutter/view/FlutterView.java @@ -165,7 +165,10 @@ public class FlutterView extends SurfaceView setLocale(getResources().getConfiguration().locale); - mOnMessageListeners.put("flutter/platform", new PlatformPlugin((Activity)getContext())); + // Configure the platform plugin. + PlatformPlugin platformPlugin = new PlatformPlugin((Activity)getContext()); + addOnMessageListener("flutter/platform", platformPlugin); + addActivityLifecycleListener(platformPlugin); if ((context.getApplicationInfo().flags & ApplicationInfo.FLAG_DEBUGGABLE) != 0) { discoveryReceiver = new DiscoveryReceiver();