Java Code Examples for com.android.internal.util.Preconditions#checkStringNotEmpty()

The following examples show how to use com.android.internal.util.Preconditions#checkStringNotEmpty() . You can vote up the ones you like or vote down the ones you don't like, and go to the original project or source file by following the links above each example. You may check out the related API usage on the sidebar.
Example 1
Source File: ShortcutService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private void verifyCaller(@NonNull String packageName, @UserIdInt int userId) {
    Preconditions.checkStringNotEmpty(packageName, "packageName");

    if (isCallerSystem()) {
        return; // no check
    }

    final int callingUid = injectBinderCallingUid();

    // Otherwise, make sure the arguments are valid.
    if (UserHandle.getUserId(callingUid) != userId) {
        throw new SecurityException("Invalid user-ID");
    }
    if (injectGetPackageUid(packageName, userId) != callingUid) {
        throw new SecurityException("Calling package name mismatch");
    }
    Preconditions.checkState(!isEphemeralApp(packageName, userId),
            "Ephemeral apps can't use ShortcutManager");
}
 
Example 2
Source File: ShortcutService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void enableShortcuts(String packageName, List shortcutIds, @UserIdInt int userId) {
    verifyCaller(packageName, userId);
    Preconditions.checkNotNull(shortcutIds, "shortcutIds must be provided");

    synchronized (mLock) {
        throwIfUserLockedL(userId);

        final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);

        ps.ensureImmutableShortcutsNotIncludedWithIds((List<String>) shortcutIds,
                /*ignoreInvisible=*/ true);

        for (int i = shortcutIds.size() - 1; i >= 0; i--) {
            final String id = Preconditions.checkStringNotEmpty((String) shortcutIds.get(i));
            if (!ps.isShortcutExistsAndVisibleToPublisher(id)) {
                continue;
            }
            ps.enableWithId(id);
        }
    }
    packageShortcutsChanged(packageName, userId);

    verifyStates();
}
 
Example 3
Source File: ShortcutService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public boolean isPinnedByCaller(int launcherUserId, @NonNull String callingPackage,
        @NonNull String packageName, @NonNull String shortcutId, int userId) {
    Preconditions.checkStringNotEmpty(packageName, "packageName");
    Preconditions.checkStringNotEmpty(shortcutId, "shortcutId");

    synchronized (mLock) {
        throwIfUserLockedL(userId);
        throwIfUserLockedL(launcherUserId);

        getLauncherShortcutsLocked(callingPackage, userId, launcherUserId)
                .attemptToRestoreIfNeededAndSave();

        final ShortcutInfo si = getShortcutInfoLocked(
                launcherUserId, callingPackage, packageName, shortcutId, userId,
                /*getPinnedByAnyLauncher=*/ false);
        return si != null && si.isPinned();
    }
}
 
Example 4
Source File: ShortcutService.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
@Override
public void pinShortcuts(int launcherUserId,
        @NonNull String callingPackage, @NonNull String packageName,
        @NonNull List<String> shortcutIds, int userId) {
    // Calling permission must be checked by LauncherAppsImpl.
    Preconditions.checkStringNotEmpty(packageName, "packageName");
    Preconditions.checkNotNull(shortcutIds, "shortcutIds");

    synchronized (mLock) {
        throwIfUserLockedL(userId);
        throwIfUserLockedL(launcherUserId);

        final ShortcutLauncher launcher =
                getLauncherShortcutsLocked(callingPackage, userId, launcherUserId);
        launcher.attemptToRestoreIfNeededAndSave();

        launcher.pinShortcuts(userId, packageName, shortcutIds, /*forPinRequest=*/ false);
    }
    packageShortcutsChanged(packageName, userId);

    verifyStates();
}
 
