2 * Copyright (C) 2015 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.internal.app;
19 import android.annotation.SystemApi;
20 import android.app.Service;
21 import android.content.Context;
22 import android.content.Intent;
23 import android.content.pm.EphemeralResolveInfo;
24 import android.os.Bundle;
25 import android.os.Handler;
26 import android.os.IBinder;
27 import android.os.IRemoteCallback;
28 import android.os.Looper;
29 import android.os.Message;
30 import android.os.RemoteException;
32 import java.util.List;
35 * Base class for implementing the resolver service.
39 public abstract class EphemeralResolverService extends Service {
40 public static final String EXTRA_RESOLVE_INFO = "com.android.internal.app.RESOLVE_INFO";
41 public static final String EXTRA_SEQUENCE = "com.android.internal.app.SEQUENCE";
42 private static final String EXTRA_PREFIX = "com.android.internal.app.PREFIX";
43 private Handler mHandler;
46 * Called to retrieve resolve info for ephemeral applications.
48 * @param digestPrefix The hash prefix of the ephemeral's domain.
49 * @param prefixMask A mask that was applied to each digest prefix. This should
50 * be used when comparing against the digest prefixes as all bits might
53 protected abstract List<EphemeralResolveInfo> getEphemeralResolveInfoList(
54 int digestPrefix[], int prefixMask);
57 protected final void attachBaseContext(Context base) {
58 super.attachBaseContext(base);
59 mHandler = new ServiceHandler(base.getMainLooper());
63 public final IBinder onBind(Intent intent) {
64 return new IEphemeralResolver.Stub() {
66 public void getEphemeralResolveInfoList(
67 IRemoteCallback callback, int digestPrefix[], int prefixMask, int sequence) {
68 final Message msg = mHandler.obtainMessage(
69 ServiceHandler.MSG_GET_EPHEMERAL_RESOLVE_INFO, prefixMask, sequence, callback);
70 final Bundle data = new Bundle();
71 data.putIntArray(EXTRA_PREFIX, digestPrefix);
78 private final class ServiceHandler extends Handler {
79 public static final int MSG_GET_EPHEMERAL_RESOLVE_INFO = 1;
81 public ServiceHandler(Looper looper) {
82 super(looper, null /*callback*/, true /*async*/);
86 @SuppressWarnings("unchecked")
87 public void handleMessage(Message message) {
88 final int action = message.what;
90 case MSG_GET_EPHEMERAL_RESOLVE_INFO: {
91 final IRemoteCallback callback = (IRemoteCallback) message.obj;
92 final int[] digestPrefix = message.getData().getIntArray(EXTRA_PREFIX);
93 final List<EphemeralResolveInfo> resolveInfo =
94 getEphemeralResolveInfoList(digestPrefix, message.arg1);
95 final Bundle data = new Bundle();
96 data.putInt(EXTRA_SEQUENCE, message.arg2);
97 data.putParcelableList(EXTRA_RESOLVE_INFO, resolveInfo);
99 callback.sendResult(data);
100 } catch (RemoteException e) {
105 throw new IllegalArgumentException("Unknown message: " + action);