RecentsActivity.java revision 7ed267f53852aa18aacb605297e56f88d7c08333
1/*
2 * Copyright (C) 2012 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.recent;
18
19import android.app.Activity;
20import android.app.ActivityManager;
21import android.content.BroadcastReceiver;
22import android.content.Context;
23import android.content.Intent;
24import android.content.IntentFilter;
25import android.os.Bundle;
26import android.os.UserHandle;
27import android.view.MotionEvent;
28import android.view.View;
29
30import com.android.systemui.R;
31import com.android.systemui.SystemUIApplication;
32import com.android.systemui.statusbar.tablet.StatusBarPanel;
33
34import java.util.List;
35
36public class RecentsActivity extends Activity {
37    public static final String TOGGLE_RECENTS_INTENT = "com.android.systemui.TOGGLE_RECENTS";
38    public static final String CLOSE_RECENTS_INTENT = "com.android.systemui.CLOSE_RECENTS";
39    private static final String WAS_SHOWING = "was_showing";
40
41    private RecentsPanelView mRecentsPanel;
42    private IntentFilter mIntentFilter;
43    private boolean mShowing;
44    private boolean mForeground;
45    private BroadcastReceiver mIntentReceiver = new BroadcastReceiver() {
46        @Override
47        public void onReceive(Context context, Intent intent) {
48            if (mRecentsPanel != null && mRecentsPanel.isShowing()) {
49                if (mShowing && !mForeground) {
50                    // Captures the case right before we transition to another activity
51                    mRecentsPanel.show(false);
52                }
53            }
54        }
55    };
56
57    public static interface WindowAnimationStartListener {
58        void onWindowAnimationStart();
59    }
60
61    public class TouchOutsideListener implements View.OnTouchListener {
62        private StatusBarPanel mPanel;
63
64        public TouchOutsideListener(StatusBarPanel panel) {
65            mPanel = panel;
66        }
67
68        public boolean onTouch(View v, MotionEvent ev) {
69            final int action = ev.getAction();
70            if (action == MotionEvent.ACTION_OUTSIDE
71                    || (action == MotionEvent.ACTION_DOWN
72                    && !mPanel.isInContentArea((int) ev.getX(), (int) ev.getY()))) {
73                dismissAndGoHome();
74                return true;
75            }
76            return false;
77        }
78    }
79
80    @Override
81    public void onPause() {
82        mForeground = false;
83        super.onPause();
84    }
85
86    @Override
87    public void onStop() {
88        mShowing = false;
89        if (mRecentsPanel != null) {
90            mRecentsPanel.onUiHidden();
91        }
92        super.onStop();
93    }
94
95    @Override
96    public void onStart() {
97        mShowing = true;
98        if (mRecentsPanel != null) {
99            mRecentsPanel.refreshViews();
100        }
101        super.onStart();
102    }
103
104    @Override
105    public void onResume() {
106        mForeground = true;
107        super.onResume();
108    }
109
110    @Override
111    public void onBackPressed() {
112        dismissAndGoBack();
113    }
114
115    public void dismissAndGoHome() {
116        if (mRecentsPanel != null) {
117            Intent homeIntent = new Intent(Intent.ACTION_MAIN, null);
118            homeIntent.addCategory(Intent.CATEGORY_HOME);
119            homeIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK
120                    | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
121            startActivityAsUser(homeIntent, new UserHandle(UserHandle.USER_CURRENT));
122            mRecentsPanel.show(false);
123        }
124    }
125
126    public void dismissAndGoBack() {
127        if (mRecentsPanel != null) {
128            final ActivityManager am = (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
129
130            final List<ActivityManager.RecentTaskInfo> recentTasks =
131                    am.getRecentTasks(2,
132                            ActivityManager.RECENT_WITH_EXCLUDED |
133                            ActivityManager.RECENT_IGNORE_UNAVAILABLE);
134            if (recentTasks.size() > 1 &&
135                    mRecentsPanel.simulateClick(recentTasks.get(1).persistentId)) {
136                // recents panel will take care of calling show(false) through simulateClick
137                return;
138            }
139            mRecentsPanel.show(false);
140        }
141        finish();
142    }
143
144    @Override
145    protected void onCreate(Bundle savedInstanceState) {
146        final SystemUIApplication app = (SystemUIApplication) getApplication();
147        final RecentTasksLoader recentTasksLoader = app.getRecentTasksLoader();
148
149        setContentView(R.layout.status_bar_recent_panel);
150        mRecentsPanel = (RecentsPanelView) findViewById(R.id.recents_root);
151        mRecentsPanel.setOnTouchListener(new TouchOutsideListener(mRecentsPanel));
152        mRecentsPanel.setRecentTasksLoader(recentTasksLoader);
153        recentTasksLoader.setRecentsPanel(mRecentsPanel, mRecentsPanel);
154        mRecentsPanel.setMinSwipeAlpha(
155                getResources().getInteger(R.integer.config_recent_item_min_alpha) / 100f);
156
157        if (savedInstanceState == null ||
158                savedInstanceState.getBoolean(WAS_SHOWING)) {
159            handleIntent(getIntent());
160        }
161        mIntentFilter = new IntentFilter();
162        mIntentFilter.addAction(CLOSE_RECENTS_INTENT);
163        registerReceiver(mIntentReceiver, mIntentFilter);
164        app.setWindowAnimationStartListener(mRecentsPanel);
165        super.onCreate(savedInstanceState);
166    }
167
168    @Override
169    protected void onSaveInstanceState(Bundle outState) {
170        outState.putBoolean(WAS_SHOWING, mRecentsPanel.isShowing());
171    }
172
173    @Override
174    protected void onDestroy() {
175        final SystemUIApplication app = (SystemUIApplication) getApplication();
176        final RecentTasksLoader recentTasksLoader = app.getRecentTasksLoader();
177        recentTasksLoader.setRecentsPanel(null, mRecentsPanel);
178        unregisterReceiver(mIntentReceiver);
179        app.setWindowAnimationStartListener(null);
180        super.onDestroy();
181    }
182
183    @Override
184    protected void onNewIntent(Intent intent) {
185        handleIntent(intent);
186    }
187
188    private void handleIntent(Intent intent) {
189        super.onNewIntent(intent);
190
191        if (TOGGLE_RECENTS_INTENT.equals(intent.getAction())) {
192            if (mRecentsPanel != null) {
193                if (mRecentsPanel.isShowing()) {
194                    dismissAndGoBack();
195                } else {
196                    final SystemUIApplication app = (SystemUIApplication) getApplication();
197                    final RecentTasksLoader recentTasksLoader = app.getRecentTasksLoader();
198                    mRecentsPanel.show(true, recentTasksLoader.getLoadedTasks(),
199                            recentTasksLoader.isFirstScreenful());
200                }
201            }
202        }
203    }
204
205    boolean isForeground() {
206        return mForeground;
207    }
208
209    boolean isActivityShowing() {
210         return mShowing;
211    }
212}
213