Advertisement
XaskeL

Untitled

Oct 17th, 2019
276
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 13.53 KB | None | 0 0
  1. package androidx.core.app;
  2.  
  3. import android.app.Notification;
  4. import android.app.Notification.Builder;
  5. import android.app.PendingIntent;
  6. import android.os.Bundle;
  7. import android.os.Parcelable;
  8. import android.util.Log;
  9. import android.util.SparseArray;
  10. import androidx.annotation.RequiresApi;
  11. import androidx.core.app.NotificationCompat.Action;
  12. import java.lang.reflect.Field;
  13. import java.util.ArrayList;
  14. import java.util.Arrays;
  15. import java.util.HashSet;
  16. import java.util.Iterator;
  17. import java.util.List;
  18. import java.util.Set;
  19.  
  20. @RequiresApi(16)
  21. class NotificationCompatJellybean {
  22. static final String EXTRA_ALLOW_GENERATED_REPLIES = "android.support.allowGeneratedReplies";
  23. static final String EXTRA_DATA_ONLY_REMOTE_INPUTS = "android.support.dataRemoteInputs";
  24. private static final String KEY_ACTION_INTENT = "actionIntent";
  25. private static final String KEY_ALLOWED_DATA_TYPES = "allowedDataTypes";
  26. private static final String KEY_ALLOW_FREE_FORM_INPUT = "allowFreeFormInput";
  27. private static final String KEY_CHOICES = "choices";
  28. private static final String KEY_DATA_ONLY_REMOTE_INPUTS = "dataOnlyRemoteInputs";
  29. private static final String KEY_EXTRAS = "extras";
  30. private static final String KEY_ICON = "icon";
  31. private static final String KEY_LABEL = "label";
  32. private static final String KEY_REMOTE_INPUTS = "remoteInputs";
  33. private static final String KEY_RESULT_KEY = "resultKey";
  34. private static final String KEY_SEMANTIC_ACTION = "semanticAction";
  35. private static final String KEY_SHOWS_USER_INTERFACE = "showsUserInterface";
  36. private static final String KEY_TITLE = "title";
  37. public static final String TAG = "NotificationCompat";
  38. private static Class<?> sActionClass;
  39. private static Field sActionIconField;
  40. private static Field sActionIntentField;
  41. private static Field sActionTitleField;
  42. private static boolean sActionsAccessFailed;
  43. private static Field sActionsField;
  44. private static final Object sActionsLock = new Object();
  45. private static Field sExtrasField;
  46. private static boolean sExtrasFieldAccessFailed;
  47. private static final Object sExtrasLock = new Object();
  48.  
  49. public static SparseArray<Bundle> buildActionExtrasMap(List<Bundle> list) {
  50. int size = list.size();
  51. SparseArray<Bundle> sparseArray = null;
  52. for (int i = 0; i < size; i++) {
  53. Bundle bundle = (Bundle) list.get(i);
  54. if (bundle != null) {
  55. if (sparseArray == null) {
  56. sparseArray = new SparseArray<>();
  57. }
  58. sparseArray.put(i, bundle);
  59. }
  60. }
  61. return sparseArray;
  62. }
  63.  
  64. public static Bundle getExtras(Notification notification) {
  65. synchronized (sExtrasLock) {
  66. if (sExtrasFieldAccessFailed) {
  67. return null;
  68. }
  69. try {
  70. if (sExtrasField == null) {
  71. Field declaredField = Notification.class.getDeclaredField(KEY_EXTRAS);
  72. if (!Bundle.class.isAssignableFrom(declaredField.getType())) {
  73. Log.e(TAG, "Notification.extras field is not of type Bundle");
  74. sExtrasFieldAccessFailed = true;
  75. return null;
  76. }
  77. declaredField.setAccessible(true);
  78. sExtrasField = declaredField;
  79. }
  80. Bundle bundle = (Bundle) sExtrasField.get(notification);
  81. if (bundle == null) {
  82. bundle = new Bundle();
  83. sExtrasField.set(notification, bundle);
  84. }
  85. return bundle;
  86. } catch (IllegalAccessException e) {
  87. Log.e(TAG, "Unable to access notification extras", e);
  88. sExtrasFieldAccessFailed = true;
  89. return null;
  90. } catch (NoSuchFieldException e2) {
  91. Log.e(TAG, "Unable to access notification extras", e2);
  92. sExtrasFieldAccessFailed = true;
  93. return null;
  94. }
  95. }
  96. }
  97.  
  98. public static Action readAction(int i, CharSequence charSequence, PendingIntent pendingIntent, Bundle bundle) {
  99. boolean z;
  100. RemoteInput[] remoteInputArr;
  101. RemoteInput[] remoteInputArr2;
  102. if (bundle != null) {
  103. RemoteInput[] fromBundleArray = fromBundleArray(getBundleArrayFromBundle(bundle, NotificationCompatExtras.EXTRA_REMOTE_INPUTS));
  104. remoteInputArr2 = fromBundleArray;
  105. remoteInputArr = fromBundleArray(getBundleArrayFromBundle(bundle, EXTRA_DATA_ONLY_REMOTE_INPUTS));
  106. z = bundle.getBoolean(EXTRA_ALLOW_GENERATED_REPLIES);
  107. } else {
  108. remoteInputArr2 = null;
  109. remoteInputArr = null;
  110. z = false;
  111. }
  112. Action action = new Action(i, charSequence, pendingIntent, bundle, remoteInputArr2, remoteInputArr, z, 0, true);
  113. return action;
  114. }
  115.  
  116. public static Bundle writeActionAndGetExtras(Builder builder, Action action) {
  117. builder.addAction(action.getIcon(), action.getTitle(), action.getActionIntent());
  118. Bundle bundle = new Bundle(action.getExtras());
  119. if (action.getRemoteInputs() != null) {
  120. bundle.putParcelableArray(NotificationCompatExtras.EXTRA_REMOTE_INPUTS, toBundleArray(action.getRemoteInputs()));
  121. }
  122. if (action.getDataOnlyRemoteInputs() != null) {
  123. bundle.putParcelableArray(EXTRA_DATA_ONLY_REMOTE_INPUTS, toBundleArray(action.getDataOnlyRemoteInputs()));
  124. }
  125. bundle.putBoolean(EXTRA_ALLOW_GENERATED_REPLIES, action.getAllowGeneratedReplies());
  126. return bundle;
  127. }
  128.  
  129. public static int getActionCount(Notification notification) {
  130. int length;
  131. synchronized (sActionsLock) {
  132. Object[] actionObjectsLocked = getActionObjectsLocked(notification);
  133. length = actionObjectsLocked != null ? actionObjectsLocked.length : 0;
  134. }
  135. return length;
  136. }
  137.  
  138. public static Action getAction(Notification notification, int i) {
  139. Bundle bundle;
  140. synchronized (sActionsLock) {
  141. try {
  142. Object[] actionObjectsLocked = getActionObjectsLocked(notification);
  143. if (actionObjectsLocked != null) {
  144. Object obj = actionObjectsLocked[i];
  145. Bundle extras = getExtras(notification);
  146. if (extras != null) {
  147. SparseArray sparseParcelableArray = extras.getSparseParcelableArray(NotificationCompatExtras.EXTRA_ACTION_EXTRAS);
  148. if (sparseParcelableArray != null) {
  149. bundle = (Bundle) sparseParcelableArray.get(i);
  150. Action readAction = readAction(sActionIconField.getInt(obj), (CharSequence) sActionTitleField.get(obj), (PendingIntent) sActionIntentField.get(obj), bundle);
  151. return readAction;
  152. }
  153. }
  154. bundle = null;
  155. Action readAction2 = readAction(sActionIconField.getInt(obj), (CharSequence) sActionTitleField.get(obj), (PendingIntent) sActionIntentField.get(obj), bundle);
  156. return readAction2;
  157. }
  158. } catch (IllegalAccessException e) {
  159. Log.e(TAG, "Unable to access notification actions", e);
  160. sActionsAccessFailed = true;
  161. } catch (Throwable th) {
  162. throw th;
  163. }
  164. }
  165. return null;
  166. }
  167.  
  168. private static Object[] getActionObjectsLocked(Notification notification) {
  169. synchronized (sActionsLock) {
  170. if (!ensureActionReflectionReadyLocked()) {
  171. return null;
  172. }
  173. try {
  174. Object[] objArr = (Object[]) sActionsField.get(notification);
  175. return objArr;
  176. } catch (IllegalAccessException e) {
  177. Log.e(TAG, "Unable to access notification actions", e);
  178. sActionsAccessFailed = true;
  179. return null;
  180. }
  181. }
  182. }
  183.  
  184. private static boolean ensureActionReflectionReadyLocked() {
  185. if (sActionsAccessFailed) {
  186. return false;
  187. }
  188. try {
  189. if (sActionsField == null) {
  190. sActionClass = Class.forName("android.app.Notification$Action");
  191. sActionIconField = sActionClass.getDeclaredField("icon");
  192. sActionTitleField = sActionClass.getDeclaredField("title");
  193. sActionIntentField = sActionClass.getDeclaredField(KEY_ACTION_INTENT);
  194. sActionsField = Notification.class.getDeclaredField("actions");
  195. sActionsField.setAccessible(true);
  196. }
  197. } catch (ClassNotFoundException e) {
  198. Log.e(TAG, "Unable to access notification actions", e);
  199. sActionsAccessFailed = true;
  200. } catch (NoSuchFieldException e2) {
  201. Log.e(TAG, "Unable to access notification actions", e2);
  202. sActionsAccessFailed = true;
  203. }
  204. return true ^ sActionsAccessFailed;
  205. }
  206.  
  207. static Action getActionFromBundle(Bundle bundle) {
  208. Bundle bundle2 = bundle.getBundle(KEY_EXTRAS);
  209. 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));
  210. return action;
  211. }
  212.  
  213. static Bundle getBundleForAction(Action action) {
  214. Bundle bundle;
  215. Bundle bundle2 = new Bundle();
  216. bundle2.putInt("icon", action.getIcon());
  217. bundle2.putCharSequence("title", action.getTitle());
  218. bundle2.putParcelable(KEY_ACTION_INTENT, action.getActionIntent());
  219. if (action.getExtras() != null) {
  220. bundle = new Bundle(action.getExtras());
  221. } else {
  222. bundle = new Bundle();
  223. }
  224. bundle.putBoolean(EXTRA_ALLOW_GENERATED_REPLIES, action.getAllowGeneratedReplies());
  225. bundle2.putBundle(KEY_EXTRAS, bundle);
  226. bundle2.putParcelableArray(KEY_REMOTE_INPUTS, toBundleArray(action.getRemoteInputs()));
  227. bundle2.putBoolean(KEY_SHOWS_USER_INTERFACE, action.getShowsUserInterface());
  228. bundle2.putInt(KEY_SEMANTIC_ACTION, action.getSemanticAction());
  229. return bundle2;
  230. }
  231.  
  232. private static RemoteInput fromBundle(Bundle bundle) {
  233. ArrayList stringArrayList = bundle.getStringArrayList(KEY_ALLOWED_DATA_TYPES);
  234. HashSet hashSet = new HashSet();
  235. if (stringArrayList != null) {
  236. Iterator it = stringArrayList.iterator();
  237. while (it.hasNext()) {
  238. hashSet.add((String) it.next());
  239. }
  240. }
  241. 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);
  242. return remoteInput;
  243. }
  244.  
  245. private static Bundle toBundle(RemoteInput remoteInput) {
  246. Bundle bundle = new Bundle();
  247. bundle.putString(KEY_RESULT_KEY, remoteInput.getResultKey());
  248. bundle.putCharSequence(KEY_LABEL, remoteInput.getLabel());
  249. bundle.putCharSequenceArray(KEY_CHOICES, remoteInput.getChoices());
  250. bundle.putBoolean(KEY_ALLOW_FREE_FORM_INPUT, remoteInput.getAllowFreeFormInput());
  251. bundle.putBundle(KEY_EXTRAS, remoteInput.getExtras());
  252. Set<String> allowedDataTypes = remoteInput.getAllowedDataTypes();
  253. if (allowedDataTypes != null && !allowedDataTypes.isEmpty()) {
  254. ArrayList arrayList = new ArrayList(allowedDataTypes.size());
  255. for (String add : allowedDataTypes) {
  256. arrayList.add(add);
  257. }
  258. bundle.putStringArrayList(KEY_ALLOWED_DATA_TYPES, arrayList);
  259. }
  260. return bundle;
  261. }
  262.  
  263. private static RemoteInput[] fromBundleArray(Bundle[] bundleArr) {
  264. if (bundleArr == null) {
  265. return null;
  266. }
  267. RemoteInput[] remoteInputArr = new RemoteInput[bundleArr.length];
  268. for (int i = 0; i < bundleArr.length; i++) {
  269. remoteInputArr[i] = fromBundle(bundleArr[i]);
  270. }
  271. return remoteInputArr;
  272. }
  273.  
  274. private static Bundle[] toBundleArray(RemoteInput[] remoteInputArr) {
  275. if (remoteInputArr == null) {
  276. return null;
  277. }
  278. Bundle[] bundleArr = new Bundle[remoteInputArr.length];
  279. for (int i = 0; i < remoteInputArr.length; i++) {
  280. bundleArr[i] = toBundle(remoteInputArr[i]);
  281. }
  282. return bundleArr;
  283. }
  284.  
  285. private static Bundle[] getBundleArrayFromBundle(Bundle bundle, String str) {
  286. Parcelable[] parcelableArray = bundle.getParcelableArray(str);
  287. if ((parcelableArray instanceof Bundle[]) || parcelableArray == null) {
  288. return (Bundle[]) parcelableArray;
  289. }
  290. Bundle[] bundleArr = (Bundle[]) Arrays.copyOf(parcelableArray, parcelableArray.length, Bundle[].class);
  291. bundle.putParcelableArray(str, bundleArr);
  292. return bundleArr;
  293. }
  294.  
  295. private NotificationCompatJellybean() {
  296. }
  297. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement