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 org.chromium.base.Log; | |
13 | |
14 import java.util.ArrayList; | |
15 import java.util.List; | |
16 | |
17 import javax.annotation.Nullable; | |
18 | |
19 /** | |
20 * Represents the contents of a GCM Message that is to be handled by the GCM Dri ver. Can be created | |
21 * based on data received from GCM, or serialized and deserialized to and from a PersistableBundle. | |
22 */ | |
23 public class GCMMessage { | |
awdf
2017/02/16 21:26:48
nit: worth implementing hashcode and equals()? mig
Peter Beverloo
2017/02/17 15:43:59
I really don't feel strongly, but mostly due to my
| |
24 private static final String TAG = "GCMMessage"; | |
25 | |
26 /** | |
27 * Keys used to store information in the persistable bundle for serializatio n purposes. | |
28 */ | |
29 private static final String BUNDLE_KEY_APP_ID = "appId"; | |
30 private static final String BUNDLE_KEY_COLLAPSE_KEY = "collapseKey"; | |
31 private static final String BUNDLE_KEY_DATA = "data"; | |
32 private static final String BUNDLE_KEY_RAW_DATA = "rawData"; | |
33 private static final String BUNDLE_KEY_SENDER_ID = "senderId"; | |
34 | |
35 private String mSenderId; | |
36 private String mAppId; | |
37 | |
38 @Nullable | |
39 private String mCollapseKey; | |
40 @Nullable | |
41 private byte[] mRawData; | |
42 | |
43 /** | |
44 * Array that contains pairs of entries in the format of {key, value}. | |
45 */ | |
46 private String[] mDataKeysAndValuesArray; | |
47 | |
48 /** | |
49 * Validates that all required fields have been set in the given bundle. | |
50 */ | |
51 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
52 public static boolean validatePersistableBundle(PersistableBundle persistabl eBundle) { | |
53 return persistableBundle.containsKey(BUNDLE_KEY_APP_ID) | |
54 && persistableBundle.containsKey(BUNDLE_KEY_COLLAPSE_KEY) | |
55 && persistableBundle.containsKey(BUNDLE_KEY_DATA) | |
56 && persistableBundle.containsKey(BUNDLE_KEY_RAW_DATA) | |
57 && persistableBundle.containsKey(BUNDLE_KEY_SENDER_ID); | |
58 } | |
59 | |
60 /** | |
61 * Creates a GCMMessage object based on data received from GCM. The extras w ill be filtered. | |
62 */ | |
63 public GCMMessage(String senderId, Bundle extras) { | |
64 final String bundleCollapseKey = "collapse_key"; | |
65 final String bundleGcmplex = "com.google.ipc.invalidation.gcmmplex."; | |
66 final String bundleRawData = "rawData"; | |
67 final String bundleSenderId = "from"; | |
68 final String bundleSubtype = "subtype"; | |
69 | |
70 if (!extras.containsKey(bundleSubtype)) { | |
71 Log.w(TAG, "Received push message with no subtype"); | |
72 return; | |
awdf
2017/02/16 21:26:48
shouldn't we throw an IllegalArgumentException or
Peter Beverloo
2017/02/17 15:43:59
Done. I've added a try/catch in ChromeGcmListenerS
| |
73 } | |
74 | |
75 mSenderId = senderId; | |
76 mAppId = extras.getString(bundleSubtype); | |
77 | |
78 mCollapseKey = extras.getString(bundleCollapseKey); // May be null. | |
79 mRawData = extras.getByteArray(bundleRawData); // May be null. | |
80 | |
81 List<String> dataKeysAndValues = new ArrayList<String>(); | |
82 for (String key : extras.keySet()) { | |
83 if (key.equals(bundleSubtype) || key.equals(bundleSenderId) | |
84 || key.equals(bundleCollapseKey) || key.equals(bundleRawData ) | |
85 || key.startsWith(bundleGcmplex)) { | |
86 continue; | |
87 } | |
88 | |
89 Object value = extras.get(key); | |
90 if (!(value instanceof String)) { | |
91 continue; | |
92 } | |
93 | |
94 dataKeysAndValues.add(key); | |
95 dataKeysAndValues.add((String) value); | |
96 } | |
97 | |
98 mDataKeysAndValuesArray = dataKeysAndValues.toArray(new String[dataKeysA ndValues.size()]); | |
99 } | |
100 | |
101 /** | |
102 * Creates a GCMMessage object based on the given bundle. Assumes that the b undle has previously | |
103 * been created through {@link #toPersistableBundle}. | |
104 */ | |
105 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
106 public GCMMessage(PersistableBundle persistableBundle) { | |
107 mSenderId = persistableBundle.getString(BUNDLE_KEY_SENDER_ID); | |
108 mAppId = persistableBundle.getString(BUNDLE_KEY_APP_ID); | |
109 mCollapseKey = persistableBundle.getString(BUNDLE_KEY_COLLAPSE_KEY); | |
110 | |
111 // The rawData field needs to distinguish between {not set, set but empt y, set with data}. | |
112 String rawDataString = persistableBundle.getString(BUNDLE_KEY_RAW_DATA); | |
113 if (rawDataString != null) { | |
114 if (rawDataString.length() > 0) { | |
115 mRawData = rawDataString.getBytes(); | |
116 } else { | |
117 mRawData = new byte[0]; | |
118 } | |
119 } | |
120 | |
121 mDataKeysAndValuesArray = persistableBundle.getStringArray(BUNDLE_KEY_DA TA); | |
122 } | |
123 | |
124 public String getSenderId() { | |
125 return mSenderId; | |
126 } | |
127 | |
128 public String getAppId() { | |
129 return mAppId; | |
130 } | |
131 | |
132 @Nullable | |
133 public String getCollapseKey() { | |
134 return mCollapseKey; | |
135 } | |
136 | |
137 @Nullable | |
138 public byte[] getRawData() { | |
139 return mRawData; | |
140 } | |
141 | |
142 public String[] getDataKeysAndValuesArray() { | |
143 return mDataKeysAndValuesArray; | |
144 } | |
145 | |
146 /** | |
147 * Serializes the contents of this GCM Message to a new persistable bundle t hat can be stored, | |
148 * for example for purposes of scheduling a job. | |
149 */ | |
150 @TargetApi(Build.VERSION_CODES.LOLLIPOP) | |
151 public PersistableBundle toPersistableBundle() { | |
152 PersistableBundle persistableBundle = new PersistableBundle(); | |
153 persistableBundle.putString(BUNDLE_KEY_SENDER_ID, mSenderId); | |
154 persistableBundle.putString(BUNDLE_KEY_APP_ID, mAppId); | |
155 persistableBundle.putString(BUNDLE_KEY_COLLAPSE_KEY, mCollapseKey); | |
156 | |
157 // The rawData field needs to distinguish between {not set, set but empt y, set with data}. | |
158 if (mRawData != null) { | |
159 if (mRawData.length > 0) { | |
160 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, new String(mRaw Data)); | |
161 } else { | |
162 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, new String()); | |
163 } | |
164 } else { | |
165 persistableBundle.putString(BUNDLE_KEY_RAW_DATA, null); | |
166 } | |
167 | |
168 persistableBundle.putStringArray(BUNDLE_KEY_DATA, mDataKeysAndValuesArra y); | |
169 return persistableBundle; | |
170 } | |
171 } | |
OLD | NEW |