1 /* 2 * Copyright (C) 2015 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package android.support.customtabs; 18 19 import android.content.Intent; 20 import android.os.Bundle; 21 import android.os.IBinder; 22 import android.os.RemoteException; 23 import android.support.v4.app.BundleCompat; 24 import android.util.Log; 25 26 /** 27 * Wrapper class that can be used as a unique identifier for a session. Also contains an accessor 28 * for the {@link CustomTabsCallback} for the session if there was any. 29 */ 30 public class CustomTabsSessionToken { 31 private static final String TAG = "CustomTabsSessionToken"; 32 private final ICustomTabsCallback mCallbackBinder; 33 private final CustomTabsCallback mCallback; 34 35 /** 36 * Obtain a {@link CustomTabsSessionToken} from an intent. See {@link CustomTabsIntent.Builder} 37 * for ways to generate an intent for custom tabs. 38 * @param intent The intent to generate the token from. This has to include an extra for 39 * {@link CustomTabsIntent#EXTRA_SESSION}. 40 * @return The token that was generated. 41 */ getSessionTokenFromIntent(Intent intent)42 public static CustomTabsSessionToken getSessionTokenFromIntent(Intent intent) { 43 Bundle b = intent.getExtras(); 44 IBinder binder = BundleCompat.getBinder(b, CustomTabsIntent.EXTRA_SESSION); 45 if (binder == null) return null; 46 return new CustomTabsSessionToken(ICustomTabsCallback.Stub.asInterface(binder)); 47 } 48 CustomTabsSessionToken(ICustomTabsCallback callbackBinder)49 CustomTabsSessionToken(ICustomTabsCallback callbackBinder) { 50 mCallbackBinder = callbackBinder; 51 mCallback = new CustomTabsCallback() { 52 53 @Override 54 public void onNavigationEvent(int navigationEvent, Bundle extras) { 55 try { 56 mCallbackBinder.onNavigationEvent(navigationEvent, extras); 57 } catch (RemoteException e) { 58 Log.e(TAG, "RemoteException during ICustomTabsCallback transaction"); 59 } 60 } 61 62 @Override 63 public void extraCallback(String callbackName, Bundle args) { 64 try { 65 mCallbackBinder.extraCallback(callbackName, args); 66 } catch (RemoteException e) { 67 Log.e(TAG, "RemoteException during ICustomTabsCallback transaction"); 68 } 69 } 70 71 @Override 72 public void onMessageChannelReady(Bundle extras) { 73 try { 74 mCallbackBinder.onMessageChannelReady(extras); 75 } catch (RemoteException e) { 76 Log.e(TAG, "RemoteException during ICustomTabsCallback transaction"); 77 } 78 } 79 80 @Override 81 public void onPostMessage(String message, Bundle extras) { 82 try { 83 mCallbackBinder.onPostMessage(message, extras); 84 } catch (RemoteException e) { 85 Log.e(TAG, "RemoteException during ICustomTabsCallback transaction"); 86 } 87 } 88 }; 89 } 90 getCallbackBinder()91 IBinder getCallbackBinder() { 92 return mCallbackBinder.asBinder(); 93 } 94 95 @Override hashCode()96 public int hashCode() { 97 return getCallbackBinder().hashCode(); 98 } 99 100 @Override equals(Object o)101 public boolean equals(Object o) { 102 if (!(o instanceof CustomTabsSessionToken)) return false; 103 CustomTabsSessionToken token = (CustomTabsSessionToken) o; 104 return token.getCallbackBinder().equals(mCallbackBinder.asBinder()); 105 } 106 107 /** 108 * @return {@link CustomTabsCallback} corresponding to this session if there was any non-null 109 * callbacks passed by the client. 110 */ getCallback()111 public CustomTabsCallback getCallback() { 112 return mCallback; 113 } 114 115 /** 116 * @return Whether this token is associated with the given session. 117 */ isAssociatedWith(CustomTabsSession session)118 public boolean isAssociatedWith(CustomTabsSession session) { 119 return session.getBinder().equals(mCallbackBinder); 120 } 121 } 122