| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
package android.service.controls.templates; |
| 18 |
|
| 19 |
import android.annotation.CallSuper; |
| 20 |
import android.annotation.IntDef; |
| 21 |
import android.annotation.NonNull; |
| 22 |
import android.annotation.Nullable; |
| 23 |
import android.content.Context; |
| 24 |
import android.os.Bundle; |
| 25 |
import android.service.controls.Control; |
| 26 |
import android.service.controls.actions.ControlAction; |
| 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 |
public abstract class ControlTemplate { |
| 45 |
|
| 46 |
private static final String TAG = "ControlTemplate"; |
| 47 |
|
| 48 |
private static final String KEY_TEMPLATE_ID = "key_template_id"; |
| 49 |
private static final String KEY_TEMPLATE_TYPE = "key_template_type"; |
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
|
| 54 |
|
| 55 |
public static final @NonNull ControlTemplate NO_TEMPLATE = new ControlTemplate("") { |
| 56 |
@Override |
| 57 |
public int getTemplateType() { |
| 58 |
return TYPE_NO_TEMPLATE; |
| 59 |
} |
| 60 |
}; |
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
private static final @NonNull ControlTemplate ERROR_TEMPLATE = new ControlTemplate("") { |
| 67 |
@Override |
| 68 |
public int getTemplateType() { |
| 69 |
return TYPE_ERROR; |
| 70 |
} |
| 71 |
}; |
| 72 |
|
| 73 |
|
| 74 |
|
| 75 |
|
| 76 |
@Retention(RetentionPolicy.SOURCE) |
| 77 |
@IntDef({ |
| 78 |
TYPE_ERROR, |
| 79 |
TYPE_NO_TEMPLATE, |
| 80 |
TYPE_TOGGLE, |
| 81 |
TYPE_RANGE, |
| 82 |
TYPE_THUMBNAIL, |
| 83 |
TYPE_TOGGLE_RANGE, |
| 84 |
TYPE_TEMPERATURE, |
| 85 |
TYPE_STATELESS |
| 86 |
}) |
| 87 |
public @interface TemplateType {} |
| 88 |
|
| 89 |
|
| 90 |
|
| 91 |
|
| 92 |
public static final @TemplateType int TYPE_ERROR = -1; |
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
public static final @TemplateType int TYPE_NO_TEMPLATE = 0; |
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
public static final @TemplateType int TYPE_TOGGLE = 1; |
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
public static final @TemplateType int TYPE_RANGE = 2; |
| 108 |
|
| 109 |
|
| 110 |
|
| 111 |
|
| 112 |
public static final @TemplateType int TYPE_THUMBNAIL = 3; |
| 113 |
|
| 114 |
|
| 115 |
|
| 116 |
|
| 117 |
public static final @TemplateType int TYPE_TOGGLE_RANGE = 6; |
| 118 |
|
| 119 |
|
| 120 |
|
| 121 |
|
| 122 |
public static final @TemplateType int TYPE_TEMPERATURE = 7; |
| 123 |
|
| 124 |
|
| 125 |
|
| 126 |
|
| 127 |
public static final @TemplateType int TYPE_STATELESS = 8; |
| 128 |
|
| 129 |
private @NonNull final String mTemplateId; |
| 130 |
|
| 131 |
|
| 132 |
|
| 133 |
|
| 134 |
@NonNull |
| 135 |
public String getTemplateId() { |
| 136 |
return mTemplateId; |
| 137 |
} |
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
public abstract @TemplateType int getTemplateType(); |
| 143 |
|
| 144 |
|
| 145 |
|
| 146 |
|
| 147 |
|
| 148 |
|
| 149 |
@CallSuper |
| 150 |
@NonNull |
| 151 |
Bundle getDataBundle() { |
| 152 |
Bundle b = new Bundle(); |
| 153 |
b.putInt(KEY_TEMPLATE_TYPE, getTemplateType()); |
| 154 |
b.putString(KEY_TEMPLATE_ID, mTemplateId); |
| 155 |
return b; |
| 156 |
} |
| 157 |
|
| 158 |
private ControlTemplate() { |
| 159 |
mTemplateId = ""; |
| 160 |
} |
| 161 |
|
| 162 |
|
| 163 |
|
| 164 |
|
| 165 |
|
| 166 |
ControlTemplate(@NonNull Bundle b) { |
| 167 |
mTemplateId = b.getString(KEY_TEMPLATE_ID); |
| 168 |
} |
| 169 |
|
| 170 |
|
| 171 |
|
| 172 |
|
| 173 |
ControlTemplate(@NonNull String templateId) { |
| 174 |
Preconditions.checkNotNull(templateId); |
| 175 |
mTemplateId = templateId; |
| 176 |
} |
| 177 |
|
| 178 |
|
| 179 |
|
| 180 |
|
| 181 |
|
| 182 |
|
| 183 |
public void prepareTemplateForBinder(@NonNull Context context) {} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
|
| 190 |
|
| 191 |
@NonNull |
| 192 |
static ControlTemplate createTemplateFromBundle(@Nullable Bundle bundle) { |
| 193 |
if (bundle == null) { |
| 194 |
Log.e(TAG, "Null bundle"); |
| 195 |
return ERROR_TEMPLATE; |
| 196 |
} |
| 197 |
int type = bundle.getInt(KEY_TEMPLATE_TYPE, TYPE_ERROR); |
| 198 |
try { |
| 199 |
switch (type) { |
| 200 |
case TYPE_TOGGLE: |
| 201 |
return new ToggleTemplate(bundle); |
| 202 |
case TYPE_RANGE: |
| 203 |
return new RangeTemplate(bundle); |
| 204 |
case TYPE_THUMBNAIL: |
| 205 |
return new ThumbnailTemplate(bundle); |
| 206 |
case TYPE_TOGGLE_RANGE: |
| 207 |
return new ToggleRangeTemplate(bundle); |
| 208 |
case TYPE_TEMPERATURE: |
| 209 |
return new TemperatureControlTemplate(bundle); |
| 210 |
case TYPE_STATELESS: |
| 211 |
return new StatelessTemplate(bundle); |
| 212 |
case TYPE_NO_TEMPLATE: |
| 213 |
return NO_TEMPLATE; |
| 214 |
case TYPE_ERROR: |
| 215 |
default: |
| 216 |
return ERROR_TEMPLATE; |
| 217 |
} |
| 218 |
} catch (Exception e) { |
| 219 |
Log.e(TAG, "Error creating template", e); |
| 220 |
return ERROR_TEMPLATE; |
| 221 |
} |
| 222 |
} |
| 223 |
|
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
@NonNull |
| 228 |
public static ControlTemplate getErrorTemplate() { |
| 229 |
return ERROR_TEMPLATE; |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
|
| 238 |
|
| 239 |
|
| 240 |
|
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
@NonNull |
| 245 |
public static ControlTemplate getNoTemplateObject() { |
| 246 |
return NO_TEMPLATE; |
| 247 |
} |
| 248 |
|
| 249 |
} |