summaryrefslogtreecommitdiffstats
path: root/src/com/android/settings/ManageApplications.java
blob: 512e547e6d428c5a234f9a012c12260f40ec8eb8 (plain)
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
/*
 * Copyright (C) 2006 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 com.android.settings;

import com.android.settings.R;
import android.app.ActivityManager;
import android.app.AlertDialog;
import android.app.ListActivity;
import android.app.ProgressDialog;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.IntentFilter;
import android.content.pm.ApplicationInfo;
import android.content.pm.IPackageStatsObserver;
import android.content.pm.PackageManager;
import android.content.pm.PackageStats;
import android.content.pm.PackageManager.NameNotFoundException;
import android.content.res.Resources;
import android.graphics.drawable.Drawable;
import android.net.Uri;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.text.format.Formatter;
import android.util.Config;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.Menu;
import android.view.MenuItem;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.widget.AdapterView;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.AdapterView.OnItemClickListener;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.TreeMap;

/**
 * Activity to pick an application that will be used to display installation information and
 * options to uninstall/delete user data for system applications. This activity
 * can be launched through Settings or via the ACTION_MANAGE_PACKAGE_STORAGE
 * intent.
 *  Initially a compute in progress message is displayed while the application retrieves
 *  the list of application information from the PackageManager. The size information
 *  for each package is refreshed to the screen. The resource(app description and
 *  icon) information for each package is not available yet, so some default values for size
 *  icon and descriptions are used initially. Later the resource information for each 
 *  application is retrieved and dynamically updated on the screen.
 *  A Broadcast receiver registers for package additions or deletions when the activity is
 *  in focus. If the user installs or deletes packages when the activity has focus, the receiver
 *  gets notified and proceeds to add/delete these packages from the list on the screen.
 *  This is an unlikely scenario but could happen. The entire list gets created every time
 *  the activity's onStart gets invoked. This is to avoid having the receiver for the entire
 *  life cycle of the application.
 *  The applications can be sorted either alphabetically or 
 *  based on size(descending). If this activity gets launched under low memory
 *  situations(A low memory notification dispatches intent 
 *  ACTION_MANAGE_PACKAGE_STORAGE) the list is sorted per size.
 *  If the user selects an application, extended info(like size, uninstall/clear data options,
 *  permissions info etc.,) is displayed via the InstalledAppDetails activity.
 *  This activity passes the package name and size information to the 
 *  InstalledAppDetailsActivity to avoid recomputation of the package size information.
 */
