| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.controls.actions; |
| 18 |
|
| 19 |
import android.annotation.CallSuper; |
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.os.Bundle; |
| 24 |
import android.service.controls.Control; |
| 25 |
import android.service.controls.ControlsProviderService; |
| 26 |
import android.service.controls.templates.ControlTemplate; |
| 27 |
import android.util.Log; |
| 28 |
|
| 29 |
import com.android.internal.util.Preconditions; |
| 30 |
|
| 31 |
import java.lang.annotation.Retention; |
| 32 |
import java.lang.annotation.RetentionPolicy; |
| 33 |
|
| 34 |
|
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
|
| 39 |
|
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
|
| 46 |
|
| 47 |
public abstract class ControlAction { |
| 48 |
|
| 49 |
private static final String TAG = "ControlAction"; |
| 50 |
|
| 51 |
private static final String KEY_ACTION_TYPE = "key_action_type"; |
| 52 |
private static final String KEY_TEMPLATE_ID = "key_template_id"; |
| 53 |
private static final String KEY_CHALLENGE_VALUE = "key_challenge_value"; |
| 54 |
|
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
@Retention(RetentionPolicy.SOURCE) |
| 59 |
@IntDef({ |
| 60 |
TYPE_ERROR, |
| 61 |
TYPE_BOOLEAN, |
| 62 |
TYPE_FLOAT, |
| 63 |
TYPE_MODE, |
| 64 |
TYPE_COMMAND |
| 65 |
}) |
| 66 |
public @interface ActionType {}; |
| 67 |
|
| 68 |
|
| 69 |
|
| 70 |
|
| 71 |
|
| 72 |
public static final @NonNull ControlAction ERROR_ACTION = new ControlAction() { |
| 73 |
@Override |
| 74 |
public int getActionType() { |
| 75 |
return TYPE_ERROR; |
| 76 |
} |
| 77 |
}; |
| 78 |
|
| 79 |
|
| 80 |
|
| 81 |
|
| 82 |
public static final @ActionType int TYPE_ERROR = -1; |
| 83 |
|
| 84 |
|
| 85 |
|
| 86 |
|
| 87 |
public static final @ActionType int TYPE_BOOLEAN = 1; |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static final @ActionType int TYPE_FLOAT = 2; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static final @ActionType int TYPE_MODE = 4; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static final @ActionType int TYPE_COMMAND = 5; |
| 103 |
|
| 104 |
|
| 105 |
public static final boolean isValidResponse(@ResponseResult int response) { |
| 106 |
return (response >= 0 && response < NUM_RESPONSE_TYPES); |
| 107 |
} |
| 108 |
private static final int NUM_RESPONSE_TYPES = 6; |
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
@Retention(RetentionPolicy.SOURCE) |
| 113 |
@IntDef({ |
| 114 |
RESPONSE_UNKNOWN, |
| 115 |
RESPONSE_OK, |
| 116 |
RESPONSE_FAIL, |
| 117 |
RESPONSE_CHALLENGE_ACK, |
| 118 |
RESPONSE_CHALLENGE_PIN, |
| 119 |
RESPONSE_CHALLENGE_PASSPHRASE |
| 120 |
}) |
| 121 |
public @interface ResponseResult {}; |
| 122 |
|
| 123 |
public static final @ResponseResult int RESPONSE_UNKNOWN = 0; |
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
|
| 128 |
|
| 129 |
|
| 130 |
public static final @ResponseResult int RESPONSE_OK = 1; |
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
|
| 135 |
public static final @ResponseResult int RESPONSE_FAIL = 2; |
| 136 |
|
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public static final @ResponseResult int RESPONSE_CHALLENGE_ACK = 3; |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
public static final @ResponseResult int RESPONSE_CHALLENGE_PIN = 4; |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
public static final @ResponseResult int RESPONSE_CHALLENGE_PASSPHRASE = 5; |
| 155 |
|
| 156 |
|
| 157 |
|
| 158 |
|
| 159 |
public abstract @ActionType int getActionType(); |
| 160 |
|
| 161 |
private final @NonNull String mTemplateId; |
| 162 |
private final @Nullable String mChallengeValue; |
| 163 |
|
| 164 |
private ControlAction() { |
| 165 |
mTemplateId = ""; |
| 166 |
mChallengeValue = null; |
| 167 |
} |
| 168 |
|
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
ControlAction(@NonNull String templateId, @Nullable String challengeValue) { |
| 173 |
Preconditions.checkNotNull(templateId); |
| 174 |
mTemplateId = templateId; |
| 175 |
mChallengeValue = challengeValue; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
ControlAction(Bundle b) { |
| 182 |
mTemplateId = b.getString(KEY_TEMPLATE_ID); |
| 183 |
mChallengeValue = b.getString(KEY_CHALLENGE_VALUE); |
| 184 |
} |
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
@NonNull |
| 190 |
public String getTemplateId() { |
| 191 |
return mTemplateId; |
| 192 |
} |
| 193 |
|
| 194 |
|
| 195 |
|
| 196 |
|
| 197 |
@Nullable |
| 198 |
public String getChallengeValue() { |
| 199 |
return mChallengeValue; |
| 200 |
} |
| 201 |
|
| 202 |
|
| 203 |
|
| 204 |
|
| 205 |
|
| 206 |
|
| 207 |
|
| 208 |
|
| 209 |
|
| 210 |
@CallSuper |
| 211 |
@NonNull |
| 212 |
Bundle getDataBundle() { |
| 213 |
Bundle b = new Bundle(); |
| 214 |
b.putInt(KEY_ACTION_TYPE, getActionType()); |
| 215 |
b.putString(KEY_TEMPLATE_ID, mTemplateId); |
| 216 |
b.putString(KEY_CHALLENGE_VALUE, mChallengeValue); |
| 217 |
return b; |
| 218 |
} |
| 219 |
|
| 220 |
|
| 221 |
|
| 222 |
|
| 223 |
|
| 224 |
|
| 225 |
@NonNull |
| 226 |
static ControlAction createActionFromBundle(@NonNull Bundle bundle) { |
| 227 |
if (bundle == null) { |
| 228 |
Log.e(TAG, "Null bundle"); |
| 229 |
return ERROR_ACTION; |
| 230 |
} |
| 231 |
int type = bundle.getInt(KEY_ACTION_TYPE, TYPE_ERROR); |
| 232 |
try { |
| 233 |
switch (type) { |
| 234 |
case TYPE_BOOLEAN: |
| 235 |
return new BooleanAction(bundle); |
| 236 |
case TYPE_FLOAT: |
| 237 |
return new FloatAction(bundle); |
| 238 |
case TYPE_MODE: |
| 239 |
return new ModeAction(bundle); |
| 240 |
case TYPE_COMMAND: |
| 241 |
return new CommandAction(bundle); |
| 242 |
case TYPE_ERROR: |
| 243 |
default: |
| 244 |
return ERROR_ACTION; |
| 245 |
} |
| 246 |
} catch (Exception e) { |
| 247 |
Log.e(TAG, "Error creating action", e); |
| 248 |
return ERROR_ACTION; |
| 249 |
} |
| 250 |
} |
| 251 |
|
| 252 |
|
| 253 |
|
| 254 |
|
| 255 |
@NonNull |
| 256 |
public static ControlAction getErrorAction() { |
| 257 |
return ERROR_ACTION; |
| 258 |
} |
| 259 |
} |