1 /* 2 * Copyright (C) 2016 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.car; 18 19 import java.util.HashMap; 20 import java.util.Map; 21 22 /** 23 * Implementation of {@link CarAppContextManager} for embedded. 24 * @hide 25 */ 26 public class CarAppContextManagerEmbedded extends CarAppContextManager { 27 28 private final android.car.CarAppContextManager mManager; 29 private AppContextChangeListenerProxy mListener; 30 private final Map<Integer, AppContextOwnershipChangeListenerProxy> mOwnershipListeners; 31 32 /** 33 * @hide 34 */ CarAppContextManagerEmbedded(Object manager)35 CarAppContextManagerEmbedded(Object manager) { 36 mManager = (android.car.CarAppContextManager) manager; 37 mOwnershipListeners = new HashMap<Integer, AppContextOwnershipChangeListenerProxy>(); 38 } 39 40 @Override registerContextListener(AppContextChangeListener listener, int contextFilter)41 public void registerContextListener(AppContextChangeListener listener, int contextFilter) 42 throws CarNotConnectedException { 43 if (listener == null) { 44 throw new IllegalArgumentException("null listener"); 45 } 46 AppContextChangeListenerProxy proxy = new AppContextChangeListenerProxy(listener); 47 synchronized(this) { 48 mListener = proxy; 49 } 50 try { 51 mManager.registerContextListener(proxy, contextFilter); 52 } catch (android.car.CarNotConnectedException e) { 53 throw new CarNotConnectedException(e); 54 } 55 } 56 57 @Override unregisterContextListener()58 public void unregisterContextListener() throws CarNotConnectedException { 59 synchronized(this) { 60 mListener = null; 61 } 62 try { 63 mManager.unregisterContextListener(); 64 } catch (android.car.CarNotConnectedException e) { 65 throw new CarNotConnectedException(e); 66 } 67 68 } 69 70 @Override getActiveAppContexts()71 public int getActiveAppContexts() throws CarNotConnectedException { 72 try { 73 return mManager.getActiveAppContexts(); 74 } catch (android.car.CarNotConnectedException e) { 75 throw new CarNotConnectedException(e); 76 } 77 } 78 79 @Override isOwningContext(int context)80 public boolean isOwningContext(int context) throws CarNotConnectedException { 81 try { 82 return mManager.isOwningContext(context); 83 } catch (android.car.CarNotConnectedException e) { 84 throw new CarNotConnectedException(e); 85 } 86 } 87 88 @Override setActiveContexts(AppContextOwnershipChangeListener ownershipListener, int contexts)89 public void setActiveContexts(AppContextOwnershipChangeListener ownershipListener, 90 int contexts) throws IllegalStateException, SecurityException, 91 CarNotConnectedException { 92 if (ownershipListener == null) { 93 throw new IllegalArgumentException("null listener"); 94 } 95 AppContextOwnershipChangeListenerProxy proxy = 96 new AppContextOwnershipChangeListenerProxy(ownershipListener); 97 synchronized(this) { 98 for (int flag = APP_CONTEXT_START_FLAG; flag <= APP_CONTEXT_END_FLAG; flag <<= 1) { 99 if ((flag & contexts) != 0) { 100 mOwnershipListeners.put(flag, proxy); 101 } 102 } 103 } 104 try{ 105 mManager.setActiveContexts(proxy, contexts); 106 } catch (android.car.CarNotConnectedException e) { 107 throw new CarNotConnectedException(e); 108 } 109 } 110 111 @Override resetActiveContexts(int contexts)112 public void resetActiveContexts(int contexts) throws CarNotConnectedException { 113 try { 114 mManager.resetActiveContexts(contexts); 115 } catch (android.car.CarNotConnectedException e) { 116 throw new CarNotConnectedException(e); 117 } 118 synchronized (this) { 119 for (int flag = APP_CONTEXT_START_FLAG; flag <= APP_CONTEXT_END_FLAG; flag <<= 1) { 120 if ((flag & contexts) != 0) { 121 mOwnershipListeners.remove(flag); 122 } 123 } 124 } 125 } 126 127 @Override onCarDisconnected()128 public void onCarDisconnected() { 129 // nothing to do 130 } 131 132 private static class AppContextChangeListenerProxy implements 133 android.car.CarAppContextManager.AppContextChangeListener { 134 135 private final AppContextChangeListener mListener; 136 AppContextChangeListenerProxy(AppContextChangeListener listener)137 public AppContextChangeListenerProxy(AppContextChangeListener listener) { 138 mListener = listener; 139 } 140 141 @Override onAppContextChange(int activeContexts)142 public void onAppContextChange(int activeContexts) { 143 mListener.onAppContextChange(activeContexts); 144 } 145 } 146 147 private static class AppContextOwnershipChangeListenerProxy implements 148 android.car.CarAppContextManager.AppContextOwnershipChangeListener { 149 150 private final AppContextOwnershipChangeListener mListener; 151 AppContextOwnershipChangeListenerProxy(AppContextOwnershipChangeListener listener)152 public AppContextOwnershipChangeListenerProxy(AppContextOwnershipChangeListener listener) { 153 mListener = listener; 154 } 155 156 @Override onAppContextOwnershipLoss(int context)157 public void onAppContextOwnershipLoss(int context) { 158 mListener.onAppContextOwnershipLoss(context); 159 } 160 } 161 } 162