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