OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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.chrome.browser.gsa; |
| 6 |
| 7 import android.content.ComponentName; |
| 8 import android.content.Context; |
| 9 import android.content.Intent; |
| 10 import android.content.ServiceConnection; |
| 11 import android.os.Bundle; |
| 12 import android.os.Handler; |
| 13 import android.os.IBinder; |
| 14 import android.os.Message; |
| 15 import android.os.Messenger; |
| 16 import android.os.RemoteException; |
| 17 import android.util.Log; |
| 18 |
| 19 import org.chromium.chrome.browser.ChromeMobileApplication; |
| 20 |
| 21 /** |
| 22 * A simple client that connects and talks to the GSAService using Messages. |
| 23 */ |
| 24 public class GSAServiceClient { |
| 25 private static final String TAG = "GSAServiceClient"; |
| 26 |
| 27 /** |
| 28 * Constants for gsa communication. These should not change without correspo
nding changes on the |
| 29 * service side in GSA. |
| 30 */ |
| 31 private static final String GSA_SERVICE = |
| 32 "com.google.android.ssb.action.SSB_SERVICE"; |
| 33 public static final int REQUEST_REGISTER_CLIENT = 2; |
| 34 public static final int RESPONSE_UPDATE_SSB = 3; |
| 35 |
| 36 public static final String KEY_GSA_STATE = "ssb_service:ssb_state"; |
| 37 public static final String KEY_GSA_CONTEXT = "ssb_service:ssb_context"; |
| 38 public static final String KEY_GSA_PACKAGE_NAME = "ssb_service:ssb_package_n
ame"; |
| 39 |
| 40 /** Messenger to handle incoming messages from the service */ |
| 41 private final Messenger mMessenger = new Messenger(new IncomingHandler()); |
| 42 private final GSAServiceConnection mConnection; |
| 43 private final GSAHelper mGsaHelper; |
| 44 private Context mContext; |
| 45 |
| 46 /** Messenger for communicating with service. */ |
| 47 private Messenger mService; |
| 48 |
| 49 |
| 50 /** |
| 51 * Handler of incoming messages from service. |
| 52 */ |
| 53 private class IncomingHandler extends Handler { |
| 54 @Override |
| 55 public void handleMessage(Message msg) { |
| 56 if (msg.what == RESPONSE_UPDATE_SSB) { |
| 57 if (mService == null) return; |
| 58 Bundle bundle = (Bundle) msg.obj; |
| 59 String account = |
| 60 mGsaHelper.getGSAAccountFromState(bundle.getByteArray(KE
Y_GSA_STATE)); |
| 61 GSAState.getInstance(mContext.getApplicationContext()).setGsaAcc
ount(account); |
| 62 } else { |
| 63 super.handleMessage(msg); |
| 64 } |
| 65 } |
| 66 } |
| 67 |
| 68 /** |
| 69 * Constructs an instance of this class. |
| 70 */ |
| 71 public GSAServiceClient(Context context) { |
| 72 mContext = context; |
| 73 mConnection = new GSAServiceConnection(); |
| 74 mGsaHelper = ((ChromeMobileApplication) mContext.getApplicationContext()
) |
| 75 .createGsaHelper(); |
| 76 } |
| 77 |
| 78 /** |
| 79 * Establishes a connection to the service. Call this method once the callba
ck passed here is |
| 80 * ready to handle calls. |
| 81 * If you pass in an GSA context, it will be sent up the service as soon as
the connection is |
| 82 * established. |
| 83 * @return Whether or not the connection to the service was established succ
essfully. |
| 84 */ |
| 85 public boolean connect() { |
| 86 if (mService != null) Log.e(TAG, "Already connected."); |
| 87 Intent intent = new Intent(GSA_SERVICE).setPackage(GSAState.SEARCH_INTEN
T_PACKAGE); |
| 88 return mContext.bindService(intent, mConnection, Context.BIND_AUTO_CREAT
E); |
| 89 } |
| 90 |
| 91 /** |
| 92 * Disconnects from the service and resets the client's state. |
| 93 */ |
| 94 public void disconnect() { |
| 95 if (mService == null) return; |
| 96 |
| 97 mContext.unbindService(mConnection); |
| 98 mContext = null; |
| 99 mService = null; |
| 100 } |
| 101 |
| 102 /** |
| 103 * Indicates whether or not the client is currently connected to the service
. |
| 104 * @return true if connected, false otherwise. |
| 105 */ |
| 106 public boolean isConnected() { |
| 107 return mService != null; |
| 108 } |
| 109 |
| 110 private class GSAServiceConnection implements ServiceConnection { |
| 111 private static final String SERVICE_CONNECTION_TAG = "GSAServiceConnecti
on"; |
| 112 |
| 113 @Override |
| 114 public void onServiceDisconnected(ComponentName name) { |
| 115 mService = null; |
| 116 } |
| 117 |
| 118 @Override |
| 119 public void onServiceConnected(ComponentName name, IBinder service) { |
| 120 // Ignore this call if we disconnected in the meantime. |
| 121 if (mContext == null) return; |
| 122 |
| 123 mService = new Messenger(service); |
| 124 try { |
| 125 Message registerClientMessage = Message.obtain( |
| 126 null, REQUEST_REGISTER_CLIENT); |
| 127 registerClientMessage.replyTo = mMessenger; |
| 128 Bundle b = mGsaHelper.getBundleForRegisteringGSAClient(mContext)
; |
| 129 registerClientMessage.setData(b); |
| 130 registerClientMessage.getData().putString( |
| 131 KEY_GSA_PACKAGE_NAME, mContext.getPackageName()); |
| 132 mService.send(registerClientMessage); |
| 133 // Send prepare overlay message if there is a pending GSA contex
t. |
| 134 } catch (RemoteException e) { |
| 135 Log.w(SERVICE_CONNECTION_TAG, "GSAServiceConnection - remote cal
l failed", e); |
| 136 } |
| 137 } |
| 138 } |
| 139 } |
OLD | NEW |