forked from irrenhaus/android_packages_apps_Launcher
-
Notifications
You must be signed in to change notification settings - Fork 150
Expand file tree
/
Copy pathLauncherModel.java
More file actions
1785 lines (1534 loc) · 74.4 KB
/
LauncherModel.java
File metadata and controls
1785 lines (1534 loc) · 74.4 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) 2008 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 org.adw.launcher;
import static android.util.Log.d;
import static android.util.Log.e;
import static android.util.Log.w;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.lang.ref.WeakReference;
import java.net.URISyntaxException;
import java.text.Collator;
import java.util.ArrayList;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.concurrent.atomic.AtomicInteger;
import org.adw.launcher.IconShader.CompiledIconShader;
import org.adw.launcher.catalogue.AppCatalogueFilters;
import android.content.ComponentName;
import android.content.ContentResolver;
import android.content.ContentValues;
import android.content.Context;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.pm.PackageManager;
import android.content.pm.ResolveInfo;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.content.res.XmlResourceParser;
import android.database.Cursor;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Rect;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Process;
/**
* Maintains in-memory state of the Launcher. It is expected that there should be only one
* LauncherModel object held in a static. Also provide APIs for updating the database state
* for the Launcher.
*/
public class LauncherModel {
static final boolean DEBUG_LOADERS = true;
static final String LOG_TAG = "HomeLoaders";
private static final int UI_NOTIFICATION_RATE = 4;
private static final int DEFAULT_APPLICATIONS_NUMBER = 42;
private static final int INITIAL_ICON_CACHE_CAPACITY = 50;
private static final Collator sCollator = Collator.getInstance();
private boolean mApplicationsLoaded;
private boolean mDesktopItemsLoaded;
private ArrayList<ItemInfo> mDesktopItems;
private ArrayList<LauncherAppWidgetInfo> mDesktopAppWidgets;
private HashMap<Long, FolderInfo> mFolders;
public static ArrayList<ApplicationInfo> mApplications;
public static ApplicationsAdapter mApplicationsAdapter;
private ApplicationsLoader mApplicationsLoader;
private DesktopItemsLoader mDesktopItemsLoader;
private Thread mApplicationsLoaderThread;
private Thread mDesktopLoaderThread;
private int mDesktopColumns;
private int mDesktopRows;
private final HashMap<ComponentName, ApplicationInfo> mAppInfoCache =
new HashMap<ComponentName, ApplicationInfo>(INITIAL_ICON_CACHE_CAPACITY);
private static String compiledIconShaderName;
private static CompiledIconShader compiledIconShader;
synchronized void abortLoaders() {
if (DEBUG_LOADERS) d(LOG_TAG, "aborting loaders");
if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) {
if (DEBUG_LOADERS) d(LOG_TAG, " --> aborting applications loader");
mApplicationsLoader.stop();
}
if (mDesktopItemsLoader != null && mDesktopItemsLoader.isRunning()) {
if (DEBUG_LOADERS) d(LOG_TAG, " --> aborting workspace loader");
mDesktopItemsLoader.stop();
mDesktopItemsLoaded = false;
}
}
/**
* Drop our cache of components to their lables & icons. We do
* this from Launcher when applications are added/removed. It's a
* bit overkill, but it's a rare operation anyway.
*/
synchronized void dropApplicationCache() {
mAppInfoCache.clear();
}
/**
* Loads the list of installed applications in mApplications.
*
* @return true if the applications loader must be started
* (see startApplicationsLoader()), false otherwise.
*/
synchronized boolean loadApplications(boolean isLaunching, Launcher launcher,
boolean localeChanged) {
if (DEBUG_LOADERS) d(LOG_TAG, "load applications");
if (isLaunching && mApplicationsLoaded && !localeChanged) {
mApplicationsAdapter = new ApplicationsAdapter(launcher, mApplications,
AppCatalogueFilters.getInstance().getDrawerFilter());
if (DEBUG_LOADERS) d(LOG_TAG, " --> applications loaded, return");
return false;
}
stopAndWaitForApplicationsLoader();
if (localeChanged) {
dropApplicationCache();
}
if (mApplicationsAdapter == null || isLaunching || localeChanged) {
mApplications = new ArrayList<ApplicationInfo>(DEFAULT_APPLICATIONS_NUMBER);
mApplicationsAdapter = new ApplicationsAdapter(launcher, mApplications,
AppCatalogueFilters.getInstance().getDrawerFilter());
}
mApplicationsLoaded = false;
if (!isLaunching) {
startApplicationsLoaderLocked(launcher, false);
return false;
}
return true;
}
private synchronized void stopAndWaitForApplicationsLoader() {
if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) {
if (DEBUG_LOADERS) {
d(LOG_TAG, " --> wait for applications loader (" + mApplicationsLoader.mId + ")");
}
mApplicationsLoader.stop();
// Wait for the currently running thread to finish, this can take a little
// time but it should be well below the timeout limit
try {
mApplicationsLoaderThread.join();
} catch (InterruptedException e) {
e(LOG_TAG, "mApplicationsLoaderThread didn't exit in time");
}
}
}
private synchronized void startApplicationsLoader(Launcher launcher, boolean isLaunching) {
if (DEBUG_LOADERS) d(LOG_TAG, " --> starting applications loader unlocked");
startApplicationsLoaderLocked(launcher, isLaunching);
}
private void startApplicationsLoaderLocked(Launcher launcher, boolean isLaunching) {
if (DEBUG_LOADERS) d(LOG_TAG, " --> starting applications loader");
stopAndWaitForApplicationsLoader();
mApplicationsLoader = new ApplicationsLoader(launcher, isLaunching);
mApplicationsLoaderThread = new Thread(mApplicationsLoader, "Applications Loader");
mApplicationsLoaderThread.start();
}
synchronized void addPackage(Launcher launcher, String packageName) {
if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) {
startApplicationsLoaderLocked(launcher, false);
return;
}
if (packageName != null && packageName.length() > 0 && mApplicationsAdapter!=null) {
final PackageManager packageManager = launcher.getPackageManager();
final List<ResolveInfo> matches = findActivitiesForPackage(packageManager, packageName);
if (matches.size() > 0) {
final ApplicationsAdapter adapter = mApplicationsAdapter;
final HashMap<ComponentName, ApplicationInfo> cache = mAppInfoCache;
for (ResolveInfo info : matches) {
adapter.setNotifyOnChange(false);
adapter.add(makeAndCacheApplicationInfo(packageManager, cache, info, launcher));
}
adapter.updateDataSet();
//adapter.sort(new ApplicationInfoComparator());
//adapter.notifyDataSetChanged();
}
}
}
synchronized void removePackage(Launcher launcher, String packageName) {
// for add/remove Package, we need use applications adapter's "full" list.
if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) {
dropApplicationCache(); // TODO: this could be optimized
startApplicationsLoaderLocked(launcher, false);
return;
}
if (packageName != null && packageName.length() > 0 && mApplicationsAdapter!=null) {
final ApplicationsAdapter adapter = mApplicationsAdapter;
final List<ApplicationInfo> toRemove = new ArrayList<ApplicationInfo>();
final ArrayList<ApplicationInfo> allItems = adapter.allItems;
final int count = allItems.size();
for (int i = 0; i < count; i++) {
final ApplicationInfo applicationInfo = allItems.get(i);
final Intent intent = applicationInfo.intent;
final ComponentName component = intent.getComponent();
if (packageName.equals(component.getPackageName())) {
toRemove.add(applicationInfo);
}
}
final HashMap<ComponentName, ApplicationInfo> cache = mAppInfoCache;
for (ApplicationInfo info : toRemove) {
adapter.setNotifyOnChange(false);
adapter.remove(info);
cache.remove(info.intent.getComponent());
}
if (toRemove.size() > 0) {
adapter.updateDataSet();
//adapter.sort(new ApplicationInfoComparator());
//adapter.notifyDataSetChanged();
}
}
}
synchronized void updatePackage(Launcher launcher, String packageName) {
if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) {
startApplicationsLoaderLocked(launcher, false);
return;
}
if (packageName != null && packageName.length() > 0 && mApplicationsAdapter!=null) {
final PackageManager packageManager = launcher.getPackageManager();
final ApplicationsAdapter adapter = mApplicationsAdapter;
final List<ResolveInfo> matches = findActivitiesForPackage(packageManager, packageName);
final int count = matches.size();
boolean changed = false;
for (int i = 0; i < count; i++) {
final ResolveInfo info = matches.get(i);
final ApplicationInfo applicationInfo = findIntent(adapter,
info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
if (applicationInfo != null) {
updateAndCacheApplicationInfo(packageManager, info, applicationInfo, launcher);
changed = true;
}
}
if (syncLocked(launcher, packageName)) changed = true;
if (changed) {
adapter.updateDataSet();
//adapter.sort(new ApplicationInfoComparator());
//adapter.notifyDataSetChanged();
}
}
}
private void updateAndCacheApplicationInfo(PackageManager packageManager, ResolveInfo info,
ApplicationInfo applicationInfo, Context context) {
updateApplicationInfoTitleAndIcon(packageManager, info, applicationInfo, context);
ComponentName componentName = new ComponentName(
info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
mAppInfoCache.put(componentName, applicationInfo);
}
synchronized void syncPackage(Launcher launcher, String packageName) {
if (mApplicationsLoader != null && mApplicationsLoader.isRunning()) {
startApplicationsLoaderLocked(launcher, false);
return;
}
if (packageName != null && packageName.length() > 0 && mApplicationsAdapter!=null) {
if (syncLocked(launcher, packageName)) {
final ApplicationsAdapter adapter = mApplicationsAdapter;
adapter.updateDataSet();
//adapter.sort(new ApplicationInfoComparator());
//adapter.notifyDataSetChanged();
}
}
}
private boolean syncLocked(Launcher launcher, String packageName) {
final PackageManager packageManager = launcher.getPackageManager();
final List<ResolveInfo> matches = findActivitiesForPackage(packageManager, packageName);
if (matches.size() > 0 && mApplicationsAdapter!=null) {
final ApplicationsAdapter adapter = mApplicationsAdapter;
// Find disabled activities and remove them from the adapter
boolean removed = removeDisabledActivities(packageName, matches, adapter);
// Find enable activities and add them to the adapter
// Also updates existing activities with new labels/icons
boolean added = addEnabledAndUpdateActivities(matches, adapter, launcher);
return added || removed;
}
return false;
}
private static List<ResolveInfo> findActivitiesForPackage(PackageManager packageManager,
String packageName) {
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final List<ResolveInfo> apps = packageManager.queryIntentActivities(mainIntent, 0);
final List<ResolveInfo> matches = new ArrayList<ResolveInfo>();
if (apps != null) {
// Find all activities that match the packageName
int count = apps.size();
for (int i = 0; i < count; i++) {
final ResolveInfo info = apps.get(i);
final ActivityInfo activityInfo = info.activityInfo;
if (packageName.equals(activityInfo.packageName)) {
matches.add(info);
}
}
}
return matches;
}
private boolean addEnabledAndUpdateActivities(List<ResolveInfo> matches,
ApplicationsAdapter adapter, Launcher launcher) {
final List<ApplicationInfo> toAdd = new ArrayList<ApplicationInfo>();
final int count = matches.size();
boolean changed = false;
for (int i = 0; i < count; i++) {
final ResolveInfo info = matches.get(i);
final ApplicationInfo applicationInfo = findIntent(adapter,
info.activityInfo.applicationInfo.packageName, info.activityInfo.name);
if (applicationInfo == null) {
toAdd.add(makeAndCacheApplicationInfo(launcher.getPackageManager(),
mAppInfoCache, info, launcher));
changed = true;
} else {
updateAndCacheApplicationInfo(
launcher.getPackageManager(), info, applicationInfo, launcher);
changed = true;
}
}
for (ApplicationInfo info : toAdd) {
adapter.setNotifyOnChange(false);
adapter.add(info);
}
return changed;
}
private boolean removeDisabledActivities(String packageName, List<ResolveInfo> matches,
ApplicationsAdapter adapter) {
final List<ApplicationInfo> toRemove = new ArrayList<ApplicationInfo>();
final int count = adapter.getCount();
boolean changed = false;
for (int i = 0; i < count; i++) {
final ApplicationInfo applicationInfo = adapter.getItem(i);
final Intent intent = applicationInfo.intent;
final ComponentName component = intent.getComponent();
if (packageName.equals(component.getPackageName())) {
if (!findIntent(matches, component)) {
toRemove.add(applicationInfo);
changed = true;
}
}
}
final HashMap<ComponentName, ApplicationInfo> cache = mAppInfoCache;
for (ApplicationInfo info : toRemove) {
adapter.setNotifyOnChange(false);
adapter.remove(info);
cache.remove(info.intent.getComponent());
}
return changed;
}
private static ApplicationInfo findIntent(ApplicationsAdapter adapter, String packageName,
String name) {
final int count = adapter.getCount();
for (int i = 0; i < count; i++) {
final ApplicationInfo applicationInfo = adapter.getItem(i);
final Intent intent = applicationInfo.intent;
final ComponentName component = intent.getComponent();
if (packageName.equals(component.getPackageName()) &&
name.equals(component.getClassName())) {
return applicationInfo;
}
}
return null;
}
private static boolean findIntent(List<ResolveInfo> apps, ComponentName component) {
final String className = component.getClassName();
for (ResolveInfo info : apps) {
final ActivityInfo activityInfo = info.activityInfo;
if (activityInfo.name.equals(className)) {
return true;
}
}
return false;
}
Drawable getApplicationInfoIcon(PackageManager manager, ApplicationInfo info, Context context) {
final ResolveInfo resolveInfo = manager.resolveActivity(info.intent, 0);
if (resolveInfo == null || info.customIcon) {
return null;
}
ComponentName componentName = new ComponentName(
resolveInfo.activityInfo.applicationInfo.packageName,
resolveInfo.activityInfo.name);
ApplicationInfo application = mAppInfoCache.get(componentName);
if (application == null) {
//return resolveInfo.activityInfo.loadIcon(manager);
return getIcon(manager, context, resolveInfo.activityInfo);
}
return application.icon;
}
private static ApplicationInfo makeAndCacheApplicationInfo(PackageManager manager,
HashMap<ComponentName, ApplicationInfo> appInfoCache, ResolveInfo info,
Context context) {
ComponentName componentName = new ComponentName(
info.activityInfo.applicationInfo.packageName,
info.activityInfo.name);
ApplicationInfo application = appInfoCache.get(componentName);
if (application == null) {
application = new ApplicationInfo();
application.container = ItemInfo.NO_ID;
updateApplicationInfoTitleAndIcon(manager, info, application, context);
application.setActivity(componentName,
Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_RESET_TASK_IF_NEEDED);
appInfoCache.put(componentName, application);
}
return application;
}
private static void updateApplicationInfoTitleAndIcon(PackageManager manager, ResolveInfo info,
ApplicationInfo application, Context context) {
application.title = info.loadLabel(manager);
if (application.title == null) {
application.title = info.activityInfo.name;
}
application.icon = getIcon(manager, context, info.activityInfo);
application.filtered = false;
}
private static final AtomicInteger sAppsLoaderCount = new AtomicInteger(1);
private static final AtomicInteger sWorkspaceLoaderCount = new AtomicInteger(1);
private class ApplicationsLoader implements Runnable {
private final WeakReference<Launcher> mLauncher;
private volatile boolean mStopped;
private volatile boolean mRunning;
private final boolean mIsLaunching;
private final int mId;
ApplicationsLoader(Launcher launcher, boolean isLaunching) {
mIsLaunching = isLaunching;
mLauncher = new WeakReference<Launcher>(launcher);
mRunning = true;
mId = sAppsLoaderCount.getAndIncrement();
}
void stop() {
mStopped = true;
}
boolean isRunning() {
return mRunning;
}
public void run() {
if (DEBUG_LOADERS) d(LOG_TAG, " ----> running applications loader (" + mId + ")");
// Elevate priority when Home launches for the first time to avoid
// starving at boot time. Staring at a blank home is not cool.
android.os.Process.setThreadPriority(mIsLaunching ? Process.THREAD_PRIORITY_DEFAULT :
Process.THREAD_PRIORITY_BACKGROUND);
final Intent mainIntent = new Intent(Intent.ACTION_MAIN, null);
mainIntent.addCategory(Intent.CATEGORY_LAUNCHER);
final Launcher launcher = mLauncher.get();
final PackageManager manager = launcher.getPackageManager();
final List<ResolveInfo> apps = manager.queryIntentActivities(mainIntent, 0);
if (apps != null && !mStopped) {
final int count = apps.size();
// Can be set to null on the UI thread by the unbind() method
// Do not access without checking for null first
final ApplicationsAdapter applicationList = mApplicationsAdapter;
ChangeNotifier action = new ChangeNotifier(applicationList, true);
final HashMap<ComponentName, ApplicationInfo> appInfoCache = mAppInfoCache;
for (int i = 0; i < count && !mStopped; i++) {
ResolveInfo info = apps.get(i);
ApplicationInfo application =
makeAndCacheApplicationInfo(manager, appInfoCache, info, launcher);
if (action.add(application) && !mStopped) {
launcher.runOnUiThread(action);
action = new ChangeNotifier(applicationList, false);
}
}
launcher.runOnUiThread(action);
}
/* If we've made it this far and mStopped isn't set, we've successfully loaded
* applications. Otherwise, applications aren't loaded. */
mApplicationsLoaded = !mStopped;
if (mStopped) {
if (DEBUG_LOADERS) d(LOG_TAG, " ----> applications loader stopped (" + mId + ")");
}
mRunning = false;
}
}
private static class ChangeNotifier implements Runnable {
private final ApplicationsAdapter mApplicationList;
private final ArrayList<ApplicationInfo> mBuffer;
private boolean mFirst = true;
ChangeNotifier(ApplicationsAdapter applicationList, boolean first) {
mApplicationList = applicationList;
mFirst = first;
mBuffer = new ArrayList<ApplicationInfo>(UI_NOTIFICATION_RATE);
}
public void run() {
final ApplicationsAdapter applicationList = mApplicationList;
// Can be set to null on the UI thread by the unbind() method
if (applicationList == null) return;
if (mFirst) {
applicationList.setNotifyOnChange(false);
applicationList.clear();
if (DEBUG_LOADERS) d(LOG_TAG, " ----> cleared application list");
mFirst = false;
}
final ArrayList<ApplicationInfo> buffer = mBuffer;
final int count = buffer.size();
for (int i = 0; i < count; i++) {
applicationList.setNotifyOnChange(false);
applicationList.add(buffer.get(i));
}
buffer.clear();
applicationList.updateDataSet();
//applicationList.sort(new ApplicationInfoComparator());
//applicationList.notifyDataSetChanged();
}
boolean add(ApplicationInfo application) {
final ArrayList<ApplicationInfo> buffer = mBuffer;
buffer.add(application);
return buffer.size() >= UI_NOTIFICATION_RATE;
}
}
static class ApplicationInfoComparator implements Comparator<ApplicationInfo> {
public final int compare(ApplicationInfo a, ApplicationInfo b) {
return sCollator.compare(a.title.toString(), b.title.toString());
}
}
boolean isDesktopLoaded() {
return mDesktopItems != null && mDesktopAppWidgets != null && mDesktopItemsLoaded;
}
/**
* Loads all of the items on the desktop, in folders, or in the dock.
* These can be apps, shortcuts or widgets
*/
void loadUserItems(boolean isLaunching, Launcher launcher, boolean localeChanged,
boolean loadApplications) {
if (DEBUG_LOADERS) d(LOG_TAG, "loading user items in " + Thread.currentThread().toString());
//ADW: load columns/rows settings
mDesktopRows=AlmostNexusSettingsHelper.getDesktopRows(launcher);
mDesktopColumns=AlmostNexusSettingsHelper.getDesktopColumns(launcher);
if (isLaunching && isDesktopLoaded()) {
if (DEBUG_LOADERS) d(LOG_TAG, " --> items loaded, return");
if (loadApplications) startApplicationsLoader(launcher, true);
//if (SystemProperties.get("debug.launcher.ignore-cache", "") == "") {
// We have already loaded our data from the DB
if (DEBUG_LOADERS) d(LOG_TAG, " --> loading from cache: " + mDesktopItems.size() + ", " + mDesktopAppWidgets.size());
launcher.onDesktopItemsLoaded(mDesktopItems, mDesktopAppWidgets);
return;
//}
//else
//{
//d(LOG_TAG, " ----> debug: forcing reload of workspace");
//}
}
if (mDesktopItemsLoader != null && mDesktopItemsLoader.isRunning()) {
if (DEBUG_LOADERS) d(LOG_TAG, " --> stopping workspace loader");
mDesktopItemsLoader.stop();
// Wait for the currently running thread to finish, this can take a little
// time but it should be well below the timeout limit
try {
mDesktopLoaderThread.join();
} catch (InterruptedException e) {
e(LOG_TAG, "mDesktopLoaderThread didn't exit in time");
}
// If the thread we are interrupting was tasked to load the list of
// applications make sure we keep that information in the new loader
// spawned below
// note: we don't apply this to localeChanged because the thread can
// only be stopped *after* the localeChanged handling has occured
loadApplications = mDesktopItemsLoader.mLoadApplications;
}
if (DEBUG_LOADERS) d(LOG_TAG, " --> starting workspace loader");
mDesktopItemsLoaded = false;
mDesktopItemsLoader = new DesktopItemsLoader(launcher, localeChanged, loadApplications,
isLaunching);
mDesktopLoaderThread = new Thread(mDesktopItemsLoader, "Desktop Items Loader");
mDesktopLoaderThread.start();
}
private static String getLabel(PackageManager manager, ActivityInfo activityInfo) {
String label = activityInfo.loadLabel(manager).toString();
if (label == null) {
label = manager.getApplicationLabel(activityInfo.applicationInfo).toString();
if (label == null) {
label = activityInfo.name;
}
}
return label;
}
static ApplicationInfo loadApplicationInfoById(Context context, long Id) {
final ContentResolver contentResolver = context.getContentResolver();
final PackageManager manager = context.getPackageManager();
final Cursor c = contentResolver.query(
LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
if (c == null)
return null;
try
{
c.moveToFirst();
while(!c.isAfterLast())
{
final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
final long id = c.getLong(idIndex);
if (id == Id)
{
final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
int container;
Intent intent;
final int itemType = c.getInt(itemTypeIndex);
if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION ||
itemType == LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT)
{
String intentDescription = c.getString(intentIndex);
try {
intent = Intent.parseUri(intentDescription, 0);
} catch (java.net.URISyntaxException e) {
return null;
}
ApplicationInfo info;
if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
info = getApplicationInfo(manager, intent, context);
} else {
info = getApplicationInfoShortcut(c, context, iconTypeIndex,
iconPackageIndex, iconResourceIndex, iconIndex);
}
if (info == null) {
info = new ApplicationInfo();
info.icon = manager.getDefaultActivityIcon();
}
if (info != null) {
info.title = c.getString(titleIndex);
info.intent = intent;
info.id = id;
container = c.getInt(containerIndex);
info.container = container;
info.screen = c.getInt(screenIndex);
info.cellX = c.getInt(cellXIndex);
info.cellY = c.getInt(cellYIndex);
}
return info;
}
}
c.moveToNext();
}
}
finally
{
c.close();
}
return null;
}
private class DesktopItemsLoader implements Runnable {
private volatile boolean mStopped;
private volatile boolean mFinished;
private final WeakReference<Launcher> mLauncher;
private final boolean mLocaleChanged;
private final boolean mLoadApplications;
private final boolean mIsLaunching;
private final int mId;
DesktopItemsLoader(Launcher launcher, boolean localeChanged, boolean loadApplications,
boolean isLaunching) {
mLoadApplications = loadApplications;
mIsLaunching = isLaunching;
mLauncher = new WeakReference<Launcher>(launcher);
mLocaleChanged = localeChanged;
mId = sWorkspaceLoaderCount.getAndIncrement();
mFinished = false;
}
void stop() {
d(LOG_TAG, " ----> workspace loader " + mId + " stopped from " + Thread.currentThread().toString());
mStopped = true;
}
boolean isRunning() {
return !mFinished;
}
public void run() {
assert(!mFinished); // can only run once
load_workspace();
mFinished = true;
}
private void load_workspace() {
if (DEBUG_LOADERS) d(LOG_TAG, " ----> running workspace loader (" + mId + ")");
android.os.Process.setThreadPriority(Process.THREAD_PRIORITY_DEFAULT);
final Launcher launcher = mLauncher.get();
final ContentResolver contentResolver = launcher.getContentResolver();
final PackageManager manager = launcher.getPackageManager();
if (mLocaleChanged) {
updateShortcutLabels(contentResolver, manager);
}
final ArrayList<ItemInfo> desktopItems = new ArrayList<ItemInfo>();
final ArrayList<LauncherAppWidgetInfo> desktopAppWidgets = new ArrayList<LauncherAppWidgetInfo>();
final HashMap<Long, FolderInfo> folders = new HashMap<Long, FolderInfo>();
final Cursor c = contentResolver.query(
LauncherSettings.Favorites.CONTENT_URI, null, null, null, null);
try {
final int idIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites._ID);
final int intentIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.INTENT);
final int titleIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.TITLE);
final int iconTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_TYPE);
final int iconIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON);
final int iconPackageIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_PACKAGE);
final int iconResourceIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ICON_RESOURCE);
final int containerIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CONTAINER);
final int itemTypeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.ITEM_TYPE);
final int appWidgetIdIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.APPWIDGET_ID);
final int screenIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SCREEN);
final int cellXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLX);
final int cellYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.CELLY);
final int spanXIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANX);
final int spanYIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.SPANY);
final int uriIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.URI);
final int displayModeIndex = c.getColumnIndexOrThrow(LauncherSettings.Favorites.DISPLAY_MODE);
ApplicationInfo info;
String intentDescription;
Widget widgetInfo;
LauncherAppWidgetInfo appWidgetInfo;
int container;
long id;
Intent intent;
while (!mStopped && c.moveToNext()) {
try {
int itemType = c.getInt(itemTypeIndex);
switch (itemType) {
case LauncherSettings.Favorites.ITEM_TYPE_APPLICATION:
case LauncherSettings.Favorites.ITEM_TYPE_SHORTCUT:
intentDescription = c.getString(intentIndex);
try {
intent = Intent.parseUri(intentDescription, 0);
} catch (java.net.URISyntaxException e) {
continue;
}
if (itemType == LauncherSettings.Favorites.ITEM_TYPE_APPLICATION) {
info = getApplicationInfo(manager, intent, launcher);
} else {
info = getApplicationInfoShortcut(c, launcher, iconTypeIndex,
iconPackageIndex, iconResourceIndex, iconIndex);
}
if (info == null) {
info = new ApplicationInfo();
info.icon = manager.getDefaultActivityIcon();
}
if (info != null) {
info.title = c.getString(titleIndex);
info.intent = intent;
info.id = c.getLong(idIndex);
container = c.getInt(containerIndex);
info.container = container;
info.screen = c.getInt(screenIndex);
info.cellX = c.getInt(cellXIndex);
info.cellY = c.getInt(cellYIndex);
switch (container) {
case LauncherSettings.Favorites.CONTAINER_DESKTOP:
case LauncherSettings.Favorites.CONTAINER_DOCKBAR:
case LauncherSettings.Favorites.CONTAINER_LAB:
case LauncherSettings.Favorites.CONTAINER_RAB:
case LauncherSettings.Favorites.CONTAINER_LAB2:
case LauncherSettings.Favorites.CONTAINER_RAB2:
case LauncherSettings.Favorites.CONTAINER_MAB:
desktopItems.add(info);
break;
default:
// Item is in a user folder
UserFolderInfo folderInfo =
findOrMakeUserFolder(folders, container);
folderInfo.add(info);
break;
}
}
break;
case LauncherSettings.Favorites.ITEM_TYPE_USER_FOLDER:
id = c.getLong(idIndex);
UserFolderInfo folderInfo = findOrMakeUserFolder(folders, id);
folderInfo.title = c.getString(titleIndex);
folderInfo.id = id;
container = c.getInt(containerIndex);
folderInfo.container = container;
folderInfo.screen = c.getInt(screenIndex);
folderInfo.cellX = c.getInt(cellXIndex);
folderInfo.cellY = c.getInt(cellYIndex);
switch (container) {
case LauncherSettings.Favorites.CONTAINER_DESKTOP:
case LauncherSettings.Favorites.CONTAINER_DOCKBAR:
case LauncherSettings.Favorites.CONTAINER_LAB:
case LauncherSettings.Favorites.CONTAINER_RAB:
case LauncherSettings.Favorites.CONTAINER_LAB2:
case LauncherSettings.Favorites.CONTAINER_RAB2:
case LauncherSettings.Favorites.CONTAINER_MAB:
desktopItems.add(folderInfo);
break;
}
break;
case LauncherSettings.Favorites.ITEM_TYPE_LIVE_FOLDER:
id = c.getLong(idIndex);
LiveFolderInfo liveFolderInfo = findOrMakeLiveFolder(folders, id);
intentDescription = c.getString(intentIndex);
intent = null;
if (intentDescription != null) {
try {
intent = Intent.parseUri(intentDescription, 0);
} catch (java.net.URISyntaxException e) {
// Ignore, a live folder might not have a base intent
}
}
liveFolderInfo.title = c.getString(titleIndex);
liveFolderInfo.id = id;
container = c.getInt(containerIndex);
liveFolderInfo.container = container;
liveFolderInfo.screen = c.getInt(screenIndex);
liveFolderInfo.cellX = c.getInt(cellXIndex);
liveFolderInfo.cellY = c.getInt(cellYIndex);
liveFolderInfo.uri = Uri.parse(c.getString(uriIndex));
liveFolderInfo.baseIntent = intent;
liveFolderInfo.displayMode = c.getInt(displayModeIndex);
loadLiveFolderIcon(launcher, c, iconTypeIndex, iconPackageIndex,
iconResourceIndex, liveFolderInfo);
switch (container) {
case LauncherSettings.Favorites.CONTAINER_DESKTOP:
case LauncherSettings.Favorites.CONTAINER_DOCKBAR:
case LauncherSettings.Favorites.CONTAINER_LAB:
case LauncherSettings.Favorites.CONTAINER_RAB:
case LauncherSettings.Favorites.CONTAINER_LAB2:
case LauncherSettings.Favorites.CONTAINER_RAB2:
case LauncherSettings.Favorites.CONTAINER_MAB:
desktopItems.add(liveFolderInfo);
break;
}
break;
case LauncherSettings.Favorites.ITEM_TYPE_WIDGET_SEARCH:
widgetInfo = Widget.makeSearch();
container = c.getInt(containerIndex);
if (container != LauncherSettings.Favorites.CONTAINER_DESKTOP) {
e(Launcher.LOG_TAG, "Widget found where container "
+ "!= CONTAINER_DESKTOP ignoring!");
continue;
}
widgetInfo.id = c.getLong(idIndex);
widgetInfo.screen = c.getInt(screenIndex);
widgetInfo.container = container;
widgetInfo.cellX = c.getInt(cellXIndex);
widgetInfo.cellY = c.getInt(cellYIndex);
widgetInfo.spanX = c.getInt(spanXIndex);
widgetInfo.spanY = c.getInt(spanYIndex);