1 /* 2 * Copyright (C) 2018 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.media; 18 19 import android.util.Log; 20 21 import java.net.CookieHandler; 22 import java.net.CookieManager; 23 import java.net.CookieStore; 24 import java.net.HttpCookie; 25 import java.util.List; 26 27 /** @hide */ 28 public class Media2Utils { 29 private static final String TAG = "Media2Utils"; 30 Media2Utils()31 private Media2Utils() { 32 } 33 34 /** 35 * Ensures that an expression checking an argument is true. 36 * 37 * @param expression the expression to check 38 * @param errorMessage the exception message to use if the check fails; will 39 * be converted to a string using {@link String#valueOf(Object)} 40 * @throws IllegalArgumentException if {@code expression} is false 41 */ checkArgument(boolean expression, String errorMessage)42 public static void checkArgument(boolean expression, String errorMessage) { 43 if (!expression) { 44 throw new IllegalArgumentException(errorMessage); 45 } 46 } 47 storeCookies(List<HttpCookie> cookies)48 public static synchronized void storeCookies(List<HttpCookie> cookies) { 49 CookieHandler cookieHandler = CookieHandler.getDefault(); 50 if (cookieHandler == null) { 51 cookieHandler = new CookieManager(); 52 CookieHandler.setDefault(cookieHandler); 53 Log.v(TAG, "storeCookies: CookieManager created: " + cookieHandler); 54 } else { 55 Log.v(TAG, "storeCookies: CookieHandler (" + cookieHandler + ") exists."); 56 } 57 58 if (cookies != null) { 59 if (cookieHandler instanceof CookieManager) { 60 CookieManager cookieManager = (CookieManager)cookieHandler; 61 CookieStore store = cookieManager.getCookieStore(); 62 for (HttpCookie cookie : cookies) { 63 try { 64 store.add(null, cookie); 65 } catch (Exception e) { 66 Log.v(TAG, "storeCookies: CookieStore.add" + cookie, e); 67 } 68 } 69 } else { 70 Log.w(TAG, "storeCookies: The installed CookieHandler is not a CookieManager." 71 + " Can’t add the provided cookies to the cookie store."); 72 } 73 } // cookies 74 75 Log.v(TAG, "storeCookies: cookieHandler: " + cookieHandler + " Cookies: " + cookies); 76 77 } 78 } 79