Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package androidx.core.app;
- import android.app.Notification;
- import android.app.Notification.Builder;
- import android.app.PendingIntent;
- import android.os.Bundle;
- import android.os.Parcelable;
- import android.util.Log;
- import android.util.SparseArray;
- import androidx.annotation.RequiresApi;
- import androidx.core.app.NotificationCompat.Action;
- import java.lang.reflect.Field;
- import java.util.ArrayList;
- import java.util.Arrays;
- import java.util.HashSet;
- import java.util.Iterator;
- import java.util.List;
- import java.util.Set;
- @RequiresApi(16)
- class NotificationCompatJellybean {
- static final String EXTRA_ALLOW_GENERATED_REPLIES = "android.support.allowGeneratedReplies";
- static final String EXTRA_DATA_ONLY_REMOTE_INPUTS = "android.support.dataRemoteInputs";
- private static final String KEY_ACTION_INTENT = "actionIntent";
- private static final String KEY_ALLOWED_DATA_TYPES = "allowedDataTypes";
- private static final String KEY_ALLOW_FREE_FORM_INPUT = "allowFreeFormInput";
- private static final String KEY_CHOICES = "choices";
- private static final String KEY_DATA_ONLY_REMOTE_INPUTS = "dataOnlyRemoteInputs";
- private static final String KEY_EXTRAS = "extras";
- private static final String KEY_ICON = "icon";
- private static final String KEY_LABEL = "label";
- private static final String KEY_REMOTE_INPUTS = "remoteInputs";
- private static final String KEY_RESULT_KEY = "resultKey";
- private static final String KEY_SEMANTIC_ACTION = "semanticAction";
- private static final String KEY_SHOWS_USER_INTERFACE = "showsUserInterface";
- private static final String KEY_TITLE = "title";
- public static final String TAG = "NotificationCompat";
- private static Class<?> sActionClass;
- private static Field sActionIconField;
- private static Field sActionIntentField;
- private static Field sActionTitleField;
- private static boolean sActionsAccessFailed;
- private static Field sActionsField;
- private static final Object sActionsLock = new Object();
- private static Field sExtrasField;
- private static boolean sExtrasFieldAccessFailed;
- private static final Object sExtrasLock = new Object();
- public static SparseArray<Bundle> buildActionExtrasMap(List<Bundle> list) {
- int size = list.size();
- SparseArray<Bundle> sparseArray = null;
- for (int i = 0; i < size; i++) {
- Bundle bundle = (Bundle) list.get(i);
- if (bundle != null) {
- if (sparseArray == null) {
- sparseArray = new SparseArray<>();
- }
- sparseArray.put(i, bundle);
- }
- }
- return sparseArray;
- }
- public static Bundle getExtras(Notification notification) {
- synchronized (sExtrasLock) {
- if (sExtrasFieldAccessFailed) {
- return null;
- }
- try {
- if (sExtrasField == null) {
- Field declaredField = Notification.class.getDeclaredField(KEY_EXTRAS);
- if (!Bundle.class.isAssignableFrom(declaredField.getType())) {
- Log.e(TAG, "Notification.extras field is not of type Bundle");
- sExtrasFieldAccessFailed = true;
- return null;
- }
- declaredField.setAccessible(true);
- sExtrasField = declaredField;
- }
- Bundle bundle = (Bundle) sExtrasField.get(notification);
- if (bundle == null) {
- bundle = new Bundle();
- sExtrasField.set(notification, bundle);
- }
- return bundle;
- } catch (IllegalAccessException e) {
- Log.e(TAG, "Unable to access notification extras", e);
- sExtrasFieldAccessFailed = true;
- return null;
- } catch (NoSuchFieldException e2) {
- Log.e(TAG, "Unable to access notification extras", e2);
- sExtrasFieldAccessFailed = true;
- return null;
- }
- }
- }
- public static Action readAction(int i, CharSequence charSequence, PendingIntent pendingIntent, Bundle bundle) {
- boolean z;
- RemoteInput[] remoteInputArr;
- RemoteInput[] remoteInputArr2;
- if (bundle != null) {
- RemoteInput[] fromBundleArray = fromBundleArray(getBundleArrayFromBundle(bundle, NotificationCompatExtras.EXTRA_REMOTE_INPUTS));
- remoteInputArr2 = fromBundleArray;
- remoteInputArr = fromBundleArray(getBundleArrayFromBundle(bundle, EXTRA_DATA_ONLY_REMOTE_INPUTS));
- z = bundle.getBoolean(EXTRA_ALLOW_GENERATED_REPLIES);
- } else {
- remoteInputArr2 = null;
- remoteInputArr = null;
- z = false;
- }
- Action action = new Action(i, charSequence, pendingIntent, bundle, remoteInputArr2, remoteInputArr, z, 0, true);
- return action;
- }
- public static Bundle writeActionAndGetExtras(Builder builder, Action action) {
- builder.addAction(action.getIcon(), action.getTitle(), action.getActionIntent());
- Bundle bundle = new Bundle(action.getExtras());
- if (action.getRemoteInputs() != null) {
- bundle.putParcelableArray(NotificationCompatExtras.EXTRA_REMOTE_INPUTS, toBundleArray(action.getRemoteInputs()));
- }
- if (action.getDataOnlyRemoteInputs() != null) {
- bundle.putParcelableArray(EXTRA_DATA_ONLY_REMOTE_INPUTS, toBundleArray(action.getDataOnlyRemoteInputs()));
- }
- bundle.putBoolean(EXTRA_ALLOW_GENERATED_REPLIES, action.getAllowGeneratedReplies());
- return bundle;
- }
- public static int getActionCount(Notification notification) {
- int length;
- synchronized (sActionsLock) {
- Object[] actionObjectsLocked = getActionObjectsLocked(notification);
- length = actionObjectsLocked != null ? actionObjectsLocked.length : 0;
- }
- return length;
- }
- public static Action getAction(Notification notification, int i) {
- Bundle bundle;
- synchronized (sActionsLock) {
- try {
- Object[] actionObjectsLocked = getActionObjectsLocked(notification);
- if (actionObjectsLocked != null) {
- Object obj = actionObjectsLocked[i];
- Bundle extras = getExtras(notification);
- if (extras != null) {
- SparseArray sparseParcelableArray = extras.getSparseParcelableArray(NotificationCompatExtras.EXTRA_ACTION_EXTRAS);
- if (sparseParcelableArray != null) {
- bundle = (Bundle) sparseParcelableArray.get(i);
- Action readAction = readAction(sActionIconField.getInt(obj), (CharSequence) sActionTitleField.get(obj), (PendingIntent) sActionIntentField.get(obj), bundle);
- return readAction;
- }
- }
- bundle = null;
- Action readAction2 = readAction(sActionIconField.getInt(obj), (CharSequence) sActionTitleField.get(obj), (PendingIntent) sActionIntentField.get(obj), bundle);
- return readAction2;
- }
- } catch (IllegalAccessException e) {
- Log.e(TAG, "Unable to access notification actions", e);
- sActionsAccessFailed = true;
- } catch (Throwable th) {
- throw th;
- }
- }
- return null;
- }
- private static Object[] getActionObjectsLocked(Notification notification) {
- synchronized (sActionsLock) {
- if (!ensureActionReflectionReadyLocked()) {
- return null;
- }
- try {
- Object[] objArr = (Object[]) sActionsField.get(notification);
- return objArr;
- } catch (IllegalAccessException e) {
- Log.e(TAG, "Unable to access notification actions", e);
- sActionsAccessFailed = true;
- return null;
- }
- }
- }
- private static boolean ensureActionReflectionReadyLocked() {
- if (sActionsAccessFailed) {
- return false;
- }
- try {
- if (sActionsField == null) {
- sActionClass = Class.forName("android.app.Notification$Action");
- sActionIconField = sActionClass.getDeclaredField("icon");
- sActionTitleField = sActionClass.getDeclaredField("title");
- sActionIntentField = sActionClass.getDeclaredField(KEY_ACTION_INTENT);
- sActionsField = Notification.class.getDeclaredField("actions");
- sActionsField.setAccessible(true);
- }
- } catch (ClassNotFoundException e) {
- Log.e(TAG, "Unable to access notification actions", e);
- sActionsAccessFailed = true;
- } catch (NoSuchFieldException e2) {
- Log.e(TAG, "Unable to access notification actions", e2);
- sActionsAccessFailed = true;
- }
- return true ^ sActionsAccessFailed;
- }
- static Action getActionFromBundle(Bundle bundle) {
- Bundle bundle2 = bundle.getBundle(KEY_EXTRAS);
- Action action = new Action(bundle.getInt("icon"), bundle.getCharSequence("title"), (PendingIntent) bundle.getParcelable(KEY_ACTION_INTENT), bundle.getBundle(KEY_EXTRAS), fromBundleArray(getBundleArrayFromBundle(bundle, KEY_REMOTE_INPUTS)), fromBundleArray(getBundleArrayFromBundle(bundle, KEY_DATA_ONLY_REMOTE_INPUTS)), bundle2 != null ? bundle2.getBoolean(EXTRA_ALLOW_GENERATED_REPLIES, false) : false, bundle.getInt(KEY_SEMANTIC_ACTION), bundle.getBoolean(KEY_SHOWS_USER_INTERFACE));
- return action;
- }
- static Bundle getBundleForAction(Action action) {
- Bundle bundle;
- Bundle bundle2 = new Bundle();
- bundle2.putInt("icon", action.getIcon());
- bundle2.putCharSequence("title", action.getTitle());
- bundle2.putParcelable(KEY_ACTION_INTENT, action.getActionIntent());
- if (action.getExtras() != null) {
- bundle = new Bundle(action.getExtras());
- } else {
- bundle = new Bundle();
- }
- bundle.putBoolean(EXTRA_ALLOW_GENERATED_REPLIES, action.getAllowGeneratedReplies());
- bundle2.putBundle(KEY_EXTRAS, bundle);
- bundle2.putParcelableArray(KEY_REMOTE_INPUTS, toBundleArray(action.getRemoteInputs()));
- bundle2.putBoolean(KEY_SHOWS_USER_INTERFACE, action.getShowsUserInterface());
- bundle2.putInt(KEY_SEMANTIC_ACTION, action.getSemanticAction());
- return bundle2;
- }
- private static RemoteInput fromBundle(Bundle bundle) {
- ArrayList stringArrayList = bundle.getStringArrayList(KEY_ALLOWED_DATA_TYPES);
- HashSet hashSet = new HashSet();
- if (stringArrayList != null) {
- Iterator it = stringArrayList.iterator();
- while (it.hasNext()) {
- hashSet.add((String) it.next());
- }
- }
- RemoteInput remoteInput = new RemoteInput(bundle.getString(KEY_RESULT_KEY), bundle.getCharSequence(KEY_LABEL), bundle.getCharSequenceArray(KEY_CHOICES), bundle.getBoolean(KEY_ALLOW_FREE_FORM_INPUT), bundle.getBundle(KEY_EXTRAS), hashSet);
- return remoteInput;
- }
- private static Bundle toBundle(RemoteInput remoteInput) {
- Bundle bundle = new Bundle();
- bundle.putString(KEY_RESULT_KEY, remoteInput.getResultKey());
- bundle.putCharSequence(KEY_LABEL, remoteInput.getLabel());
- bundle.putCharSequenceArray(KEY_CHOICES, remoteInput.getChoices());
- bundle.putBoolean(KEY_ALLOW_FREE_FORM_INPUT, remoteInput.getAllowFreeFormInput());
- bundle.putBundle(KEY_EXTRAS, remoteInput.getExtras());
- Set<String> allowedDataTypes = remoteInput.getAllowedDataTypes();
- if (allowedDataTypes != null && !allowedDataTypes.isEmpty()) {
- ArrayList arrayList = new ArrayList(allowedDataTypes.size());
- for (String add : allowedDataTypes) {
- arrayList.add(add);
- }
- bundle.putStringArrayList(KEY_ALLOWED_DATA_TYPES, arrayList);
- }
- return bundle;
- }
- private static RemoteInput[] fromBundleArray(Bundle[] bundleArr) {
- if (bundleArr == null) {
- return null;
- }
- RemoteInput[] remoteInputArr = new RemoteInput[bundleArr.length];
- for (int i = 0; i < bundleArr.length; i++) {
- remoteInputArr[i] = fromBundle(bundleArr[i]);
- }
- return remoteInputArr;
- }
- private static Bundle[] toBundleArray(RemoteInput[] remoteInputArr) {
- if (remoteInputArr == null) {
- return null;
- }
- Bundle[] bundleArr = new Bundle[remoteInputArr.length];
- for (int i = 0; i < remoteInputArr.length; i++) {
- bundleArr[i] = toBundle(remoteInputArr[i]);
- }
- return bundleArr;
- }
- private static Bundle[] getBundleArrayFromBundle(Bundle bundle, String str) {
- Parcelable[] parcelableArray = bundle.getParcelableArray(str);
- if ((parcelableArray instanceof Bundle[]) || parcelableArray == null) {
- return (Bundle[]) parcelableArray;
- }
- Bundle[] bundleArr = (Bundle[]) Arrays.copyOf(parcelableArray, parcelableArray.length, Bundle[].class);
- bundle.putParcelableArray(str, bundleArr);
- return bundleArr;
- }
- private NotificationCompatJellybean() {
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement