| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.components.gcm_driver; | 5 package org.chromium.components.gcm_driver; |
| 6 | 6 |
| 7 import android.app.PendingIntent; | 7 import android.app.PendingIntent; |
| 8 import android.content.Context; | |
| 9 import android.content.Intent; | 8 import android.content.Intent; |
| 10 import android.os.Bundle; | 9 import android.os.Bundle; |
| 11 import android.os.Handler; | 10 import android.os.Handler; |
| 12 import android.os.Looper; | 11 import android.os.Looper; |
| 13 import android.os.Message; | 12 import android.os.Message; |
| 14 import android.os.Messenger; | 13 import android.os.Messenger; |
| 15 | 14 |
| 15 import org.chromium.base.ContextUtils; |
| 16 import org.chromium.base.PackageUtils; | 16 import org.chromium.base.PackageUtils; |
| 17 | 17 |
| 18 import java.io.IOException; | 18 import java.io.IOException; |
| 19 import java.util.concurrent.BlockingQueue; | 19 import java.util.concurrent.BlockingQueue; |
| 20 import java.util.concurrent.LinkedBlockingQueue; | 20 import java.util.concurrent.LinkedBlockingQueue; |
| 21 import java.util.concurrent.TimeUnit; | 21 import java.util.concurrent.TimeUnit; |
| 22 | 22 |
| 23 import javax.annotation.Nullable; | 23 import javax.annotation.Nullable; |
| 24 | 24 |
| 25 /** | 25 /** |
| 26 * Temporary code for sending subtypes when (un)subscribing with GCM. | 26 * Temporary code for sending subtypes when (un)subscribing with GCM. |
| 27 * Subtypes are experimental and may change without notice! | 27 * Subtypes are experimental and may change without notice! |
| 28 * TODO(johnme): Remove this file, once we switch to the GMS client library. | 28 * TODO(johnme): Remove this file, once we switch to the GMS client library. |
| 29 */ | 29 */ |
| 30 public class GoogleCloudMessagingV2 implements GoogleCloudMessagingSubscriber { | 30 public class GoogleCloudMessagingV2 implements GoogleCloudMessagingSubscriber { |
| 31 private static final String GOOGLE_PLAY_SERVICES_PACKAGE = "com.google.andro
id.gms"; | 31 private static final String GOOGLE_PLAY_SERVICES_PACKAGE = "com.google.andro
id.gms"; |
| 32 private static final long REGISTER_TIMEOUT = 5000; | 32 private static final long REGISTER_TIMEOUT = 5000; |
| 33 private static final String ACTION_C2DM_REGISTER = "com.google.android.c2dm.
intent.REGISTER"; | 33 private static final String ACTION_C2DM_REGISTER = "com.google.android.c2dm.
intent.REGISTER"; |
| 34 private static final String C2DM_EXTRA_ERROR = "error"; | 34 private static final String C2DM_EXTRA_ERROR = "error"; |
| 35 private static final String INTENT_PARAM_APP = "app"; | 35 private static final String INTENT_PARAM_APP = "app"; |
| 36 private static final String ERROR_MAIN_THREAD = "MAIN_THREAD"; | 36 private static final String ERROR_MAIN_THREAD = "MAIN_THREAD"; |
| 37 private static final String ERROR_SERVICE_NOT_AVAILABLE = "SERVICE_NOT_AVAIL
ABLE"; | 37 private static final String ERROR_SERVICE_NOT_AVAILABLE = "SERVICE_NOT_AVAIL
ABLE"; |
| 38 private static final String EXTRA_DELETE = "delete"; | 38 private static final String EXTRA_DELETE = "delete"; |
| 39 private static final String EXTRA_REGISTRATION_ID = "registration_id"; | 39 private static final String EXTRA_REGISTRATION_ID = "registration_id"; |
| 40 private static final String EXTRA_SENDER = "sender"; | 40 private static final String EXTRA_SENDER = "sender"; |
| 41 private static final String EXTRA_MESSENGER = "google.messenger"; | 41 private static final String EXTRA_MESSENGER = "google.messenger"; |
| 42 private static final String EXTRA_SUBTYPE = "subtype"; | 42 private static final String EXTRA_SUBTYPE = "subtype"; |
| 43 private static final String EXTRA_SUBSCRIPTION = "subscription"; | 43 private static final String EXTRA_SUBSCRIPTION = "subscription"; |
| 44 | 44 |
| 45 private Context mContext; | |
| 46 private PendingIntent mAppPendingIntent; | 45 private PendingIntent mAppPendingIntent; |
| 47 private final Object mAppPendingIntentLock = new Object(); | 46 private final Object mAppPendingIntentLock = new Object(); |
| 48 | 47 |
| 49 public GoogleCloudMessagingV2(Context context) { | 48 public GoogleCloudMessagingV2() {} |
| 50 mContext = context; | |
| 51 } | |
| 52 | 49 |
| 53 @Override | 50 @Override |
| 54 public String subscribe(String source, String subtype, @Nullable Bundle data
) | 51 public String subscribe(String source, String subtype, @Nullable Bundle data
) |
| 55 throws IOException { | 52 throws IOException { |
| 56 if (data == null) { | 53 if (data == null) { |
| 57 data = new Bundle(); | 54 data = new Bundle(); |
| 58 } | 55 } |
| 59 data.putString(EXTRA_SUBTYPE, subtype); | 56 data.putString(EXTRA_SUBTYPE, subtype); |
| 60 Bundle result = subscribe(source, data); | 57 Bundle result = subscribe(source, data); |
| 61 return result.getString(EXTRA_REGISTRATION_ID); | 58 return result.getString(EXTRA_REGISTRATION_ID); |
| (...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 127 // Use the register servlet, with 'delete=true'. | 124 // Use the register servlet, with 'delete=true'. |
| 128 // Registration service returns a registration_id on success - or an err
or code. | 125 // Registration service returns a registration_id on success - or an err
or code. |
| 129 data.putString(EXTRA_DELETE, "1"); | 126 data.putString(EXTRA_DELETE, "1"); |
| 130 subscribe(source, data); | 127 subscribe(source, data); |
| 131 } | 128 } |
| 132 | 129 |
| 133 private Intent registerRpc(Bundle data) throws IOException { | 130 private Intent registerRpc(Bundle data) throws IOException { |
| 134 if (Looper.getMainLooper() == Looper.myLooper()) { | 131 if (Looper.getMainLooper() == Looper.myLooper()) { |
| 135 throw new IOException(ERROR_MAIN_THREAD); | 132 throw new IOException(ERROR_MAIN_THREAD); |
| 136 } | 133 } |
| 137 if (PackageUtils.getPackageVersion(mContext, GOOGLE_PLAY_SERVICES_PACKAG
E) < 0) { | 134 if (PackageUtils.getPackageVersion( |
| 135 ContextUtils.getApplicationContext(), GOOGLE_PLAY_SERVICES_P
ACKAGE) |
| 136 < 0) { |
| 138 throw new IOException("Google Play Services missing"); | 137 throw new IOException("Google Play Services missing"); |
| 139 } | 138 } |
| 140 if (data == null) { | 139 if (data == null) { |
| 141 data = new Bundle(); | 140 data = new Bundle(); |
| 142 } | 141 } |
| 143 | 142 |
| 144 final BlockingQueue<Intent> responseResult = new LinkedBlockingQueue<Int
ent>(); | 143 final BlockingQueue<Intent> responseResult = new LinkedBlockingQueue<Int
ent>(); |
| 145 Handler responseHandler = new Handler(Looper.getMainLooper()) { | 144 Handler responseHandler = new Handler(Looper.getMainLooper()) { |
| 146 @Override | 145 @Override |
| 147 public void handleMessage(Message msg) { | 146 public void handleMessage(Message msg) { |
| 148 Intent res = (Intent) msg.obj; | 147 Intent res = (Intent) msg.obj; |
| 149 responseResult.add(res); | 148 responseResult.add(res); |
| 150 } | 149 } |
| 151 }; | 150 }; |
| 152 Messenger responseMessenger = new Messenger(responseHandler); | 151 Messenger responseMessenger = new Messenger(responseHandler); |
| 153 | 152 |
| 154 Intent intent = new Intent(ACTION_C2DM_REGISTER); | 153 Intent intent = new Intent(ACTION_C2DM_REGISTER); |
| 155 intent.setPackage(GOOGLE_PLAY_SERVICES_PACKAGE); | 154 intent.setPackage(GOOGLE_PLAY_SERVICES_PACKAGE); |
| 156 setPackageNameExtra(intent); | 155 setPackageNameExtra(intent); |
| 157 intent.putExtras(data); | 156 intent.putExtras(data); |
| 158 intent.putExtra(EXTRA_MESSENGER, responseMessenger); | 157 intent.putExtra(EXTRA_MESSENGER, responseMessenger); |
| 159 mContext.startService(intent); | 158 ContextUtils.getApplicationContext().startService(intent); |
| 160 try { | 159 try { |
| 161 return responseResult.poll(REGISTER_TIMEOUT, TimeUnit.MILLISECONDS); | 160 return responseResult.poll(REGISTER_TIMEOUT, TimeUnit.MILLISECONDS); |
| 162 } catch (InterruptedException e) { | 161 } catch (InterruptedException e) { |
| 163 throw new IOException(e.getMessage()); | 162 throw new IOException(e.getMessage()); |
| 164 } | 163 } |
| 165 } | 164 } |
| 166 | 165 |
| 167 private String getExtraOrThrow(Intent intent, String extraKey) throws IOExce
ption { | 166 private String getExtraOrThrow(Intent intent, String extraKey) throws IOExce
ption { |
| 168 if (intent == null) { | 167 if (intent == null) { |
| 169 throw new IOException(ERROR_SERVICE_NOT_AVAILABLE); | 168 throw new IOException(ERROR_SERVICE_NOT_AVAILABLE); |
| (...skipping 12 matching lines...) Expand all Loading... |
| 182 } | 181 } |
| 183 } | 182 } |
| 184 | 183 |
| 185 private void setPackageNameExtra(Intent intent) { | 184 private void setPackageNameExtra(Intent intent) { |
| 186 synchronized (mAppPendingIntentLock) { | 185 synchronized (mAppPendingIntentLock) { |
| 187 if (mAppPendingIntent == null) { | 186 if (mAppPendingIntent == null) { |
| 188 Intent target = new Intent(); | 187 Intent target = new Intent(); |
| 189 // Fill in the package, to prevent the intent from being used. | 188 // Fill in the package, to prevent the intent from being used. |
| 190 target.setPackage("com.google.example.invalidpackage"); | 189 target.setPackage("com.google.example.invalidpackage"); |
| 191 mAppPendingIntent = PendingIntent.getBroadcast( | 190 mAppPendingIntent = PendingIntent.getBroadcast( |
| 192 mContext.getApplicationContext(), 0, target, 0); | 191 ContextUtils.getApplicationContext(), 0, target, 0); |
| 193 } | 192 } |
| 194 } | 193 } |
| 195 intent.putExtra(INTENT_PARAM_APP, mAppPendingIntent); | 194 intent.putExtra(INTENT_PARAM_APP, mAppPendingIntent); |
| 196 } | 195 } |
| 197 } | 196 } |
| OLD | NEW |