aboutsummaryrefslogtreecommitdiffstats
path: root/sdk/src/java/cyanogenmod/app/LiveLockScreenManager.java
blob: bd6a515f3f81d275d6342dbf858603add9619a45 (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
/*
 * 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 cyanogenmod.app;

import android.annotation.NonNull;
import android.annotation.Nullable;
import android.annotation.SdkConstant;
import android.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.os.UserHandle;
import android.util.Log;

/**
 * Manages enabling/disabling Live lock screens as well as what Live lock screen to display when
 * enabled.
 */
public class LiveLockScreenManager {
    private static final String TAG = LiveLockScreenManager.class.getSimpleName();
    private static ILiveLockScreenManager sService;
    private static LiveLockScreenManager sInstance;

    private Context mContext;

    /**
     * The {@link android.content.Intent} that must be declared as handled by the service.
     */
    @SdkConstant(SdkConstant.SdkConstantType.SERVICE_ACTION)
    public static final String SERVICE_INTERFACE
            = "cyanogenmod.app.LiveLockScreenManagerService";

    private LiveLockScreenManager(Context context) {
        mContext = context;
        sService = getService();
        if (context.getPackageManager().hasSystemFeature(
                CMContextConstants.Features.LIVE_LOCK_SCREEN) && sService == null) {
            Log.wtf(TAG, "Unable to get LiveLockScreenManagerService. " +
                    "The service either crashed, was not started, or the interface has " +
                    "been called to early in SystemServer init");
        }
    }

    private ILiveLockScreenManager getService() {
        if (sService == null) {
            IBinder b = ServiceManager.getService(CMContextConstants.CM_LIVE_LOCK_SCREEN_SERVICE);
            if (b != null) {
                sService = ILiveLockScreenManager.Stub.asInterface(b);
            }
        }

        return sService;
    }

    private void logServiceException(Exception e) {
        Log.w(TAG, "Unable to access LiveLockScreenServiceBroker", e);
    }

    public static LiveLockScreenManager getInstance(Context context) {
        if (sInstance == null) {
            sInstance = new LiveLockScreenManager(context);
        }

        return sInstance;
    }

    /**
     * Requests a Live lock screen, defined in {@param lls}, to be displayed with the given id.
     * @param id An identifier for this notification unique within your application.
     * @param llsInfo A {@link LiveLockScreenInfo} object describing what Live lock screen to show
     *                the user.
     * @return True if the Live lock screen was successfully received by the backing service
     */
    public boolean show(int id, @NonNull final LiveLockScreenInfo llsInfo) {
        int[] idOut = new int[1];
        String pkg = mContext.getPackageName();
        boolean success = true;
        try {
            sService.enqueueLiveLockScreen(pkg, id, llsInfo, idOut, UserHandle.myUserId());
            if (id != idOut[0]) {
                Log.w(TAG, "show: id corrupted: sent " + id + ", got back " + idOut[0]);
                success = false;
            }
        } catch (RemoteException e) {
            logServiceException(e);
            success = false;
        }

        return success;
    }

    /**
     * Cancels a previously shown Live lock screen.
     * @param id An identifier for this notification unique within your application.
     */
    public void cancel(int id) {
        String pkg = mContext.getPackageName();
        try {
            sService.cancelLiveLockScreen(pkg, id, UserHandle.myUserId());
        } catch (RemoteException e) {
            logServiceException(e);
        }
    }

    /**
     * Sets the default Live lock screen to display when no other Live lock screens are queued
     * up for display.
     * <p>
     *     This is not available to third party applications.
     * </p>
     */
    public void setDefaultLiveLockScreen(@Nullable LiveLockScreenInfo llsInfo) {
        try {
            sService.setDefaultLiveLockScreen(llsInfo);
        } catch (RemoteException e) {
            logServiceException(e);
        }
    }

    /**
     * Gets the default Live lock screen that is displayed when no other Live lock screens are
     * queued up for display.
     * <p>
     *     This is not available to third party applications.
     * </p>
     */
    public LiveLockScreenInfo getDefaultLiveLockScreen() {
        try {
            return sService.getDefaultLiveLockScreen();
        } catch (RemoteException e) {
            logServiceException(e);
        }

        return null;
    }

    /** @hide */
    public LiveLockScreenInfo getCurrentLiveLockScreen() {
        LiveLockScreenInfo lls = null;
        try {
            lls = sService.getCurrentLiveLockScreen();
        } catch (RemoteException e) {
            logServiceException(e);
        }

        return lls;
    }

    /** @hide */
    public boolean getLiveLockScreenEnabled() {
        try {
            return sService.getLiveLockScreenEnabled();
        } catch (RemoteException e) {
            logServiceException(e);
        }

        return false;
    }

    /** @hide */
    public void setLiveLockScreenEnabled(boolean enabled) {
        try {
            sService.setLiveLockScreenEnabled(enabled);
        } catch (RemoteException e) {
            logServiceException(e);
        }
    }
}