Files
engine/shell/platform/android/io/flutter/app/FlutterActivity.java
T
Todd Volkert ec8cbe0fb6 Refactor FlutterActivity to be more composable (#3748)
This factors the functionality that was in `FlutterActivity`
to live in `FlutterActivityDelegate`. This will allow the creation of a
`FlutterFragmentActivity` that has the same core functionality, which in
turn unlocks certain Android plugins that choose to require the v4
support library (like Google Sign-In).

https://github.com/flutter/flutter/issues/10072
2017-06-07 12:28:41 -07:00

143 lines
4.1 KiB
Java

// Copyright 2015 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.
package io.flutter.app;
import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.content.res.Configuration;
import android.os.Bundle;
import io.flutter.app.FlutterActivityDelegate.ViewFactory;
import io.flutter.plugin.common.PluginRegistry;
import io.flutter.plugin.common.PluginRegistry.Registrar;
import io.flutter.view.FlutterView;
/**
* Base class for activities that use Flutter.
*/
public class FlutterActivity extends Activity implements FlutterView.Provider, PluginRegistry, ViewFactory {
private final FlutterActivityDelegate delegate = new FlutterActivityDelegate(this, this);
// These aliases ensure that the methods we forward to the delegate adhere
// to relevant interfaces versus just existing in FlutterActivityDelegate.
private final FlutterActivityEvents eventDelegate = delegate;
private final FlutterView.Provider viewProvider = delegate;
private final PluginRegistry pluginRegistry = delegate;
/**
* Returns the Flutter view used by this activity; will be null before
* {@link #onCreate(Bundle)} is called.
*/
@Override
public FlutterView getFlutterView() {
return viewProvider.getFlutterView();
}
/**
* Hook for subclasses to customize their startup behavior.
*
* @deprecated Just override {@link #onCreate(Bundle)} instead, and add your
* logic after calling {@code super.onCreate()}.
*/
@Deprecated
protected void onFlutterReady() {}
/**
* Hook for subclasses to customize the creation of the
* {@code FlutterView}.
* <p/>
* The default implementation returns {@code null}, which will cause the
* activity to use a newly instantiated full-screen view.
*/
@Override
public FlutterView createFlutterView(Context context) {
return null;
}
@Override
public final boolean hasPlugin(String key) {
return pluginRegistry.hasPlugin(key);
}
@Override
public final <T> T valuePublishedByPlugin(String pluginKey) {
return pluginRegistry.valuePublishedByPlugin(pluginKey);
}
@Override
public final Registrar registrarFor(String pluginKey) {
return pluginRegistry.registrarFor(pluginKey);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
eventDelegate.onCreate(savedInstanceState);
onFlutterReady();
}
@Override
protected void onDestroy() {
eventDelegate.onDestroy();
super.onDestroy();
}
@Override
public void onBackPressed() {
if (!eventDelegate.onBackPressed()) {
super.onBackPressed();
}
}
@Override
protected void onPause() {
super.onPause();
eventDelegate.onPause();
}
@Override
protected void onPostResume() {
super.onPostResume();
eventDelegate.onPostResume();
}
// @Override - added in API level 23
public void onRequestPermissionsResult(int requestCode, String[] permissions, int[] grantResults) {
eventDelegate.onRequestPermissionResult(requestCode, permissions, grantResults);
}
@Override
protected void onActivityResult(int requestCode, int resultCode, Intent data) {
if (!eventDelegate.onActivityResult(requestCode, resultCode, data)) {
super.onActivityResult(requestCode, resultCode, data);
}
}
@Override
protected void onNewIntent(Intent intent) {
eventDelegate.onNewIntent(intent);
}
@Override
public void onUserLeaveHint() {
eventDelegate.onUserLeaveHint();
}
@Override
public void onTrimMemory(int level) {
eventDelegate.onTrimMemory(level);
}
@Override
public void onLowMemory() {
eventDelegate.onLowMemory();
}
@Override
public void onConfigurationChanged(Configuration newConfig) {
eventDelegate.onConfigurationChanged(newConfig);
}
}