355 lines
13 KiB
Java
355 lines
13 KiB
Java
package androidx.core.content.pm;
|
|
|
|
import android.content.ComponentName;
|
|
import android.content.Context;
|
|
import android.content.Intent;
|
|
import android.content.pm.PackageManager;
|
|
import android.content.pm.ShortcutInfo;
|
|
import android.graphics.drawable.Drawable;
|
|
import android.os.Build;
|
|
import android.os.PersistableBundle;
|
|
import android.text.TextUtils;
|
|
import androidx.annotation.NonNull;
|
|
import androidx.annotation.Nullable;
|
|
import androidx.annotation.RequiresApi;
|
|
import androidx.annotation.RestrictTo;
|
|
import androidx.annotation.VisibleForTesting;
|
|
import androidx.core.app.Person;
|
|
import androidx.core.graphics.drawable.IconCompat;
|
|
import c.d.b.a.a;
|
|
import java.util.Arrays;
|
|
import java.util.HashSet;
|
|
import java.util.Set;
|
|
public class ShortcutInfoCompat {
|
|
private static final String EXTRA_LONG_LIVED = "extraLongLived";
|
|
private static final String EXTRA_PERSON_ = "extraPerson_";
|
|
private static final String EXTRA_PERSON_COUNT = "extraPersonCount";
|
|
public ComponentName mActivity;
|
|
public Set<String> mCategories;
|
|
public Context mContext;
|
|
public CharSequence mDisabledMessage;
|
|
public IconCompat mIcon;
|
|
public String mId;
|
|
public Intent[] mIntents;
|
|
public boolean mIsAlwaysBadged;
|
|
public boolean mIsLongLived;
|
|
public CharSequence mLabel;
|
|
public CharSequence mLongLabel;
|
|
public Person[] mPersons;
|
|
public int mRank;
|
|
|
|
public static class Builder {
|
|
private final ShortcutInfoCompat mInfo;
|
|
|
|
@RequiresApi(25)
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
|
public Builder(@NonNull Context context, @NonNull ShortcutInfo shortcutInfo) {
|
|
ShortcutInfoCompat shortcutInfoCompat = new ShortcutInfoCompat();
|
|
this.mInfo = shortcutInfoCompat;
|
|
shortcutInfoCompat.mContext = context;
|
|
shortcutInfoCompat.mId = shortcutInfo.getId();
|
|
Intent[] intents = shortcutInfo.getIntents();
|
|
shortcutInfoCompat.mIntents = (Intent[]) Arrays.copyOf(intents, intents.length);
|
|
shortcutInfoCompat.mActivity = shortcutInfo.getActivity();
|
|
shortcutInfoCompat.mLabel = shortcutInfo.getShortLabel();
|
|
shortcutInfoCompat.mLongLabel = shortcutInfo.getLongLabel();
|
|
shortcutInfoCompat.mDisabledMessage = shortcutInfo.getDisabledMessage();
|
|
shortcutInfoCompat.mCategories = shortcutInfo.getCategories();
|
|
shortcutInfoCompat.mPersons = ShortcutInfoCompat.getPersonsFromExtra(shortcutInfo.getExtras());
|
|
shortcutInfoCompat.mRank = shortcutInfo.getRank();
|
|
}
|
|
|
|
public Builder(@NonNull Context context, @NonNull String str) {
|
|
ShortcutInfoCompat shortcutInfoCompat = new ShortcutInfoCompat();
|
|
this.mInfo = shortcutInfoCompat;
|
|
shortcutInfoCompat.mContext = context;
|
|
shortcutInfoCompat.mId = str;
|
|
}
|
|
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
|
public Builder(@NonNull ShortcutInfoCompat shortcutInfoCompat) {
|
|
ShortcutInfoCompat shortcutInfoCompat2 = new ShortcutInfoCompat();
|
|
this.mInfo = shortcutInfoCompat2;
|
|
shortcutInfoCompat2.mContext = shortcutInfoCompat.mContext;
|
|
shortcutInfoCompat2.mId = shortcutInfoCompat.mId;
|
|
Intent[] intentArr = shortcutInfoCompat.mIntents;
|
|
shortcutInfoCompat2.mIntents = (Intent[]) Arrays.copyOf(intentArr, intentArr.length);
|
|
shortcutInfoCompat2.mActivity = shortcutInfoCompat.mActivity;
|
|
shortcutInfoCompat2.mLabel = shortcutInfoCompat.mLabel;
|
|
shortcutInfoCompat2.mLongLabel = shortcutInfoCompat.mLongLabel;
|
|
shortcutInfoCompat2.mDisabledMessage = shortcutInfoCompat.mDisabledMessage;
|
|
shortcutInfoCompat2.mIcon = shortcutInfoCompat.mIcon;
|
|
shortcutInfoCompat2.mIsAlwaysBadged = shortcutInfoCompat.mIsAlwaysBadged;
|
|
shortcutInfoCompat2.mIsLongLived = shortcutInfoCompat.mIsLongLived;
|
|
shortcutInfoCompat2.mRank = shortcutInfoCompat.mRank;
|
|
Person[] personArr = shortcutInfoCompat.mPersons;
|
|
if (personArr != null) {
|
|
shortcutInfoCompat2.mPersons = (Person[]) Arrays.copyOf(personArr, personArr.length);
|
|
}
|
|
if (shortcutInfoCompat.mCategories != null) {
|
|
shortcutInfoCompat2.mCategories = new HashSet(shortcutInfoCompat.mCategories);
|
|
}
|
|
}
|
|
|
|
@NonNull
|
|
public ShortcutInfoCompat build() {
|
|
if (!TextUtils.isEmpty(this.mInfo.mLabel)) {
|
|
ShortcutInfoCompat shortcutInfoCompat = this.mInfo;
|
|
Intent[] intentArr = shortcutInfoCompat.mIntents;
|
|
if (intentArr != null && intentArr.length != 0) {
|
|
return shortcutInfoCompat;
|
|
}
|
|
throw new IllegalArgumentException("Shortcut must have an intent");
|
|
}
|
|
throw new IllegalArgumentException("Shortcut must have a non-empty label");
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setActivity(@NonNull ComponentName componentName) {
|
|
this.mInfo.mActivity = componentName;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setAlwaysBadged() {
|
|
this.mInfo.mIsAlwaysBadged = true;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setCategories(@NonNull Set<String> set) {
|
|
this.mInfo.mCategories = set;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setDisabledMessage(@NonNull CharSequence charSequence) {
|
|
this.mInfo.mDisabledMessage = charSequence;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setIcon(IconCompat iconCompat) {
|
|
this.mInfo.mIcon = iconCompat;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setIntent(@NonNull Intent intent) {
|
|
return setIntents(new Intent[]{intent});
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setIntents(@NonNull Intent[] intentArr) {
|
|
this.mInfo.mIntents = intentArr;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setLongLabel(@NonNull CharSequence charSequence) {
|
|
this.mInfo.mLongLabel = charSequence;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
@Deprecated
|
|
public Builder setLongLived() {
|
|
this.mInfo.mIsLongLived = true;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setLongLived(boolean z2) {
|
|
this.mInfo.mIsLongLived = z2;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setPerson(@NonNull Person person) {
|
|
return setPersons(new Person[]{person});
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setPersons(@NonNull Person[] personArr) {
|
|
this.mInfo.mPersons = personArr;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setRank(int i) {
|
|
this.mInfo.mRank = i;
|
|
return this;
|
|
}
|
|
|
|
@NonNull
|
|
public Builder setShortLabel(@NonNull CharSequence charSequence) {
|
|
this.mInfo.mLabel = charSequence;
|
|
return this;
|
|
}
|
|
}
|
|
|
|
@RequiresApi(22)
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
|
private PersistableBundle buildLegacyExtrasBundle() {
|
|
PersistableBundle persistableBundle = new PersistableBundle();
|
|
Person[] personArr = this.mPersons;
|
|
if (personArr != null && personArr.length > 0) {
|
|
persistableBundle.putInt("extraPersonCount", personArr.length);
|
|
int i = 0;
|
|
while (i < this.mPersons.length) {
|
|
StringBuilder K = a.K("extraPerson_");
|
|
int i2 = i + 1;
|
|
K.append(i2);
|
|
persistableBundle.putPersistableBundle(K.toString(), this.mPersons[i].toPersistableBundle());
|
|
i = i2;
|
|
}
|
|
}
|
|
persistableBundle.putBoolean("extraLongLived", this.mIsLongLived);
|
|
return persistableBundle;
|
|
}
|
|
|
|
@RequiresApi(25)
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
|
@VisibleForTesting
|
|
public static boolean getLongLivedFromExtra(@NonNull PersistableBundle persistableBundle) {
|
|
if (persistableBundle == null || !persistableBundle.containsKey("extraLongLived")) {
|
|
return false;
|
|
}
|
|
return persistableBundle.getBoolean("extraLongLived");
|
|
}
|
|
|
|
@VisibleForTesting
|
|
@Nullable
|
|
@RequiresApi(25)
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
|
public static Person[] getPersonsFromExtra(@NonNull PersistableBundle persistableBundle) {
|
|
if (persistableBundle == null || !persistableBundle.containsKey("extraPersonCount")) {
|
|
return null;
|
|
}
|
|
int i = persistableBundle.getInt("extraPersonCount");
|
|
Person[] personArr = new Person[i];
|
|
int i2 = 0;
|
|
while (i2 < i) {
|
|
StringBuilder K = a.K("extraPerson_");
|
|
int i3 = i2 + 1;
|
|
K.append(i3);
|
|
personArr[i2] = Person.fromPersistableBundle(persistableBundle.getPersistableBundle(K.toString()));
|
|
i2 = i3;
|
|
}
|
|
return personArr;
|
|
}
|
|
|
|
public Intent addToIntent(Intent intent) {
|
|
Intent[] intentArr = this.mIntents;
|
|
intent.putExtra("android.intent.extra.shortcut.INTENT", intentArr[intentArr.length - 1]).putExtra("android.intent.extra.shortcut.NAME", this.mLabel.toString());
|
|
if (this.mIcon != null) {
|
|
Drawable drawable = null;
|
|
if (this.mIsAlwaysBadged) {
|
|
PackageManager packageManager = this.mContext.getPackageManager();
|
|
ComponentName componentName = this.mActivity;
|
|
if (componentName != null) {
|
|
try {
|
|
drawable = packageManager.getActivityIcon(componentName);
|
|
} catch (PackageManager.NameNotFoundException unused) {
|
|
}
|
|
}
|
|
if (drawable == null) {
|
|
drawable = this.mContext.getApplicationInfo().loadIcon(packageManager);
|
|
}
|
|
}
|
|
this.mIcon.addToShortcutIntent(intent, drawable, this.mContext);
|
|
}
|
|
return intent;
|
|
}
|
|
|
|
@Nullable
|
|
public ComponentName getActivity() {
|
|
return this.mActivity;
|
|
}
|
|
|
|
@Nullable
|
|
public Set<String> getCategories() {
|
|
return this.mCategories;
|
|
}
|
|
|
|
@Nullable
|
|
public CharSequence getDisabledMessage() {
|
|
return this.mDisabledMessage;
|
|
}
|
|
|
|
@RestrictTo({RestrictTo.Scope.LIBRARY_GROUP_PREFIX})
|
|
public IconCompat getIcon() {
|
|
return this.mIcon;
|
|
}
|
|
|
|
@NonNull
|
|
public String getId() {
|
|
return this.mId;
|
|
}
|
|
|
|
@NonNull
|
|
public Intent getIntent() {
|
|
Intent[] intentArr = this.mIntents;
|
|
return intentArr[intentArr.length - 1];
|
|
}
|
|
|
|
@NonNull
|
|
public Intent[] getIntents() {
|
|
Intent[] intentArr = this.mIntents;
|
|
return (Intent[]) Arrays.copyOf(intentArr, intentArr.length);
|
|
}
|
|
|
|
@Nullable
|
|
public CharSequence getLongLabel() {
|
|
return this.mLongLabel;
|
|
}
|
|
|
|
public int getRank() {
|
|
return this.mRank;
|
|
}
|
|
|
|
@NonNull
|
|
public CharSequence getShortLabel() {
|
|
return this.mLabel;
|
|
}
|
|
|
|
@RequiresApi(25)
|
|
public ShortcutInfo toShortcutInfo() {
|
|
ShortcutInfo.Builder intents = new ShortcutInfo.Builder(this.mContext, this.mId).setShortLabel(this.mLabel).setIntents(this.mIntents);
|
|
IconCompat iconCompat = this.mIcon;
|
|
if (iconCompat != null) {
|
|
intents.setIcon(iconCompat.toIcon(this.mContext));
|
|
}
|
|
if (!TextUtils.isEmpty(this.mLongLabel)) {
|
|
intents.setLongLabel(this.mLongLabel);
|
|
}
|
|
if (!TextUtils.isEmpty(this.mDisabledMessage)) {
|
|
intents.setDisabledMessage(this.mDisabledMessage);
|
|
}
|
|
ComponentName componentName = this.mActivity;
|
|
if (componentName != null) {
|
|
intents.setActivity(componentName);
|
|
}
|
|
Set<String> set = this.mCategories;
|
|
if (set != null) {
|
|
intents.setCategories(set);
|
|
}
|
|
intents.setRank(this.mRank);
|
|
if (Build.VERSION.SDK_INT >= 29) {
|
|
Person[] personArr = this.mPersons;
|
|
if (personArr != null && personArr.length > 0) {
|
|
int length = personArr.length;
|
|
android.app.Person[] personArr2 = new android.app.Person[length];
|
|
for (int i = 0; i < length; i++) {
|
|
personArr2[i] = this.mPersons[i].toAndroidPerson();
|
|
}
|
|
intents.setPersons(personArr2);
|
|
}
|
|
intents.setLongLived(this.mIsLongLived);
|
|
} else {
|
|
intents.setExtras(buildLegacyExtrasBundle());
|
|
}
|
|
return intents.build();
|
|
}
|
|
}
|