Example 5
Source File: ShortcutInfo.java    From android_9.0.0_r45 with Apache License 2.0 6 votes vote down vote up
private ShortcutInfo(Builder b) {
    mUserId = b.mContext.getUserId();

    mId = Preconditions.checkStringNotEmpty(b.mId, "Shortcut ID must be provided");

    // Note we can't do other null checks here because SM.updateShortcuts() takes partial
    // information.
    mPackageName = b.mContext.getPackageName();
    mActivity = b.mActivity;
    mIcon = b.mIcon;
    mTitle = b.mTitle;
    mTitleResId = b.mTitleResId;
    mText = b.mText;
    mTextResId = b.mTextResId;
    mDisabledMessage = b.mDisabledMessage;
    mDisabledMessageResId = b.mDisabledMessageResId;
    mCategories = cloneCategories(b.mCategories);
    mIntents = cloneIntents(b.mIntents);
    fixUpIntentExtras();
    mRank = b.mRank;
    mExtras = b.mExtras;
    updateTimestamp();
}
 
Example 6
Source File: ShortcutPackageItem.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
protected ShortcutPackageItem(@NonNull ShortcutUser shortcutUser,
        int packageUserId, @NonNull String packageName,
        @NonNull ShortcutPackageInfo packageInfo) {
    mShortcutUser = shortcutUser;
    mPackageUserId = packageUserId;
    mPackageName = Preconditions.checkStringNotEmpty(packageName);
    mPackageInfo = Preconditions.checkNotNull(packageInfo);
}
 
Example 7
Source File: ShortcutService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void disableShortcuts(String packageName, List shortcutIds,
        CharSequence disabledMessage, int disabledMessageResId, @UserIdInt int userId) {
    verifyCaller(packageName, userId);
    Preconditions.checkNotNull(shortcutIds, "shortcutIds must be provided");

    synchronized (mLock) {
        throwIfUserLockedL(userId);

        final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);

        ps.ensureImmutableShortcutsNotIncludedWithIds((List<String>) shortcutIds,
                /*ignoreInvisible=*/ true);

        final String disabledMessageString =
                (disabledMessage == null) ? null : disabledMessage.toString();

        for (int i = shortcutIds.size() - 1; i >= 0; i--) {
            final String id = Preconditions.checkStringNotEmpty((String) shortcutIds.get(i));
            if (!ps.isShortcutExistsAndVisibleToPublisher(id)) {
                continue;
            }
            ps.disableWithId(id,
                    disabledMessageString, disabledMessageResId,
                    /* overrideImmutable=*/ false, /*ignoreInvisible=*/ true,
                    ShortcutInfo.DISABLED_REASON_BY_APP);
        }

        // We may have removed dynamic shortcuts which may have left a gap, so adjust the ranks.
        ps.adjustRanks();
    }
    packageShortcutsChanged(packageName, userId);

    verifyStates();
}
 
Example 8
Source File: ShortcutService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void removeDynamicShortcuts(String packageName, List shortcutIds,
        @UserIdInt int userId) {
    verifyCaller(packageName, userId);
    Preconditions.checkNotNull(shortcutIds, "shortcutIds must be provided");

    synchronized (mLock) {
        throwIfUserLockedL(userId);

        final ShortcutPackage ps = getPackageShortcutsForPublisherLocked(packageName, userId);

        ps.ensureImmutableShortcutsNotIncludedWithIds((List<String>) shortcutIds,
                /*ignoreInvisible=*/ true);

        for (int i = shortcutIds.size() - 1; i >= 0; i--) {
            final String id = Preconditions.checkStringNotEmpty((String) shortcutIds.get(i));
            if (!ps.isShortcutExistsAndVisibleToPublisher(id)) {
                continue;
            }
            ps.deleteDynamicWithId(id, /*ignoreInvisible=*/ true);
        }

        // We may have removed dynamic shortcuts which may have left a gap, so adjust the ranks.
        ps.adjustRanks();
    }
    packageShortcutsChanged(packageName, userId);

    verifyStates();
}
 
