| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.selectiontoolbar; |
| 18 |
|
| 19 |
import static com.android.internal.util.function.pooled.PooledLambda.obtainMessage; |
| 20 |
|
| 21 |
import android.annotation.CallSuper; |
| 22 |
import android.annotation.NonNull; |
| 23 |
import android.annotation.Nullable; |
| 24 |
import android.app.Service; |
| 25 |
import android.content.Intent; |
| 26 |
import android.os.Handler; |
| 27 |
import android.os.IBinder; |
| 28 |
import android.os.Looper; |
| 29 |
import android.os.RemoteException; |
| 30 |
import android.util.Log; |
| 31 |
import android.util.Pair; |
| 32 |
import android.util.SparseArray; |
| 33 |
import android.view.selectiontoolbar.ISelectionToolbarCallback; |
| 34 |
import android.view.selectiontoolbar.ShowInfo; |
| 35 |
import android.view.selectiontoolbar.ToolbarMenuItem; |
| 36 |
import android.view.selectiontoolbar.WidgetInfo; |
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
public abstract class SelectionToolbarRenderService extends Service { |
| 44 |
|
| 45 |
private static final String TAG = "SelectionToolbarRenderService"; |
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
private static final int CACHE_CLEAN_AFTER_SHOW_TIMEOUT_IN_MS = 10 * 60 * 1000; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
public static final String SERVICE_INTERFACE = |
| 59 |
"android.service.selectiontoolbar.SelectionToolbarRenderService"; |
| 60 |
|
| 61 |
private Handler mHandler; |
| 62 |
private ISelectionToolbarRenderServiceCallback mServiceCallback; |
| 63 |
|
| 64 |
private final SparseArray<Pair<RemoteCallbackWrapper, CleanCacheRunnable>> mCache = |
| 65 |
new SparseArray<>(); |
| 66 |
|
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
private final ISelectionToolbarRenderService mInterface = |
| 71 |
new ISelectionToolbarRenderService.Stub() { |
| 72 |
|
| 73 |
@Override |
| 74 |
public void onShow(int callingUid, ShowInfo showInfo, ISelectionToolbarCallback callback) { |
| 75 |
if (mCache.indexOfKey(callingUid) < 0) { |
| 76 |
mCache.put(callingUid, new Pair<>(new RemoteCallbackWrapper(callback), |
| 77 |
new CleanCacheRunnable(callingUid))); |
| 78 |
} |
| 79 |
Pair<RemoteCallbackWrapper, CleanCacheRunnable> toolbarPair = mCache.get(callingUid); |
| 80 |
CleanCacheRunnable cleanRunnable = toolbarPair.second; |
| 81 |
mHandler.removeCallbacks(cleanRunnable); |
| 82 |
mHandler.sendMessage(obtainMessage(SelectionToolbarRenderService::onShow, |
| 83 |
SelectionToolbarRenderService.this, callingUid, showInfo, |
| 84 |
toolbarPair.first)); |
| 85 |
mHandler.postDelayed(cleanRunnable, CACHE_CLEAN_AFTER_SHOW_TIMEOUT_IN_MS); |
| 86 |
} |
| 87 |
|
| 88 |
@Override |
| 89 |
public void onHide(long widgetToken) { |
| 90 |
mHandler.sendMessage(obtainMessage(SelectionToolbarRenderService::onHide, |
| 91 |
SelectionToolbarRenderService.this, widgetToken)); |
| 92 |
} |
| 93 |
|
| 94 |
@Override |
| 95 |
public void onDismiss(int callingUid, long widgetToken) { |
| 96 |
mHandler.sendMessage(obtainMessage(SelectionToolbarRenderService::onDismiss, |
| 97 |
SelectionToolbarRenderService.this, widgetToken)); |
| 98 |
Pair<RemoteCallbackWrapper, CleanCacheRunnable> toolbarPair = mCache.get(callingUid); |
| 99 |
if (toolbarPair != null) { |
| 100 |
mHandler.removeCallbacks(toolbarPair.second); |
| 101 |
mCache.remove(callingUid); |
| 102 |
} |
| 103 |
} |
| 104 |
|
| 105 |
@Override |
| 106 |
public void onConnected(IBinder callback) { |
| 107 |
mHandler.sendMessage(obtainMessage(SelectionToolbarRenderService::handleOnConnected, |
| 108 |
SelectionToolbarRenderService.this, callback)); |
| 109 |
} |
| 110 |
}; |
| 111 |
|
| 112 |
@CallSuper |
| 113 |
@Override |
| 114 |
public void onCreate() { |
| 115 |
super.onCreate(); |
| 116 |
mHandler = new Handler(Looper.getMainLooper(), null, true); |
| 117 |
} |
| 118 |
|
| 119 |
@Override |
| 120 |
@Nullable |
| 121 |
public final IBinder onBind(@NonNull Intent intent) { |
| 122 |
if (SERVICE_INTERFACE.equals(intent.getAction())) { |
| 123 |
return mInterface.asBinder(); |
| 124 |
} |
| 125 |
Log.w(TAG, "Tried to bind to wrong intent (should be " + SERVICE_INTERFACE + ": " + intent); |
| 126 |
return null; |
| 127 |
} |
| 128 |
|
| 129 |
private void handleOnConnected(@NonNull IBinder callback) { |
| 130 |
mServiceCallback = ISelectionToolbarRenderServiceCallback.Stub.asInterface(callback); |
| 131 |
} |
| 132 |
|
| 133 |
protected void transferTouch(@NonNull IBinder source, @NonNull IBinder target) { |
| 134 |
final ISelectionToolbarRenderServiceCallback callback = mServiceCallback; |
| 135 |
if (callback == null) { |
| 136 |
Log.e(TAG, "transferTouch(): no server callback"); |
| 137 |
return; |
| 138 |
} |
| 139 |
try { |
| 140 |
callback.transferTouch(source, target); |
| 141 |
} catch (RemoteException e) { |
| 142 |
|
| 143 |
} |
| 144 |
} |
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
public abstract void onShow(int callingUid, ShowInfo showInfo, |
| 150 |
RemoteCallbackWrapper callbackWrapper); |
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
public abstract void onHide(long widgetToken); |
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
|
| 160 |
|
| 161 |
public abstract void onDismiss(long widgetToken); |
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
|
| 167 |
public void onToolbarShowTimeout(int callingUid) { |
| 168 |
|
| 169 |
} |
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
|
| 174 |
public static final class RemoteCallbackWrapper implements SelectionToolbarRenderCallback { |
| 175 |
|
| 176 |
private final ISelectionToolbarCallback mRemoteCallback; |
| 177 |
|
| 178 |
RemoteCallbackWrapper(ISelectionToolbarCallback remoteCallback) { |
| 179 |
|
| 180 |
mRemoteCallback = remoteCallback; |
| 181 |
} |
| 182 |
|
| 183 |
@Override |
| 184 |
public void onShown(WidgetInfo widgetInfo) { |
| 185 |
try { |
| 186 |
mRemoteCallback.onShown(widgetInfo); |
| 187 |
} catch (RemoteException e) { |
| 188 |
|
| 189 |
} |
| 190 |
} |
| 191 |
|
| 192 |
@Override |
| 193 |
public void onToolbarShowTimeout() { |
| 194 |
try { |
| 195 |
mRemoteCallback.onToolbarShowTimeout(); |
| 196 |
} catch (RemoteException e) { |
| 197 |
|
| 198 |
} |
| 199 |
} |
| 200 |
|
| 201 |
@Override |
| 202 |
public void onWidgetUpdated(WidgetInfo widgetInfo) { |
| 203 |
try { |
| 204 |
mRemoteCallback.onWidgetUpdated(widgetInfo); |
| 205 |
} catch (RemoteException e) { |
| 206 |
|
| 207 |
} |
| 208 |
} |
| 209 |
|
| 210 |
@Override |
| 211 |
public void onMenuItemClicked(ToolbarMenuItem item) { |
| 212 |
try { |
| 213 |
mRemoteCallback.onMenuItemClicked(item); |
| 214 |
} catch (RemoteException e) { |
| 215 |
|
| 216 |
} |
| 217 |
} |
| 218 |
|
| 219 |
@Override |
| 220 |
public void onError(int errorCode) { |
| 221 |
try { |
| 222 |
mRemoteCallback.onError(errorCode); |
| 223 |
} catch (RemoteException e) { |
| 224 |
|
| 225 |
} |
| 226 |
} |
| 227 |
} |
| 228 |
|
| 229 |
private class CleanCacheRunnable implements Runnable { |
| 230 |
|
| 231 |
int mCleanUid; |
| 232 |
|
| 233 |
CleanCacheRunnable(int cleanUid) { |
| 234 |
mCleanUid = cleanUid; |
| 235 |
} |
| 236 |
|
| 237 |
@Override |
| 238 |
public void run() { |
| 239 |
Pair<RemoteCallbackWrapper, CleanCacheRunnable> toolbarPair = mCache.get(mCleanUid); |
| 240 |
if (toolbarPair != null) { |
| 241 |
Log.w(TAG, "CleanCacheRunnable: remove " + mCleanUid + " from cache."); |
| 242 |
mCache.remove(mCleanUid); |
| 243 |
onToolbarShowTimeout(mCleanUid); |
| 244 |
} |
| 245 |
} |
| 246 |
} |
| 247 |
|
| 248 |
|
| 249 |
|
| 250 |
|
| 251 |
public interface TransferTouchListener { |
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
void onTransferTouch(IBinder source, IBinder target); |
| 256 |
} |
| 257 |
} |