aboutsummaryrefslogtreecommitdiffstats
path: root/src/java/cyanogenmod/app/PartnerInterface.java
blob: a7661ff8be2e8db962c0bdda0c3c1ffa07878bfe (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
/*
 * Copyright (C) 2015 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.content.Context;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.ServiceManager;
import android.util.Log;

/**
 * <p>
 * The PartnerInterface allows applications to interact with a subset of system settings.
 * </p>
 */
public class PartnerInterface {
    /**
     * Turn off zen mode. This restores the original ring and interruption
     * settings that the user had set before zen mode was enabled.
     *
     * @see #setZenMode
     */
    public static final int ZEN_MODE_OFF = 0;
    /**
     * Sets zen mode to important interruptions mode, so that
     * only notifications that the user has chosen in Settings
     * to be of high importance will cause the user's device to notify them.
     *
     * This condition is held indefinitely until changed again.
     *
     * @see #setZenMode and #setZenModeWithDuration
     */
    public static final int ZEN_MODE_IMPORTANT_INTERRUPTIONS = 1;
    /**
     * Sets zen mode so that no interruptions will be allowed. The device is
     * effectively silenced indefinitely, until the mode is changed again.
     *
     * @see #setZenMode and #setZenModeWithDuration
     */
    public static final int ZEN_MODE_NO_INTERRUPTIONS = 2;


    private static IPartnerInterface sService;

    private Context mContext;

    /**
     * Allows an application to change network settings,
     * such as enabling or disabling airplane mode.
     */
    public static final String MODIFY_NETWORK_SETTINGS_PERMISSION =
            "cyanogenmod.permission.MODIFY_NETWORK_SETTINGS";

    /**
     * Allows an application to change system sound settings, such as the zen mode.
     */
    public static final String MODIFY_SOUND_SETTINGS_PERMISSION =
            "cyanogenmod.permission.MODIFY_SOUND_SETTINGS";

    private static final String TAG = "PartnerInterface";

    private static PartnerInterface sPartnerInterfaceInstance;

    private PartnerInterface(Context context) {
        Context appContext = context.getApplicationContext();
        if (appContext != null) {
            mContext = appContext;
        } else {
            mContext = context;
        }
        sService = getService();
        if (context.getPackageManager().hasSystemFeature(
               CMContextConstants.Features.PARTNER) && sService == null) {
            throw new RuntimeException("Unable to get PartnerInterfaceService. The service" +
                    " either crashed, was not started, or the interface has been called to early" +
                    " in SystemServer init");
        }
    }

    /**
     * Get or create an instance of the {@link cyanogenmod.app.PartnerInterface}
     * @param context
     * @return {@link PartnerInterface}
     */
    public static PartnerInterface getInstance(Context context) {
        if (sPartnerInterfaceInstance == null) {
            sPartnerInterfaceInstance = new PartnerInterface(context);
        }
        return sPartnerInterfaceInstance;
    }

    /** @hide */
    static public IPartnerInterface getService() {
        if (sService != null) {
            return sService;
        }
        IBinder b = ServiceManager.getService(CMContextConstants.CM_PARTNER_INTERFACE);
        if (b != null) {
            sService = IPartnerInterface.Stub.asInterface(b);
            return sService;
        } else {
            return null;
        }
    }

    /**
     * Turns on or off airplane mode.
     *
     * You will need {@link #MODIFY_NETWORK_SETTINGS_PERMISSION}
     * to utilize this functionality.
     * @param enabled if true, sets airplane mode to enabled, otherwise disables airplane mode.
     */
    public void setAirplaneModeEnabled(boolean enabled) {
        if (sService == null) {
            return;
        }
        try {
            sService.setAirplaneModeEnabled(enabled);
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
    }

    /**
     * Turns on or off mobile network.
     *
     * You will need {@link #MODIFY_NETWORK_SETTINGS_PERMISSION}
     * to utilize this functionality.
     * @param enabled if true, sets mobile network to enabled, otherwise disables mobile network.
     */
    public void setMobileDataEnabled(boolean enabled) {
        if (sService == null) {
            return;
        }
        try {
            sService.setMobileDataEnabled(enabled);
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
    }

    /**
     * Set the zen mode for the device.
     *
     * You will need {@link #MODIFY_SOUND_SETTINGS_PERMISSION}
     * to utilize this functionality.
     *
     * @see #ZEN_MODE_IMPORTANT_INTERRUPTIONS
     * @see #ZEN_MODE_NO_INTERRUPTIONS
     * @see #ZEN_MODE_OFF
     * @param mode The zen mode to set the device to.
     *             One of {@link #ZEN_MODE_IMPORTANT_INTERRUPTIONS},
     *             {@link #ZEN_MODE_NO_INTERRUPTIONS} or
     *             {@link #ZEN_MODE_OFF}.
     */
    public boolean setZenMode(int mode) {
        if (sService == null) {
            return false;
        }
        try {
            return sService.setZenMode(mode);
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
        return false;
    }

    /**
     * Set the zen mode for the device, allow a duration parameter
     *
     * You will need {@link #MODIFY_SOUND_SETTINGS_PERMISSION}
     * to utilize this functionality.
     *
     * @see #ZEN_MODE_IMPORTANT_INTERRUPTIONS
     * @see #ZEN_MODE_NO_INTERRUPTIONS
     * @param mode The zen mode to set the device to.
     *             One of {@link #ZEN_MODE_IMPORTANT_INTERRUPTIONS},
     *             {@link #ZEN_MODE_NO_INTERRUPTIONS}.
     *             If using {@link #ZEN_MODE_OFF}, the duration will be ignored
     * @param durationMillis The duration in milliseconds,
     *                       if duration + System.currentTimeMillis() results in
     *                       long overflow, duration will be "indefinitely".
     *                       all durationMillis < 0 will mean "indefinitely".
     *
     */
    public boolean setZenModeWithDuration(int mode, long durationMillis) {
        if (sService == null) {
            return false;
        }
        try {
            return sService.setZenModeWithDuration(mode, durationMillis);
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
        return false;
    }

    /**
     * Shuts down the device, immediately.
     *
     * You will need {@link android.Manifest.permission.REBOOT}
     * to utilize this functionality.
     */
    public void shutdownDevice() {
        if (sService == null) {
            return;
        }
        try {
            sService.shutdown();
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
    }

    /**
     * Reboots the device, immediately.
     *
     * You will need {@link android.Manifest.permission.REBOOT}
     * to utilize this functionality.
     */
    public void rebootDevice() {
        if (sService == null) {
            return;
        }
        try {
            sService.reboot();
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
    }

    /**
     * Retrieves the package name of the application that currently holds the
     * {@link cyanogenmod.media.MediaRecorder.AudioSource#HOTWORD} input.
     * @return The package name or null if no application currently holds the HOTWORD input.
     */
    public String getCurrentHotwordPackageName() {
        if (sService == null) {
            return null;
        }
        try {
            return sService.getCurrentHotwordPackageName();
        } catch (RemoteException e) {
            Log.e(TAG, e.getLocalizedMessage(), e);
        }
        return null;
    }
}