Search in sources :

Example 36 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by ResurrectionRemix.

the class NotificationManagerService method buzzBeepBlinkLocked.

@VisibleForTesting
void buzzBeepBlinkLocked(NotificationRecord record) {
    boolean buzz = false;
    boolean beep = false;
    boolean blink = false;
    final Notification notification = record.sbn.getNotification();
    final String key = record.getKey();
    final String pkg = record.sbn.getPackageName();
    // Should this notification make noise, vibe, or use the LED?
    final boolean aboveThreshold = importanceToLevel(record.getImportance()) >= importanceToLevel(IMPORTANCE_DEFAULT);
    final boolean canInterrupt = aboveThreshold && !record.isIntercepted();
    if (DBG || record.isIntercepted())
        Slog.v(TAG, "pkg=" + pkg + " canInterrupt=" + canInterrupt + " intercept=" + record.isIntercepted());
    final int currentUser;
    final long token = Binder.clearCallingIdentity();
    try {
        currentUser = ActivityManager.getCurrentUser();
    } finally {
        Binder.restoreCallingIdentity(token);
    }
    // If we're not supposed to beep, vibrate, etc. then don't.
    final String disableEffects = disableNotificationEffects(record);
    if (disableEffects != null) {
        ZenLog.traceDisableEffects(record, disableEffects);
    }
    // Remember if this notification already owns the notification channels.
    boolean wasBeep = key != null && key.equals(mSoundNotificationKey);
    boolean wasBuzz = key != null && key.equals(mVibrateNotificationKey);
    // These are set inside the conditional if the notification is allowed to make noise.
    boolean hasValidVibrate = false;
    boolean hasValidSound = false;
    boolean readyForBeepOrBuzz = disableEffects == null && (record.getUserId() == UserHandle.USER_ALL || record.getUserId() == currentUser || mUserProfiles.isCurrentProfile(record.getUserId())) && !isInSoundTimeoutPeriod(record) && mSystemReady && !notificationIsAnnoying(pkg) && mAudioManager != null;
    boolean canBeep = readyForBeepOrBuzz && canInterrupt;
    boolean canBuzz = readyForBeepOrBuzz && (canInterrupt || (aboveThreshold && mZenModeHelper.allowVibrationForNotifications()));
    if (canBeep || canBuzz) {
        if (DBG)
            Slog.v(TAG, "Interrupting!");
        // should we use the default notification sound? (indicated either by
        // DEFAULT_SOUND or because notification.sound is pointing at
        // Settings.System.NOTIFICATION_SOUND)
        final boolean useDefaultSound = (notification.defaults & Notification.DEFAULT_SOUND) != 0 || Settings.System.DEFAULT_NOTIFICATION_URI.equals(notification.sound);
        Uri soundUri = null;
        if (useDefaultSound) {
            soundUri = Settings.System.DEFAULT_NOTIFICATION_URI;
            // check to see if the default notification sound is silent
            hasValidSound = mSystemNotificationSound != null;
        } else if (notification.sound != null) {
            soundUri = notification.sound;
            hasValidSound = (soundUri != null);
        }
        // Does the notification want to specify its own vibration?
        final boolean hasCustomVibrate = notification.vibrate != null;
        // new in 4.2: if there was supposed to be a sound and we're in vibrate
        // mode, and no other vibration is specified, we fall back to vibration
        final boolean convertSoundToVibration = !hasCustomVibrate && hasValidSound && (mAudioManager.getRingerModeInternal() == AudioManager.RINGER_MODE_VIBRATE);
        // The DEFAULT_VIBRATE flag trumps any custom vibration AND the fallback.
        final boolean useDefaultVibrate = (notification.defaults & Notification.DEFAULT_VIBRATE) != 0;
        hasValidVibrate = useDefaultVibrate || convertSoundToVibration || hasCustomVibrate;
        // it once, and we already have, then don't.
        if (!(record.isUpdate && (notification.flags & Notification.FLAG_ONLY_ALERT_ONCE) != 0)) {
            sendAccessibilityEvent(notification, record.sbn.getPackageName());
            if (canBeep && hasValidSound) {
                boolean looping = (notification.flags & Notification.FLAG_INSISTENT) != 0;
                AudioAttributes audioAttributes = audioAttributesForNotification(notification);
                mSoundNotificationKey = key;
                // ringer mode is silent) or if there is a user of exclusive audio focus
                if ((mAudioManager.getStreamVolume(AudioAttributes.toLegacyStreamType(audioAttributes)) != 0) && !mAudioManager.isAudioFocusExclusive()) {
                    final long identity = Binder.clearCallingIdentity();
                    try {
                        final IRingtonePlayer player = mAudioManager.getRingtonePlayer();
                        if (player != null) {
                            if (DBG)
                                Slog.v(TAG, "Playing sound " + soundUri + " with attributes " + audioAttributes);
                            player.playAsync(soundUri, record.sbn.getUser(), looping, audioAttributes);
                            beep = true;
                        }
                    } catch (RemoteException e) {
                    } finally {
                        Binder.restoreCallingIdentity(identity);
                    }
                }
            }
            if (canBuzz && hasValidVibrate && mAudioManager.getRingerModeInternal() != AudioManager.RINGER_MODE_SILENT) {
                mVibrateNotificationKey = key;
                if (useDefaultVibrate || convertSoundToVibration) {
                    // Escalate privileges so we can use the vibrator even if the
                    // notifying app does not have the VIBRATE permission.
                    long identity = Binder.clearCallingIdentity();
                    try {
                        mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), useDefaultVibrate ? mDefaultVibrationPattern : mFallbackVibrationPattern, ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0 : -1, audioAttributesForNotification(notification));
                        buzz = true;
                    } finally {
                        Binder.restoreCallingIdentity(identity);
                    }
                } else if (notification.vibrate.length > 1) {
                    // If you want your own vibration pattern, you need the VIBRATE
                    // permission
                    mVibrator.vibrate(record.sbn.getUid(), record.sbn.getOpPkg(), notification.vibrate, ((notification.flags & Notification.FLAG_INSISTENT) != 0) ? 0 : -1, audioAttributesForNotification(notification));
                    buzz = true;
                }
            }
        }
    }
    // cancel that feedback now
    if (wasBeep && !hasValidSound) {
        clearSoundLocked();
    }
    if (wasBuzz && !hasValidVibrate) {
        clearVibrateLocked();
    }
    // light
    // release the light
    boolean wasShowLights = mLights.remove(key);
    final boolean canInterruptWithLight = canInterrupt || isLedNotificationForcedOn(record) || (!canInterrupt && mZenModeHelper.getAllowLights());
    if ((notification.flags & Notification.FLAG_SHOW_LIGHTS) != 0 && canInterruptWithLight && ((record.getSuppressedVisualEffects() & NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF) == 0)) {
        mLights.add(key);
        updateLightsLocked();
        if (mUseAttentionLight) {
            mAttentionLight.pulse();
        }
        blink = true;
    } else if (wasShowLights) {
        updateLightsLocked();
    }
    if (buzz || beep) {
        mLastSoundTimestamps.put(generateLastSoundTimeoutKey(record), SystemClock.elapsedRealtime());
    }
    if (buzz || beep || blink) {
        if (((record.getSuppressedVisualEffects() & NotificationListenerService.SUPPRESSED_EFFECT_SCREEN_OFF) != 0)) {
            if (DBG)
                Slog.v(TAG, "Suppressed SystemUI from triggering screen on");
        } else {
            EventLogTags.writeNotificationAlert(key, buzz ? 1 : 0, beep ? 1 : 0, blink ? 1 : 0);
            mHandler.post(mBuzzBeepBlinked);
        }
    }
}
Also used : AudioAttributes(android.media.AudioAttributes) IRingtonePlayer(android.media.IRingtonePlayer) RemoteException(android.os.RemoteException) Uri(android.net.Uri) ITransientNotification(android.app.ITransientNotification) Notification(android.app.Notification) StatusBarNotification(android.service.notification.StatusBarNotification) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 37 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by ResurrectionRemix.

