1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
|
/*
* Copyright (C) 2007 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package android.widget;
import android.content.Context;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Parcel;
import android.os.Parcelable;
import android.text.TextUtils;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.LayoutInflater.Filter;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.util.ArrayList;
/**
* A class that describes a view hierarchy that can be displayed in
* another process. The hierarchy is inflated from a layout resource
* file, and this class provides some basic operations for modifying
* the content of the inflated hierarchy.
*/
public class RemoteViews implements Parcelable, Filter {
private static final String LOG_TAG = "RemoteViews";
/**
* The package name of the package containing the layout
* resource. (Added to the parcel)
*/
private String mPackage;
/**
* The resource ID of the layout file. (Added to the parcel)
*/
private int mLayoutId;
/**
* The Context object used to inflate the layout file. Also may
* be used by actions if they need access to the senders resources.
*/
private Context mContext;
/**
* An array of actions to perform on the view tree once it has been
* inflated
*/
private ArrayList<Action> mActions;
/**
* This annotation indicates that a subclass of View is alllowed to be used with the
* {@link android.widget.RemoteViews} mechanism.
*/
@Target({ ElementType.TYPE })
@Retention(RetentionPolicy.RUNTIME)
public @interface RemoteView {
}
/**
* Exception to send when something goes wrong executing an action
*
*/
public static class ActionException extends RuntimeException {
public ActionException(String message) {
super(message);
}
}
/**
* Base class for all actions that can be performed on an
* inflated view.
*
*/
private abstract static class Action implements Parcelable {
public abstract void apply(View root) throws ActionException;
public int describeContents() {
return 0;
}
};
/**
* Equivalent to calling View.setVisibility
*/
private class SetViewVisibility extends Action {
public SetViewVisibility(int id, int vis) {
viewId = id;
visibility = vis;
}
public SetViewVisibility(Parcel parcel) {
viewId = parcel.readInt();
visibility = parcel.readInt();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
dest.writeInt(visibility);
}
@Override
public void apply(View root) {
View target = root.findViewById(viewId);
if (target != null) {
target.setVisibility(visibility);
}
}
private int viewId;
private int visibility;
public final static int TAG = 0;
}
/**
* Equivalent to calling TextView.setText
*/
private class SetTextViewText extends Action {
public SetTextViewText(int id, CharSequence t) {
viewId = id;
text = t;
}
public SetTextViewText(Parcel parcel) {
viewId = parcel.readInt();
text = TextUtils.CHAR_SEQUENCE_CREATOR.createFromParcel(parcel);
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
TextUtils.writeToParcel(text, dest, flags);
}
@Override
public void apply(View root) {
TextView target = (TextView) root.findViewById(viewId);
if (target != null) {
target.setText(text);
}
}
int viewId;
CharSequence text;
public final static int TAG = 1;
}
/**
* Equivalent to calling ImageView.setResource
*/
private class SetImageViewResource extends Action {
public SetImageViewResource(int id, int src) {
viewId = id;
srcId = src;
}
public SetImageViewResource(Parcel parcel) {
viewId = parcel.readInt();
srcId = parcel.readInt();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
dest.writeInt(srcId);
}
@Override
public void apply(View root) {
ImageView target = (ImageView) root.findViewById(viewId);
Drawable d = mContext.getResources().getDrawable(srcId);
if (target != null) {
target.setImageDrawable(d);
}
}
int viewId;
int srcId;
public final static int TAG = 2;
}
/**
* Equivalent to calling ImageView.setImageURI
*/
private class SetImageViewUri extends Action {
public SetImageViewUri(int id, Uri u) {
viewId = id;
uri = u;
}
public SetImageViewUri(Parcel parcel) {
viewId = parcel.readInt();
uri = Uri.CREATOR.createFromParcel(parcel);
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
Uri.writeToParcel(dest, uri);
}
@Override
public void apply(View root) {
ImageView target = (ImageView) root.findViewById(viewId);
if (target != null) {
target.setImageURI(uri);
}
}
int viewId;
Uri uri;
public final static int TAG = 3;
}
/**
* Equivalent to calling ImageView.setImageBitmap
*/
private class SetImageViewBitmap extends Action {
public SetImageViewBitmap(int id, Bitmap src) {
viewId = id;
bitmap = src;
}
public SetImageViewBitmap(Parcel parcel) {
viewId = parcel.readInt();
bitmap = Bitmap.CREATOR.createFromParcel(parcel);
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
if (bitmap != null) {
bitmap.writeToParcel(dest, flags);
}
}
@Override
public void apply(View root) {
if (bitmap != null) {
ImageView target = (ImageView) root.findViewById(viewId);
Drawable d = new BitmapDrawable(bitmap);
if (target != null) {
target.setImageDrawable(d);
}
}
}
int viewId;
Bitmap bitmap;
public final static int TAG = 4;
}
/**
* Equivalent to calling Chronometer.setBase, Chronometer.setFormat,
* and Chronometer.start/stop.
*/
private class SetChronometer extends Action {
public SetChronometer(int id, long base, String format, boolean running) {
this.viewId = id;
this.base = base;
this.format = format;
this.running = running;
}
public SetChronometer(Parcel parcel) {
viewId = parcel.readInt();
base = parcel.readLong();
format = parcel.readString();
running = parcel.readInt() != 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
dest.writeLong(base);
dest.writeString(format);
dest.writeInt(running ? 1 : 0);
}
@Override
public void apply(View root) {
Chronometer target = (Chronometer) root.findViewById(viewId);
if (target != null) {
target.setBase(base);
target.setFormat(format);
if (running) {
target.start();
} else {
target.stop();
}
}
}
int viewId;
boolean running;
long base;
String format;
public final static int TAG = 5;
}
/**
* Equivalent to calling ProgressBar.setMax, ProgressBar.setProgress and
* ProgressBar.setIndeterminate
*/
private class SetProgressBar extends Action {
public SetProgressBar(int id, int max, int progress, boolean indeterminate) {
this.viewId = id;
this.progress = progress;
this.max = max;
this.indeterminate = indeterminate;
}
public SetProgressBar(Parcel parcel) {
viewId = parcel.readInt();
progress = parcel.readInt();
max = parcel.readInt();
indeterminate = parcel.readInt() != 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
dest.writeInt(progress);
dest.writeInt(max);
dest.writeInt(indeterminate ? 1 : 0);
}
@Override
public void apply(View root) {
ProgressBar target = (ProgressBar) root.findViewById(viewId);
if (target != null) {
target.setIndeterminate(indeterminate);
if (!indeterminate) {
target.setMax(max);
target.setProgress(progress);
}
}
}
int viewId;
boolean indeterminate;
int progress;
int max;
public final static int TAG = 6;
}
/**
* Create a new RemoteViews object that will display the views contained
* in the specified layout file.
*
* @param packageName Name of the package that contains the layout resource
* @param layoutId The id of the layout resource
*/
public RemoteViews(String packageName, int layoutId) {
mPackage = packageName;
mLayoutId = layoutId;
}
/**
* Reads a RemoteViews object from a parcel.
*
* @param parcel
*/
public RemoteViews(Parcel parcel) {
mPackage = parcel.readString();
mLayoutId = parcel.readInt();
int count = parcel.readInt();
if (count > 0) {
mActions = new ArrayList<Action>(count);
for (int i=0; i<count; i++) {
int tag = parcel.readInt();
switch (tag) {
case SetViewVisibility.TAG:
mActions.add(new SetViewVisibility(parcel));
break;
case SetTextViewText.TAG:
mActions.add(new SetTextViewText(parcel));
break;
case SetImageViewResource.TAG:
mActions.add(new SetImageViewResource(parcel));
break;
case SetImageViewUri.TAG:
mActions.add(new SetImageViewUri(parcel));
break;
case SetImageViewBitmap.TAG:
mActions.add(new SetImageViewBitmap(parcel));
break;
case SetChronometer.TAG:
mActions.add(new SetChronometer(parcel));
break;
case SetProgressBar.TAG:
mActions.add(new SetProgressBar(parcel));
break;
default:
throw new ActionException("Tag " + tag + "not found");
}
}
}
}
public String getPackage() {
return mPackage;
}
public int getLayoutId() {
return mLayoutId;
}
/**
* Add an action to be executed on the remote side when apply is called.
*
* @param a The action to add
*/
private void addAction(Action a) {
if (mActions == null) {
mActions = new ArrayList<Action>();
}
mActions.add(a);
}
/**
* Equivalent to calling View.setVisibility
*
* @param viewId The id of the view whose visibility should change
* @param visibility The new visibility for the view
*/
public void setViewVisibility(int viewId, int visibility) {
addAction(new SetViewVisibility(viewId, visibility));
}
/**
* Equivalent to calling TextView.setText
*
* @param viewId The id of the view whose text should change
* @param text The new text for the view
*/
public void setTextViewText(int viewId, CharSequence text) {
addAction(new SetTextViewText(viewId, text));
}
/**
* Equivalent to calling ImageView.setImageResource
*
* @param viewId The id of the view whose drawable should change
* @param srcId The new resource id for the drawable
*/
public void setImageViewResource(int viewId, int srcId) {
addAction(new SetImageViewResource(viewId, srcId));
}
/**
* Equivalent to calling ImageView.setImageURI
*
* @param viewId The id of the view whose drawable should change
* @param uri The Uri for the image
*/
public void setImageViewUri(int viewId, Uri uri) {
addAction(new SetImageViewUri(viewId, uri));
}
/**
* Equivalent to calling ImageView.setImageBitmap
*
* @param viewId The id of the view whose drawable should change
* @param bitmap The new Bitmap for the drawable
*
* @hide pending API Council approval to extend the public API
*/
public void setImageViewBitmap(int viewId, Bitmap bitmap) {
addAction(new SetImageViewBitmap(viewId, bitmap));
}
/**
* Equivalent to calling {@link Chronometer#setBase Chronometer.setBase},
* {@link Chronometer#setFormat Chronometer.setFormat},
* and {@link Chronometer#start Chronometer.start()} or
* {@link Chronometer#stop Chronometer.stop()}.
*
* @param viewId The id of the view whose text should change
* @param base The time at which the timer would have read 0:00. This
* time should be based off of
* {@link android.os.SystemClock#elapsedRealtime SystemClock.elapsedRealtime()}.
* @param format The Chronometer format string, or null to
* simply display the timer value.
* @param running True if you want the clock to be running, false if not.
*/
public void setChronometer(int viewId, long base, String format, boolean running) {
addAction(new SetChronometer(viewId, base, format, running));
}
/**
* Equivalent to calling {@link ProgressBar#setMax ProgressBar.setMax},
* {@link ProgressBar#setProgress ProgressBar.setProgress}, and
* {@link ProgressBar#setIndeterminate ProgressBar.setIndeterminate}
*
* @param viewId The id of the view whose text should change
* @param max The 100% value for the progress bar
* @param progress The current value of the progress bar.
* @param indeterminate True if the progress bar is indeterminate,
* false if not.
*/
public void setProgressBar(int viewId, int max, int progress,
boolean indeterminate) {
addAction(new SetProgressBar(viewId, max, progress, indeterminate));
}
/**
* Inflates the view hierarchy represented by this object and applies
* all of the actions.
*
* <p><strong>Caller beware: this may throw</strong>
*
* @param context Default context to use
* @param parent Parent that the resulting view hierarchy will be attached to. This method
* does <strong>not</strong> attach the hierarchy. The caller should do so when appropriate.
* @return The inflated view hierarchy
*/
public View apply(Context context, ViewGroup parent) {
View result = null;
Context c = prepareContext(context);
Resources r = c.getResources();
LayoutInflater inflater = (LayoutInflater) c
.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
inflater = inflater.cloneInContext(c);
inflater.setFilter(this);
result = inflater.inflate(mLayoutId, parent, false);
performApply(result);
return result;
}
/**
* Applies all of the actions to the provided view.
*
* <p><strong>Caller beware: this may throw</strong>
*
* @param v The view to apply the actions to. This should be the result of
* the {@link #apply(Context,ViewGroup)} call.
*/
public void reapply(Context context, View v) {
prepareContext(context);
performApply(v);
}
private void performApply(View v) {
if (mActions != null) {
final int count = mActions.size();
for (int i = 0; i < count; i++) {
Action a = mActions.get(i);
a.apply(v);
}
}
}
private Context prepareContext(Context context) {
Context c = null;
String packageName = mPackage;
if (packageName != null) {
try {
c = context.createPackageContext(packageName, 0);
} catch (NameNotFoundException e) {
Log.e(LOG_TAG, "Package name " + packageName + " not found");
c = context;
}
} else {
c = context;
}
mContext = c;
return c;
}
/* (non-Javadoc)
* Used to restrict the views which can be inflated
*
* @see android.view.LayoutInflater.Filter#onLoadClass(java.lang.Class)
*/
public boolean onLoadClass(Class clazz) {
return clazz.isAnnotationPresent(RemoteView.class);
}
public int describeContents() {
return 0;
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeString(mPackage);
dest.writeInt(mLayoutId);
int count;
if (mActions != null) {
count = mActions.size();
} else {
count = 0;
}
dest.writeInt(count);
for (int i=0; i<count; i++) {
Action a = mActions.get(i);
a.writeToParcel(dest, 0);
}
}
/**
* Parcelable.Creator that instantiates RemoteViews objects
*/
public static final Parcelable.Creator<RemoteViews> CREATOR = new Parcelable.Creator<RemoteViews>() {
public RemoteViews createFromParcel(Parcel parcel) {
return new RemoteViews(parcel);
}
public RemoteViews[] newArray(int size) {
return new RemoteViews[size];
}
};
}
|