| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.attention; |
| 18 |
|
| 19 |
import android.annotation.IntDef; |
| 20 |
import android.annotation.NonNull; |
| 21 |
import android.annotation.Nullable; |
| 22 |
import android.annotation.SystemApi; |
| 23 |
import android.app.Service; |
| 24 |
import android.content.Intent; |
| 25 |
import android.os.IBinder; |
| 26 |
import android.os.RemoteException; |
| 27 |
import android.util.Slog; |
| 28 |
|
| 29 |
import com.android.internal.util.Preconditions; |
| 30 |
|
| 31 |
import java.lang.annotation.Retention; |
| 32 |
import java.lang.annotation.RetentionPolicy; |
| 33 |
import java.lang.ref.WeakReference; |
| 34 |
import java.util.Objects; |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
|
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
@SystemApi |
| 56 |
public abstract class AttentionService extends Service { |
| 57 |
private static final String LOG_TAG = "AttentionService"; |
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
public static final String SERVICE_INTERFACE = |
| 64 |
"android.service.attention.AttentionService"; |
| 65 |
|
| 66 |
|
| 67 |
public static final int ATTENTION_SUCCESS_ABSENT = 0; |
| 68 |
|
| 69 |
|
| 70 |
public static final int ATTENTION_SUCCESS_PRESENT = 1; |
| 71 |
|
| 72 |
|
| 73 |
public static final int ATTENTION_FAILURE_UNKNOWN = 2; |
| 74 |
|
| 75 |
|
| 76 |
public static final int ATTENTION_FAILURE_CANCELLED = 3; |
| 77 |
|
| 78 |
|
| 79 |
public static final int ATTENTION_FAILURE_PREEMPTED = 4; |
| 80 |
|
| 81 |
|
| 82 |
public static final int ATTENTION_FAILURE_TIMED_OUT = 5; |
| 83 |
|
| 84 |
|
| 85 |
public static final int ATTENTION_FAILURE_CAMERA_PERMISSION_ABSENT = 6; |
| 86 |
|
| 87 |
*/ |
| 88 |
public static final double PROXIMITY_UNKNOWN = -1; |
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
@IntDef(prefix = {"ATTENTION_SUCCESS_"}, value = {ATTENTION_SUCCESS_ABSENT, |
| 96 |
ATTENTION_SUCCESS_PRESENT}) |
| 97 |
@Retention(RetentionPolicy.SOURCE) |
| 98 |
public @interface AttentionSuccessCodes { |
| 99 |
} |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
@IntDef(prefix = {"ATTENTION_FAILURE_"}, value = {ATTENTION_FAILURE_UNKNOWN, |
| 107 |
ATTENTION_FAILURE_CANCELLED, ATTENTION_FAILURE_PREEMPTED, ATTENTION_FAILURE_TIMED_OUT, |
| 108 |
ATTENTION_FAILURE_CAMERA_PERMISSION_ABSENT}) |
| 109 |
@Retention(RetentionPolicy.SOURCE) |
| 110 |
public @interface AttentionFailureCodes { |
| 111 |
} |
| 112 |
|
| 113 |
private final IAttentionService.Stub mBinder = new IAttentionService.Stub() { |
| 114 |
|
| 115 |
|
| 116 |
@Override |
| 117 |
public void checkAttention(IAttentionCallback callback) { |
| 118 |
Preconditions.checkNotNull(callback); |
| 119 |
AttentionService.this.onCheckAttention(new AttentionCallback(callback)); |
| 120 |
} |
| 121 |
|
| 122 |
|
| 123 |
@Override |
| 124 |
public void cancelAttentionCheck(IAttentionCallback callback) { |
| 125 |
Preconditions.checkNotNull(callback); |
| 126 |
AttentionService.this.onCancelAttentionCheck(new AttentionCallback(callback)); |
| 127 |
} |
| 128 |
|
| 129 |
|
| 130 |
@Override |
| 131 |
public void onStartProximityUpdates(IProximityUpdateCallback callback) { |
| 132 |
Objects.requireNonNull(callback); |
| 133 |
AttentionService.this.onStartProximityUpdates(new ProximityUpdateCallback(callback)); |
| 134 |
|
| 135 |
} |
| 136 |
|
| 137 |
|
| 138 |
@Override |
| 139 |
public void onStopProximityUpdates() { |
| 140 |
AttentionService.this.onStopProximityUpdates(); |
| 141 |
} |
| 142 |
}; |
| 143 |
|
| 144 |
@Nullable |
| 145 |
@Override |
| 146 |
public final IBinder onBind(@NonNull Intent intent) { |
| 147 |
if (SERVICE_INTERFACE.equals(intent.getAction())) { |
| 148 |
return mBinder; |
| 149 |
} |
| 150 |
return null; |
| 151 |
} |
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
public abstract void onCheckAttention(@NonNull AttentionCallback callback); |
| 159 |
|
| 160 |
|
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
public abstract void onCancelAttentionCheck(@NonNull AttentionCallback callback); |
| 166 |
|
| 167 |
|
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
public void onStartProximityUpdates(@NonNull ProximityUpdateCallback callback) { |
| 174 |
Slog.w(LOG_TAG, "Override this method."); |
| 175 |
} |
| 176 |
|
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
public void onStopProximityUpdates() { |
| 181 |
Slog.w(LOG_TAG, "Override this method."); |
| 182 |
} |
| 183 |
|
| 184 |
|
| 185 |
public static final class AttentionCallback { |
| 186 |
@NonNull private final IAttentionCallback mCallback; |
| 187 |
|
| 188 |
private AttentionCallback(@NonNull IAttentionCallback callback) { |
| 189 |
mCallback = callback; |
| 190 |
} |
| 191 |
|
| 192 |
|
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
|
| 198 |
public void onSuccess(@AttentionSuccessCodes int result, long timestamp) { |
| 199 |
try { |
| 200 |
mCallback.onSuccess(result, timestamp); |
| 201 |
} catch (RemoteException e) { |
| 202 |
e.rethrowFromSystemServer(); |
| 203 |
} |
| 204 |
} |
| 205 |
|
| 206 |
|
| 207 |
public void onFailure(@AttentionFailureCodes int error) { |
| 208 |
try { |
| 209 |
mCallback.onFailure(error); |
| 210 |
} catch (RemoteException e) { |
| 211 |
e.rethrowFromSystemServer(); |
| 212 |
} |
| 213 |
} |
| 214 |
} |
| 215 |
|
| 216 |
|
| 217 |
public static final class ProximityUpdateCallback { |
| 218 |
@NonNull private final WeakReference<IProximityUpdateCallback> mCallback; |
| 219 |
|
| 220 |
private ProximityUpdateCallback(@NonNull IProximityUpdateCallback callback) { |
| 221 |
mCallback = new WeakReference<>(callback); |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
|
| 228 |
|
| 229 |
public void onProximityUpdate(double distance) { |
| 230 |
try { |
| 231 |
if (mCallback.get() != null) { |
| 232 |
mCallback.get().onProximityUpdate(distance); |
| 233 |
} |
| 234 |
} catch (RemoteException e) { |
| 235 |
e.rethrowFromSystemServer(); |
| 236 |
} |
| 237 |
} |
| 238 |
} |
| 239 |
} |