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