RecentsActivity.java revision b44c24fb50845dfbc1f49e78085cf5e01a32067f
1/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.systemui.recents;
18
19import android.app.Activity;
20import android.content.BroadcastReceiver;
21import android.content.Context;
22import android.content.Intent;
23import android.content.IntentFilter;
24import android.os.Bundle;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.WindowManager;
28import android.widget.FrameLayout;
29import com.android.systemui.R;
30import com.android.systemui.recents.model.SpaceNode;
31import com.android.systemui.recents.model.TaskStack;
32import com.android.systemui.recents.views.RecentsView;
33
34import java.util.ArrayList;
35
36
37/* Activity */
38public class RecentsActivity extends Activity implements RecentsView.RecentsViewCallbacks {
39    FrameLayout mContainerView;
40    RecentsView mRecentsView;
41    View mEmptyView;
42
43    boolean mVisible;
44    boolean mTaskLaunched;
45
46    // Broadcast receiver to handle messages from our RecentsService
47    BroadcastReceiver mServiceBroadcastReceiver = new BroadcastReceiver() {
48        @Override
49        public void onReceive(Context context, Intent intent) {
50            String action = intent.getAction();
51            Console.log(Constants.DebugFlags.App.SystemUIHandshake,
52                    "[RecentsActivity|serviceBroadcast]", action, Console.AnsiRed);
53            if (action.equals(RecentsService.ACTION_FINISH_RECENTS_ACTIVITY)) {
54                if (Constants.DebugFlags.App.EnableToggleNewRecentsActivity) {
55                    finish();
56                }
57            } else if (action.equals(RecentsService.ACTION_TOGGLE_RECENTS_ACTIVITY)) {
58                // Try and unfilter and filtered stacks
59                if (!mRecentsView.unfilterFilteredStacks()) {
60                    // If there are no filtered stacks, dismiss recents and launch the first task
61                    dismissRecentsIfVisible();
62                }
63            }
64        }
65    };
66
67    // Broadcast receiver to handle messages from the system
68    BroadcastReceiver mScreenOffReceiver = new BroadcastReceiver() {
69        @Override
70        public void onReceive(Context context, Intent intent) {
71            finish();
72        }
73    };
74
75    /** Updates the set of recent tasks */
76    void updateRecentsTasks(Intent launchIntent) {
77        // Update the configuration based on the launch intent
78        RecentsConfiguration config = RecentsConfiguration.getInstance();
79        config.launchedWithThumbnailAnimation = launchIntent.getBooleanExtra(
80                AlternateRecentsComponent.EXTRA_ANIMATING_WITH_THUMBNAIL, false);
81
82        RecentsTaskLoader loader = RecentsTaskLoader.getInstance();
83        SpaceNode root = loader.reload(this, Constants.Values.RecentsTaskLoader.PreloadFirstTasksCount);
84        ArrayList<TaskStack> stacks = root.getStacks();
85        if (!stacks.isEmpty()) {
86            mRecentsView.setBSP(root);
87        }
88
89        // Add the default no-recents layout
90        if (stacks.size() == 1 && stacks.get(0).getTaskCount() == 0) {
91            mEmptyView.setVisibility(View.VISIBLE);
92
93            // Dim the background even more
94            WindowManager.LayoutParams wlp = getWindow().getAttributes();
95            wlp.dimAmount = Constants.Values.Window.DarkBackgroundDim;
96            getWindow().setAttributes(wlp);
97            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
98        } else {
99            mEmptyView.setVisibility(View.GONE);
100        }
101    }
102
103    /** Dismisses recents if we are already visible and the intent is to toggle the recents view */
104    boolean dismissRecentsIfVisible() {
105        if (mVisible) {
106            if (!mRecentsView.launchFirstTask()) {
107                finish();
108            }
109            return true;
110        }
111        return false;
112    }
113
114    /** Called with the activity is first created. */
115    @Override
116    public void onCreate(Bundle savedInstanceState) {
117        super.onCreate(savedInstanceState);
118        Console.logDivider(Constants.DebugFlags.App.SystemUIHandshake);
119        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onCreate]",
120                getIntent().getAction() + " visible: " + mVisible, Console.AnsiRed);
121        Console.logTraceTime(Constants.DebugFlags.App.TimeRecentsStartup,
122                Constants.DebugFlags.App.TimeRecentsStartupKey, "onCreate");
123
124        // Initialize the loader and the configuration
125        RecentsTaskLoader.initialize(this);
126        RecentsConfiguration.reinitialize(this);
127
128        // Create the view hierarchy
129        mRecentsView = new RecentsView(this);
130        mRecentsView.setCallbacks(this);
131        mRecentsView.setLayoutParams(new FrameLayout.LayoutParams(
132                FrameLayout.LayoutParams.MATCH_PARENT,
133                FrameLayout.LayoutParams.MATCH_PARENT));
134
135        // Create the empty view
136        LayoutInflater inflater = LayoutInflater.from(this);
137        mEmptyView = inflater.inflate(R.layout.recents_empty, mContainerView, false);
138
139        mContainerView = new FrameLayout(this);
140        mContainerView.addView(mRecentsView);
141        mContainerView.addView(mEmptyView);
142        setContentView(mContainerView);
143
144        // Update the recent tasks
145        updateRecentsTasks(getIntent());
146    }
147
148    @Override
149    protected void onNewIntent(Intent intent) {
150        super.onNewIntent(intent);
151        // Reset the task launched flag if we encounter an onNewIntent() before onStop()
152        mTaskLaunched = false;
153
154        Console.logDivider(Constants.DebugFlags.App.SystemUIHandshake);
155        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onNewIntent]",
156                intent.getAction() + " visible: " + mVisible, Console.AnsiRed);
157        Console.logTraceTime(Constants.DebugFlags.App.TimeRecentsStartup,
158                Constants.DebugFlags.App.TimeRecentsStartupKey, "onNewIntent");
159
160        // Initialize the loader and the configuration
161        RecentsTaskLoader.initialize(this);
162        RecentsConfiguration.reinitialize(this);
163
164        // Update the recent tasks
165        updateRecentsTasks(intent);
166    }
167
168    @Override
169    protected void onStart() {
170        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onStart]", "",
171                Console.AnsiRed);
172        super.onStart();
173        mVisible = true;
174    }
175
176    @Override
177    protected void onResume() {
178        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onResume]", "",
179                Console.AnsiRed);
180        super.onResume();
181    }
182
183    @Override
184    public void onAttachedToWindow() {
185        Console.log(Constants.DebugFlags.App.SystemUIHandshake,
186                "[RecentsActivity|onAttachedToWindow]", "",
187                Console.AnsiRed);
188        super.onAttachedToWindow();
189
190        // Register the broadcast receiver to handle messages from our service
191        IntentFilter filter = new IntentFilter();
192        filter.addAction(RecentsService.ACTION_TOGGLE_RECENTS_ACTIVITY);
193        filter.addAction(RecentsService.ACTION_FINISH_RECENTS_ACTIVITY);
194        registerReceiver(mServiceBroadcastReceiver, filter);
195
196        // Register the broadcast receiver to handle messages when the screen is turned off
197        filter = new IntentFilter();
198        filter.addAction(Intent.ACTION_SCREEN_OFF);
199        registerReceiver(mScreenOffReceiver, filter);
200    }
201
202    @Override
203    public void onDetachedFromWindow() {
204        Console.log(Constants.DebugFlags.App.SystemUIHandshake,
205                "[RecentsActivity|onDetachedFromWindow]", "",
206                Console.AnsiRed);
207        super.onDetachedFromWindow();
208
209        // Unregister any broadcast receivers we have registered
210        unregisterReceiver(mServiceBroadcastReceiver);
211        unregisterReceiver(mScreenOffReceiver);
212    }
213
214    @Override
215    protected void onPause() {
216        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onPause]", "",
217                Console.AnsiRed);
218        super.onPause();
219    }
220
221    @Override
222    protected void onStop() {
223        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onStop]", "",
224                Console.AnsiRed);
225        super.onStop();
226
227        // Finish the current recents activity after we have launched a task
228        if (mTaskLaunched && Constants.DebugFlags.App.EnableToggleNewRecentsActivity) {
229            finish();
230        }
231
232        mVisible = false;
233        mTaskLaunched = false;
234    }
235
236    @Override
237    protected void onDestroy() {
238        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onDestroy]", "",
239                Console.AnsiRed);
240        super.onDestroy();
241    }
242
243    @Override
244    public void onTrimMemory(int level) {
245        RecentsTaskLoader loader = RecentsTaskLoader.getInstance();
246        if (loader != null) {
247            loader.onTrimMemory(level);
248        }
249    }
250
251    @Override
252    public void onBackPressed() {
253        if (!mRecentsView.unfilterFilteredStacks()) {
254            super.onBackPressed();
255        }
256    }
257
258    @Override
259    public void onTaskLaunching() {
260        mTaskLaunched = true;
261    }
262}
263