OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.components.gcm_driver; | |
6 | |
7 import android.annotation.TargetApi; | |
8 import android.os.Build; | |
9 import android.os.Bundle; | |
10 import android.os.PersistableBundle; | |
11 | |
12 import java.util.ArrayList; | |
13 import java.util.List; | |
14 | |
15 import javax.annotation.Nullable; | |
16 | |
17 /** | |
18 * Represents the contents of a GCM Message that is to be handled by the GCM Dri ver. Can be created | |
19 * based on data received from GCM, or serialized and deserialized to and from a PersistableBundle. | |
20 */ | |
21 public class GCMMessage { | |
22 /** | |
23 * Keys used to store information in the persistable bundle for serializatio n purposes. | |
24 */ | |
25 private static final String BUNDLE_KEY_APP_ID = "appId"; | |
26 private static final String BUNDLE_KEY_COLLAPSE_KEY = "collapseKey"; | |
27 private static final String BUNDLE_KEY_DATA = "data"; | |
28 private static final String BUNDLE_KEY_RAW_DATA = "rawData"; | |
29 private static final String BUNDLE_KEY_SENDER_ID = "senderId"; | |
30 | |
31 private String mSenderId; | |
32 private String mAppId; | |
33 | |
34 @Nullable | |
35 private String mCollapseKey; | |
36 @Nullable | |
37 private byte[] mRawData; | |
38 | |
39 /** | |
40 * Array that contains pairs of entries in the format of {key, value}. | |
41 */ | |
42 private String[] mDataKeysAndValuesArray; | |
43 | |
44 /** | |
45 * Validates that all required fields have been set in the given bundle. | |
46 */ | |
47 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
48 public static boolean validatePersistableBundle(PersistableBundle persistabl eBundle) { | |
49 return persistableBundle.containsKey(BUNDLE_KEY_APP_ID) | |
50 && persistableBundle.containsKey(BUNDLE_KEY_COLLAPSE_KEY) | |
51 && persistableBundle.containsKey(BUNDLE_KEY_DATA) | |
52 && persistableBundle.containsKey(BUNDLE_KEY_RAW_DATA) | |
53 && persistableBundle.containsKey(BUNDLE_KEY_SENDER_ID); | |
54 } | |
55 | |
56 /** | |
57 * Creates a GCMMessage object based on data received from GCM. The extras w ill be filtered. | |
58 */ | |
59 public GCMMessage(String senderId, Bundle extras) { | |
60 final String bundleCollapseKey = "collapse_key"; | |
61 final String bundleGcmplex = "com.google.ipc.invalidation.gcmmplex."; | |
62 final String bundleRawData = "rawData"; | |
63 final String bundleSenderId = "from"; | |
64 final String bundleSubtype = "subtype"; | |
65 | |
66 if (!extras.containsKey(bundleSubtype)) { | |
67 throw new IllegalArgumentException("Received push message with no su btype"); | |
68 } | |
69 | |
70 mSenderId = senderId; | |
71 mAppId = extras.getString(bundleSubtype); | |
72 | |
73 mCollapseKey = extras.getString(bundleCollapseKey); // May be null. | |
74 mRawData = extras.getByteArray(bundleRawData); // May be null. | |
75 | |
76 List<String> dataKeysAndValues = new ArrayList<String>(); | |
77 for (String key : extras.keySet()) { | |
78 if (key.equals(bundleSubtype) || key.equals(bundleSenderId) | |
79 || key.equals(bundleCollapseKey) || key.equals(bundleRawData ) | |
80 || key.startsWith(bundleGcmplex)) { | |
81 continue; | |
82 } | |
83 | |
84 Object value = extras.get(key); | |
85 if (!(value instanceof String)) { | |
86 continue; | |
87 } | |
88 | |
89 dataKeysAndValues.add(key); | |
90 dataKeysAndValues.add((String) value); | |
91 } | |
92 | |
93 mDataKeysAndValuesArray = dataKeysAndValues.toArray(new String[dataKeysA ndValues.size()]); | |
94 } | |
95 | |
96 /** | |
97 * Creates a GCMMessage object based on the given bundle. Assumes that the b undle has previously | |
98 * been created through {@link #toPersistableBundle}. | |
99 */ | |
100 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
101 public GCMMessage(PersistableBundle persistableBundle) { | |
102 mSenderId = persistableBundle.getString(BUNDLE_KEY_SENDER_ID); | |
103 mAppId = persistableBundle.getString(BUNDLE_KEY_APP_ID); | |
104 mCollapseKey = persistableBundle.getString(BUNDLE_KEY_COLLAPSE_KEY); | |
105 | |
106 // The rawData field needs to distinguish between {not set, set but empt y, set with data}. | |
107 String rawDataString = persistableBundle.getString(BUNDLE_KEY_RAW_DATA); | |
108 if (rawDataString != null) { | |
109 if (rawDataString.length() > 0) { | |
110 mRawData = rawDataString.getBytes(); | |
111 } else { | |
112 mRawData = new byte[0]; | |
113 } | |
114 } | |
115 | |
116 mDataKeysAndValuesArray = persistableBundle.getStringArray(BUNDLE_KEY_DA TA); | |
117 } | |
118 | |
119 public String getSenderId() { | |
120 return mSenderId; | |
121 } | |
122 | |
123 public String getAppId() { | |
124 return mAppId; | |
125 } | |
126 | |
127 @Nullable | |
128 public String getCollapseKey() { | |
129 return mCollapseKey; | |
130 } | |
131 | |
132 @Nullable | |
133 public byte[] getRawData() { | |
134 return mRawData; | |
Peter Beverloo
2017/02/17 17:33:22
I guess I either make a copy or suppress the warni
awdf
2017/02/18 07:51:41
I guess it's okay since the two places it's called
Peter Beverloo
2017/02/21 14:56:10
Done.
https://build.chromium.org/p/tryserver.chro
| |
135 } | |
136 | |
137 public String[] getDataKeysAndValuesArray() { | |
138 return mDataKeysAndValuesArray; | |
Peter Beverloo
2017/02/17 17:33:22
I guess I either make a copy or suppress the warni
Peter Beverloo
2017/02/21 14:56:10
Done.
| |
139 } | |
140 | |
141 /** | |
142 * Serializes the contents of this GCM Message to a new persistable bundle t hat can be stored, | |
143 * for example for purposes of scheduling a job. | |
144 */ | |
145 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
146 public PersistableBundle toPersistableBundle() { | |
147 PersistableBundle persistableBundle = new PersistableBundle(); | |
148 persistableBundle.putString(BUNDLE_KEY_SENDER_ID, mSenderId); | |
149 persistableBundle.putString(BUNDLE_KEY_APP_ID, mAppId); | |
150 persistableBundle.putString(BUNDLE_KEY_COLLAPSE_KEY, mCollapseKey); | |
151 | |
152 // The rawData field needs to distinguish between {not set, set but empt y, set with data}. | |
153 if (mRawData != null) { | |
154 if (mRawData.length > 0) { | |
155 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, new String(mRaw Data)); | |
156 } else { | |
157 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, new String()); | |
Peter Beverloo
2017/02/17 17:33:22
I mean, I want a non-null empty string. What are t
awdf
2017/02/18 07:51:41
can't you just pass "" ?
Peter Beverloo
2017/02/21 14:56:10
Let's try.
| |
158 } | |
159 } else { | |
160 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, null); | |
161 } | |
162 | |
163 persistableBundle.putStringArray(BUNDLE_KEY_DATA, mDataKeysAndValuesArra y); | |
164 return persistableBundle; | |
165 } | |
166 } | |
OLD | NEW |