RecentsActivity.java revision c6a1623cc48581380b698ae87b43bfafb9c935ba
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    BroadcastReceiver mServiceBroadcastReceiver = new BroadcastReceiver() {
47        @Override
48        public void onReceive(Context context, Intent intent) {
49            String action = intent.getAction();
50            Console.log(Constants.DebugFlags.App.SystemUIHandshake,
51                    "[RecentsActivity|serviceBroadcast]", action, Console.AnsiRed);
52            if (action.equals(RecentsService.ACTION_FINISH_RECENTS_ACTIVITY)) {
53                if (Constants.DebugFlags.App.EnableToggleNewRecentsActivity) {
54                    finish();
55                }
56            } else if (action.equals(RecentsService.ACTION_TOGGLE_RECENTS_ACTIVITY)) {
57                // Try and unfilter and filtered stacks
58                if (!mRecentsView.unfilterFilteredStacks()) {
59                    // If there are no filtered stacks, dismiss recents and launch the first task
60                    dismissRecentsIfVisible();
61                }
62            }
63        }
64    };
65
66    /** Updates the set of recent tasks */
67    void updateRecentsTasks() {
68        RecentsTaskLoader loader = RecentsTaskLoader.getInstance();
69        SpaceNode root = loader.reload(this, Constants.Values.RecentsTaskLoader.PreloadFirstTasksCount);
70        ArrayList<TaskStack> stacks = root.getStacks();
71        if (!stacks.isEmpty()) {
72            mRecentsView.setBSP(root);
73        }
74
75        // Add the default no-recents layout
76        if (stacks.size() == 1 && stacks.get(0).getTaskCount() == 0) {
77            mEmptyView.setVisibility(View.VISIBLE);
78
79            // Dim the background even more
80            WindowManager.LayoutParams wlp = getWindow().getAttributes();
81            wlp.dimAmount = Constants.Values.Window.DarkBackgroundDim;
82            getWindow().setAttributes(wlp);
83            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
84        } else {
85            mEmptyView.setVisibility(View.GONE);
86        }
87    }
88
89    /** Dismisses recents if we are already visible and the intent is to toggle the recents view */
90    boolean dismissRecentsIfVisible() {
91        if (mVisible) {
92            if (!mRecentsView.launchFirstTask()) {
93                finish();
94            }
95            return true;
96        }
97        return false;
98    }
99
100    /** Called with the activity is first created. */
101    @Override
102    public void onCreate(Bundle savedInstanceState) {
103        super.onCreate(savedInstanceState);
104        Console.logDivider(Constants.DebugFlags.App.SystemUIHandshake);
105        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onCreate]",
106                getIntent().getAction() + " visible: " + mVisible, Console.AnsiRed);
107        Console.logTraceTime(Constants.DebugFlags.App.TimeRecentsStartup,
108                Constants.DebugFlags.App.TimeRecentsStartupKey, "onCreate");
109
110        // Initialize the loader and the configuration
111        RecentsTaskLoader.initialize(this);
112        RecentsConfiguration.reinitialize(this);
113
114        // Set the background dim
115        WindowManager.LayoutParams wlp = getWindow().getAttributes();
116        wlp.dimAmount = Constants.Values.Window.BackgroundDim;
117        getWindow().setAttributes(wlp);
118        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
119
120        // Create the view hierarchy
121        mRecentsView = new RecentsView(this);
122        mRecentsView.setCallbacks(this);
123        mRecentsView.setLayoutParams(new FrameLayout.LayoutParams(
124                FrameLayout.LayoutParams.MATCH_PARENT,
125                FrameLayout.LayoutParams.MATCH_PARENT));
126
127        // Create the empty view
128        LayoutInflater inflater = LayoutInflater.from(this);
129        mEmptyView = inflater.inflate(R.layout.recents_empty, mContainerView, false);
130
131        mContainerView = new FrameLayout(this);
132        mContainerView.addView(mRecentsView);
133        mContainerView.addView(mEmptyView);
134        setContentView(mContainerView);
135
136        // Update the recent tasks
137        updateRecentsTasks();
138    }
139
140    @Override
141    protected void onNewIntent(Intent intent) {
142        super.onNewIntent(intent);
143        // Reset the task launched flag if we encounter an onNewIntent() before onStop()
144        mTaskLaunched = false;
145
146        Console.logDivider(Constants.DebugFlags.App.SystemUIHandshake);
147        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onNewIntent]",
148                intent.getAction() + " visible: " + mVisible, Console.AnsiRed);
149        Console.logTraceTime(Constants.DebugFlags.App.TimeRecentsStartup,
150                Constants.DebugFlags.App.TimeRecentsStartupKey, "onNewIntent");
151
152        // Initialize the loader and the configuration
153        RecentsTaskLoader.initialize(this);
154        RecentsConfiguration.reinitialize(this);
155
156        // Update the recent tasks
157        updateRecentsTasks();
158    }
159
160    @Override
161    protected void onStart() {
162        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onStart]", "",
163                Console.AnsiRed);
164        super.onStart();
165        mVisible = true;
166    }
167
168    @Override
169    protected void onResume() {
170        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onResume]", "",
171                Console.AnsiRed);
172        super.onResume();
173
174        // Register the broadcast receiver to handle messages from our service
175        IntentFilter filter = new IntentFilter();
176        filter.addAction(RecentsService.ACTION_TOGGLE_RECENTS_ACTIVITY);
177        filter.addAction(RecentsService.ACTION_FINISH_RECENTS_ACTIVITY);
178        registerReceiver(mServiceBroadcastReceiver, filter);
179    }
180
181    @Override
182    protected void onPause() {
183        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onPause]", "",
184                Console.AnsiRed);
185        super.onPause();
186
187        // Unregister any broadcast receivers we have registered
188        unregisterReceiver(mServiceBroadcastReceiver);
189    }
190
191    @Override
192    protected void onStop() {
193        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onStop]", "",
194                Console.AnsiRed);
195        super.onStop();
196
197        // Finish the current recents activity after we have launched a task
198        if (mTaskLaunched && Constants.DebugFlags.App.EnableToggleNewRecentsActivity) {
199            finish();
200        }
201
202        mVisible = false;
203        mTaskLaunched = false;
204    }
205
206    @Override
207    protected void onDestroy() {
208        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onDestroy]", "",
209                Console.AnsiRed);
210        super.onDestroy();
211    }
212
213    @Override
214    public void onTrimMemory(int level) {
215        RecentsTaskLoader loader = RecentsTaskLoader.getInstance();
216        if (loader != null) {
217            loader.onTrimMemory(level);
218        }
219    }
220
221    @Override
222    public void onBackPressed() {
223        if (!mRecentsView.unfilterFilteredStacks()) {
224            super.onBackPressed();
225        }
226    }
227
228    @Override
229    public void onTaskLaunching() {
230        mTaskLaunched = true;
231    }
232}
233