-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathRemoteViews.java
More file actions
2773 lines (2460 loc) · 102 KB
/
RemoteViews.java
File metadata and controls
2773 lines (2460 loc) · 102 KB
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
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* 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.app.ActivityOptions;
import android.app.ActivityThread;
import android.app.Application;
import android.app.PendingIntent;
import android.appwidget.AppWidgetHostView;
import android.content.Context;
import android.content.ContextWrapper;
import android.content.Intent;
import android.content.IntentSender;
import android.content.pm.ApplicationInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.ColorStateList;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.PorterDuff;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.os.Parcel;
import android.os.Parcelable;
import android.os.StrictMode;
import android.os.UserHandle;
import android.text.TextUtils;
import android.util.ArrayMap;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.LayoutInflater.Filter;
import android.view.RemotableViewMethod;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.ViewGroup;
import android.widget.AdapterView.OnItemClickListener;
import libcore.util.Objects;
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
import java.lang.reflect.Method;
import java.util.ArrayList;
import java.util.HashMap;
/**
* 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 intent extra that contains the appWidgetId.
* @hide
*/
static final String EXTRA_REMOTEADAPTER_APPWIDGET_ID = "remoteAdapterAppWidgetId";
/**
* Application that hosts the remote views.
*
* @hide
*/
private ApplicationInfo mApplication;
/**
* The resource ID of the layout file. (Added to the parcel)
*/
private final int mLayoutId;
/**
* An array of actions to perform on the view tree once it has been
* inflated
*/
private ArrayList<Action> mActions;
/**
* A class to keep track of memory usage by this RemoteViews
*/
private MemoryUsageCounter mMemoryUsageCounter;
/**
* Maps bitmaps to unique indicies to avoid Bitmap duplication.
*/
private BitmapCache mBitmapCache;
/**
* Indicates whether or not this RemoteViews object is contained as a child of any other
* RemoteViews.
*/
private boolean mIsRoot = true;
/**
* Constants to whether or not this RemoteViews is composed of a landscape and portrait
* RemoteViews.
*/
private static final int MODE_NORMAL = 0;
private static final int MODE_HAS_LANDSCAPE_AND_PORTRAIT = 1;
/**
* Used in conjunction with the special constructor
* {@link #RemoteViews(RemoteViews, RemoteViews)} to keep track of the landscape and portrait
* RemoteViews.
*/
private RemoteViews mLandscape = null;
private RemoteViews mPortrait = null;
/**
* This flag indicates whether this RemoteViews object is being created from a
* RemoteViewsService for use as a child of a widget collection. This flag is used
* to determine whether or not certain features are available, in particular,
* setting on click extras and setting on click pending intents. The former is enabled,
* and the latter disabled when this flag is true.
*/
private boolean mIsWidgetCollectionChild = false;
private static final OnClickHandler DEFAULT_ON_CLICK_HANDLER = new OnClickHandler();
private static final Object[] sMethodsLock = new Object[0];
private static final ArrayMap<Class<? extends View>, ArrayMap<MutablePair<String, Class<?>>, Method>> sMethods =
new ArrayMap<Class<? extends View>, ArrayMap<MutablePair<String, Class<?>>, Method>>();
private static final ThreadLocal<Object[]> sInvokeArgsTls = new ThreadLocal<Object[]>() {
@Override
protected Object[] initialValue() {
return new Object[1];
}
};
/**
* Handle with care!
*/
static class MutablePair<F, S> {
F first;
S second;
MutablePair(F first, S second) {
this.first = first;
this.second = second;
}
@Override
public boolean equals(Object o) {
if (!(o instanceof MutablePair)) {
return false;
}
MutablePair<?, ?> p = (MutablePair<?, ?>) o;
return Objects.equal(p.first, first) && Objects.equal(p.second, second);
}
@Override
public int hashCode() {
return (first == null ? 0 : first.hashCode()) ^ (second == null ? 0 : second.hashCode());
}
}
/**
* This pair is used to perform lookups in sMethods without causing allocations.
*/
private final MutablePair<String, Class<?>> mPair =
new MutablePair<String, Class<?>>(null, null);
/**
* This annotation indicates that a subclass of View is alllowed to be used
* with the {@link 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(Exception ex) {
super(ex);
}
public ActionException(String message) {
super(message);
}
}
/** @hide */
public static class OnClickHandler {
public boolean onClickHandler(View view, PendingIntent pendingIntent,
Intent fillInIntent) {
try {
// TODO: Unregister this handler if PendingIntent.FLAG_ONE_SHOT?
Context context = view.getContext();
ActivityOptions opts = ActivityOptions.makeScaleUpAnimation(view,
0, 0,
view.getMeasuredWidth(), view.getMeasuredHeight());
context.startIntentSender(
pendingIntent.getIntentSender(), fillInIntent,
Intent.FLAG_ACTIVITY_NEW_TASK,
Intent.FLAG_ACTIVITY_NEW_TASK, 0, opts.toBundle());
} catch (IntentSender.SendIntentException e) {
android.util.Log.e(LOG_TAG, "Cannot send pending intent: ", e);
return false;
} catch (Exception e) {
android.util.Log.e(LOG_TAG, "Cannot send pending intent due to " +
"unknown exception: ", e);
return false;
}
return true;
}
}
/**
* Base class for all actions that can be performed on an
* inflated view.
*
* SUBCLASSES MUST BE IMMUTABLE SO CLONE WORKS!!!!!
*/
private abstract static class Action implements Parcelable {
public abstract void apply(View root, ViewGroup rootParent,
OnClickHandler handler) throws ActionException;
public static final int MERGE_REPLACE = 0;
public static final int MERGE_APPEND = 1;
public static final int MERGE_IGNORE = 2;
public int describeContents() {
return 0;
}
/**
* Overridden by each class to report on it's own memory usage
*/
public void updateMemoryUsageEstimate(MemoryUsageCounter counter) {
// We currently only calculate Bitmap memory usage, so by default,
// don't do anything here
}
public void setBitmapCache(BitmapCache bitmapCache) {
// Do nothing
}
public int mergeBehavior() {
return MERGE_REPLACE;
}
public abstract String getActionName();
public String getUniqueKey() {
return (getActionName() + viewId);
}
int viewId;
}
/**
* Merges the passed RemoteViews actions with this RemoteViews actions according to
* action-specific merge rules.
*
* @param newRv
*
* @hide
*/
public void mergeRemoteViews(RemoteViews newRv) {
if (newRv == null) return;
// We first copy the new RemoteViews, as the process of merging modifies the way the actions
// reference the bitmap cache. We don't want to modify the object as it may need to
// be merged and applied multiple times.
RemoteViews copy = newRv.clone();
HashMap<String, Action> map = new HashMap<String, Action>();
if (mActions == null) {
mActions = new ArrayList<Action>();
}
int count = mActions.size();
for (int i = 0; i < count; i++) {
Action a = mActions.get(i);
map.put(a.getUniqueKey(), a);
}
ArrayList<Action> newActions = copy.mActions;
if (newActions == null) return;
count = newActions.size();
for (int i = 0; i < count; i++) {
Action a = newActions.get(i);
String key = newActions.get(i).getUniqueKey();
int mergeBehavior = newActions.get(i).mergeBehavior();
if (map.containsKey(key) && mergeBehavior == Action.MERGE_REPLACE) {
mActions.remove(map.get(key));
map.remove(key);
}
// If the merge behavior is ignore, we don't bother keeping the extra action
if (mergeBehavior == Action.MERGE_REPLACE || mergeBehavior == Action.MERGE_APPEND) {
mActions.add(a);
}
}
// Because pruning can remove the need for bitmaps, we reconstruct the bitmap cache
mBitmapCache = new BitmapCache();
setBitmapCache(mBitmapCache);
}
private class SetEmptyView extends Action {
int viewId;
int emptyViewId;
public final static int TAG = 6;
SetEmptyView(int viewId, int emptyViewId) {
this.viewId = viewId;
this.emptyViewId = emptyViewId;
}
SetEmptyView(Parcel in) {
this.viewId = in.readInt();
this.emptyViewId = in.readInt();
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(TAG);
out.writeInt(this.viewId);
out.writeInt(this.emptyViewId);
}
@Override
public void apply(View root, ViewGroup rootParent, OnClickHandler handler) {
final View view = root.findViewById(viewId);
if (!(view instanceof AdapterView<?>)) return;
AdapterView<?> adapterView = (AdapterView<?>) view;
final View emptyView = root.findViewById(emptyViewId);
if (emptyView == null) return;
adapterView.setEmptyView(emptyView);
}
public String getActionName() {
return "SetEmptyView";
}
}
private class SetOnClickFillInIntent extends Action {
public SetOnClickFillInIntent(int id, Intent fillInIntent) {
this.viewId = id;
this.fillInIntent = fillInIntent;
}
public SetOnClickFillInIntent(Parcel parcel) {
viewId = parcel.readInt();
fillInIntent = Intent.CREATOR.createFromParcel(parcel);
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
fillInIntent.writeToParcel(dest, 0 /* no flags */);
}
@Override
public void apply(View root, ViewGroup rootParent, final OnClickHandler handler) {
final View target = root.findViewById(viewId);
if (target == null) return;
if (!mIsWidgetCollectionChild) {
Log.e(LOG_TAG, "The method setOnClickFillInIntent is available " +
"only from RemoteViewsFactory (ie. on collection items).");
return;
}
if (target == root) {
target.setTagInternal(com.android.internal.R.id.fillInIntent, fillInIntent);
} else if (fillInIntent != null) {
OnClickListener listener = new OnClickListener() {
public void onClick(View v) {
// Insure that this view is a child of an AdapterView
View parent = (View) v.getParent();
while (parent != null && !(parent instanceof AdapterView<?>)
&& !(parent instanceof AppWidgetHostView)) {
parent = (View) parent.getParent();
}
if (parent instanceof AppWidgetHostView || parent == null) {
// Somehow they've managed to get this far without having
// and AdapterView as a parent.
Log.e(LOG_TAG, "Collection item doesn't have AdapterView parent");
return;
}
// Insure that a template pending intent has been set on an ancestor
if (!(parent.getTag() instanceof PendingIntent)) {
Log.e(LOG_TAG, "Attempting setOnClickFillInIntent without" +
" calling setPendingIntentTemplate on parent.");
return;
}
PendingIntent pendingIntent = (PendingIntent) parent.getTag();
final Rect rect = getSourceBounds(v);
fillInIntent.setSourceBounds(rect);
handler.onClickHandler(v, pendingIntent, fillInIntent);
}
};
target.setOnClickListener(listener);
}
}
public String getActionName() {
return "SetOnClickFillInIntent";
}
Intent fillInIntent;
public final static int TAG = 9;
}
private class SetPendingIntentTemplate extends Action {
public SetPendingIntentTemplate(int id, PendingIntent pendingIntentTemplate) {
this.viewId = id;
this.pendingIntentTemplate = pendingIntentTemplate;
}
public SetPendingIntentTemplate(Parcel parcel) {
viewId = parcel.readInt();
pendingIntentTemplate = PendingIntent.readPendingIntentOrNullFromParcel(parcel);
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
pendingIntentTemplate.writeToParcel(dest, 0 /* no flags */);
}
@Override
public void apply(View root, ViewGroup rootParent, final OnClickHandler handler) {
final View target = root.findViewById(viewId);
if (target == null) return;
// If the view isn't an AdapterView, setting a PendingIntent template doesn't make sense
if (target instanceof AdapterView<?>) {
AdapterView<?> av = (AdapterView<?>) target;
// The PendingIntent template is stored in the view's tag.
OnItemClickListener listener = new OnItemClickListener() {
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
// The view should be a frame layout
if (view instanceof ViewGroup) {
ViewGroup vg = (ViewGroup) view;
// AdapterViews contain their children in a frame
// so we need to go one layer deeper here.
if (parent instanceof AdapterViewAnimator) {
vg = (ViewGroup) vg.getChildAt(0);
}
if (vg == null) return;
Intent fillInIntent = null;
int childCount = vg.getChildCount();
for (int i = 0; i < childCount; i++) {
Object tag = vg.getChildAt(i).getTag(com.android.internal.R.id.fillInIntent);
if (tag instanceof Intent) {
fillInIntent = (Intent) tag;
break;
}
}
if (fillInIntent == null) return;
final Rect rect = getSourceBounds(view);
final Intent intent = new Intent();
intent.setSourceBounds(rect);
handler.onClickHandler(view, pendingIntentTemplate, fillInIntent);
}
}
};
av.setOnItemClickListener(listener);
av.setTag(pendingIntentTemplate);
} else {
Log.e(LOG_TAG, "Cannot setPendingIntentTemplate on a view which is not" +
"an AdapterView (id: " + viewId + ")");
return;
}
}
public String getActionName() {
return "SetPendingIntentTemplate";
}
PendingIntent pendingIntentTemplate;
public final static int TAG = 8;
}
private class SetRemoteViewsAdapterList extends Action {
public SetRemoteViewsAdapterList(int id, ArrayList<RemoteViews> list, int viewTypeCount) {
this.viewId = id;
this.list = list;
this.viewTypeCount = viewTypeCount;
}
public SetRemoteViewsAdapterList(Parcel parcel) {
viewId = parcel.readInt();
viewTypeCount = parcel.readInt();
int count = parcel.readInt();
list = new ArrayList<RemoteViews>();
for (int i = 0; i < count; i++) {
RemoteViews rv = RemoteViews.CREATOR.createFromParcel(parcel);
list.add(rv);
}
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
dest.writeInt(viewTypeCount);
if (list == null || list.size() == 0) {
dest.writeInt(0);
} else {
int count = list.size();
dest.writeInt(count);
for (int i = 0; i < count; i++) {
RemoteViews rv = list.get(i);
rv.writeToParcel(dest, flags);
}
}
}
@Override
public void apply(View root, ViewGroup rootParent, OnClickHandler handler) {
final View target = root.findViewById(viewId);
if (target == null) return;
// Ensure that we are applying to an AppWidget root
if (!(rootParent instanceof AppWidgetHostView)) {
Log.e(LOG_TAG, "SetRemoteViewsAdapterIntent action can only be used for " +
"AppWidgets (root id: " + viewId + ")");
return;
}
// Ensure that we are calling setRemoteAdapter on an AdapterView that supports it
if (!(target instanceof AbsListView) && !(target instanceof AdapterViewAnimator)) {
Log.e(LOG_TAG, "Cannot setRemoteViewsAdapter on a view which is not " +
"an AbsListView or AdapterViewAnimator (id: " + viewId + ")");
return;
}
if (target instanceof AbsListView) {
AbsListView v = (AbsListView) target;
Adapter a = v.getAdapter();
if (a instanceof RemoteViewsListAdapter && viewTypeCount <= a.getViewTypeCount()) {
((RemoteViewsListAdapter) a).setViewsList(list);
} else {
v.setAdapter(new RemoteViewsListAdapter(v.getContext(), list, viewTypeCount));
}
} else if (target instanceof AdapterViewAnimator) {
AdapterViewAnimator v = (AdapterViewAnimator) target;
Adapter a = v.getAdapter();
if (a instanceof RemoteViewsListAdapter && viewTypeCount <= a.getViewTypeCount()) {
((RemoteViewsListAdapter) a).setViewsList(list);
} else {
v.setAdapter(new RemoteViewsListAdapter(v.getContext(), list, viewTypeCount));
}
}
}
public String getActionName() {
return "SetRemoteViewsAdapterList";
}
int viewTypeCount;
ArrayList<RemoteViews> list;
public final static int TAG = 15;
}
private class SetRemoteViewsAdapterIntent extends Action {
public SetRemoteViewsAdapterIntent(int id, Intent intent) {
this.viewId = id;
this.intent = intent;
}
public SetRemoteViewsAdapterIntent(Parcel parcel) {
viewId = parcel.readInt();
intent = Intent.CREATOR.createFromParcel(parcel);
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
intent.writeToParcel(dest, flags);
}
@Override
public void apply(View root, ViewGroup rootParent, OnClickHandler handler) {
final View target = root.findViewById(viewId);
if (target == null) return;
// Ensure that we are applying to an AppWidget root
if (!(rootParent instanceof AppWidgetHostView)) {
Log.e(LOG_TAG, "SetRemoteViewsAdapterIntent action can only be used for " +
"AppWidgets (root id: " + viewId + ")");
return;
}
// Ensure that we are calling setRemoteAdapter on an AdapterView that supports it
if (!(target instanceof AbsListView) && !(target instanceof AdapterViewAnimator)) {
Log.e(LOG_TAG, "Cannot setRemoteViewsAdapter on a view which is not " +
"an AbsListView or AdapterViewAnimator (id: " + viewId + ")");
return;
}
// Embed the AppWidget Id for use in RemoteViewsAdapter when connecting to the intent
// RemoteViewsService
AppWidgetHostView host = (AppWidgetHostView) rootParent;
intent.putExtra(EXTRA_REMOTEADAPTER_APPWIDGET_ID, host.getAppWidgetId());
if (target instanceof AbsListView) {
AbsListView v = (AbsListView) target;
v.setRemoteViewsAdapter(intent);
v.setRemoteViewsOnClickHandler(handler);
} else if (target instanceof AdapterViewAnimator) {
AdapterViewAnimator v = (AdapterViewAnimator) target;
v.setRemoteViewsAdapter(intent);
v.setRemoteViewsOnClickHandler(handler);
}
}
public String getActionName() {
return "SetRemoteViewsAdapterIntent";
}
Intent intent;
public final static int TAG = 10;
}
/**
* Equivalent to calling
* {@link android.view.View#setOnClickListener(android.view.View.OnClickListener)}
* to launch the provided {@link PendingIntent}.
*/
private class SetOnClickPendingIntent extends Action {
public SetOnClickPendingIntent(int id, PendingIntent pendingIntent) {
this.viewId = id;
this.pendingIntent = pendingIntent;
}
public SetOnClickPendingIntent(Parcel parcel) {
viewId = parcel.readInt();
// We check a flag to determine if the parcel contains a PendingIntent.
if (parcel.readInt() != 0) {
pendingIntent = PendingIntent.readPendingIntentOrNullFromParcel(parcel);
}
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
// We use a flag to indicate whether the parcel contains a valid object.
dest.writeInt(pendingIntent != null ? 1 : 0);
if (pendingIntent != null) {
pendingIntent.writeToParcel(dest, 0 /* no flags */);
}
}
@Override
public void apply(View root, ViewGroup rootParent, final OnClickHandler handler) {
final View target = root.findViewById(viewId);
if (target == null) return;
// If the view is an AdapterView, setting a PendingIntent on click doesn't make much
// sense, do they mean to set a PendingIntent template for the AdapterView's children?
if (mIsWidgetCollectionChild) {
Log.w(LOG_TAG, "Cannot setOnClickPendingIntent for collection item " +
"(id: " + viewId + ")");
ApplicationInfo appInfo = root.getContext().getApplicationInfo();
// We let this slide for HC and ICS so as to not break compatibility. It should have
// been disabled from the outset, but was left open by accident.
if (appInfo != null &&
appInfo.targetSdkVersion >= Build.VERSION_CODES.JELLY_BEAN) {
return;
}
}
// If the pendingIntent is null, we clear the onClickListener
OnClickListener listener = null;
if (pendingIntent != null) {
listener = new OnClickListener() {
public void onClick(View v) {
// Find target view location in screen coordinates and
// fill into PendingIntent before sending.
final Rect rect = getSourceBounds(v);
final Intent intent = new Intent();
intent.setSourceBounds(rect);
handler.onClickHandler(v, pendingIntent, intent);
}
};
}
target.setOnClickListener(listener);
}
public String getActionName() {
return "SetOnClickPendingIntent";
}
PendingIntent pendingIntent;
public final static int TAG = 1;
}
private static Rect getSourceBounds(View v) {
final float appScale = v.getContext().getResources()
.getCompatibilityInfo().applicationScale;
final int[] pos = new int[2];
v.getLocationOnScreen(pos);
final Rect rect = new Rect();
rect.left = (int) (pos[0] * appScale + 0.5f);
rect.top = (int) (pos[1] * appScale + 0.5f);
rect.right = (int) ((pos[0] + v.getWidth()) * appScale + 0.5f);
rect.bottom = (int) ((pos[1] + v.getHeight()) * appScale + 0.5f);
return rect;
}
private Method getMethod(View view, String methodName, Class<?> paramType) {
Method method;
Class<? extends View> klass = view.getClass();
synchronized (sMethodsLock) {
ArrayMap<MutablePair<String, Class<?>>, Method> methods = sMethods.get(klass);
if (methods == null) {
methods = new ArrayMap<MutablePair<String, Class<?>>, Method>();
sMethods.put(klass, methods);
}
mPair.first = methodName;
mPair.second = paramType;
method = methods.get(mPair);
if (method == null) {
try {
if (paramType == null) {
method = klass.getMethod(methodName);
} else {
method = klass.getMethod(methodName, paramType);
}
} catch (NoSuchMethodException ex) {
throw new ActionException("view: " + klass.getName() + " doesn't have method: "
+ methodName + getParameters(paramType));
}
if (!method.isAnnotationPresent(RemotableViewMethod.class)) {
throw new ActionException("view: " + klass.getName()
+ " can't use method with RemoteViews: "
+ methodName + getParameters(paramType));
}
methods.put(new MutablePair<String, Class<?>>(methodName, paramType), method);
}
}
return method;
}
private static String getParameters(Class<?> paramType) {
if (paramType == null) return "()";
return "(" + paramType + ")";
}
private static Object[] wrapArg(Object value) {
Object[] args = sInvokeArgsTls.get();
args[0] = value;
return args;
}
/**
* Equivalent to calling a combination of {@link Drawable#setAlpha(int)},
* {@link Drawable#setColorFilter(int, android.graphics.PorterDuff.Mode)},
* and/or {@link Drawable#setLevel(int)} on the {@link Drawable} of a given view.
* <p>
* These operations will be performed on the {@link Drawable} returned by the
* target {@link View#getBackground()} by default. If targetBackground is false,
* we assume the target is an {@link ImageView} and try applying the operations
* to {@link ImageView#getDrawable()}.
* <p>
* You can omit specific calls by marking their values with null or -1.
*/
private class SetDrawableParameters extends Action {
public SetDrawableParameters(int id, boolean targetBackground, int alpha,
int colorFilter, PorterDuff.Mode mode, int level) {
this.viewId = id;
this.targetBackground = targetBackground;
this.alpha = alpha;
this.colorFilter = colorFilter;
this.filterMode = mode;
this.level = level;
}
public SetDrawableParameters(Parcel parcel) {
viewId = parcel.readInt();
targetBackground = parcel.readInt() != 0;
alpha = parcel.readInt();
colorFilter = parcel.readInt();
boolean hasMode = parcel.readInt() != 0;
if (hasMode) {
filterMode = PorterDuff.Mode.valueOf(parcel.readString());
} else {
filterMode = null;
}
level = parcel.readInt();
}
public void writeToParcel(Parcel dest, int flags) {
dest.writeInt(TAG);
dest.writeInt(viewId);
dest.writeInt(targetBackground ? 1 : 0);
dest.writeInt(alpha);
dest.writeInt(colorFilter);
if (filterMode != null) {
dest.writeInt(1);
dest.writeString(filterMode.toString());
} else {
dest.writeInt(0);
}
dest.writeInt(level);
}
@Override
public void apply(View root, ViewGroup rootParent, OnClickHandler handler) {
final View target = root.findViewById(viewId);
if (target == null) return;
// Pick the correct drawable to modify for this view
Drawable targetDrawable = null;
if (targetBackground) {
targetDrawable = target.getBackground();
} else if (target instanceof ImageView) {
ImageView imageView = (ImageView) target;
targetDrawable = imageView.getDrawable();
}
if (targetDrawable != null) {
// Perform modifications only if values are set correctly
if (alpha != -1) {
targetDrawable.setAlpha(alpha);
}
if (filterMode != null) {
targetDrawable.setColorFilter(colorFilter, filterMode);
}
if (level != -1) {
targetDrawable.setLevel(level);
}
}
}
public String getActionName() {
return "SetDrawableParameters";
}
boolean targetBackground;
int alpha;
int colorFilter;
PorterDuff.Mode filterMode;
int level;
public final static int TAG = 3;
}
private final class ReflectionActionWithoutParams extends Action {
final String methodName;
public final static int TAG = 5;
ReflectionActionWithoutParams(int viewId, String methodName) {
this.viewId = viewId;
this.methodName = methodName;
}
ReflectionActionWithoutParams(Parcel in) {
this.viewId = in.readInt();
this.methodName = in.readString();
}
public void writeToParcel(Parcel out, int flags) {
out.writeInt(TAG);
out.writeInt(this.viewId);
out.writeString(this.methodName);
}
@Override
public void apply(View root, ViewGroup rootParent, OnClickHandler handler) {
final View view = root.findViewById(viewId);
if (view == null) return;
try {
getMethod(view, this.methodName, null).invoke(view);
} catch (ActionException e) {
throw e;
} catch (Exception ex) {
throw new ActionException(ex);
}
}
public int mergeBehavior() {
// we don't need to build up showNext or showPrevious calls
if (methodName.equals("showNext") || methodName.equals("showPrevious")) {
return MERGE_IGNORE;
} else {
return MERGE_REPLACE;
}
}
public String getActionName() {
return "ReflectionActionWithoutParams";
}
}
private static class BitmapCache {
ArrayList<Bitmap> mBitmaps;
public BitmapCache() {
mBitmaps = new ArrayList<Bitmap>();
}
public BitmapCache(Parcel source) {
int count = source.readInt();
mBitmaps = new ArrayList<Bitmap>();
for (int i = 0; i < count; i++) {
Bitmap b = Bitmap.CREATOR.createFromParcel(source);
mBitmaps.add(b);
}
}
public int getBitmapId(Bitmap b) {
if (b == null) {
return -1;
} else {
if (mBitmaps.contains(b)) {
return mBitmaps.indexOf(b);
} else {
mBitmaps.add(b);
return (mBitmaps.size() - 1);
}
}
}
public Bitmap getBitmapForId(int id) {
if (id == -1 || id >= mBitmaps.size()) {
return null;
} else {
return mBitmaps.get(id);
}
}
public void writeBitmapsToParcel(Parcel dest, int flags) {
int count = mBitmaps.size();
dest.writeInt(count);
for (int i = 0; i < count; i++) {
mBitmaps.get(i).writeToParcel(dest, flags);
}
}
public void assimilate(BitmapCache bitmapCache) {
ArrayList<Bitmap> bitmapsToBeAdded = bitmapCache.mBitmaps;
int count = bitmapsToBeAdded.size();
for (int i = 0; i < count; i++) {
Bitmap b = bitmapsToBeAdded.get(i);
if (!mBitmaps.contains(b)) {
mBitmaps.add(b);
}
}
}
public void addBitmapMemory(MemoryUsageCounter memoryCounter) {
for (int i = 0; i < mBitmaps.size(); i++) {
memoryCounter.addBitmapMemory(mBitmaps.get(i));
}