Example 9
Source File: ShortcutService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public Intent[] createShortcutIntents(int launcherUserId,
        @NonNull String callingPackage,
        @NonNull String packageName, @NonNull String shortcutId, int userId,
        int callingPid, int callingUid) {
    // Calling permission must be checked by LauncherAppsImpl.
    Preconditions.checkStringNotEmpty(packageName, "packageName can't be empty");
    Preconditions.checkStringNotEmpty(shortcutId, "shortcutId can't be empty");

    synchronized (mLock) {
        throwIfUserLockedL(userId);
        throwIfUserLockedL(launcherUserId);

        getLauncherShortcutsLocked(callingPackage, userId, launcherUserId)
                .attemptToRestoreIfNeededAndSave();

        final boolean getPinnedByAnyLauncher =
                canSeeAnyPinnedShortcut(callingPackage, launcherUserId,
                        callingPid, callingUid);

        // Make sure the shortcut is actually visible to the launcher.
        final ShortcutInfo si = getShortcutInfoLocked(
                launcherUserId, callingPackage, packageName, shortcutId, userId,
                getPinnedByAnyLauncher);
        // "si == null" should suffice here, but check the flags too just to make sure.
        if (si == null || !si.isEnabled() || !(si.isAlive() || getPinnedByAnyLauncher)) {
            Log.e(TAG, "Shortcut " + shortcutId + " does not exist or disabled");
            return null;
        }
        return si.getIntents();
    }
}
 
Example 10
Source File: ArtManagerService.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
@Override
public void snapshotRuntimeProfile(@ProfileType int profileType, @Nullable String packageName,
        @Nullable String codePath, @NonNull ISnapshotRuntimeProfileCallback callback,
        String callingPackage) {
    int callingUid = Binder.getCallingUid();
    if (!checkShellPermissions(profileType, packageName, callingUid) &&
            !checkAndroidPermissions(callingUid, callingPackage)) {
        try {
            callback.onError(ArtManager.SNAPSHOT_FAILED_INTERNAL_ERROR);
        } catch (RemoteException ignored) {
        }
        return;
    }

    // Sanity checks on the arguments.
    Preconditions.checkNotNull(callback);

    boolean bootImageProfile = profileType == ArtManager.PROFILE_BOOT_IMAGE;
    if (!bootImageProfile) {
        Preconditions.checkStringNotEmpty(codePath);
        Preconditions.checkStringNotEmpty(packageName);
    }

    // Verify that runtime profiling is enabled.
    if (!isRuntimeProfilingEnabled(profileType, callingPackage)) {
        throw new IllegalStateException("Runtime profiling is not enabled for " + profileType);
    }

    if (DEBUG) {
        Slog.d(TAG, "Requested snapshot for " + packageName + ":" + codePath);
    }

    if (bootImageProfile) {
        snapshotBootImageProfile(callback);
    } else {
        snapshotAppProfile(packageName, codePath, callback);
    }
}
 
Example 11
Source File: PrintDocumentInfo.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Creates a new instance.
 *
 * @param parcel Data from which to initialize.
 */
private PrintDocumentInfo(Parcel parcel) {
    mName = Preconditions.checkStringNotEmpty(parcel.readString());
    mPageCount = parcel.readInt();
    Preconditions.checkArgument(mPageCount == PAGE_COUNT_UNKNOWN || mPageCount > 0);
    mContentType = parcel.readInt();
    mDataSize = Preconditions.checkArgumentNonnegative(parcel.readLong());
}
 
Example 12
Source File: ShortcutInfo.java    From android_9.0.0_r45 with Apache License 2.0 5 votes vote down vote up
/**
 * Used with the old style constructor, kept for unit tests.
 * @hide
 */
@NonNull
@Deprecated
public Builder setId(@NonNull String id) {
    mId = Preconditions.checkStringNotEmpty(id, "id cannot be empty");
    return this;
}
 
Example 13
Source File: StringNetworkSpecifier.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
public StringNetworkSpecifier(String specifier) {
    Preconditions.checkStringNotEmpty(specifier);
    this.specifier = specifier;
}
 
Example 14
Source File: PrintAttributes.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/**
 * Creates a new instance.
 *
 * @param id The unique media size id. It is unique amongst other media sizes
 *        supported by the printer.
 * @param label The <strong>localized</strong> human readable label.
 * @param packageName The name of the creating package.
 * @param widthMils The width in mils (thousandths of an inch).
 * @param heightMils The height in mils (thousandths of an inch).
 * @param labelResId The resource if of a human readable label.
 *
 * @throws IllegalArgumentException If the id is empty or the label is unset
 * or the widthMils is less than or equal to zero or the heightMils is less
 * than or equal to zero.
 *
 * @hide
 */
