summaryrefslogtreecommitdiff
path: root/packages/SystemUI/src/com/android/systemui/recents/Recents.java
blob: 7c5f0856709fb613acb152f0e879ecd486a26b04 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
/*
 * Copyright (C) 2014 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.recents;

import android.content.ContentResolver;
import android.content.Context;
import android.content.res.Configuration;
import android.provider.Settings;

import com.android.systemui.CoreStartable;
import com.android.systemui.Dependency;
import com.android.systemui.statusbar.CommandQueue;
import com.android.systemui.slimrecent.RecentController;
import com.android.systemui.tuner.TunerService;

import java.io.PrintWriter;

/**
 * A proxy to a Recents implementation.
 */
public class Recents implements CoreStartable, CommandQueue.Callbacks, TunerService.Tunable {

    private static final String USE_SLIM_RECENTS = "system:" + Settings.System.USE_SLIM_RECENTS;

    private final Context mContext;
    private RecentsImplementation mImpl;
    private final CommandQueue mCommandQueue;

    private RecentsImplementation mDefaultImpl;
    private RecentController mSlimImpl;

    private boolean mStarted = false;
    private boolean mBootCompleted = false;

    private boolean mUseSlimRecents = false;

    public Recents(Context context, RecentsImplementation impl, CommandQueue commandQueue) {
        mContext = context;
        mDefaultImpl = impl;
        mCommandQueue = commandQueue;
        mImpl = getCurrentImpl();
    }

    @Override
    public void start() {
        mStarted = true;
        mCommandQueue.addCallback(this);
        mDefaultImpl.onStart(mContext);
        if (mSlimImpl != null) {
            mSlimImpl.onStart(mContext);
        }
        Dependency.get(TunerService.class).addTunable(this, USE_SLIM_RECENTS);
    }

    @Override
    public void onBootCompleted() {
        mBootCompleted = true;
        mDefaultImpl.onBootCompleted();
        if (mSlimImpl != null) {
            mSlimImpl.onBootCompleted();
        }
    }

    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        mImpl.onConfigurationChanged(newConfig);
    }

    @Override
    public void appTransitionFinished(int displayId) {
        if (mContext.getDisplayId() == displayId) {
            mImpl.onAppTransitionFinished();
        }
    }

    @Override
    public void showRecentApps(boolean triggeredFromAltTab) {
        // Ensure the device has been provisioned before allowing the user to interact with
        // recents
        if (!isUserSetup()) {
            return;
        }

        mImpl.showRecentApps(triggeredFromAltTab);
    }

    @Override
    public void hideRecentApps(boolean triggeredFromAltTab, boolean triggeredFromHomeKey) {
        // Ensure the device has been provisioned before allowing the user to interact with
        // recents
        if (!isUserSetup()) {
            return;
        }

        mImpl.hideRecentApps(triggeredFromAltTab, triggeredFromHomeKey);
    }

    @Override
    public void toggleRecentApps() {
        // Ensure the device has been provisioned before allowing the user to interact with
        // recents
        if (!isUserSetup()) {
            return;
        }

        mImpl.toggleRecentApps();
    }

    @Override
    public void preloadRecentApps() {
        // Ensure the device has been provisioned before allowing the user to interact with
        // recents
        if (!isUserSetup()) {
            return;
        }

        mImpl.preloadRecentApps();
    }

    @Override
    public void cancelPreloadRecentApps() {
        // Ensure the device has been provisioned before allowing the user to interact with
        // recents
        if (!isUserSetup()) {
            return;
        }

        mImpl.cancelPreloadRecentApps();
    }

    /**
     * @return whether this device is provisioned and the current user is set up.
     */
    private boolean isUserSetup() {
        ContentResolver cr = mContext.getContentResolver();
        return (Settings.Global.getInt(cr, Settings.Global.DEVICE_PROVISIONED, 0) != 0) &&
                (Settings.Secure.getInt(cr, Settings.Secure.USER_SETUP_COMPLETE, 0) != 0);
    }

    @Override
    public void dump(PrintWriter pw, String[] args) {
        mImpl.dump(pw);
    }

    private RecentsImplementation getCurrentImpl() {
        RecentsImplementation newImpl;
        boolean newCreated = false;
        if (mUseSlimRecents) {
            if (mSlimImpl == null) {
                newCreated = true;
                mSlimImpl = new RecentController(mDefaultImpl);
            }
            newImpl = mSlimImpl;
        } else {
            newImpl = mDefaultImpl;
        }
        if (newCreated) {
            if (mStarted) {
                newImpl.onStart(mContext);
            }
            if (mBootCompleted) {
                newImpl.onBootCompleted();
            }
        }
        return newImpl;
    }

    @Override
    public void onTuningChanged(String key, String newValue) {
        switch (key) {
            case USE_SLIM_RECENTS:
                mUseSlimRecents = "1".equals(newValue);
                mImpl = getCurrentImpl();
                break;
         }
     }
}