summaryrefslogtreecommitdiffstats
path: root/packages/SystemUI/src/com/android/systemui/statusbar/policy/LiveLockScreenController.java
blob: 69720efde8549769a57d58c9a260da35f57d624e (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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
package com.android.systemui.statusbar.policy;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Handler;
import android.os.Looper;
import android.os.PowerManager;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.EventLog;

import android.view.View;
import com.android.systemui.EventLogTags;
import com.android.systemui.statusbar.StatusBarState;
import com.android.systemui.statusbar.phone.NotificationPanelView;
import com.android.systemui.statusbar.phone.PhoneStatusBar;

import cyanogenmod.app.CMContextConstants;
import cyanogenmod.app.ILiveLockScreenChangeListener;
import cyanogenmod.app.ILiveLockScreenManager;
import cyanogenmod.app.LiveLockScreenInfo;
import cyanogenmod.externalviews.KeyguardExternalView;

import java.util.Objects;

public class LiveLockScreenController {
    private static final String TAG = LiveLockScreenController.class.getSimpleName();

    private ILiveLockScreenManager mLLSM;
    private Context mContext;
    private PhoneStatusBar mBar;
    private NotificationPanelView mPanelView;
    private ComponentName mLiveLockScreenComponentName;
    private KeyguardExternalView mLiveLockScreenView;
    private Handler mHandler;

    private int mStatusBarState;

    private PowerManager mPowerManager;

    private boolean mLlsHasFocus = false;

    private boolean mScreenOnAndInteractive;

    private String mLlsName;

    public LiveLockScreenController(Context context, PhoneStatusBar bar,
            NotificationPanelView panelView) {
        mContext = context;
        mHandler = new Handler(Looper.getMainLooper());

        mLLSM = ILiveLockScreenManager.Stub.asInterface(ServiceManager.getService(
                CMContextConstants.CM_LIVE_LOCK_SCREEN_SERVICE));
        mBar = bar;
        mPanelView = panelView;
        mPowerManager = context.getSystemService(PowerManager.class);
        registerListener();
        try {
            LiveLockScreenInfo llsInfo = mLLSM.getCurrentLiveLockScreen();
            if (llsInfo != null && llsInfo.component != null) {
                updateLiveLockScreenView(llsInfo.component);
            }
        } catch (RemoteException e) {
            /* ignore */
        }
    }

    public void cleanup() {
        unregisterListener();
        mPanelView = null;
        if (mLiveLockScreenView != null) {
            mLiveLockScreenView.setProviderComponent(null);
        }
        mLiveLockScreenView = null;
        mLiveLockScreenComponentName = null;
    }

    public void setBarState(int statusBarState) {
        if (mStatusBarState != StatusBarState.SHADE && statusBarState == StatusBarState.SHADE) {
            // going from KEYGUARD or SHADE_LOCKED to SHADE so device has been unlocked
            onKeyguardDismissed();
        }

        if (statusBarState == StatusBarState.KEYGUARD) {
            mBar.getScrimController().forceHideScrims(false);
        }

        mStatusBarState = statusBarState;
        if (statusBarState == StatusBarState.KEYGUARD ||
                statusBarState == StatusBarState.SHADE_LOCKED) {
            if (mLiveLockScreenComponentName != null) {
                if (mLiveLockScreenView == null) {
                    mLiveLockScreenView =
                            getExternalKeyguardView(mLiveLockScreenComponentName);
                    if (mLiveLockScreenView != null) {
                        mLiveLockScreenView.registerKeyguardExternalViewCallback(
                                mExternalKeyguardViewCallbacks);
                    }
                }
                if (mLiveLockScreenView != null && !mLiveLockScreenView.isAttachedToWindow()) {
                    mBar.updateRowStates();
                    mPanelView.addView(mLiveLockScreenView, 0);
                }
            }
        } else {
            if (isShowingLiveLockScreenView() && !mBar.isKeyguardInputRestricted()) {
                mPanelView.removeView(mLiveLockScreenView);
            }
            mLlsHasFocus = false;
        }
    }

    private ILiveLockScreenChangeListener mChangeListener =
            new ILiveLockScreenChangeListener.Stub() {
        @Override
        public void onLiveLockScreenChanged(LiveLockScreenInfo llsInfo) throws RemoteException {
            if (mPanelView != null) {
                updateLiveLockScreenView(llsInfo != null ? llsInfo.component : null);
            }
        }
    };

    private void registerListener() {
        try {
            mLLSM.registerChangeListener(mChangeListener);
        } catch (RemoteException e) {
            /* ignore */
        }
    }

    private void unregisterListener() {
        try {
            mLLSM.unregisterChangeListener(mChangeListener);
        } catch (RemoteException e) {
            /* ignore */
        }
    }

    private KeyguardExternalView getExternalKeyguardView(ComponentName componentName) {
        try {
            return new KeyguardExternalView(mContext, null, componentName);
        } catch (Exception e) {
            // just return null below and move on
        }
        return null;
    }

    private KeyguardExternalView.KeyguardExternalViewCallbacks mExternalKeyguardViewCallbacks =
            new KeyguardExternalView.KeyguardExternalViewCallbacks() {
        @Override
        public boolean requestDismiss() {
            if (isShowingLiveLockScreenView()) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mBar.showKeyguard();
                        mBar.showBouncer();
                    }
                });
                return true;
            }
            return false;
        }

        @Override
        public boolean requestDismissAndStartActivity(final Intent intent) {
            if (isShowingLiveLockScreenView()) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mBar.startActivityDismissingKeyguard(intent, false, true, true,
                                null);
                    }
                });
                return true;
            }
            return false;
        }

        @Override
        public void providerDied() {
            mLiveLockScreenView.unregisterKeyguardExternalViewCallback(
                    mExternalKeyguardViewCallbacks);
            mLiveLockScreenView = null;
            // make sure we're showing the notification panel if the LLS crashed while it had focus
            if (mLlsHasFocus) {
                mLlsHasFocus = false;
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mBar.showKeyguard();
                    }
                });
            }
        }

        @Override
        public void slideLockscreenIn() {
            if (mLlsHasFocus) {
                mHandler.post(new Runnable() {
                    @Override
                    public void run() {
                        mBar.showKeyguard();
                    }
                });
            }
        }
    };

    public boolean isShowingLiveLockScreenView() {
        return mLiveLockScreenView != null && mLiveLockScreenView.isAttachedToWindow();
    }

    public boolean isLiveLockScreenInteractive() {
        return mLiveLockScreenView != null && mLiveLockScreenView.isInteractive();
    }

    public KeyguardExternalView getLiveLockScreenView() {
        return mLiveLockScreenView;
    }

    public void onScreenTurnedOn() {
        mScreenOnAndInteractive = mPowerManager.isInteractive();
        if (mScreenOnAndInteractive) {
            if (mLiveLockScreenView != null) mLiveLockScreenView.onScreenTurnedOn();
            EventLog.writeEvent(EventLogTags.SYSUI_LLS_KEYGUARD_SHOWING, 1);
        }
    }

    public void onScreenTurnedOff() {
        if (mScreenOnAndInteractive) {
            if (mLiveLockScreenView != null) mLiveLockScreenView.onScreenTurnedOff();
            if (mStatusBarState != StatusBarState.SHADE) {
                EventLog.writeEvent(EventLogTags.SYSUI_LLS_KEYGUARD_SHOWING, 0);
            }
            mScreenOnAndInteractive = false;
        }
    }

    public void onLiveLockScreenFocusChanged(boolean hasFocus) {
        if (mLiveLockScreenView != null) {
            // make sure the LLS knows where the notification panel is
            mLiveLockScreenView.onLockscreenSlideOffsetChanged(hasFocus ? 0f : 1f);
        }
        // don't log focus changes when screen is not interactive
        if (hasFocus != mLlsHasFocus && mPowerManager.isInteractive()) {
            EventLog.writeEvent(EventLogTags.SYSUI_LLS_NOTIFICATION_PANEL_SHOWN,
                    hasFocus ? 0 : 1);
        }
        // Hide statusbar and scrim if live lockscreen
        // currently has focus
        mBar.setStatusBarViewVisibility(!hasFocus);
        mBar.getScrimController().forceHideScrims(hasFocus);
        mLlsHasFocus = hasFocus;
    }

    public void onKeyguardDismissed() {
        if (mLiveLockScreenView != null) mLiveLockScreenView.onKeyguardDismissed();
        EventLog.writeEvent(EventLogTags.SYSUI_LLS_KEYGUARD_DISMISSED, mLlsHasFocus ? 1 : 0);
        // Ensure we reset visibility when keyguard is dismissed
        mBar.setStatusBarViewVisibility(true);
        mBar.getScrimController().forceHideScrims(false);
    }

    public boolean getLiveLockScreenHasFocus() {
        return mLlsHasFocus;
    }

    public String getLiveLockScreenName() {
        return mLlsName;
    }

    private String getLlsNameFromComponentName(ComponentName cn) {
        if (cn == null) return null;

        PackageManager pm = mContext.getPackageManager();
        Intent intent = new Intent();
        intent.setComponent(cn);
        ResolveInfo ri = pm.resolveService(intent, 0);
        return ri != null ? ri.serviceInfo.loadLabel(pm).toString() : null;
    }

    private Runnable mAddNewLiveLockScreenRunnable = new Runnable() {
        @Override
        public void run() {
            if (mLiveLockScreenComponentName != null) {
                mLiveLockScreenView =
                        getExternalKeyguardView(mLiveLockScreenComponentName);
                mLiveLockScreenView.registerKeyguardExternalViewCallback(
                        mExternalKeyguardViewCallbacks);
                if (mStatusBarState != StatusBarState.SHADE) {
                    mPanelView.addView(mLiveLockScreenView);
                    mLiveLockScreenView.onKeyguardShowing(true);
                }
            } else {
                mLiveLockScreenView = null;
            }
        }
    };

    private void updateLiveLockScreenView(final ComponentName cn) {
        mHandler.post(new Runnable() {
            @Override
            public void run() {
                // If mThirdPartyKeyguardViewComponent differs from cn, go ahead and update
                if (!Objects.equals(mLiveLockScreenComponentName, cn)) {
                    mLiveLockScreenComponentName = cn;
                    mLlsName = getLlsNameFromComponentName(cn);
                    if (mLiveLockScreenView != null) {
                        mLiveLockScreenView.unregisterKeyguardExternalViewCallback(
                                mExternalKeyguardViewCallbacks);
                        // setProviderComponent(null) will unbind the existing service
                        mLiveLockScreenView.setProviderComponent(null);
                        if (mPanelView.indexOfChild(mLiveLockScreenView) >= 0) {
                            mLiveLockScreenView.registerOnWindowAttachmentChangedListener(
                                    new KeyguardExternalView.OnWindowAttachmentChangedListener() {
                                        @Override
                                        public void onAttachedToWindow() {
                                        }

                                        @Override
                                        public void onDetachedFromWindow() {
                                            mLiveLockScreenView
                                                    .unregisterOnWindowAttachmentChangedListener(
                                                            this);
                                            mHandler.post(mAddNewLiveLockScreenRunnable);
                                        }
                                    }
                            );
                            mPanelView.removeView(mLiveLockScreenView);
                        } else {
                            mAddNewLiveLockScreenRunnable.run();
                        }
                    }
                }
            }
        });
    }
}