public MediaSize(String id, String label, String packageName, int widthMils, int heightMils,
        int labelResId) {
    mPackageName = packageName;
    mId = Preconditions.checkStringNotEmpty(id, "id cannot be empty.");
    mLabelResId = labelResId;
    mWidthMils = Preconditions.checkArgumentPositive(widthMils, "widthMils cannot be " +
            "less than or equal to zero.");
    mHeightMils = Preconditions.checkArgumentPositive(heightMils, "heightMils cannot be " +
            "less than or equal to zero.");
    mLabel = label;

    // The label has to be either a string ot a StringRes
    Preconditions.checkArgument(!TextUtils.isEmpty(label) !=
            (!TextUtils.isEmpty(packageName) && labelResId != 0), "label cannot be empty.");
}
 
Example 15
Source File: TextUtils.java    From android_9.0.0_r45 with Apache License 2.0 4 votes vote down vote up
/** {@hide} */
public static String firstNotEmpty(@Nullable String a, @NonNull String b) {
    return !isEmpty(a) ? a : Preconditions.checkStringNotEmpty(b);
}
 
Example 16
Source File: ShortcutInfo.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the short title of a shortcut.
 *
 * <p>This is a mandatory field when publishing a new shortcut with
 * {@link ShortcutManager#addDynamicShortcuts(List)} or
 * {@link ShortcutManager#setDynamicShortcuts(List)}.
 *
 * <p>This field is intended to be a concise description of a shortcut.
 *
 * <p>The recommended maximum length is 10 characters.
 *
 * @see ShortcutInfo#getShortLabel()
 */
@NonNull
public Builder setShortLabel(@NonNull CharSequence shortLabel) {
    Preconditions.checkState(mTitleResId == 0, "shortLabelResId already set");
    mTitle = Preconditions.checkStringNotEmpty(shortLabel, "shortLabel cannot be empty");
    return this;
}
 
Example 17
Source File: ShortcutInfo.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Sets the text of a shortcut.
 *
 * <p>This field is intended to be more descriptive than the shortcut title.  The launcher
 * shows this instead of the short title when it has enough space.
 *
 * <p>The recommend maximum length is 25 characters.
 *
 * @see ShortcutInfo#getLongLabel()
 */
@NonNull
public Builder setLongLabel(@NonNull CharSequence longLabel) {
    Preconditions.checkState(mTextResId == 0, "longLabelResId already set");
    mText = Preconditions.checkStringNotEmpty(longLabel, "longLabel cannot be empty");
    return this;
}
 
Example 18
Source File: RecommendationInfo.java    From android_9.0.0_r45 with Apache License 2.0 3 votes vote down vote up
/**
 * Create a new recommendation.
 *
 * @param packageName                  Package name of the print service
 * @param name                         Display name of the print service
 * @param discoveredPrinters           The {@link InetAddress addresses} of the discovered
 *                                     printers. Cannot be null or empty.
 * @param recommendsMultiVendorService If the service detects printer from multiple vendor
 */
public RecommendationInfo(@NonNull CharSequence packageName, @NonNull CharSequence name,
        @NonNull List<InetAddress> discoveredPrinters, boolean recommendsMultiVendorService) {
    mPackageName = Preconditions.checkStringNotEmpty(packageName);
    mName = Preconditions.checkStringNotEmpty(name);
    mDiscoveredPrinters = Preconditions.checkCollectionElementsNotNull(discoveredPrinters,
                "discoveredPrinters");
    mRecommendsMultiVendorService = recommendsMultiVendorService;
}
 
Example 19
Source File: ShortcutInfo.java    From android_9.0.0_r45 with Apache License 2.0 2 votes vote down vote up
/**
 * Constructor.
 *
 * @param context Client context.
 * @param id ID of the shortcut.
 */
public Builder(Context context, String id) {
    mContext = context;
    mId = Preconditions.checkStringNotEmpty(id, "id cannot be empty");
}
 
Example 20
Source File: PrinterInfo.java    From android_9.0.0_r45 with Apache License 2.0 2 votes vote down vote up
/**
 * Check if name is valid.
 *
 * @param name The name that might be valid
 * @return The valid name
 * @throws IllegalArgumentException if name is not valid.
 */
private static @NonNull String checkName(String name) {
    return Preconditions.checkStringNotEmpty(name, "name cannot be empty.");
}