RecentsActivity.java revision 303e1ff1fec8b240b587bb18b981247a99833aa8
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.Intent;
21import android.os.Bundle;
22import android.view.LayoutInflater;
23import android.view.View;
24import android.view.WindowManager;
25import android.widget.FrameLayout;
26import com.android.systemui.recents.model.SpaceNode;
27import com.android.systemui.recents.model.TaskStack;
28import com.android.systemui.recents.views.RecentsView;
29import com.android.systemui.R;
30
31import java.util.ArrayList;
32
33
34/* Activity */
35public class RecentsActivity extends Activity {
36    FrameLayout mContainerView;
37    RecentsView mRecentsView;
38    View mEmptyView;
39    boolean mVisible;
40
41    /** Updates the set of recent tasks */
42    void updateRecentsTasks() {
43        RecentsTaskLoader loader = RecentsTaskLoader.getInstance();
44        SpaceNode root = loader.reload(this, Constants.Values.RecentsTaskLoader.PreloadFirstTasksCount);
45        ArrayList<TaskStack> stacks = root.getStacks();
46        if (!stacks.isEmpty()) {
47            // XXX: We just replace the root every time for now, we will change this in the future
48            mRecentsView.setBSP(root);
49        }
50
51        // Add the default no-recents layout
52        if (stacks.size() == 1 && stacks.get(0).getTaskCount() == 0) {
53            mEmptyView.setVisibility(View.VISIBLE);
54
55            // Dim the background even more
56            WindowManager.LayoutParams wlp = getWindow().getAttributes();
57            wlp.dimAmount = Constants.Values.Window.DarkBackgroundDim;
58            getWindow().setAttributes(wlp);
59            getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
60        } else {
61            mEmptyView.setVisibility(View.GONE);
62        }
63    }
64
65    /** Dismisses recents if we are already visible and the intent is to toggle the recents view */
66    boolean dismissRecentsIfVisible(Intent intent) {
67        if ("com.android.systemui.recents.TOGGLE_RECENTS".equals(intent.getAction())) {
68            if (mVisible) {
69                if (!mRecentsView.launchFirstTask()) {
70                    finish();
71                }
72                return true;
73            }
74        }
75        return false;
76    }
77
78    /** Called with the activity is first created. */
79    @Override
80    public void onCreate(Bundle savedInstanceState) {
81        super.onCreate(savedInstanceState);
82        Console.logDivider(Constants.DebugFlags.App.SystemUIHandshake);
83        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onCreate]",
84                getIntent().getAction() + " visible: " + mVisible, Console.AnsiRed);
85
86        // Initialize the loader and the configuration
87        RecentsTaskLoader.initialize(this);
88        RecentsConfiguration.reinitialize(this);
89
90        // Dismiss recents if it is visible and we are toggling
91        if (dismissRecentsIfVisible(getIntent())) return;
92
93        // Set the background dim
94        WindowManager.LayoutParams wlp = getWindow().getAttributes();
95        wlp.dimAmount = Constants.Values.Window.BackgroundDim;
96        getWindow().setAttributes(wlp);
97        getWindow().addFlags(WindowManager.LayoutParams.FLAG_DIM_BEHIND);
98
99        // Create the view hierarchy
100        mRecentsView = new RecentsView(this);
101        mRecentsView.setLayoutParams(new FrameLayout.LayoutParams(
102                FrameLayout.LayoutParams.MATCH_PARENT,
103                FrameLayout.LayoutParams.MATCH_PARENT));
104
105        // Create the empty view
106        LayoutInflater inflater = LayoutInflater.from(this);
107        mEmptyView = inflater.inflate(R.layout.recents_empty, mContainerView, false);
108
109        mContainerView = new FrameLayout(this);
110        mContainerView.addView(mRecentsView);
111        mContainerView.addView(mEmptyView);
112        setContentView(mContainerView);
113
114        // Update the recent tasks
115        updateRecentsTasks();
116    }
117
118    @Override
119    protected void onNewIntent(Intent intent) {
120        super.onNewIntent(intent);
121        Console.logDivider(Constants.DebugFlags.App.SystemUIHandshake);
122        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onNewIntent]",
123                intent.getAction() + " visible: " + mVisible, Console.AnsiRed);
124
125        // Dismiss recents if it is visible and we are toggling
126        if (dismissRecentsIfVisible(intent)) return;
127
128        // Initialize the loader and the configuration
129        RecentsTaskLoader.initialize(this);
130        RecentsConfiguration.reinitialize(this);
131
132        // Update the recent tasks
133        updateRecentsTasks();
134    }
135
136    @Override
137    protected void onStart() {
138        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onStart]", "",
139                Console.AnsiRed);
140        super.onStart();
141        mVisible = true;
142    }
143
144    @Override
145    protected void onResume() {
146        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onResume]", "",
147                Console.AnsiRed);
148        super.onResume();
149    }
150
151    @Override
152    protected void onPause() {
153        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onPause]", "",
154                Console.AnsiRed);
155        super.onPause();
156
157        // Stop the loader when we leave Recents
158        RecentsTaskLoader loader = RecentsTaskLoader.getInstance();
159        loader.stopLoader();
160    }
161
162    @Override
163    protected void onStop() {
164        Console.log(Constants.DebugFlags.App.SystemUIHandshake, "[RecentsActivity|onStop]", "",
165                Console.AnsiRed);
166        super.onStop();
167        mVisible = false;
168    }
169
170    @Override
171    public void onBackPressed() {
172        if (!mRecentsView.unfilterFilteredStacks()) {
173            super.onBackPressed();
174        }
175    }
176}
177