the class ShortcutService method saveBaseStateLocked.

@VisibleForTesting
void saveBaseStateLocked() {
    final AtomicFile file = getBaseStateFile();
    if (DEBUG) {
        Slog.d(TAG, "Saving to " + file.getBaseFile());
    }
    FileOutputStream outs = null;
    try {
        outs = file.startWrite();
        // Write to XML
        XmlSerializer out = new FastXmlSerializer();
        out.setOutput(outs, StandardCharsets.UTF_8.name());
        out.startDocument(null, true);
        out.startTag(null, TAG_ROOT);
        // Body.
        writeTagValue(out, TAG_LAST_RESET_TIME, mRawLastResetTime);
        // Epilogue.
        out.endTag(null, TAG_ROOT);
        out.endDocument();
        // Close.
        file.finishWrite(outs);
    } catch (IOException e) {
        Slog.e(TAG, "Failed to write to file " + file.getBaseFile(), e);
        file.failWrite(outs);
    }
}
Also used : FastXmlSerializer(com.android.internal.util.FastXmlSerializer) AtomicFile(android.util.AtomicFile) FileOutputStream(java.io.FileOutputStream) IOException(java.io.IOException) XmlSerializer(org.xmlpull.v1.XmlSerializer) FastXmlSerializer(com.android.internal.util.FastXmlSerializer) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 38 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by ResurrectionRemix.

the class ShortcutService method updateConfigurationLocked.

/**
     * Load the configuration from Settings.
     */
