CustomTabsClient.java revision 08889acbc842c73b64f94a761910154d9d42ee4c
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.content.ComponentName; 20import android.content.Context; 21import android.content.Intent; 22import android.content.ServiceConnection; 23import android.net.Uri; 24import android.os.Bundle; 25import android.os.RemoteException; 26import android.text.TextUtils; 27 28import java.util.List; 29 30/** 31 * Class to communicate with a {@link CustomTabsService} and create 32 * {@link CustomTabsSession} from it. 33 */ 34public class CustomTabsClient { 35 private final ICustomTabsService mService; 36 private final ComponentName mServiceComponentName; 37 38 /**@hide*/ 39 CustomTabsClient(ICustomTabsService service, ComponentName componentName) { 40 mService = service; 41 mServiceComponentName = componentName; 42 } 43 44 /** 45 * Bind to a {@link CustomTabsService} using the given package name and 46 * {@link ServiceConnection}. 47 * @param context {@link Context} to use while calling 48 * {@link Context#bindService(Intent, ServiceConnection, int)} 49 * @param packageName Package name to set on the {@link Intent} for binding. 50 * @param connection {@link CustomTabsServiceConnection} to use when binding. This will 51 * return a {@link CustomTabsClient} on 52 * {@link CustomTabsServiceConnection 53 * #onCustomTabsServiceConnected(ComponentName, CustomTabsClient)} 54 * @return Whether the binding was successful. 55 */ 56 public static boolean bindCustomTabsService(Context context, 57 String packageName, CustomTabsServiceConnection connection) { 58 Intent intent = new Intent(CustomTabsService.ACTION_CUSTOM_TABS_CONNECTION); 59 if (!TextUtils.isEmpty(packageName)) intent.setPackage(packageName); 60 return context.bindService(intent, connection, 61 Context.BIND_AUTO_CREATE | Context.BIND_WAIVE_PRIORITY); 62 } 63 64 /** 65 * Warm up the browser process. 66 * @param flags Reserved for future use. 67 * @return Whether the warmup was successful. 68 */ 69 public boolean warmup(long flags) { 70 try { 71 return mService.warmup(flags); 72 } catch (RemoteException e) { 73 return false; 74 } 75 } 76 77 /** 78 * Creates a new session through an ICustomTabsService with the optional callback. This session 79 * can be used to associate any related communication through the service with an intent and 80 * then later with a Custom Tab. The client can then send later service calls or intents to 81 * through same session-intent-Custom Tab association. 82 * @param callback The callback through which the client will receive updates about the created 83 * session. Can be null. 84 * @return The session object that was created as a result of the transaction. The client can 85 * use this to relay {@link CustomTabsSession#mayLaunchUrl(Uri, Bundle, List)} calls. 86 * Null on error. 87 */ 88 public CustomTabsSession newSession(final CustomTabsCallback callback) { 89 ICustomTabsCallback.Stub wrapper = new ICustomTabsCallback.Stub() { 90 @Override 91 public void onNavigationEvent(int navigationEvent, Bundle extras) { 92 if (callback != null) callback.onNavigationEvent(navigationEvent, extras); 93 } 94 }; 95 96 try { 97 if (!mService.newSession(wrapper)) return null; 98 } catch (RemoteException e) { 99 return null; 100 } 101 return new CustomTabsSession(mService, wrapper, mServiceComponentName); 102 } 103} 104