CMSDK: add duration for priority/quiet mode

Add setZenModeWithDuration API call with long durationMills
Use NotificationManager.setZenMode with Condition URI
specifying end time.
Update settings/CMPartnerInterfaceTest
TODO: unit test to be added in:
tests/src/org/cyanogenmod/tests/settings/unit.

FOR-156

Change-Id: I18dd661fc07419a7019b6372e81df430a70f86e9
This commit is contained in:
Janet Sun 2015-12-21 14:35:31 -08:00 committed by Gerrit Code Review
parent 4497e249d9
commit 57dc3b3240
4 changed files with 87 additions and 13 deletions

View File

@ -23,6 +23,7 @@ import android.content.Intent;
import android.content.pm.PackageInfo; import android.content.pm.PackageInfo;
import android.content.pm.PackageManager; import android.content.pm.PackageManager;
import android.media.IAudioService; import android.media.IAudioService;
import android.net.Uri;
import android.os.IBinder; import android.os.IBinder;
import android.os.IPowerManager; import android.os.IPowerManager;
@ -46,6 +47,7 @@ import java.security.cert.X509Certificate;
import java.security.interfaces.RSAPublicKey; import java.security.interfaces.RSAPublicKey;
/** @hide */ /** @hide */
public class PartnerInterfaceService extends SystemService { public class PartnerInterfaceService extends SystemService {
private static final String TAG = "CMSettingsService"; private static final String TAG = "CMSettingsService";
@ -154,7 +156,21 @@ public class PartnerInterfaceService extends SystemService {
* not allowed by the caller's permissions. * not allowed by the caller's permissions.
*/ */
long token = clearCallingIdentity(); long token = clearCallingIdentity();
boolean success = setZenModeInternal(mode); boolean success = setZenModeInternal(mode, -1);
restoreCallingIdentity(token);
return success;
}
@Override
public boolean setZenModeWithDuration(int mode, long durationMillis) {
enforceModifySoundSettingsPermission();
/*
* We need to clear the caller's identity in order to
* allow this method call to modify settings
* not allowed by the caller's permissions.
*/
long token = clearCallingIdentity();
boolean success = setZenModeInternal(mode, durationMillis);
restoreCallingIdentity(token); restoreCallingIdentity(token);
return success; return success;
} }
@ -198,41 +214,54 @@ public class PartnerInterfaceService extends SystemService {
} }
} }
private boolean setZenModeInternal(int mode) { private boolean setZenModeInternal(int mode, long durationMillis) {
ContentResolver contentResolver = mContext.getContentResolver();
int zenModeValue = -1; int zenModeValue = -1;
Uri zenModeConditionUri = null;
switch(mode) { switch(mode) {
case PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS: case PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS:
zenModeValue = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS; zenModeValue = Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS;
zenModeConditionUri = createZenModeConditionUri(durationMillis);
break; break;
case PartnerInterface.ZEN_MODE_OFF: case PartnerInterface.ZEN_MODE_OFF:
zenModeValue = Settings.Global.ZEN_MODE_OFF; zenModeValue = Settings.Global.ZEN_MODE_OFF;
// Leaving the condition Uri to null signifies "indefinitely"
// durationMillis is ignored
break; break;
case PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS: case PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS:
zenModeValue = Settings.Global.ZEN_MODE_NO_INTERRUPTIONS; zenModeValue = Settings.Global.ZEN_MODE_NO_INTERRUPTIONS;
zenModeConditionUri = createZenModeConditionUri(durationMillis);
break; break;
default: default:
// Invalid mode parameter // Invalid mode parameter
Log.w(TAG, "setZenMode() called with invalid mode: " + mode); Log.w(TAG, "setZenMode() called with invalid mode: " + mode);
return false; return false;
} }
Settings.Global.putInt(contentResolver,
Settings.Global.ZEN_MODE,
zenModeValue);
/*
try { try {
// Setting the exit condition to null signifies "indefinitely" mNotificationManager.setZenMode(zenModeValue, zenModeConditionUri, "setZenMode (PartnerInterface)");
mNotificationManager.setZenModeCondition(null);
} catch (RemoteException e) { } catch (RemoteException e) {
// An error occurred, return false since the // An error occurred, return false since the
// condition failed to set. // condition failed to set.
Log.e(TAG, "setZenMode() failed for mode: " + mode); Log.e(TAG, "setZenMode() failed for mode: " + mode);
return false; return false;
} }
*/
return true; return true;
} }
private Uri createZenModeConditionUri(long durationMillis) {
// duration values that mean "indefinitely"
if (durationMillis == Long.MAX_VALUE || durationMillis < 0) {
return null;
}
final long endTimeMillis = System.currentTimeMillis() + durationMillis;
// long overflow also means "indefinitely"
if (endTimeMillis < 0) {
Log.w(TAG, "createZenModeCondition duration exceeds the max numerical limit. Defaulting to Indefinite");
return null;
}
return android.service.notification.ZenModeConfig.toCountdownConditionId(endTimeMillis);
}
public String getHotwordPackageNameInternal() { public String getHotwordPackageNameInternal() {
String packageName = null; String packageName = null;
/* /*

View File

@ -23,6 +23,7 @@ interface IPartnerInterface
void setAirplaneModeEnabled(boolean enabled); void setAirplaneModeEnabled(boolean enabled);
void setMobileDataEnabled(boolean enabled); void setMobileDataEnabled(boolean enabled);
boolean setZenMode(int mode); boolean setZenMode(int mode);
boolean setZenModeWithDuration(int mode, long durationMillis);
void shutdown(); void shutdown();
void reboot(); void reboot();
String getCurrentHotwordPackageName(); String getCurrentHotwordPackageName();

View File

@ -42,17 +42,18 @@ public class PartnerInterface {
* *
* This condition is held indefinitely until changed again. * This condition is held indefinitely until changed again.
* *
* @see #setZenMode * @see #setZenMode and #setZenModeWithDuration
*/ */
public static final int ZEN_MODE_IMPORTANT_INTERRUPTIONS = 1; public static final int ZEN_MODE_IMPORTANT_INTERRUPTIONS = 1;
/** /**
* Sets zen mode so that no interruptions will be allowed. The device is * Sets zen mode so that no interruptions will be allowed. The device is
* effectively silenced indefinitely, until the mode is changed again. * effectively silenced indefinitely, until the mode is changed again.
* *
* @see #setZenMode * @see #setZenMode and #setZenModeWithDuration
*/ */
public static final int ZEN_MODE_NO_INTERRUPTIONS = 2; public static final int ZEN_MODE_NO_INTERRUPTIONS = 2;
private static IPartnerInterface sService; private static IPartnerInterface sService;
private Context mContext; private Context mContext;
@ -172,6 +173,36 @@ public class PartnerInterface {
return false; 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. * Shuts down the device, immediately.
* *

View File

@ -7,6 +7,9 @@ import cyanogenmod.app.PartnerInterface;
* Tests functionality added in {@link cyanogenmod.app.PartnerInterface} * Tests functionality added in {@link cyanogenmod.app.PartnerInterface}
*/ */
public class CMPartnerInterfaceTest extends TestActivity { public class CMPartnerInterfaceTest extends TestActivity {
// Zen Mode to 15 minutes
private static final long ZEN_MODE_DURATION_15_MINUTES_MS = 15 * 60000;
PartnerInterface mPartnerInterface; PartnerInterface mPartnerInterface;
@Override @Override
protected String tag() { protected String tag() {
@ -55,15 +58,25 @@ public class CMPartnerInterfaceTest extends TestActivity {
mPartnerInterface.setZenMode(PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS); mPartnerInterface.setZenMode(PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS);
} }
}, },
new Test("Test set zen mode to important interruptions with 15 mins duration") {
public void run() {
mPartnerInterface.setZenModeWithDuration(PartnerInterface.ZEN_MODE_IMPORTANT_INTERRUPTIONS, ZEN_MODE_DURATION_15_MINUTES_MS);
}
},
new Test("Test set zen mode to no interruptions") { new Test("Test set zen mode to no interruptions") {
public void run() { public void run() {
mPartnerInterface.setZenMode(PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS); mPartnerInterface.setZenMode(PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS);
} }
}, },
new Test("Test set zen mode to no interruptions with 15 mins duration") {
public void run() {
mPartnerInterface.setZenModeWithDuration(PartnerInterface.ZEN_MODE_NO_INTERRUPTIONS, ZEN_MODE_DURATION_15_MINUTES_MS);
}
},
new Test("Test turn zen mode off") { new Test("Test turn zen mode off") {
public void run() { public void run() {
mPartnerInterface.setZenMode(PartnerInterface.ZEN_MODE_OFF); mPartnerInterface.setZenMode(PartnerInterface.ZEN_MODE_OFF);
} }
}, },
}; };
} }