• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2013 The Flutter 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 package io.flutter.app;
6 
7 import android.app.Activity;
8 import android.content.Context;
9 import android.content.Intent;
10 import android.content.res.Configuration;
11 import android.os.Bundle;
12 import android.support.annotation.NonNull;
13 import android.util.Log;
14 import io.flutter.app.FlutterActivityDelegate.ViewFactory;
15 import io.flutter.plugin.common.PluginRegistry;
16 import io.flutter.view.FlutterNativeView;
17 import io.flutter.view.FlutterView;
18 
19 /**
20  * Base class for activities that use Flutter.
21  */
22 public class FlutterActivity extends Activity implements FlutterView.Provider, PluginRegistry, ViewFactory {
23     private static final String TAG = "FlutterActivity";
24 
25     private final FlutterActivityDelegate delegate = new FlutterActivityDelegate(this, this);
26 
27     // These aliases ensure that the methods we forward to the delegate adhere
28     // to relevant interfaces versus just existing in FlutterActivityDelegate.
29     private final FlutterActivityEvents eventDelegate = delegate;
30     private final FlutterView.Provider viewProvider = delegate;
31     private final PluginRegistry pluginRegistry = delegate;
32 
33     /**
34      * Returns the Flutter view used by this activity; will be null before
35      * {@link #onCreate(Bundle)} is called.
36      */
37     @Override
getFlutterView()38     public FlutterView getFlutterView() {
39         return viewProvider.getFlutterView();
40     }
41 
42     /**
43      * Hook for subclasses to customize the creation of the
44      * {@code FlutterView}.
45      *
46      * <p>The default implementation returns {@code null}, which will cause the
47      * activity to use a newly instantiated full-screen view.</p>
48      */
49     @Override
createFlutterView(Context context)50     public FlutterView createFlutterView(Context context) {
51         return null;
52     }
53 
54     /**
55      * Hook for subclasses to customize the creation of the
56      * {@code FlutterNativeView}.
57      *
58      * <p>The default implementation returns {@code null}, which will cause the
59      * activity to use a newly instantiated native view object.</p>
60      */
61     @Override
createFlutterNativeView()62     public FlutterNativeView createFlutterNativeView() {
63         return null;
64     }
65 
66     @Override
retainFlutterNativeView()67     public boolean retainFlutterNativeView() {
68         return false;
69     }
70 
71     @Override
hasPlugin(String key)72     public final boolean hasPlugin(String key) {
73         return pluginRegistry.hasPlugin(key);
74     }
75 
76     @Override
valuePublishedByPlugin(String pluginKey)77     public final <T> T valuePublishedByPlugin(String pluginKey) {
78         return pluginRegistry.valuePublishedByPlugin(pluginKey);
79     }
80 
81     @Override
registrarFor(String pluginKey)82     public final Registrar registrarFor(String pluginKey) {
83         return pluginRegistry.registrarFor(pluginKey);
84     }
85 
86     @Override
onCreate(Bundle savedInstanceState)87     protected void onCreate(Bundle savedInstanceState) {
88         super.onCreate(savedInstanceState);
89         eventDelegate.onCreate(savedInstanceState);
90     }
91 
92     @Override
onStart()93     protected void onStart() {
94         super.onStart();
95         eventDelegate.onStart();
96     }
97 
98     @Override
onResume()99     protected void onResume() {
100         super.onResume();
101         eventDelegate.onResume();
102     }
103 
104     @Override
onDestroy()105     protected void onDestroy() {
106         eventDelegate.onDestroy();
107         super.onDestroy();
108     }
109 
110     @Override
onBackPressed()111     public void onBackPressed() {
112         if (!eventDelegate.onBackPressed()) {
113             super.onBackPressed();
114         }
115     }
116 
117     @Override
onStop()118     protected void onStop() {
119         eventDelegate.onStop();
120         super.onStop();
121     }
122 
123     @Override
onPause()124     protected void onPause() {
125         super.onPause();
126         eventDelegate.onPause();
127     }
128 
129     @Override
onPostResume()130     protected void onPostResume() {
131         super.onPostResume();
132         eventDelegate.onPostResume();
133     }
134 
135     // @Override - added in API level 23
onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults)136     public void onRequestPermissionsResult(int requestCode, @NonNull String[] permissions, @NonNull int[] grantResults) {
137         eventDelegate.onRequestPermissionsResult(requestCode, permissions, grantResults);
138     }
139 
140     @Override
onActivityResult(int requestCode, int resultCode, Intent data)141     protected void onActivityResult(int requestCode, int resultCode, Intent data) {
142         if (!eventDelegate.onActivityResult(requestCode, resultCode, data)) {
143             super.onActivityResult(requestCode, resultCode, data);
144         }
145     }
146 
147     @Override
onNewIntent(Intent intent)148     protected void onNewIntent(Intent intent) {
149         eventDelegate.onNewIntent(intent);
150     }
151 
152     @Override
onUserLeaveHint()153     public void onUserLeaveHint() {
154         eventDelegate.onUserLeaveHint();
155     }
156 
157     @Override
onTrimMemory(int level)158     public void onTrimMemory(int level) {
159         eventDelegate.onTrimMemory(level);
160     }
161 
162     @Override
onLowMemory()163     public void onLowMemory() {
164         eventDelegate.onLowMemory();
165     }
166 
167     @Override
onConfigurationChanged(Configuration newConfig)168     public void onConfigurationChanged(Configuration newConfig) {
169         super.onConfigurationChanged(newConfig);
170         eventDelegate.onConfigurationChanged(newConfig);
171     }
172 }
173