public class ManageApplications extends ListActivity implements
        OnItemClickListener, DialogInterface.OnCancelListener,
        DialogInterface.OnClickListener {
    // TAG for this activity
    private static final String TAG = "ManageApplications";
    
    // log information boolean
    private boolean localLOGV = Config.LOGV || false;
    
    // attributes used as keys when passing values to InstalledAppDetails activity
    public static final String APP_PKG_PREFIX = "com.android.settings.";
    public static final String APP_PKG_NAME = APP_PKG_PREFIX+"ApplicationPkgName";
    public static final String APP_PKG_SIZE = APP_PKG_PREFIX+"size";
    public static final String APP_CHG = APP_PKG_PREFIX+"changed";
    
    // attribute name used in receiver for tagging names of added/deleted packages
    private static final String ATTR_PKG_NAME="PackageName";
    private static final String ATTR_APP_PKG_STATS="ApplicationPackageStats";
    
    // constant value that can be used to check return code from sub activity.
    private static final int INSTALLED_APP_DETAILS = 1;
    
    // sort order that can be changed through the menu can be sorted alphabetically
    // or size(descending)
    private static final int MENU_OPTIONS_BASE = 0;
    public static final int SORT_ORDER_ALPHA = MENU_OPTIONS_BASE + 0;
    public static final int SORT_ORDER_SIZE = MENU_OPTIONS_BASE + 1;
    // Filter options used for displayed list of applications
    public static final int FILTER_APPS_ALL = MENU_OPTIONS_BASE + 2;
    public static final int FILTER_APPS_THIRD_PARTY = MENU_OPTIONS_BASE + 3;
    public static final int FILTER_APPS_RUNNING = MENU_OPTIONS_BASE + 4;
    public static final int FILTER_OPTIONS = MENU_OPTIONS_BASE + 5;
    // Alert Dialog presented to user to find out the filter option
    AlertDialog.Builder mAlertDlgBuilder;
    // sort order
    private int mSortOrder = SORT_ORDER_ALPHA;
    // Filter value
    int mFilterApps = FILTER_APPS_ALL;
    
    // Custom Adapter used for managing items in the list
    private AppInfoAdapter mAppInfoAdapter;
    
    // messages posted to the handler
    private static final int HANDLER_MESSAGE_BASE = 0;
    private static final int INIT_PKG_INFO = HANDLER_MESSAGE_BASE+1;
    private static final int COMPUTE_PKG_SIZE_DONE = HANDLER_MESSAGE_BASE+2;
    private static final int REMOVE_PKG = HANDLER_MESSAGE_BASE+3;
    private static final int REORDER_LIST = HANDLER_MESSAGE_BASE+4;
    private static final int ADD_PKG_START = HANDLER_MESSAGE_BASE+5;
    private static final int ADD_PKG_DONE = HANDLER_MESSAGE_BASE+6;
    private static final int REFRESH_ICONS = HANDLER_MESSAGE_BASE+7;
    private static final int NEXT_LOAD_STEP = HANDLER_MESSAGE_BASE+8;
    
    // observer object used for computing pkg sizes
    private PkgSizeObserver mObserver;
    // local handle to PackageManager
    private PackageManager mPm;
    // Broadcast Receiver object that receives notifications for added/deleted
    // packages
    private PackageIntentReceiver mReceiver;
    // atomic variable used to track if computing pkg sizes is in progress. should be volatile?
    
    private boolean mComputeSizes = false;
    // default icon thats used when displaying applications initially before resource info is
    // retrieved
    private Drawable mDefaultAppIcon;
    
    // temporary dialog displayed while the application info loads
    private ProgressDialog mLoadingDlg = null;
    
    // compute index used to track the application size computations
    private int mComputeIndex;
    
    // Size resource used for packages whose size computation failed for some reason
    private CharSequence mInvalidSizeStr;
    private CharSequence mComputingSizeStr;
    
    // map used to store list of added and removed packages. Immutable Boolean
    // variables indicate if a package has been added or removed. If a package is
    // added or deleted multiple times a single entry with the latest operation will
    // be recorded in the map.
    private Map<String, Boolean> mAddRemoveMap;
    
    // layout inflater object used to inflate views
    private LayoutInflater mInflater;
    
    // invalid size value used initially and also when size retrieval through PackageManager
    // fails for whatever reason
    private static final int SIZE_INVALID = -1;
    
    // debug boolean variable to test delays from PackageManager API's
    private boolean DEBUG_PKG_DELAY = false;
    
    // Thread to load resources
    ResourceLoaderThread mResourceThread;
    
    String mCurrentPkgName;
    
    //TODO implement a cache system
    private Map<String, AppInfo> mAppPropCache;
    
    // empty message displayed when list is empty
    private TextView mEmptyView;
    
    // Boolean variables indicating state
    private boolean mLoadLabels = false;
    private boolean mSizesFirst = false;
    
    /*
     * Handler class to handle messages for various operations
     * Most of the operations that effect Application related data
     * are posted as messages to the handler to avoid synchronization
     * when accessing these structures.
     * When the size retrieval gets kicked off for the first time, a COMPUTE_PKG_SIZE_START
     * message is posted to the handler which invokes the getSizeInfo for the pkg at index 0
     * When the PackageManager's asynchronous call back through
     * PkgSizeObserver.onGetStatsCompleted gets invoked, the application resources like
     * label, description, icon etc., is loaded in the same thread and these values are
     * set on the observer. The observer then posts a COMPUTE_PKG_SIZE_DONE message
     * to the handler. This information is updated on the AppInfoAdapter associated with
     * the list view of this activity and size info retrieval is initiated for the next package as 
     * indicated by mComputeIndex
     * When a package gets added while the activity has focus, the PkgSizeObserver posts
     * ADD_PKG_START message to the handler.  If the computation is not in progress, the size
     * is retrieved for the newly added package through the observer object and the newly
     * installed app info is updated on the screen. If the computation is still in progress
     * the package is added to an internal structure and action deferred till the computation
     * is done for all the packages. 
     * When a package gets deleted, REMOVE_PKG is posted to the handler
     *  if computation is not in progress(as indicated by
     * mDoneIniting), the package is deleted from the displayed list of apps. If computation is
     * still in progress the package is added to an internal structure and action deferred till
     * the computation is done for all packages.
     * When the sizes of all packages is computed, the newly
     * added or removed packages are processed in order.
     * If the user changes the order in  which these applications are viewed by hitting the
     * menu key, REORDER_LIST message is posted to the handler. this sorts the list
     * of items based on the sort order.
     */
    private Handler mHandler = new Handler() {
        public void handleMessage(Message msg) {
            PackageStats ps;
            ApplicationInfo info;
            Bundle data;
            String pkgName = null;
            AppInfo appInfo;
            data = msg.getData();
            if(data != null) {
                pkgName = data.getString(ATTR_PKG_NAME);
            }
            switch (msg.what) {
            case INIT_PKG_INFO:
                if(localLOGV) Log.i(TAG, "Message INIT_PKG_INFO");
                setProgressBarIndeterminateVisibility(true);
                mComputeIndex = 0;
                // Retrieve the package list and init some structures
                initAppList(mFilterApps);
                mHandler.sendEmptyMessage(NEXT_LOAD_STEP);
                break;
            case COMPUTE_PKG_SIZE_DONE:
                if(localLOGV) Log.i(TAG, "Message COMPUTE_PKG_SIZE_DONE");
                if(pkgName == null) {
                     Log.w(TAG, "Ignoring message");
                     break;
                }
                ps = data.getParcelable(ATTR_APP_PKG_STATS);
                if(ps == null) {
                    Log.i(TAG, "Invalid package stats for package:"+pkgName);
                } else {
                    int pkgId = mAppInfoAdapter.getIndex(pkgName);
                    if(mComputeIndex != pkgId) {
                        //spurious call from stale observer
                        Log.w(TAG, "Stale call back from PkgSizeObserver");
                        break;
                    }
                    mAppInfoAdapter.updateAppSize(pkgName, ps);
                }
                mComputeIndex++;
                if (mComputeIndex < mAppInfoAdapter.getCount()) {
                    // initiate compute package size for next pkg in list
                    mObserver.invokeGetSizeInfo(mAppInfoAdapter.getApplicationInfo(
                            mComputeIndex), 
                            COMPUTE_PKG_SIZE_DONE);
                } else {
                    // check for added/removed packages
                    Set<String> keys =  mAddRemoveMap.keySet();
                    Iterator<String> iter = keys.iterator();
                    List<String> removeList = new ArrayList<String>();
                    boolean added = false;
                    boolean removed = false;
                    while (iter.hasNext()) {
                        String key = iter.next();
                        if (mAddRemoveMap.get(key) == Boolean.TRUE) {
                            // add
                            try {
                                info = mPm.getApplicationInfo(key, 0);
                                mAppInfoAdapter.addApplicationInfo(info);
                                added = true;
                            } catch (NameNotFoundException e) {
                                Log.w(TAG, "Invalid added package:"+key+" Ignoring entry");
                            }   
                        } else {
                            // remove
                            removeList.add(key);
                            removed = true;
                        }
                    }
                    // remove uninstalled packages from list
                    if (removed) {
                        mAppInfoAdapter.removeFromList(removeList);
                    }
                    // handle newly installed packages
                    if (added) {
                        mObserver.invokeGetSizeInfo(mAppInfoAdapter.getApplicationInfo(
                                mComputeIndex), 
                                COMPUTE_PKG_SIZE_DONE);
                    } else {
                        // end computation here
                        mComputeSizes = true;
                        mAppInfoAdapter.sortList(mSortOrder);
                        mHandler.sendEmptyMessage(NEXT_LOAD_STEP);
                    }
                }
                break;
            case REMOVE_PKG:
                if(localLOGV) Log.i(TAG, "Message REMOVE_PKG");
                if(pkgName == null) {
                    Log.w(TAG, "Ignoring message:REMOVE_PKG for null pkgName");
                    break;
                }
                if (!mComputeSizes) {
                    Boolean currB = mAddRemoveMap.get(pkgName);
                    if (currB == null || (currB.equals(Boolean.TRUE))) {
                        mAddRemoveMap.put(pkgName, Boolean.FALSE);
                    }
                    break;
                }
                List<String> pkgList = new ArrayList<String>();
                pkgList.add(pkgName);
                mAppInfoAdapter.removeFromList(pkgList);
                break;
            case REORDER_LIST:
                if(localLOGV) Log.i(TAG, "Message REORDER_LIST");
                int menuOption = msg.arg1;
                if((menuOption == SORT_ORDER_ALPHA) || 
                        (menuOption == SORT_ORDER_SIZE)) {
                    // Option to sort list
                    if (menuOption != mSortOrder) {
                        mSortOrder = menuOption;
                        if (localLOGV) Log.i(TAG, "Changing sort order to "+mSortOrder);
                        mAppInfoAdapter.sortList(mSortOrder);
                    }
                } else if(menuOption != mFilterApps) {
                    // Option to filter list
                    mFilterApps = menuOption;
                    boolean ret = mAppInfoAdapter.resetAppList(mFilterApps, 
                            getInstalledApps(mFilterApps));
                    if(!ret) {
                        // Reset cache
                        mAppPropCache = null;
                        mFilterApps = FILTER_APPS_ALL;
                        mHandler.sendEmptyMessage(INIT_PKG_INFO);
                        sendMessageToHandler(REORDER_LIST, menuOption);
                    }
                }
                break;
            case ADD_PKG_START:
                if(localLOGV) Log.i(TAG, "Message ADD_PKG_START");
                if(pkgName == null) {
                    Log.w(TAG, "Ignoring message:ADD_PKG_START for null pkgName");
                    break;
                }
                if (!mComputeSizes) {
                    Boolean currB = mAddRemoveMap.get(pkgName);
                    if (currB == null || (currB.equals(Boolean.FALSE))) {
                        mAddRemoveMap.put(pkgName, Boolean.TRUE);
                    }
                    break;
                }
                try {
                        info = mPm.getApplicationInfo(pkgName, 0);
                    } catch (NameNotFoundException e) {
                        Log.w(TAG, "Couldnt find application info for:"+pkgName);
                        break;
                    }
                mObserver.invokeGetSizeInfo(info, ADD_PKG_DONE);
                break;
            case ADD_PKG_DONE:
                if(localLOGV) Log.i(TAG, "Message COMPUTE_PKG_SIZE_DONE");
                if(pkgName == null) {
                    Log.w(TAG, "Ignoring message:ADD_PKG_START for null pkgName");
                    break;
                }
                ps = data.getParcelable(ATTR_APP_PKG_STATS);
                mAppInfoAdapter.addToList(pkgName, ps);
                break;
            case REFRESH_ICONS:
                Map<String, AppInfo> iconMap = (Map<String, AppInfo>) msg.obj;
                if(iconMap == null) {
                    Log.w(TAG, "Error loading icons for applications");
                } else {
                    mAppInfoAdapter.updateAppsResourceInfo(iconMap);   
                }
                mLoadLabels = true;
                mHandler.sendEmptyMessage(NEXT_LOAD_STEP);
                break;
            case NEXT_LOAD_STEP:
                if (mComputeSizes && mLoadLabels) {
                    doneLoadingData();
                } else if (!mComputeSizes && !mLoadLabels) {
                     // Either load the package labels or initiate get size info
                    if (mSizesFirst) {
                        initComputeSizes();
                    } else {
                        initResourceThread();
                    }
                } else {
                    // Create list view from the adapter here. Wait till the sort order
                    // of list is defined. its either by label or by size. so atleast one of the
                    // first steps should be complete before creating the list
                    createListView();
                    if (!mComputeSizes) {
                        initComputeSizes();
                    } else if (!mLoadLabels) {
                        initResourceThread();
                    }
                }
                break;
            default:
                break;
            }
        }
    };
    
    private void doneLoadingData() {
        setProgressBarIndeterminateVisibility(false);
    }
    
    List<ApplicationInfo> getInstalledApps(int filterOption) {
        List<ApplicationInfo> installedAppList = mPm.getInstalledApplications(
                PackageManager.GET_UNINSTALLED_PACKAGES);
        if (installedAppList == null) {
            return new ArrayList<ApplicationInfo> ();
        }
        if (filterOption == FILTER_APPS_THIRD_PARTY) {
            List<ApplicationInfo> appList =new ArrayList<ApplicationInfo> ();
            for (ApplicationInfo appInfo : installedAppList) {
                boolean flag = false;
                if ((appInfo.flags & ApplicationInfo.FLAG_UPDATED_SYSTEM_APP) != 0) {
                    // Updated system app
                    flag = true;
                } else if ((appInfo.flags & ApplicationInfo.FLAG_SYSTEM) == 0) {
                    // Non-system app
                    flag = true;
                }
                if (flag) {
                    appList.add(appInfo);
                }
            }
            return appList;
        } else if (filterOption == FILTER_APPS_RUNNING) {
            List<ApplicationInfo> appList =new ArrayList<ApplicationInfo> ();
            List<ActivityManager.RunningAppProcessInfo> procList = getRunningAppProcessesList();
            if ((procList == null) || (procList.size() == 0)) {
                return appList;
            }
            // Retrieve running processes from ActivityManager
            for (ActivityManager.RunningAppProcessInfo appProcInfo : procList) {
                if ((appProcInfo != null)  && (appProcInfo.pkgList != null)){
                    int size = appProcInfo.pkgList.length;
                    for (int i = 0; i < size; i++) {
                        ApplicationInfo appInfo = null;
                        try {
                            appInfo = mPm.getApplicationInfo(appProcInfo.pkgList[i], 
                                    PackageManager.GET_UNINSTALLED_PACKAGES);
                        } catch (NameNotFoundException e) {
                           Log.w(TAG, "Error retrieving ApplicationInfo for pkg:"+appProcInfo.pkgList[i]);
                           continue;
                        }
                        if(appInfo != null) {
                            appList.add(appInfo);
                        }
                    }
                }
            }
            return appList;
        } else {
            return installedAppList;
        }
    }
    
    private List<ActivityManager.RunningAppProcessInfo> getRunningAppProcessesList() {
        ActivityManager am = (ActivityManager)getSystemService(Context.ACTIVITY_SERVICE);
        return am.getRunningAppProcesses();
    }
    
    // some initialization code used when kicking off the size computation
    private void initAppList(int filterOption) {
        mComputeSizes = false;
        // Initialize lists
        List<ApplicationInfo> appList = getInstalledApps(filterOption);
        mAddRemoveMap = new TreeMap<String, Boolean>();
        mAppInfoAdapter = new AppInfoAdapter(this, appList);       
        // register receiver
        mReceiver.registerReceiver();
    }
    
    // Utility method to start a thread to read application labels and icons
    private void initResourceThread() {
        //load resources now
        if(mResourceThread.isAlive()) {
            mResourceThread.interrupt();
        }
        mResourceThread.loadAllResources(mAppInfoAdapter.getAppList());
    }
    
    private void initComputeSizes() {
         // initiate compute pkg sizes
        if (localLOGV) Log.i(TAG, "Initiating compute sizes for first time");
        if (mAppInfoAdapter.getCount() > 0) {
            mObserver.invokeGetSizeInfo(mAppInfoAdapter.getApplicationInfo(0),
                    COMPUTE_PKG_SIZE_DONE);
        } else {
            mComputeSizes = true;
        }
    }
    
    private void showEmptyViewIfListEmpty() {
        if (localLOGV) Log.i(TAG, "Checking for empty view");
        if (mAppInfoAdapter.getCount() > 0) {
            mEmptyView.setVisibility(View.GONE);
        } else {
            mEmptyView.setVisibility(View.VISIBLE);
        }
    }

    private void createListView() {
        dismissLoadingMsg();
        // get list and set listeners and adapter
        ListView lv= (ListView) findViewById(android.R.id.list);
        lv.setAdapter(mAppInfoAdapter);
        lv.setOnItemClickListener(this);
        lv.setSaveEnabled(true);
        lv.setItemsCanFocus(true);
        lv.setOnItemClickListener(this);
        showEmptyViewIfListEmpty();
    }
    
    // internal structure used to track added and deleted packages when
    // the activity has focus
    class AddRemoveInfo {
        String pkgName;
        boolean add;
        public AddRemoveInfo(String pPkgName, boolean pAdd) {
            pkgName = pPkgName;
            add = pAdd;
        }
    }
    
    class ResourceLoaderThread extends Thread {
        List<ApplicationInfo> mAppList;
        
        void loadAllResources(List<ApplicationInfo> appList) {
            mAppList = appList;
            start();
        }

        public void run() {
            Map<String, AppInfo> iconMap = new HashMap<String, AppInfo>();
            if(mAppList == null || mAppList.size() <= 0) {
                Log.w(TAG, "Empty or null application list");
            } else {
                for (ApplicationInfo appInfo : mAppList) {
                    CharSequence appName = appInfo.loadLabel(mPm);
                    Drawable appIcon = appInfo.loadIcon(mPm);
                    iconMap.put(appInfo.packageName, 
                            new AppInfo(appInfo.packageName, appName, appIcon));
                }
            }
            Message msg = mHandler.obtainMessage(REFRESH_ICONS);
            msg.obj = iconMap;
            mHandler.sendMessage(msg);
        }
    }
    
    /* Internal class representing an application or packages displayable attributes
     * 
     */
    class AppInfo {
        public String pkgName;
        int index;
        public  CharSequence appName;
        public  Drawable appIcon;
        public CharSequence appSize;
        public PackageStats appStats;
        
        public void refreshIcon(AppInfo pInfo) {
            appName = pInfo.appName;
            appIcon = pInfo.appIcon;
        }

        public AppInfo(String pName, CharSequence aName, Drawable aIcon) {
            index = -1;
            pkgName = pName;
            appName = aName;
            appIcon = aIcon;
            appStats = null;
            appSize = mComputingSizeStr;
        }
        
        public AppInfo(String pName, int pIndex, CharSequence aName, Drawable aIcon, 
                PackageStats ps) {
            index = pIndex;
            pkgName = pName;
            appName = aName;
            appIcon = aIcon;
            if(ps == null) {
                appSize = mComputingSizeStr;
            } else {
                appStats = ps;
                appSize = getSizeStr();
            }
        }
        public void setSize(PackageStats ps) {
            appStats = ps;
            if (ps != null) {
                appSize = getSizeStr();
            }
        }
        public long getTotalSize() {
            PackageStats ps = appStats;
            if (ps != null) {
                return ps.cacheSize+ps.codeSize+ps.dataSize;
            }
            return SIZE_INVALID;
        }
        
        private String getSizeStr() {
            PackageStats ps = appStats;
            String retStr = "";
            // insert total size information into map to display in view
            // at this point its guaranteed that ps is not null. but checking anyway
            if (ps != null) {
                long size = getTotalSize();
                if (size == SIZE_INVALID) {
                    return mInvalidSizeStr.toString();
                }
                return Formatter.formatFileSize(ManageApplications.this, size);
            }
            return retStr;
        }
    }
    
    // View Holder used when displaying views
    static class AppViewHolder {
        TextView appName;
        ImageView appIcon;
        TextView appSize;
    }
    
    /* Custom adapter implementation for the ListView
     * This adapter maintains a map for each displayed application and its properties
     * An index value on each AppInfo object indicates the correct position or index
     * in the list. If the list gets updated dynamically when the user is viewing the list of
     * applications, we need to return the correct index of position. This is done by mapping
     * the getId methods via the package name into the internal maps and indices.
     * The order of applications in the list is mirrored in mAppLocalList
     */
    class AppInfoAdapter extends BaseAdapter {
        private Map<String, AppInfo> mAppPropMap;
        private List<ApplicationInfo> mAppLocalList;
        ApplicationInfo.DisplayNameComparator mAlphaComparator;
        AppInfoComparator mSizeComparator;
        
        private AppInfo getFromCache(String packageName) {
            if(mAppPropCache == null) {
                return null;
            }
            return mAppPropCache.get(packageName);
        }
        
        public AppInfoAdapter(Context c, List<ApplicationInfo> appList) {
            mAppLocalList = appList;
            boolean useCache = false;
            int sortOrder = SORT_ORDER_ALPHA;
            int imax = mAppLocalList.size();
            if(mAppPropCache != null) {
                useCache = true;
                // Activity has been resumed. can use the cache to populate values initially
                mAppPropMap = mAppPropCache;
                sortOrder = mSortOrder;
            }
            sortAppList(sortOrder);
            // Recreate property map
            mAppPropMap = new TreeMap<String, AppInfo>();
            for (int i = 0; i < imax; i++) {
                ApplicationInfo info = mAppLocalList.get(i);
                AppInfo aInfo = getFromCache(info.packageName);
                if(aInfo == null){
                    aInfo = new AppInfo(info.packageName, i, 
                            info.packageName, mDefaultAppIcon, null);   
                } else {
                    aInfo.index = i;
                }
                mAppPropMap.put(info.packageName, aInfo);
            }
        }
        
        public int getCount() {
            return mAppLocalList.size();
        }
        
        public Object getItem(int position) {
            return mAppLocalList.get(position);
        }
        
        /*
         * This method returns the index of the package position in the application list
         */
        public int getIndex(String pkgName) {
            if(pkgName == null) {
                Log.w(TAG, "Getting index of null package in List Adapter");
            }
            int imax = mAppLocalList.size();
            ApplicationInfo appInfo;
            for(int i = 0; i < imax; i++) {
                appInfo = mAppLocalList.get(i);
                if(appInfo.packageName.equalsIgnoreCase(pkgName)) {
                    return i;
                }
            }
            return -1;
        }
        
        public ApplicationInfo getApplicationInfo(int position) {
            int imax = mAppLocalList.size();
            if( (position < 0) || (position >= imax)) {
                Log.w(TAG, "Position out of bounds in List Adapter");
                return null;
            }
            return mAppLocalList.get(position);
        }
        
        public void addApplicationInfo(ApplicationInfo info) {
            if(info == null) {
                Log.w(TAG, "Ignoring null add in List Adapter");
                return;
            }
            mAppLocalList.add(info);
        }

        public long getItemId(int position) {
            int imax = mAppLocalList.size();
            if( (position < 0) || (position >= imax)) {
                Log.w(TAG, "Position out of bounds in List Adapter");
                return -1;
            }
            return mAppPropMap.get(mAppLocalList.get(position).packageName).index;
        }
        
        public List<ApplicationInfo> getAppList() {
            return mAppLocalList;
        }
        
        public View getView(int position, View convertView, ViewGroup parent) {
            if (position >= mAppLocalList.size()) {
                Log.w(TAG, "Invalid view position:"+position+", actual size is:"+mAppLocalList.size());
                return null;
            }
            // A ViewHolder keeps references to children views to avoid unneccessary calls
            // to findViewById() on each row.
            AppViewHolder holder;

            // When convertView is not null, we can reuse it directly, there is no need
            // to reinflate it. We only inflate a new View when the convertView supplied
            // by ListView is null.
            if (convertView == null) {
                convertView = mInflater.inflate(R.layout.manage_applications_item, null);

                // Creates a ViewHolder and store references to the two children views
                // we want to bind data to.
                holder = new AppViewHolder();
                holder.appName = (TextView) convertView.findViewById(R.id.app_name);
                holder.appIcon = (ImageView) convertView.findViewById(R.id.app_icon);
                holder.appSize = (TextView) convertView.findViewById(R.id.app_size);
                convertView.setTag(holder);
            } else {
                // Get the ViewHolder back to get fast access to the TextView
                // and the ImageView.
                holder = (AppViewHolder) convertView.getTag();
            }

            // Bind the data efficiently with the holder
            ApplicationInfo appInfo = mAppLocalList.get(position);
            AppInfo mInfo = mAppPropMap.get(appInfo.packageName);
            if(mInfo != null) {
                if(mInfo.appName != null) {
                    holder.appName.setText(mInfo.appName);
                }
                if(mInfo.appIcon != null) {
                    holder.appIcon.setImageDrawable(mInfo.appIcon);
                }
                if (mInfo.appSize != null) {
                    holder.appSize.setText(mInfo.appSize);
                }
            } else {
                Log.w(TAG, "No info for package:"+appInfo.packageName+" in property map");
            }
            return convertView;
        }
        
        private void adjustIndex() {
            int imax = mAppLocalList.size();
            ApplicationInfo info;
            for (int i = 0; i < imax; i++) {
                info = mAppLocalList.get(i);
                mAppPropMap.get(info.packageName).index = i;
            }
        }
        
        public void sortAppList(int sortOrder) {
            Collections.sort(mAppLocalList, getAppComparator(sortOrder));
        }
        
        public void sortList(int sortOrder) {
            sortAppList(sortOrder);
            adjustIndex();
            notifyDataSetChanged();
        }
        
        public boolean resetAppList(int filterOption, List<ApplicationInfo> appList) {
           // Create application list based on the filter value
           mAppLocalList = appList;
           // Check for all properties in map before sorting. Populate values from cache
           for(ApplicationInfo applicationInfo : mAppLocalList) {
               AppInfo appInfo = mAppPropMap.get(applicationInfo.packageName);
               if(appInfo == null) {
                   AppInfo rInfo = getFromCache(applicationInfo.packageName);
                   if(rInfo == null) {
                       // Need to load resources again. Inconsistency somewhere
                       return false;
                   }
                   mAppPropMap.put(applicationInfo.packageName, rInfo);
               }
           }
           if (mAppLocalList.size() > 0) {
               sortList(mSortOrder);
           } else {
               notifyDataSetChanged();
           }
           showEmptyViewIfListEmpty();
           return true;
        }
        
        private Comparator<ApplicationInfo> getAppComparator(int sortOrder) {
            if (sortOrder == SORT_ORDER_ALPHA) {
                // Lazy initialization
                if (mAlphaComparator == null) {
                    mAlphaComparator = new ApplicationInfo.DisplayNameComparator(mPm);
                }
                return mAlphaComparator;
            }
            // Lazy initialization
            if(mSizeComparator == null) {
                mSizeComparator = new AppInfoComparator(mAppPropMap);
            }
            return mSizeComparator;
        }
        
        public void updateAppsResourceInfo(Map<String, AppInfo> iconMap) {
            if(iconMap == null) {
                Log.w(TAG, "Null iconMap when refreshing icon in List Adapter");
                return;
            }
            boolean changed = false;
            for (ApplicationInfo info : mAppLocalList) {
                AppInfo pInfo = iconMap.get(info.packageName);
                if(pInfo != null) {
                    AppInfo aInfo = mAppPropMap.get(info.packageName);
                    aInfo.refreshIcon(pInfo);
                    changed = true;
                }
            }
            if(changed) {
                notifyDataSetChanged();
            }
        }
        
        public void addToList(String pkgName, PackageStats ps) {
            if(pkgName == null) {
                Log.w(TAG, "Adding null pkg to List Adapter");
                return;
            }
            ApplicationInfo info;
            try {
                info = mPm.getApplicationInfo(pkgName, 0);
            } catch (NameNotFoundException e) {
                Log.w(TAG, "Ignoring non-existent package:"+pkgName);
                return;
            }
            if(info == null) {
                // Nothing to do log error message and return
                Log.i(TAG, "Null ApplicationInfo for package:"+pkgName);
                return;
            }
            // Binary search returns a negative index (ie --index) of the position where
            // this might be inserted. 
            int newIdx = Collections.binarySearch(mAppLocalList, info, 
                    getAppComparator(mSortOrder));
            if(newIdx >= 0) {
                Log.i(TAG, "Strange. Package:"+pkgName+" is not new");
                return;
            }
            // New entry
            newIdx = -newIdx-1;
            mAppLocalList.add(newIdx, info);
            mAppPropMap.put(info.packageName, new AppInfo(pkgName, newIdx,
                    info.loadLabel(mPm), info.loadIcon(mPm), ps));
            adjustIndex();
            notifyDataSetChanged();
        }
        
        public void removeFromList(List<String> pkgNames) {
            if(pkgNames == null) {
                Log.w(TAG, "Removing null pkg list from List Adapter");
                return;
            }
            int imax = mAppLocalList.size();
            boolean found = false;
            ApplicationInfo info;
            int i, k;
            String pkgName;
            int kmax = pkgNames.size();
            if(kmax  <= 0) {
                Log.w(TAG, "Removing empty pkg list from List Adapter");
                return;
            }
            int idxArr[] = new int[kmax];
            for (k = 0; k < kmax; k++) {
                idxArr[k] = -1;
            }
            for (i = 0; i < imax; i++) {
                info = mAppLocalList.get(i);
                for (k = 0; k < kmax; k++) {
                    pkgName = pkgNames.get(k);
                    if (info.packageName.equalsIgnoreCase(pkgName)) {
                        idxArr[k] = i;
                        found = true;
                        break;
                    }
                }
            }
            // Sort idxArr
            Arrays.sort(idxArr);
            // remove the packages based on decending indices
            for (k = kmax-1; k >= 0; k--) {
                // Check if package has been found in the list of existing apps first
                if(idxArr[k] == -1) {
                    break;
                }
                info = mAppLocalList.get(idxArr[k]);
                mAppLocalList.remove(idxArr[k]);
                mAppPropMap.remove(info.packageName);
                if (localLOGV) Log.i(TAG, "Removed pkg:"+info.packageName+ " list");
            }
            if (found) {
                adjustIndex();
                notifyDataSetChanged();
            }
        }   
        
        public void updateAppSize(String pkgName, PackageStats ps) {
            if(pkgName == null) {
                return;
            }
            AppInfo entry = mAppPropMap.get(pkgName);
            if (entry == null) {
                Log.w(TAG, "Entry for package:"+pkgName+"doesnt exist in map");
                return;
            }
            // Copy the index into the newly updated entry
            entry.setSize(ps);
            notifyDataSetChanged();
        }

        public PackageStats getAppStats(String pkgName) {
            if(pkgName == null) {
                return null;
            }
            AppInfo entry = mAppPropMap.get(pkgName);
            if (entry == null) {
                return null;
            }
            return entry.appStats;
        }
    }
    
    /*
     * Utility method to clear messages to Handler
     * We need'nt synchronize on the Handler since posting messages is guaranteed
     * to be thread safe. Even if the other thread that retrieves package sizes
     * posts a message, we do a cursory check of validity on mAppInfoAdapter's applist
     */
    private void clearMessagesInHandler() {
        mHandler.removeMessages(INIT_PKG_INFO);
        mHandler.removeMessages(COMPUTE_PKG_SIZE_DONE);
        mHandler.removeMessages(REMOVE_PKG);
        mHandler.removeMessages(REORDER_LIST);
        mHandler.removeMessages(ADD_PKG_START);
        mHandler.removeMessages(ADD_PKG_DONE);
    }
    
    private void sendMessageToHandler(int msgId, int arg1) {
        Message msg = mHandler.obtainMessage(msgId);
        msg.arg1 = arg1;
        mHandler.sendMessage(msg);
    }
    
    private void sendMessageToHandler(int msgId, Bundle data) {
        Message msg = mHandler.obtainMessage(msgId);
        msg.setData(data);
        mHandler.sendMessage(msg);
    }
    
    private void sendMessageToHandler(int msgId) {
        mHandler.sendEmptyMessage(msgId);
    }
    
    /*
     * Stats Observer class used to compute package sizes and retrieve size information
     * PkgSizeOberver is the call back thats used when invoking getPackageSizeInfo on
     * PackageManager. The values in call back onGetStatsCompleted are validated
     * and the specified message is passed to mHandler. The package name
     * and the AppInfo object corresponding to the package name are set on the message
     */
    class PkgSizeObserver extends IPackageStatsObserver.Stub {
        private ApplicationInfo mAppInfo;
        private int mMsgId; 
        public void onGetStatsCompleted(PackageStats pStats, boolean pSucceeded) {
            if(DEBUG_PKG_DELAY) {
                try {
                    Thread.sleep(10*1000);
                } catch (InterruptedException e) {
                }
            }
            AppInfo appInfo = null;
            Bundle data = new Bundle();
            data.putString(ATTR_PKG_NAME, mAppInfo.packageName);
            if(pSucceeded && pStats != null) {
                if (localLOGV) Log.i(TAG, "onGetStatsCompleted::"+pStats.packageName+", ("+
                        pStats.cacheSize+","+
                        pStats.codeSize+", "+pStats.dataSize);
                data.putParcelable(ATTR_APP_PKG_STATS, pStats);
            } else {
                Log.w(TAG, "Invalid package stats from PackageManager");
            }
            //post message to Handler
            Message msg = mHandler.obtainMessage(mMsgId, data);
            msg.setData(data);
            mHandler.sendMessage(msg);
        }

        public void invokeGetSizeInfo(ApplicationInfo pAppInfo, int msgId) {
            if(pAppInfo == null || pAppInfo.packageName == null) {
                return;
            }
            if(localLOGV) Log.i(TAG, "Invoking getPackageSizeInfo for package:"+
                    pAppInfo.packageName);
            mMsgId = msgId;
            mAppInfo = pAppInfo;
            mPm.getPackageSizeInfo(pAppInfo.packageName, this);
        }
    }
    
    /**
     * Receives notifications when applications are added/removed.
     */
    private class PackageIntentReceiver extends BroadcastReceiver {
         void registerReceiver() {
             IntentFilter filter = new IntentFilter(Intent.ACTION_PACKAGE_ADDED);
             filter.addAction(Intent.ACTION_PACKAGE_REMOVED);
             filter.addAction(Intent.ACTION_PACKAGE_CHANGED);
             filter.addDataScheme("package");
             ManageApplications.this.registerReceiver(this, filter);
         }
        @Override
        public void onReceive(Context context, Intent intent) {
            String actionStr = intent.getAction();
            Uri data = intent.getData();
            String pkgName = data.getEncodedSchemeSpecificPart();
            if (localLOGV) Log.i(TAG, "action:"+actionStr+", for package:"+pkgName);
            updatePackageList(actionStr, pkgName);
        }
    }
    
    private void updatePackageList(String actionStr, String pkgName) {
        // technically we dont have to invoke handler since onReceive is invoked on
        // the main thread but doing it here for better clarity
        if (Intent.ACTION_PACKAGE_ADDED.equalsIgnoreCase(actionStr)) {
            Bundle data = new Bundle();
            data.putString(ATTR_PKG_NAME, pkgName);
            sendMessageToHandler(ADD_PKG_START, data);
        } else if (Intent.ACTION_PACKAGE_REMOVED.equalsIgnoreCase(actionStr)) {
            Bundle data = new Bundle();
            data.putString(ATTR_PKG_NAME, pkgName);
            sendMessageToHandler(REMOVE_PKG, data);
        }
    }
    
    
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        Intent lIntent = getIntent();
        String action = lIntent.getAction();
        if (action.equals(Intent.ACTION_MANAGE_PACKAGE_STORAGE)) {
            mSortOrder = SORT_ORDER_SIZE;
            mSizesFirst = true;
        }
        mPm = getPackageManager();
        // initialize some window features
        requestWindowFeature(Window.FEATURE_RIGHT_ICON);
        requestWindowFeature(Window.FEATURE_PROGRESS);
        requestWindowFeature(Window.FEATURE_INDETERMINATE_PROGRESS);
        setContentView(R.layout.compute_sizes);
        // init mLoadingDlg
        mLoadingDlg = new ProgressDialog(this);
        mLoadingDlg.setProgressStyle(ProgressDialog.STYLE_SPINNER);
        mLoadingDlg.setMessage(getText(R.string.loading));
        mLoadingDlg.setIndeterminate(true);        
        mLoadingDlg.setOnCancelListener(this);
        mDefaultAppIcon =Resources.getSystem().getDrawable(
                com.android.internal.R.drawable.sym_def_app_icon);
        mInvalidSizeStr = getText(R.string.invalid_size_value);
        mComputingSizeStr = getText(R.string.computing_size);
        // initialize the inflater
        mInflater = (LayoutInflater)getSystemService(Context.LAYOUT_INFLATER_SERVICE);
        mReceiver = new PackageIntentReceiver();
        mEmptyView = (TextView) findViewById(R.id.empty_view);
        mObserver = new PkgSizeObserver();
    }
    
    private void showLoadingMsg() {
        if (mLoadingDlg != null) {
            if(localLOGV) Log.i(TAG, "Displaying Loading message");
            mLoadingDlg.show();
        }
    }
    
    private void dismissLoadingMsg() {
        if ((mLoadingDlg != null) && (mLoadingDlg.isShowing())) {
            if(localLOGV) Log.i(TAG, "Dismissing Loading message");
            mLoadingDlg.dismiss();
        }
    }
    
    @Override
    public void onStart() {
        super.onStart();
        showLoadingMsg();
        // Create a thread to load resources
        mResourceThread = new ResourceLoaderThread();
        sendMessageToHandler(INIT_PKG_INFO);
    }

    @Override
    public void onStop() {
        super.onStop();
        // clear all messages related to application list
        clearMessagesInHandler();
        // register receiver here
        unregisterReceiver(mReceiver);        
        mAppPropCache = mAppInfoAdapter.mAppPropMap;
    }
    
    /*
     * comparator class used to sort AppInfo objects based on size
     */
    public static class AppInfoComparator implements Comparator<ApplicationInfo> {
        public AppInfoComparator(Map<String, AppInfo> pAppPropMap) {
            mAppPropMap= pAppPropMap;
        }

        public final int compare(ApplicationInfo a, ApplicationInfo b) {
            AppInfo ainfo = mAppPropMap.get(a.packageName);
            AppInfo binfo = mAppPropMap.get(b.packageName);
            long atotal = ainfo.getTotalSize();
            long btotal = binfo.getTotalSize();
            long ret = atotal - btotal;
            // negate result to sort in descending order
            if (ret < 0) {
                return 1;
            }
            if (ret == 0) {
                return 0;
            }
            return -1;
        }
        private Map<String, AppInfo> mAppPropMap;
    }
     
    // utility method used to start sub activity
    private void startApplicationDetailsActivity(ApplicationInfo info, PackageStats ps) {
        // Create intent to start new activity
        Intent intent = new Intent(Intent.ACTION_VIEW);
        intent.setClass(this, InstalledAppDetails.class);
        mCurrentPkgName = info.packageName;
        intent.putExtra(APP_PKG_NAME, mCurrentPkgName);
        intent.putExtra(APP_PKG_SIZE, ps);
        // start new activity to display extended information
        startActivityForResult(intent, INSTALLED_APP_DETAILS);
    }
    
    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        menu.add(0, SORT_ORDER_ALPHA, 1, R.string.sort_order_alpha)
                .setIcon(android.R.drawable.ic_menu_sort_alphabetically);
        menu.add(0, SORT_ORDER_SIZE, 2, R.string.sort_order_size)
                .setIcon(android.R.drawable.ic_menu_sort_by_size); 
        menu.add(0, FILTER_OPTIONS, 3, R.string.filter)
                .setIcon(R.drawable.ic_menu_filter_settings);
        return true;
    }
    
    @Override
    public boolean onPrepareOptionsMenu(Menu menu) {
        if (mComputeSizes) {
            menu.findItem(SORT_ORDER_ALPHA).setVisible(mSortOrder != SORT_ORDER_ALPHA);
            menu.findItem(SORT_ORDER_SIZE).setVisible(mSortOrder != SORT_ORDER_SIZE);
            menu.findItem(FILTER_OPTIONS).setVisible(true);
            return true;
        } 
        return false;
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        int menuId = item.getItemId();
        if ((menuId == SORT_ORDER_ALPHA) || (menuId == SORT_ORDER_SIZE)) {
            sendMessageToHandler(REORDER_LIST, menuId);
        } else if (menuId == FILTER_OPTIONS) {
            if (mAlertDlgBuilder == null) {
                mAlertDlgBuilder = new AlertDialog.Builder(this).
                setTitle(R.string.filter_dlg_title).
                setNeutralButton(R.string.cancel, this).
                setSingleChoiceItems(new CharSequence[] {getText(R.string.filter_apps_all),
                        getText(R.string.filter_apps_running),
                        getText(R.string.filter_apps_third_party)},
                        -1, this);
            }
            mAlertDlgBuilder.show();
        }
        return true;
    }

    public void onItemClick(AdapterView<?> parent, View view, int position,
            long id) {
        ApplicationInfo info = (ApplicationInfo)mAppInfoAdapter.getItem(position);
        startApplicationDetailsActivity(info, mAppInfoAdapter.getAppStats(info.packageName));
    }
    
    // onCancel call back for dialog thats displayed when data is being loaded
    public void onCancel(DialogInterface dialog) {
        mLoadingDlg = null;
        finish();
    }

    public void onClick(DialogInterface dialog, int which) {
        int newOption;
        switch (which) {
        // Make sure that values of 0, 1, 2 match options all, running, third_party when
        // created via the AlertDialog.Builder
        case 0:
            newOption = FILTER_APPS_ALL;
            break;
        case 1:
            newOption = FILTER_APPS_RUNNING;
            break;
        case 2:
            newOption = FILTER_APPS_THIRD_PARTY;
            break;
        default:
            return;
        }
        sendMessageToHandler(REORDER_LIST, newOption);
    }
}