aboutsummaryrefslogtreecommitdiffstats
path: root/cm/lib/main/java/org/cyanogenmod/platform/internal/LiveLockScreenServiceBroker.java
blob: e133aa6566e0722e45c386ff46a7c46509781d00 (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
/*
 * Copyright (C) 2016 The CyanogenMod 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 org.cyanogenmod.platform.internal;

import android.annotation.Nullable;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.ServiceConnection;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.os.Binder;
import android.os.Handler;
import android.os.IBinder;
import android.os.IInterface;
import android.os.Message;
import android.os.RemoteCallbackList;
import android.os.RemoteException;
import android.os.SystemClock;
import android.text.TextUtils;
import android.util.Slog;

import com.android.server.SystemService;

import cyanogenmod.app.CMContextConstants;
import cyanogenmod.app.ILiveLockScreenChangeListener;
import cyanogenmod.app.ILiveLockScreenManager;
import cyanogenmod.app.ILiveLockScreenManagerProvider;
import cyanogenmod.app.LiveLockScreenInfo;
import cyanogenmod.app.LiveLockScreenManager;
import cyanogenmod.platform.Manifest;
import cyanogenmod.providers.CMSettings;

import org.cyanogenmod.platform.internal.common.BrokeredServiceConnection;

import java.util.List;

/**
 * Live lock screen service broker for connecting clients to a backing Live lock screen manager
 * service.
 *
 * @hide
 */
public class LiveLockScreenServiceBroker extends
        BrokerableCMSystemService<ILiveLockScreenManagerProvider> {
    private static final String TAG = LiveLockScreenServiceBroker.class.getSimpleName();
    private static final boolean DEBUG = false;

    private static final String DEPRECATED_THIRD_PARTY_KEYGUARD_PERMISSION =
            "android.permission.THIRD_PARTY_KEYGUARD";

    private Context mContext;

    // Cached change listeners
    private final RemoteCallbackList<ILiveLockScreenChangeListener> mChangeListeners =
            new RemoteCallbackList<>();

    private LiveLockScreenInfo mDefaultLlsInfo;

    /**
     * ILiveLockScreenManager implementation to use when no backing service can be found.
     */
    private final ILiveLockScreenManagerProvider mServiceStubForFailure =
            new ILiveLockScreenManagerProvider() {
        @Override
        public void enqueueLiveLockScreen(String pkg, int id, LiveLockScreenInfo lls,
                int[] idReceived, int userid) throws RemoteException {
        }

        @Override
        public void cancelLiveLockScreen(String pkg, int id, int userId) throws RemoteException {
        }

        @Override
        public LiveLockScreenInfo getCurrentLiveLockScreen() throws RemoteException {
            return null;
        }

        @Override
        public void updateDefaultLiveLockScreen(LiveLockScreenInfo llsInfo) throws RemoteException {

        }

        @Override
        public boolean getLiveLockScreenEnabled() throws RemoteException {
            return false;
        }

        @Override
        public boolean registerChangeListener(
                ILiveLockScreenChangeListener listener) throws RemoteException {
            return false;
        }

        @Override
        public boolean unregisterChangeListener(
                ILiveLockScreenChangeListener listener) throws RemoteException {
            return false;
        }

        @Override
        public IBinder asBinder() {
            return null;
        }
    };

    private final class BinderService extends ILiveLockScreenManager.Stub {

        @Override
        public void enqueueLiveLockScreen(String pkg, int id,
                LiveLockScreenInfo lls, int[] idReceived, int userId) throws RemoteException {
            getBrokeredService().enqueueLiveLockScreen(pkg, id, lls, idReceived, userId);
        }

        @Override
        public void cancelLiveLockScreen(String pkg, int id, int userId) throws RemoteException {
            getBrokeredService().cancelLiveLockScreen(pkg, id, userId);
        }

        @Override
        public LiveLockScreenInfo getCurrentLiveLockScreen() throws RemoteException {
            return getBrokeredService().getCurrentLiveLockScreen();
        }

        @Override
        public LiveLockScreenInfo getDefaultLiveLockScreen() throws RemoteException {
            enforcePrivateAccessPermission();
            return getDefaultLiveLockScreenInternal();
        }

        @Override
        public void setDefaultLiveLockScreen(LiveLockScreenInfo llsInfo) throws RemoteException {
            enforcePrivateAccessPermission();
            setDefaultLiveLockScreenInternal(llsInfo);
        }

        @Override
        public void setLiveLockScreenEnabled(boolean enabled) throws RemoteException {
            enforcePrivateAccessPermission();
            setLiveLockScreenEnabledInternal(enabled);
        }

        @Override
        public boolean getLiveLockScreenEnabled() throws RemoteException {
            return getBrokeredService().getLiveLockScreenEnabled();
        }

        @Override
        public boolean registerChangeListener(
                ILiveLockScreenChangeListener listener) throws RemoteException {
            boolean registered = getBrokeredService().registerChangeListener(listener);
            if (registered) {
                mChangeListeners.register(listener);
            }
            return registered;
        }

        @Override
        public boolean unregisterChangeListener(
                ILiveLockScreenChangeListener listener) throws RemoteException {
            boolean unregistered = getBrokeredService().unregisterChangeListener(listener);
            if (unregistered) {
                mChangeListeners.unregister(listener);
            }
            return unregistered;
        }
    }

    public LiveLockScreenServiceBroker(Context context) {
        super(context);
        mContext = context;
        setBrokeredServiceConnection(mServiceConnection);
    }

    @Override
    public String getFeatureDeclaration() {
        return CMContextConstants.Features.LIVE_LOCK_SCREEN;
    }

    @Override
    public void onStart() {
        if (DEBUG) Slog.d(TAG, "service started");
        publishBinderService(CMContextConstants.CM_LIVE_LOCK_SCREEN_SERVICE, new BinderService());
    }

    @Override
    protected ILiveLockScreenManagerProvider getIBinderAsIInterface(IBinder service) {
        return ILiveLockScreenManagerProvider.Stub.asInterface(service);
    }

    @Override
    public ILiveLockScreenManagerProvider getDefaultImplementation() {
        return mServiceStubForFailure;
    }

    private BrokeredServiceConnection mServiceConnection = new BrokeredServiceConnection() {
        @Override
        public void onBrokeredServiceConnected() {
            // If any change listeners are cached, register them with the newly connected
            // service.
            try {
                int N = mChangeListeners.beginBroadcast();
                ILiveLockScreenManagerProvider iLiveLockScreenManagerProvider =
                        getBrokeredService();
                if (iLiveLockScreenManagerProvider != null && N > 0) {
                    for (int i = 0; i < N; i++) {
                        iLiveLockScreenManagerProvider
                                .registerChangeListener(mChangeListeners.getBroadcastItem(i));
                    }
                }
            } catch (RemoteException e) {
                    /* ignore */
            } finally {
                mChangeListeners.finishBroadcast();
            }
        }

        @Override
        public void onBrokeredServiceDisconnected() {

        }
    };

    @Override
    protected String getComponentFilteringPermission() {
        // Live lock screen service has its own vertical providing permission
        return Manifest.permission.LIVE_LOCK_SCREEN_MANAGER_PROVIDER;
    }

    @Override
    protected ComponentName getServiceComponent() {
        PackageManager pm = mContext.getPackageManager();
        Intent intent = new Intent(LiveLockScreenManager.SERVICE_INTERFACE);
        List<ResolveInfo> resolveInfos = pm.queryIntentServices(intent, 0);
        for (ResolveInfo info : resolveInfos) {
            if (info != null) {
                if (info.serviceInfo.isEnabled()) {
                    return new ComponentName(info.serviceInfo.packageName, info.serviceInfo.name);
                }
            }
        }
        return null;
    }

    @Override
    public void onBootPhase(int phase) {
        if (phase == PHASE_THIRD_PARTY_APPS_CAN_START) {
            if (DEBUG) Slog.d(TAG, "Third party apps ready");

            // Initialize the default LLS component
            String defComponent = CMSettings.Secure.getString(mContext.getContentResolver(),
                    CMSettings.Secure.DEFAULT_LIVE_LOCK_SCREEN_COMPONENT);
            if (!TextUtils.isEmpty(defComponent)) {
                mDefaultLlsInfo = new LiveLockScreenInfo.Builder()
                        .setComponent(ComponentName.unflattenFromString(defComponent))
                        .build();
            }
        }
        super.onBootPhase(phase);
    }

    /**
     * Enforces the
     * {@link cyanogenmod.platform.Manifest.permission#LIVE_LOCK_SCREEN_MANAGER_ACCESS_PRIVATE}
     * permission.
     */
    private void enforcePrivateAccessPermission() {
        mContext.enforceCallingPermission(
                Manifest.permission.LIVE_LOCK_SCREEN_MANAGER_ACCESS_PRIVATE, null);
    }

    private LiveLockScreenInfo getDefaultLiveLockScreenInternal() {
        return mDefaultLlsInfo;
    }

    private void setDefaultLiveLockScreenInternal(LiveLockScreenInfo llsInfo) {
        if (llsInfo != null && llsInfo.component != null) {
            // Check that the package this component belongs to has the third party keyguard perm
            final PackageManager pm = mContext.getPackageManager();
            final boolean hasThirdPartyKeyguardPermission = pm.checkPermission(
                    Manifest.permission.THIRD_PARTY_KEYGUARD,
                    llsInfo.component.getPackageName()) == PackageManager.PERMISSION_GRANTED
                    || pm.checkPermission(DEPRECATED_THIRD_PARTY_KEYGUARD_PERMISSION,
                    llsInfo.component.getPackageName()) == PackageManager.PERMISSION_GRANTED;
            if (!hasThirdPartyKeyguardPermission) {
                Slog.e(TAG, "Package " + llsInfo.component.getPackageName() +
                        " does not have " + Manifest.permission.THIRD_PARTY_KEYGUARD);
                return;
            }
        }

        long token = Binder.clearCallingIdentity();
        try {
            CMSettings.Secure.putString(mContext.getContentResolver(),
                    CMSettings.Secure.DEFAULT_LIVE_LOCK_SCREEN_COMPONENT,
                    (llsInfo != null && llsInfo.component != null)
                            ? llsInfo.component.flattenToString()
                            : "");
        } finally {
            Binder.restoreCallingIdentity(token);
        }

        mDefaultLlsInfo = llsInfo;
        try {
            getBrokeredService().updateDefaultLiveLockScreen(llsInfo);
        } catch (RemoteException e) {
            /* ignore */
        }
    }

    private void setLiveLockScreenEnabledInternal(boolean enabled) {
        long token = Binder.clearCallingIdentity();
        CMSettings.Secure.putInt(mContext.getContentResolver(),
                CMSettings.Secure.LIVE_LOCK_SCREEN_ENABLED, enabled ? 1 : 0);
        Binder.restoreCallingIdentity(token);
    }
}