launcher点击,加载,拖动图标过程二

并且把我们mAllAppsList清空,这个mAllAppsList我们前面说过,这里不再赘述,然后

?
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
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
                 for ( int j= 0 ; i<n &&= "" j<batchsize;= "" j++)= "" {= "" this = "" builds= "" the= "" icon= "" bitmaps.= "" mallappslist.add( new = "" applicationinfo(packagemanager,= "" apps.get(i),= "" miconcache,= "" mlabelcache));= "" i++;= "" }<= "" pre= "" ><br>
去加载我们的应用,ApplicationInfo之前我们也说过,但是前面只看了它存储了我们应用的什么信息,这里我们去看看它的构造函数
<p></p>
<p></p>
<pre class = "brush:java;" >    public ApplicationInfo(PackageManager pm, ResolveInfo info, IconCache iconCache,
             HashMap<object, charsequence= "" > labelCache) {
         final String packageName = info.activityInfo.applicationInfo.packageName;
 
         this .componentName = new ComponentName(packageName, info.activityInfo.name);
         this .container = ItemInfo.NO_ID;
         this .setActivity(componentName,
                 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
 
         try {
             int appFlags = pm.getApplicationInfo(packageName, 0 ).flags;
             if ((appFlags & android.content.pm.ApplicationInfo.FLAG_SYSTEM) == 0 ) {
                 flags |= DOWNLOADED_FLAG;
 
                 if ((appFlags & android.content.pm.ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0 ) {
                     flags |= UPDATED_SYSTEM_APP_FLAG;
                 }
             }
             firstInstallTime = pm.getPackageInfo(packageName, 0 ).firstInstallTime;
         } catch (NameNotFoundException e) {
             Log.d(TAG, "PackageManager.getApplicationInfo failed for " + packageName);
         }
 
         iconCache.getTitleAndIcon( this , info, labelCache);
     }</object,></pre><br>
首先,给ApplicationInfo中的componentName和container变量赋值,然后
<p></p>
<p></p>
<pre class = "brush:java;" >        this .setActivity(componentName,
                 Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);</pre><br>
也就是
<p></p>
<p></p>
<pre class = "brush:java;" >    final void setActivity(ComponentName className, int launchFlags) {
         intent = new Intent(Intent.ACTION_MAIN);
         intent.addCategory(Intent.CATEGORY_LAUNCHER);
         intent.setComponent(className);
         intent.setFlags(launchFlags);
         itemType = LauncherSettings.BaseLauncherColumns.ITEM_TYPE_APPLICATION;
     }</pre><br>
给ApplicationInfo的intent赋值,这里我们可以看出通过intent就可以启动我们相对应的应用了。接着就是给ApplicationInfo的flags和firstInstallTime赋值,这些都不再详细解说,我们详细看看iconCache.getTitleAndIcon( this , info, labelCache)也就是
<p></p>
<p></p>
<pre class = "brush:java;" >    public void getTitleAndIcon(ApplicationInfo application, ResolveInfo info,
             HashMap<object, charsequence= "" > labelCache) {
         synchronized (mCache) {
             CacheEntry entry = cacheLocked(application.componentName, info, labelCache);
 
             application.title = entry.title;
             application.iconBitmap = createBitmap(application.componentName, entry.icon,
                     application);
         }
     }</object,></pre><br>
CacheEntry也就是
<p></p>
<p></p>
<pre class = "brush:java;" >    private static class CacheEntry {
         public Bitmap icon;
         public String title;
     }</pre><br>
只保存了应用的图标和名字。cacheLocked(application.componentName, info, labelCache)也就是
<p></p>
<p></p>
<pre class = "brush:java;" >    private CacheEntry cacheLocked(ComponentName componentName, ResolveInfo info,
             HashMap<object, charsequence= "" > labelCache) {
         CacheEntry entry = mCache.get(componentName);
         if (entry == null ) {
             entry = new CacheEntry();
 
             mCache.put(componentName, entry);
 
             ComponentName key = LauncherModel.getComponentNameFromResolveInfo(info);
             if (labelCache != null && labelCache.containsKey(key)) {
                 entry.title = labelCache.get(key).toString();
             } else {
                 entry.title = info.loadLabel(mPackageManager).toString();
                 if (labelCache != null ) {
                     labelCache.put(key, entry.title);
                 }
             }
             if (entry.title == null ) {
                 entry.title = info.activityInfo.name;
             }
 
             entry.icon = Utilities.createIconBitmap(
                     getFullResIcon(info), mContext);
         }
         return entry;
     }</object,></pre><br>
首先从我们的缓存中获取,如果有就直接返回,如果没有就去获取。获取title也一样,先从缓存中获取,如果有就使用,如果没有就从应用的信息中获取,这里我们可以更改应用在Launcher中显示的名字,这些都容易理解,不做过多解释。接下来就是Icon的获取
<p></p>
<p></p>
<pre class = "brush:java;" >            entry.icon = Utilities.createIconBitmap(
                     getFullResIcon(info), mContext);</pre><br>
我们看看getFullResIcon也就是
<p></p>
<p></p>
<pre class = "brush:java;" >    public Drawable getFullResIcon(ResolveInfo info) {
         Resources resources;
         try {
             resources = mPackageManager.getResourcesForApplication(
                     info.activityInfo.applicationInfo);
         } catch (PackageManager.NameNotFoundException e) {
             resources = null ;
         }
         if (resources != null ) {
             int iconId = info.activityInfo.getIconResource();
             if (iconId != 0 ) {
                 return getFullResIcon(resources, iconId);
             }
         }
         return getFullResDefaultActivityIcon();
     }</pre><br>
首先获取到我们加载应用的资源信息<br>
<p></p>
<p></p>
<pre class = "brush:java;" >            resources = mPackageManager.getResourcesForApplication(
                     info.activityInfo.applicationInfo);</pre>
<p></p>
<p>如果获取资源不成功就返回getFullResDefaultActivityIcon(),如果获取资源成功</p>
<p></p>
<pre class = "brush:java;" >        if (resources != null ) {
             int iconId = info.activityInfo.getIconResource();
             if (iconId != 0 ) {
                 return getFullResIcon(resources, iconId);
             }
         }</pre><br>
就得到相应应用图标的ID,然后返回getFullResIcon也就是
<p></p>
<p></p>
<pre class = "brush:java;" >    public Drawable getFullResIcon(Resources resources, int iconId) {
         Drawable d;
         try {
             d = resources.getDrawableForDensity(iconId, mIconDpi);
         } catch (Resources.NotFoundException e) {
             d = null ;
         }
 
         return (d != null ) ? d : getFullResDefaultActivityIcon();
     }</pre><br>
通过图标的ID获取到图片Drawable返回,如果获取图片不成功同样返回getFullResDefaultActivityIcon(),也就是
<p></p>
<p></p>
<pre class = "brush:java;" >    public Drawable getFullResDefaultActivityIcon() {
         return getFullResIcon(Resources.getSystem(),
                 com.android.internal.R.mipmap.sym_def_app_icon);
     }</pre><br>
这个资源是我们framework下面的一张图片,也就是我们经常见的那个android小人人,可能不同代码这个图片有改动。如果想要把Launcher的图标改成我们想要的就可在这部分动手脚了,例如我们获取到一个我们准备好的图片,然后返回就可以了,这些不再多讲。现在回到cacheLocked中,还看
<p></p>
<p></p>
<pre class = "brush:java;" >            entry.icon = Utilities.createIconBitmap(
                     getFullResIcon(info), mContext);</pre><br>
这句话,刚才我们分析了getFullResIcon(info)返回一个Drawable,现在我们看看Utilities.createIconBitmap,Utilities.createIconBitmap是一个重构函数,一个传进去Bitmap,一个传进去Drawable,我们看传进去Drawable的函数
<p></p>
<p></p>
<pre class = "brush:java;" >    static Bitmap createIconBitmap(Drawable icon, Context context) {
         synchronized (sCanvas) { // we share the statics :-(
             if (sIconWidth == - 1 ) {
                 initStatics(context);
             }
 
             int width = sIconWidth;
             int height = sIconHeight;
 
             if (icon instanceof PaintDrawable) {
                 PaintDrawable painter = (PaintDrawable) icon;
                 painter.setIntrinsicWidth(width);
                 painter.setIntrinsicHeight(height);
             } else if (icon instanceof BitmapDrawable) {
                 // Ensure the bitmap has a density.
                 BitmapDrawable bitmapDrawable = (BitmapDrawable) icon;
                 Bitmap bitmap = bitmapDrawable.getBitmap();
                 if (bitmap.getDensity() == Bitmap.DENSITY_NONE) {
                     bitmapDrawable.setTargetDensity(context.getResources().getDisplayMetrics());
                 }
             }
             int sourceWidth = icon.getIntrinsicWidth();
             int sourceHeight = icon.getIntrinsicHeight();
 
             if (sourceWidth > 0 && sourceHeight > 0 ) {
                 // There are intrinsic sizes.
                 if (width < sourceWidth || height < sourceHeight) {
                     // It's too big, scale it down.
                     final float ratio = ( float ) sourceWidth / sourceHeight;
                     if (sourceWidth > sourceHeight) {
                         height = ( int ) (width / ratio);
                     } else if (sourceHeight > sourceWidth) {
                         width = ( int ) (height * ratio);
                     }
                 } else if (sourceWidth < width && sourceHeight < height) {
                     // Don't scale up the icon
                     width = sourceWidth;
                     height = sourceHeight;
                 }
             }
 
             // no intrinsic size --> use default size
             int textureWidth = sIconTextureWidth;
             int textureHeight = sIconTextureHeight;
 
             final Bitmap bitmap = Bitmap.createBitmap(textureWidth, textureHeight,
                     Bitmap.Config.ARGB_8888);
             final Canvas canvas = sCanvas;
             canvas.setBitmap(bitmap);
 
             final int left = (textureWidth-width) / 2 ;
             final int top = (textureHeight-height) / 2 ;
 
             if ( false ) {
                 // draw a big box for the icon for debugging
                 canvas.drawColor(sColors[sColorIndex]);
                 if (++sColorIndex >= sColors.length) sColorIndex = 0 ;
                 Paint debugPaint = new Paint();
                 debugPaint.setColor( 0xffcccc00 );
                 canvas.drawRect(left, top, left+width, top+height, debugPaint);
             }
 
             sOldBounds.set(icon.getBounds());
             icon.setBounds(left, top, left+width, top+height);
             icon.draw(canvas);
             icon.setBounds(sOldBounds);
             canvas.setBitmap( null );
 
             return bitmap;
         }
     }</pre><br>
很明显在这里对图片大小等做了修整吧,如果要控制Launcher图标显示就在这里做手脚吧,如可以添加一个背景图片什么的,或显示大小什么的,不再多说。然后我们回到getTitleAndIcon,接着看
<p></p>
<p></p>
<pre class = "brush:java;" >            application.title = entry.title;
             application.iconBitmap = createBitmap(application.componentName, entry.icon,
                     application);</pre><br>
就是给我们的ApplicationInfo中的title和iconBitmap赋值了。这里有疑问了?我们的图标图片不是已经做好了,这里怎么又createBitmap呢?我们去看看
<p></p>
<p></p>
<pre class = "brush:java;" >    public Bitmap createBitmap(ComponentName componentName, Bitmap bitmap,
             ApplicationInfo application) {
         if (!componentName.getPackageName().equals( "com.android.mms" )) {
             return bitmap;
         }
         //return the Bitmap with unRead Tip
         return MessageManager.getInstance(mContext).createMmsBitmap(bitmap, application);
     }</pre><br>
这下我们恍然大悟了吧,这是在做什么呢?是在把我们未读短信的个数显示在图标上,明白了吧。如果我们要把未接电话的个数显示在图片上就可以在这里动手脚了,至于怎么获取到未读短信,未接电话个数的,怎么把数字做到图片上的,这些都是学习android的必备知识,不在详细解说。不懂的可以顺这代码看看就知道了。到此我们一个应用的ApplicationInfo制作完成了。然后就是循环的问题了,我们回到LauncherModel中loadAllAppsByBatch继续看
<p></p>
<p></p>
<pre class = "brush:java;" >                for ( int j= 0 ; i<n &&= "" j<batchsize;= "" j++)= "" {= "" this = "" builds= "" the= "" icon= "" bitmaps.= "" mallappslist.add( new = "" applicationinfo(packagemanager,= "" apps.get(i),= "" miconcache,= "" mlabelcache));= "" i++;= "" }<= "" pre= "" ><br>
刚才我们用了大量的篇幅讲解了制作一个ApplicationInfo的过程,希望大家都能明白。接着我们看看mAllAppsList.add也就是
<p></p>
<p></p>
<pre class = "brush:java;" >    public void add(ApplicationInfo info) {
         if (findActivity(data, info.componentName)) {
             return ;
         }
         data.add(info);
         added.add(info);
     }</pre><br>
把我们制作好的ApplicationInfo给了AllAppsList的data和added这两个我们前面说过,这里不再多说。继续看LauncherModel中loadAllAppsByBatch后面的内容
<p></p>
<p></p>
<pre class = "brush:java;" >                final boolean first = i <= batchSize;
                 final Callbacks callbacks = tryGetCallbacks(oldCallbacks);
                 final ArrayList added = mAllAppsList.added;
                 mAllAppsList.added = new ArrayList();
 
                 mHandler.post( new Runnable() {
                     public void run() {
                         final long t = SystemClock.uptimeMillis();
                         if (callbacks != null ) {
                             isNeedSave = true ;
                             if (first) {
                                 callbacks.bindAllApplications(added);
                             } else {
                                 callbacks.bindAppsAdded(added);
                             }
                             if (DEBUG_LOADERS) {
                                 Log.d(TAG, "bound " + added.size() + " apps in "
                                     + (SystemClock.uptimeMillis() - t) + "ms" );
                             }
                         } else {
                             Log.i(TAG, "not binding apps: no Launcher activity" );
                         }
                     }
                 });</applicationinfo></applicationinfo></pre><br>
把我们的mAllAppsList.added给了一个新申请的ArrayList也就是added,然后把我们的mAllAppsList.added重新申请,置空。最后又启动了一个线程来加载和显示我们的应用了,也就是
<p></p>
<p></p>
<pre class = "brush:java;" >                            if (first) {
                                 callbacks.bindAllApplications(added);
                             } else {
                                 callbacks.bindAppsAdded(added);
                             }</pre><br>
这两个差不多,最终走向一样,我们只看一个bindAllApplications,bindAllApplications是在Launcher中实现的
<p></p>
<p></p>
<pre class = "brush:java;" >    public void bindAllApplications( final ArrayList apps) {
         // Remove the progress bar entirely; we could also make it GONE
         // but better to remove it since we know it's not going to be used
         View progressBar = mAppsCustomizeTabHost.
             findViewById(R.id.apps_customize_progress_bar);
         if (progressBar != null ) {
             ((ViewGroup)progressBar.getParent()).removeView(progressBar);
         }
         if (LOGD) Log.d(TAG, "bindAllApplications " + apps.toString());
         // We just post the call to setApps so the user sees the progress bar
         // disappear-- otherwise, it just looks like the progress bar froze
         // which doesn't look great
         mAppsCustomizeTabHost.post( new Runnable() {
             public void run() {
                 if (mAppsCustomizeContent != null ) {
                     mAppsCustomizeContent.setApps(apps);
                 }
             }
         });
     }</applicationinfo></pre><br>
这里出现两个布局mAppsCustomizeTabHost和mAppsCustomizeContent,这里插个小曲,来说一下Launcher布局,我们不从最底层说起,我们从DragLayout这层说起,顾名思义是拖动层了,这一层包含AppsCustomizeTabHost也就是主菜单,Hotseat最下面的那一行应用图标,Workspace就是待机界面了等等吧,其它不重要的就不说了。接着说说Workspace,Workspace包含很多CellLayout,CellLayout就是我们在待机左右滑动时的页,CellLayout又包含CellLayoutChildren,CellLayoutChildren包含许多LauncherAppWidgetHostView接下来就是我们的Widget了,CellLayoutChildren还包含BubbleTextView,就是我们的App图标了,这就是Workspace的构造。接着说说主菜单,也就是AppsCustomizeTabHost,AppsCustomizeTabHost之上有很多层,不再解释,直接到AppsCustomizePagedView层,AppsCustomizePagedView包含很多PagedViewCellLayout,PagedViewCellLayout就是我们在主菜单左右滑动出现的页了,PagedViewCellLayout之上是PagedViewCellLayoutChildren,PagedViewCellLayoutChildren包含很多PagedViewIcon也就是我们的应用图标。这样我们就清楚了Launcher的大致布局了。好了,我们接着说mAppsCustomizeTabHost和mAppsCustomizeContent,这两个也就是AppsCustomizeTabHost和AppsCustomizePagedView,通过上面的解释可以明白它们之间的关系了,我们这里要加载应用,就是去AppsCustomizePagedView中加载了,也就是mAppsCustomizeContent.setApps(apps)了
<p></p>
<p></p>
<pre class = "brush:java;" >    public void setApps(ArrayList list) {
         mApps = list;
         Collections.sort(mApps, LauncherModel.APP_NAME_COMPARATOR);
         updatePageCounts();
 
         // The next layout pass will trigger data-ready if both widgets and apps are set, so
         // request a layout to do this test and invalidate the page data when ready.
         LauncherModel.cacheAllApp(mContext, mApps);
         if (testDataReady()) requestLayout();
         invalidatePageData();
     }</applicationinfo></pre><br>
首先重新计算我们的page页个数
<p></p>
<p></p>
<pre class = "brush:java;" >    private void updatePageCounts() {
         mNumWidgetPages = ( int ) Math.ceil(mWidgets.size() /
                 ( float ) (mWidgetCountX * mWidgetCountY));
         mNumAppsPages = ( int ) Math.ceil(( float ) mApps.size() / (mCellCountX * mCellCountY));
     }</pre><br>
接着备份我们的应用信息到数据库LauncherModel.cacheAllApp(mContext, mApps),也就我们前面说的从数据库加载应用的过程,由于代码不同,有的代码没有这一部分,所以不做讲解,好处就是开机加载应用图标比较快。然后就是更新我们布局,就可以把我们的应用显示出来了。而invalidatePageData()是什么呢?就是Launcher页面都放满了图标,就新增一页,来放图标,最终还是通过requestLayout()来从新分布布局刷新显示了。至此我们的图标就显示出来了。
<p></p>
<p>    接着我们说说当点击图标的时候怎样启动应用的。这个分为两个,一个是点击主菜单图标,一个点击待机图标。我么先说点击待机图标也就是Workspace图标,这个事件响应再Launcher中,也就是</p>
<p></p>
<pre class = "brush:java;" >    public void onClick(View v) {
         // Make sure that rogue clicks don't get through while allapps is launching, or after the
         // view has detached (it's possible for this to happen if the view is removed mid touch).
         if (v.getWindowToken() == null ) {
             return ;
         }
 
         if (mWorkspace.isSwitchingState()) {
             return ;
         }
 
         Object tag = v.getTag();
         if (tag instanceof ShortcutInfo) {
             // Open shortcut
             final Intent intent = ((ShortcutInfo) tag).intent;
             int [] pos = new int [ 2 ];
             v.getLocationOnScreen(pos);
             intent.setSourceBounds( new Rect(pos[ 0 ], pos[ 1 ],
                     pos[ 0 ] + v.getWidth(), pos[ 1 ] + v.getHeight()));
             boolean success = startActivitySafely(intent, tag);
 
             if (success && v instanceof BubbleTextView) {
                 mWaitingForResume = (BubbleTextView) v;
                 mWaitingForResume.setStayPressed( true );
             }
         } else if (tag instanceof FolderInfo) {
             if (v instanceof FolderIcon) {
                 FolderIcon fi = (FolderIcon) v;
                 handleFolderClick(fi);
             }
         } else if (v == mAllAppsButton) {
             if (mState == State.APPS_CUSTOMIZE) {
                 showWorkspace( true );
             } else {
                 onClickAllAppsButton(v);
             }
         }
     }</pre><br>
先说一下ShortcutInfo,ShortcutInfo的信息是从ApplicationInfo信息中获取的,至于是怎么获取,这里就不再解释,童鞋们可以自己研究。所以这里的((ShortcutInfo) tag).intent信息大家就很明白了,是可以启动一个应用的,前面说过,不再解释。至于下面的FolderInfo就是点击文件夹时做了什么,这里不讲解了,自己研究。然后看看主菜单点击时是如何启动应用的,这部分在AppsCustomizePagedView中
<p></p>
<p></p>
<pre class = "brush:java;" >    public void onClick(View v) {
         // When we have exited all apps or are in transition, disregard clicks
         if (!mLauncher.isAllAppsCustomizeOpen() ||
                 mLauncher.getWorkspace().isSwitchingState()) return ;
 
         if (v instanceof PagedViewIcon) {
             // Animate some feedback to the click
             final ApplicationInfo appInfo = (ApplicationInfo) v.getTag();
             // bug 211336 begin
             //if (OptConfig.LC_RAM_SUPPORT) {
                 // remove the animation when click
                 mLauncher.startActivitySafely(appInfo.intent, appInfo);
             //} else {
             //    animateClickFeedback(v, new Runnable() {
             //        @Override
             //        public void run() {
             //            mLauncher.startActivitySafely(appInfo.intent, appInfo);
             //        }
             //    });
             //}
             // bug 211336 end
         } else if (v instanceof PagedViewWidget) {
             // Let the user know that they have to long press to add a widget
             Toast.makeText(getContext(), R.string.long_press_widget_to_add,
                     Toast.LENGTH_SHORT).show();
 
             // Create a little animation to show that the widget can move
             float offsetY = getResources().getDimensionPixelSize(R.dimen.dragViewOffsetY);
             final ImageView p = (ImageView) v.findViewById(R.id.widget_preview);
             AnimatorSet bounce = new AnimatorSet();
             ValueAnimator tyuAnim = ObjectAnimator.ofFloat(p, "translationY" , offsetY);
             tyuAnim.setDuration( 125 );
             ValueAnimator tydAnim = ObjectAnimator.ofFloat(p, "translationY" , 0f);
             tydAnim.setDuration( 100 );
             bounce.play(tyuAnim).before(tydAnim);
             bounce.setInterpolator( new AccelerateInterpolator());
             bounce.start();
         }
     }</pre><br>
两部分,一部分是点击主菜单图标,一部分是点击Widget,也就是长按Widget添加到Workspace待机,这里只说点击主菜单图标也就是PagedViewIcon,单点击PagedViewIcon的时候就会获取到响应的ApplicationInfo信息,通过ApplicationInfo的intent来启动一个应用是完全可以的,这个我们前面已经强调过很多次了。
<p></p>
<p>    点击图标启动应用就讲这么多,不再多讲,接下来我们说说拖动图标或者widget。当我们长按时就可以从待机移动图标或widget,还可一从主菜单把图标或widget移动到待机,这个过程是怎么一回事呢?这里做一下讲解。先说在workspace待机拖动图标,我们从长按事件说起,这个在Launcher中</p>
<p></p>
<pre class = "brush:java;" >    public boolean onLongClick(View v) {
         if (mState != State.WORKSPACE) {
             return false ;
         }
 
         if (isWorkspaceLocked()) {
             return false ;
         }
 
         if (!(v instanceof CellLayout)) {
             v = (View) v.getParent().getParent();
         }
 
         resetAddInfo();
         CellLayout.CellInfo longClickCellInfo = (CellLayout.CellInfo) v.getTag();
         // This happens when long clicking an item with the dpad/trackball
         if (longClickCellInfo == null ) {
             return true ;
         }
 
         // The hotseat touch handling does not go through Workspace, and we always allow long press
         // on hotseat items.
         final View itemUnderLongClick = longClickCellInfo.cell;
         boolean allowLongPress = isHotseatLayout(v) || mWorkspace.allowLongPress();
         if (allowLongPress && !mDragController.isDragging()) {
             if (itemUnderLongClick == null ) {
                 // User long pressed on empty space
                 mWorkspace.performHapticFeedback(HapticFeedbackConstants.LONG_PRESS,
                         HapticFeedbackConstants.FLAG_IGNORE_VIEW_SETTING);
                 startWallpaper();
             } else {
                 if (!(itemUnderLongClick instanceof Folder)) {
                     // User long pressed on an item
                     mWorkspace.startDrag(longClickCellInfo);
                 }
             }
         }
         return true ;
     }</pre><br>
也就是mWorkspace.startDrag(longClickCellInfo)其它内容不做讲解,感兴趣可以看看。mWorkspace.startDrag(longClickCellInfo)也就是
<p></p>
<p></p>
<pre class = "brush:java;" >    void startDrag(CellLayout.CellInfo cellInfo) {
         View child = cellInfo.cell;
 
         // Make sure the drag was started by a long press as opposed to a long click.
         if (!child.isInTouchMode()) {
             return ;
         }
 
         mDragInfo = cellInfo;
         child.setVisibility(GONE);
 
         child.clearFocus();
         child.setPressed( false );
 
         final Canvas canvas = new Canvas();
 
         // We need to add extra padding to the bitmap to make room for the glow effect
         final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
 
         // The outline is used to visualize where the item will land if dropped
         mDragOutline = createDragOutline(child, canvas, bitmapPadding);
         beginDragShared(child, this );
     }</pre><br>
创建一个Bitmap为mDragOutline这个mDragOutline保存的是原始的图片,等到UP的时候也就是松手的时候会用到。然后就是beginDragShared也就是
<p></p>
<p></p>
<pre class = "brush:java;" >    public void beginDragShared(View child, DragSource source) {
         Resources r = getResources();
 
         // We need to add extra padding to the bitmap to make room for the glow effect
         final int bitmapPadding = HolographicOutlineHelper.MAX_OUTER_BLUR_RADIUS;
 
         // The drag bitmap follows the touch point around on the screen
         final Bitmap b = createDragBitmap(child, new Canvas(), bitmapPadding);
 
         final int bmpWidth = b.getWidth();
 
         mLauncher.getDragLayer().getLocationInDragLayer(child, mTempXY);
         final int dragLayerX = ( int ) mTempXY[ 0 ] + (child.getWidth() - bmpWidth) / 2 ;
         int dragLayerY = mTempXY[ 1 ] - bitmapPadding / 2 ;
 
         Point dragVisualizeOffset = null ;
         Rect dragRect = null ;
         if (child instanceof BubbleTextView || child instanceof PagedViewIcon) {
             int iconSize = r.getDimensionPixelSize(R.dimen.app_icon_size);
             int iconPaddingTop = r.getDimensionPixelSize(R.dimen.app_icon_padding_top);
             int top = child.getPaddingTop();
             int left = (bmpWidth - iconSize) / 2 ;
             int right = left + iconSize;
             int bottom = top + iconSize;
             dragLayerY += top;
             // Note: The drag region is used to calculate drag layer offsets, but the
             // dragVisualizeOffset in addition to the dragRect (the size) to position the outline.
             dragVisualizeOffset = new Point(-bitmapPadding / 2 , iconPaddingTop - bitmapPadding / 2 );
             dragRect = new Rect(left, top, right, bottom);
         } else if (child instanceof FolderIcon) {
             int previewSize = r.getDimensionPixelSize(R.dimen.folder_preview_size);
             dragRect = new Rect( 0 , 0 , child.getWidth(), previewSize);
         }
 
         // Clear the pressed state if necessary
         if (child instanceof BubbleTextView) {
             BubbleTextView icon = (BubbleTextView) child;
             icon.clearPressedOrFocusedBackground();
         }
 
         mDragController.startDrag(b, dragLayerX, dragLayerY, source, child.getTag(),
                 DragController.DRAG_ACTION_MOVE, dragVisualizeOffset, dragRect);
         b.recycle();
     }</pre><br>

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值