@VisibleForTesting
boolean updateConfigurationLocked(String config) {
    boolean result = true;
    final KeyValueListParser parser = new KeyValueListParser(',');
    try {
        parser.setString(config);
    } catch (IllegalArgumentException e) {
        // Failed to parse the settings string, log this and move on
        // with defaults.
        Slog.e(TAG, "Bad shortcut manager settings", e);
        result = false;
    }
    mSaveDelayMillis = Math.max(0, (int) parser.getLong(ConfigConstants.KEY_SAVE_DELAY_MILLIS, DEFAULT_SAVE_DELAY_MS));
    mResetInterval = Math.max(1, parser.getLong(ConfigConstants.KEY_RESET_INTERVAL_SEC, DEFAULT_RESET_INTERVAL_SEC) * 1000L);
    mMaxUpdatesPerInterval = Math.max(0, (int) parser.getLong(ConfigConstants.KEY_MAX_UPDATES_PER_INTERVAL, DEFAULT_MAX_UPDATES_PER_INTERVAL));
    mMaxShortcuts = Math.max(0, (int) parser.getLong(ConfigConstants.KEY_MAX_SHORTCUTS, DEFAULT_MAX_SHORTCUTS_PER_APP));
    final int iconDimensionDp = Math.max(1, injectIsLowRamDevice() ? (int) parser.getLong(ConfigConstants.KEY_MAX_ICON_DIMENSION_DP_LOWRAM, DEFAULT_MAX_ICON_DIMENSION_LOWRAM_DP) : (int) parser.getLong(ConfigConstants.KEY_MAX_ICON_DIMENSION_DP, DEFAULT_MAX_ICON_DIMENSION_DP));
    mMaxIconDimension = injectDipToPixel(iconDimensionDp);
    mIconPersistFormat = CompressFormat.valueOf(parser.getString(ConfigConstants.KEY_ICON_FORMAT, DEFAULT_ICON_PERSIST_FORMAT));
    mIconPersistQuality = (int) parser.getLong(ConfigConstants.KEY_ICON_QUALITY, DEFAULT_ICON_PERSIST_QUALITY);
    return result;
}
Also used : KeyValueListParser(android.util.KeyValueListParser) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 39 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by ResurrectionRemix.

the class ShortcutService method openIconFileForWrite.

/**
     * Build the cached bitmap filename for a shortcut icon.
     *
     * The filename will be based on the ID, except certain characters will be escaped.
     */
@VisibleForTesting
FileOutputStreamWithPath openIconFileForWrite(@UserIdInt int userId, ShortcutInfo shortcut) throws IOException {
    final File packagePath = new File(getUserBitmapFilePath(userId), shortcut.getPackage());
    if (!packagePath.isDirectory()) {
        packagePath.mkdirs();
        if (!packagePath.isDirectory()) {
            throw new IOException("Unable to create directory " + packagePath);
        }
        SELinux.restorecon(packagePath);
    }
    final String baseName = String.valueOf(injectCurrentTimeMillis());
    for (int suffix = 0; ; suffix++) {
        final String filename = (suffix == 0 ? baseName : baseName + "_" + suffix) + ".png";
        final File file = new File(packagePath, filename);
        if (!file.exists()) {
            if (DEBUG) {
                Slog.d(TAG, "Saving icon to " + file.getAbsolutePath());
            }
            return new FileOutputStreamWithPath(file);
        }
    }
}
Also used : IOException(java.io.IOException) File(java.io.File) AtomicFile(android.util.AtomicFile) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Example 40 with VisibleForTesting

use of com.android.internal.annotations.VisibleForTesting in project android_frameworks_base by ResurrectionRemix.

the class DevicePolicyManagerService method getDeviceOwnerAdminLocked.

// Returns the active device owner or null if there is no device owner.
@VisibleForTesting
ActiveAdmin getDeviceOwnerAdminLocked() {
    ComponentName component = mOwners.getDeviceOwnerComponent();
    if (component == null) {
        return null;
    }
    DevicePolicyData policy = getUserData(mOwners.getDeviceOwnerUserId());
    final int n = policy.mAdminList.size();
    for (int i = 0; i < n; i++) {
        ActiveAdmin admin = policy.mAdminList.get(i);
        if (component.equals(admin.info.getComponent())) {
            return admin;
        }
    }
    Slog.wtf(LOG_TAG, "Active admin for device owner not found. component=" + component);
    return null;
}
Also used : ComponentName(android.content.ComponentName) VisibleForTesting(com.android.internal.annotations.VisibleForTesting)

Aggregations

VisibleForTesting (com.android.internal.annotations.VisibleForTesting)141 ArrayList (java.util.ArrayList)39 IOException (java.io.IOException)26 XmlPullParserException (org.xmlpull.v1.XmlPullParserException)18 ComponentName (android.content.ComponentName)15 File (java.io.File)14 RemoteException (android.os.RemoteException)13 ArraySet (android.util.ArraySet)10 AtomicFile (android.util.AtomicFile)10 FileInputStream (java.io.FileInputStream)10 Locale (java.util.Locale)10 NameNotFoundException (android.content.pm.PackageManager.NameNotFoundException)9 FileNotFoundException (java.io.FileNotFoundException)9 Notification (android.app.Notification)6 ErrnoException (android.system.ErrnoException)6 FastXmlSerializer (com.android.internal.util.FastXmlSerializer)6 FileOutputStream (java.io.FileOutputStream)6 XmlSerializer (org.xmlpull.v1.XmlSerializer)6 ITransientNotification (android.app.ITransientNotification)5 UserInfo (android.content.pm.UserInfo)5