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