CustomTabsService.java revision 97ee4f3f353309991efd3ceca369548b485e9a5f
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 17package android.support.customtabs; 18 19import android.app.Service; 20import android.content.Intent; 21import android.net.Uri; 22import android.os.Bundle; 23import android.os.IBinder; 24import android.os.IBinder.DeathRecipient; 25import android.os.RemoteException; 26import android.util.ArrayMap; 27 28import java.util.List; 29import java.util.Map; 30import java.util.NoSuchElementException; 31 32/** 33 * Abstract service class for implementing Custom Tabs related functionality. The service should 34 * be responding to the action ACTION_CUSTOM_TABS_CONNECTION. This class should be used by 35 * implementers that want to provide Custom Tabs functionality, not by clients that want to launch 36 * Custom Tabs. 37 */ 38 public abstract class CustomTabsService extends Service { 39 /** 40 * The Intent action that a CustomTabsService must respond to. 41 */ 42 public static final String ACTION_CUSTOM_TABS_CONNECTION = 43 "android.support.customtabs.action.CustomTabsService"; 44 45 /** 46 * For {@link CustomTabsService#mayLaunchUrl} calls that wants to specify more than one url, 47 * this key can be used with {@link Bundle#putParcelable(String, android.os.Parcelable)} 48 * to insert a new url to each bundle inside list of bundles. 49 */ 50 public static final String KEY_URL = 51 "android.support.customtabs.otherurls.URL"; 52 53 private Map<IBinder, DeathRecipient> mDeathRecipientMap = new ArrayMap<>(); 54 55 private ICustomTabsService.Stub mBinder = new ICustomTabsService.Stub() { 56 57 @Override 58 public boolean warmup(long flags) { 59 return CustomTabsService.this.warmup(flags); 60 } 61 62 @Override 63 public boolean newSession(ICustomTabsCallback callback) { 64 final CustomTabsSessionToken sessionToken = new CustomTabsSessionToken(callback); 65 try { 66 DeathRecipient deathRecipient = new IBinder.DeathRecipient() { 67 @Override 68 public void binderDied() { 69 cleanUpSession(sessionToken); 70 } 71 }; 72 synchronized (mDeathRecipientMap) { 73 callback.asBinder().linkToDeath(deathRecipient, 0); 74 mDeathRecipientMap.put(callback.asBinder(), deathRecipient); 75 } 76 return CustomTabsService.this.newSession(sessionToken); 77 } catch (RemoteException e) { 78 return false; 79 } 80 } 81 82 @Override 83 public boolean mayLaunchUrl(ICustomTabsCallback callback, Uri url, 84 Bundle extras, List<Bundle> otherLikelyBundles) { 85 return CustomTabsService.this.mayLaunchUrl( 86 new CustomTabsSessionToken(callback), url, extras, otherLikelyBundles); 87 } 88 }; 89 90 @Override 91 public IBinder onBind(Intent intent) { 92 return mBinder; 93 } 94 95 /** 96 * Called when the client side {@link IBinder} for this {@link CustomTabsSessionToken} is dead. 97 * Can also be used to clean up {@link DeathRecipient} instances allocated for the given token. 98 * @param sessionToken The session token for which the {@link DeathRecipient} call has been 99 * received. 100 * @return Whether the clean up was successful. Multiple calls with two tokens holdings the 101 * same binder will return false. 102 */ 103 protected boolean cleanUpSession(CustomTabsSessionToken sessionToken) { 104 try { 105 synchronized (mDeathRecipientMap) { 106 IBinder binder = sessionToken.getCallbackBinder(); 107 DeathRecipient deathRecipient = 108 mDeathRecipientMap.get(binder); 109 binder.unlinkToDeath(deathRecipient, 0); 110 mDeathRecipientMap.remove(binder); 111 } 112 } catch (NoSuchElementException e) { 113 return false; 114 } 115 return true; 116 } 117 118 /** 119 * Warms up the browser process asynchronously. 120 * 121 * @param flags Reserved for future use. 122 * @return Whether warmup was/had been completed successfully. Multiple successful 123 * calls will return true. 124 */ 125 protected abstract boolean warmup(long flags); 126 127 /** 128 * Creates a new session through an ICustomTabsService with the optional callback. This session 129 * can be used to associate any related communication through the service with an intent and 130 * then later with a Custom Tab. The client can then send later service calls or intents to 131 * through same session-intent-Custom Tab association. 132 * @param sessionToken Session token to be used as a unique identifier. This also has access 133 * to the {@link CustomTabsCallback} passed from the client side through 134 * {@link CustomTabsSessionToken#getCallback()}. 135 * @return Whether a new session was successfully created. 136 */ 137 protected abstract boolean newSession(CustomTabsSessionToken sessionToken); 138 139 /** 140 * Tells the browser of a likely future navigation to a URL. 141 * 142 * The method {@link CustomTabsService#warmup(long)} has to be called beforehand. 143 * The most likely URL has to be specified explicitly. Optionally, a list of 144 * other likely URLs can be provided. They are treated as less likely than 145 * the first one, and have to be sorted in decreasing priority order. These 146 * additional URLs may be ignored. 147 * All previous calls to this method will be deprioritized. 148 * 149 * @param sessionToken The unique identifier for the session. Can not be null. 150 * @param url Most likely URL. 151 * @param extras Reserved for future use. 152 * @param otherLikelyBundles Other likely destinations, sorted in decreasing 153 * likelihood order. Each Bundle has to provide a url. 154 * @return Whether the call was successful. 155 */ 156 protected abstract boolean mayLaunchUrl(CustomTabsSessionToken sessionToken, Uri url, 157 Bundle extras, List<Bundle> otherLikelyBundles); 158} 159