| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
package android.app.smartspace; |
| 17 |
|
| 18 |
import android.annotation.CallbackExecutor; |
| 19 |
import android.annotation.NonNull; |
| 20 |
import android.annotation.SystemApi; |
| 21 |
import android.app.smartspace.ISmartspaceCallback.Stub; |
| 22 |
import android.content.Context; |
| 23 |
import android.content.pm.ParceledListSlice; |
| 24 |
import android.os.Binder; |
| 25 |
import android.os.IBinder; |
| 26 |
import android.os.RemoteException; |
| 27 |
import android.os.ServiceManager; |
| 28 |
import android.util.ArrayMap; |
| 29 |
import android.util.Log; |
| 30 |
|
| 31 |
import dalvik.system.CloseGuard; |
| 32 |
|
| 33 |
import java.util.List; |
| 34 |
import java.util.UUID; |
| 35 |
import java.util.concurrent.Executor; |
| 36 |
import java.util.concurrent.atomic.AtomicBoolean; |
| 37 |
import java.util.function.Consumer; |
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
|
| 73 |
@SystemApi |
| 74 |
public final class SmartspaceSession implements AutoCloseable { |
| 75 |
|
| 76 |
private static final String TAG = SmartspaceSession.class.getSimpleName(); |
| 77 |
private static final boolean DEBUG = false; |
| 78 |
|
| 79 |
private final android.app.smartspace.ISmartspaceManager mInterface; |
| 80 |
private final CloseGuard mCloseGuard = CloseGuard.get(); |
| 81 |
private final AtomicBoolean mIsClosed = new AtomicBoolean(false); |
| 82 |
|
| 83 |
private final SmartspaceSessionId mSessionId; |
| 84 |
private final ArrayMap<OnTargetsAvailableListener, CallbackWrapper> mRegisteredCallbacks = |
| 85 |
new ArrayMap<>(); |
| 86 |
private final IBinder mToken = new Binder(); |
| 87 |
|
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
SmartspaceSession(@NonNull Context context, @NonNull SmartspaceConfig smartspaceConfig) { |
| 99 |
IBinder b = ServiceManager.getService(Context.SMARTSPACE_SERVICE); |
| 100 |
mInterface = android.app.smartspace.ISmartspaceManager.Stub.asInterface(b); |
| 101 |
mSessionId = new SmartspaceSessionId( |
| 102 |
context.getPackageName() + ":" + UUID.randomUUID().toString(), context.getUser()); |
| 103 |
try { |
| 104 |
mInterface.createSmartspaceSession(smartspaceConfig, mSessionId, mToken); |
| 105 |
} catch (RemoteException e) { |
| 106 |
Log.e(TAG, "Failed to create Smartspace session", e); |
| 107 |
e.rethrowFromSystemServer(); |
| 108 |
} |
| 109 |
|
| 110 |
mCloseGuard.open("SmartspaceSession.close"); |
| 111 |
} |
| 112 |
|
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
public void notifySmartspaceEvent(@NonNull SmartspaceTargetEvent event) { |
| 119 |
if (mIsClosed.get()) { |
| 120 |
throw new IllegalStateException("This client has already been destroyed."); |
| 121 |
} |
| 122 |
try { |
| 123 |
mInterface.notifySmartspaceEvent(mSessionId, event); |
| 124 |
} catch (RemoteException e) { |
| 125 |
Log.e(TAG, "Failed to notify event", e); |
| 126 |
e.rethrowFromSystemServer(); |
| 127 |
} |
| 128 |
} |
| 129 |
|
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
public void requestSmartspaceUpdate() { |
| 134 |
if (mIsClosed.get()) { |
| 135 |
throw new IllegalStateException("This client has already been destroyed."); |
| 136 |
} |
| 137 |
try { |
| 138 |
mInterface.requestSmartspaceUpdate(mSessionId); |
| 139 |
} catch (RemoteException e) { |
| 140 |
Log.e(TAG, "Failed to request update.", e); |
| 141 |
e.rethrowFromSystemServer(); |
| 142 |
} |
| 143 |
} |
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
public void addOnTargetsAvailableListener(@NonNull @CallbackExecutor Executor listenerExecutor, |
| 154 |
@NonNull OnTargetsAvailableListener listener) { |
| 155 |
if (mIsClosed.get()) { |
| 156 |
throw new IllegalStateException("This client has already been destroyed."); |
| 157 |
} |
| 158 |
|
| 159 |
if (mRegisteredCallbacks.containsKey(listener)) { |
| 160 |
|
| 161 |
return; |
| 162 |
} |
| 163 |
try { |
| 164 |
final CallbackWrapper callbackWrapper = new CallbackWrapper(listenerExecutor, |
| 165 |
listener::onTargetsAvailable); |
| 166 |
mRegisteredCallbacks.put(listener, callbackWrapper); |
| 167 |
mInterface.registerSmartspaceUpdates(mSessionId, callbackWrapper); |
| 168 |
mInterface.requestSmartspaceUpdate(mSessionId); |
| 169 |
} catch (RemoteException e) { |
| 170 |
Log.e(TAG, "Failed to register for smartspace updates", e); |
| 171 |
e.rethrowAsRuntimeException(); |
| 172 |
} |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public void removeOnTargetsAvailableListener(@NonNull OnTargetsAvailableListener listener) { |
| 184 |
if (mIsClosed.get()) { |
| 185 |
throw new IllegalStateException("This client has already been destroyed."); |
| 186 |
} |
| 187 |
|
| 188 |
if (!mRegisteredCallbacks.containsKey(listener)) { |
| 189 |
|
| 190 |
return; |
| 191 |
} |
| 192 |
try { |
| 193 |
final CallbackWrapper callbackWrapper = mRegisteredCallbacks.remove(listener); |
| 194 |
mInterface.unregisterSmartspaceUpdates(mSessionId, callbackWrapper); |
| 195 |
} catch (RemoteException e) { |
| 196 |
Log.e(TAG, "Failed to unregister for smartspace updates", e); |
| 197 |
e.rethrowAsRuntimeException(); |
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
private void destroy() { |
| 206 |
if (!mIsClosed.getAndSet(true)) { |
| 207 |
mCloseGuard.close(); |
| 208 |
|
| 209 |
|
| 210 |
try { |
| 211 |
mInterface.destroySmartspaceSession(mSessionId); |
| 212 |
} catch (RemoteException e) { |
| 213 |
Log.e(TAG, "Failed to notify Smartspace target event", e); |
| 214 |
e.rethrowFromSystemServer(); |
| 215 |
} |
| 216 |
} else { |
| 217 |
throw new IllegalStateException("This client has already been destroyed."); |
| 218 |
} |
| 219 |
} |
| 220 |
|
| 221 |
@Override |
| 222 |
protected void finalize() { |
| 223 |
try { |
| 224 |
if (mCloseGuard != null) { |
| 225 |
mCloseGuard.warnIfOpen(); |
| 226 |
} |
| 227 |
if (!mIsClosed.get()) { |
| 228 |
destroy(); |
| 229 |
} |
| 230 |
} finally { |
| 231 |
try { |
| 232 |
super.finalize(); |
| 233 |
} catch (Throwable throwable) { |
| 234 |
throwable.printStackTrace(); |
| 235 |
} |
| 236 |
} |
| 237 |
} |
| 238 |
|
| 239 |
@Override |
| 240 |
public void close() { |
| 241 |
try { |
| 242 |
destroy(); |
| 243 |
finalize(); |
| 244 |
} catch (Throwable throwable) { |
| 245 |
throwable.printStackTrace(); |
| 246 |
} |
| 247 |
} |
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
|
| 252 |
public interface OnTargetsAvailableListener { |
| 253 |
|
| 254 |
|
| 255 |
|
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
void onTargetsAvailable(@NonNull List<SmartspaceTarget> targets); |
| 260 |
} |
| 261 |
|
| 262 |
static class CallbackWrapper extends Stub { |
| 263 |
|
| 264 |
private final Consumer<List<SmartspaceTarget>> mCallback; |
| 265 |
private final Executor mExecutor; |
| 266 |
|
| 267 |
CallbackWrapper(@NonNull Executor callbackExecutor, |
| 268 |
@NonNull Consumer<List<SmartspaceTarget>> callback) { |
| 269 |
mCallback = callback; |
| 270 |
mExecutor = callbackExecutor; |
| 271 |
} |
| 272 |
|
| 273 |
@Override |
| 274 |
public void onResult(ParceledListSlice result) { |
| 275 |
final long identity = Binder.clearCallingIdentity(); |
| 276 |
try { |
| 277 |
if (DEBUG) { |
| 278 |
Log.d(TAG, "CallbackWrapper.onResult result=" + result.getList()); |
| 279 |
} |
| 280 |
mExecutor.execute(() -> mCallback.accept(result.getList())); |
| 281 |
} finally { |
| 282 |
Binder.restoreCallingIdentity(identity); |
| 283 |
} |
| 284 |
} |
| 285 |
} |
| 286 |
} |