3
3
import android .content .BroadcastReceiver ;
4
4
import android .content .Context ;
5
5
import android .content .Intent ;
6
+ import android .content .pm .ResolveInfo ;
7
+
6
8
import org .ligi .fast .App ;
9
+ import org .ligi .fast .model .AppInfo ;
7
10
import org .ligi .fast .model .AppInfoList ;
8
11
import org .ligi .fast .util .AppInfoListStore ;
9
12
13
+ import java .io .File ;
14
+ import java .util .Iterator ;
15
+ import java .util .List ;
16
+
17
+ /**
18
+ * Whenever an app is installed, uninstalled or components change
19
+ * (e.g. the app disabled one of it's activities to hide it from the launcher)
20
+ * this receiver takes care of removing or updating corresponding entries
21
+ * (namely all activities and aliases) from AppInfoList and deletes their icons
22
+ * from cache to clean up when uninstalling or to cause a refresh when updating.
23
+ */
10
24
public class AppInstallOrRemoveReceiver extends BroadcastReceiver {
25
+ //public final static String LOG_TAG = "FAST.AppInstallOrRemoveReceiver";
26
+
11
27
@ Override
12
28
public void onReceive (Context context , Intent intent ) {
13
- final AppInfoListStore appInfoListStore = new AppInfoListStore (context );
29
+ String packageName = intent .getData ().getSchemeSpecificPart ();
30
+ String action = intent .getAction ();
31
+ AppInfoListStore appInfoListStore = new AppInfoListStore (context );
32
+ AppInfoList appInfoList = null ;
33
+ if (App .backingAppInfoList != null ) {
34
+ appInfoList = App .backingAppInfoList .get ();
35
+ }
36
+ if (appInfoList == null ) {
37
+ appInfoList = appInfoListStore .load ();
38
+ }
39
+ AppInfoList matchedAppInfoList = new AppInfoList ();
40
+ Iterator <AppInfo > appInfoIterator = appInfoList .iterator ();
41
+ while (appInfoIterator .hasNext ()) {
42
+ AppInfo appInfo = appInfoIterator .next ();
43
+ if (appInfo .getPackageName ().equals (packageName )) {
44
+ matchedAppInfoList .add (appInfo );
45
+ appInfoIterator .remove ();
46
+ File icon = new File (App .getBaseDir () + "/" + appInfo .getHash () + ".png" );
47
+ icon .delete ();
48
+ /*
49
+ if (!icon.delete()) {
50
+ Log.d(App.LOG_TAG, "AppInstallOrRemoveReceiver: Icon deletion failed for hash: " + appInfo.getHash());
51
+ Log.d(AppInstallOrRemoveReceiver.LOG_TAG, "Icon deletion failed for hash: " + appInfo.getHash());
52
+ }
53
+ */
54
+ }
55
+ }
14
56
15
- if (App .packageChangedListener == null ) {
16
- App .packageChangedListener = new App .PackageChangedListener () {
17
- @ Override
18
- public void onPackageChange (AppInfoList appInfoList ) {
19
- appInfoListStore .save (appInfoList );
57
+ if (!action .equals (Intent .ACTION_PACKAGE_FULLY_REMOVED )) {
58
+ Intent launcherIntent = new Intent (Intent .ACTION_MAIN );
59
+ launcherIntent .addCategory (Intent .CATEGORY_LAUNCHER );
60
+ launcherIntent .setPackage (packageName );
61
+ List <ResolveInfo > resolveInfoList = context .getPackageManager ().queryIntentActivities (launcherIntent , 0 );
62
+
63
+ Intent homeIntent = new Intent (Intent .ACTION_MAIN );
64
+ homeIntent .addCategory (Intent .CATEGORY_HOME );
65
+ homeIntent .setPackage (packageName );
66
+ List <ResolveInfo > homeInfoList = context .getPackageManager ().queryIntentActivities (homeIntent , 0 );
67
+
68
+ // If there are no activities that should be displayed on the launcher we can quit here
69
+ if (resolveInfoList .size () == 0 && homeInfoList .size () == 0 ) {
70
+ //Log.d(App.LOG_TAG, "AppInstallOrRemoveReceiver: No launcher Activities"
71
+ // + "\n\tPackage: " + packageName);
72
+ return ;
73
+ }
74
+
75
+ // Deduplicate Resolve Info of activities with both categories - like SearchActivity (see manifest)
76
+ for (ResolveInfo info : resolveInfoList ) {
77
+ Iterator <ResolveInfo > homeIterator = homeInfoList .iterator ();
78
+ while (homeIterator .hasNext ()) {
79
+ ResolveInfo homeInfo = homeIterator .next ();
80
+ if (homeInfo .activityInfo .name .equals (info .activityInfo .name )) {
81
+ homeIterator .remove ();
82
+ break ;
83
+ }
84
+ }
85
+ if (!homeIterator .hasNext ()) {
86
+ break ;
87
+ }
88
+ }
89
+ resolveInfoList .addAll (homeInfoList );
90
+
91
+ /*
92
+ String log =
93
+ "AppInstallOrRemoveReceiver: Updating info:"
94
+ + "\n\tAction: " + action
95
+ + "\n\tPackage: " + packageName
96
+ + "\n\tLabel: " + resolveInfoList.get(0).activityInfo.loadLabel(context.getPackageManager())
97
+ + "\n\tActivities: " + String.valueOf(resolveInfoList.size());
98
+ Log.d(App.LOG_TAG, log);
99
+ Log.d(AppInstallOrRemoveReceiver.LOG_TAG, log);
100
+ for (ResolveInfo i : resolveInfoList) {
101
+ Log.d(App.LOG_TAG, "\t " + i.activityInfo.name);
102
+ Log.d(AppInstallOrRemoveReceiver.LOG_TAG, "\t " + i.activityInfo.name);
103
+ }
104
+ */
105
+
106
+ if (matchedAppInfoList .size () == 0 ) { // New app: Package name not amongst known apps
107
+ for (ResolveInfo info : resolveInfoList ) {
108
+ appInfoList .add (new AppInfo (context , info ));
20
109
}
21
- };
110
+ } else { // Update: Package name included in known apps
111
+ for (ResolveInfo info : resolveInfoList ) {
112
+ AppInfo actAppInfo = new AppInfo (context , info );
113
+
114
+ Iterator <AppInfo > oldInfoIterator = matchedAppInfoList .iterator ();
115
+ while (oldInfoIterator .hasNext ()) {
116
+ AppInfo oldInfo = oldInfoIterator .next ();
117
+ if (oldInfo .getActivityName ().equals (actAppInfo .getActivityName ())) {
118
+ if (oldInfo .getLabelMode () == 2 ) { // AppInfo is alias
119
+ oldInfo .setLabel (actAppInfo .getLabel ());
120
+ oldInfo .setInstallTime (actAppInfo .getInstallTime ());
121
+ appInfoList .add (oldInfo );
122
+ } else {
123
+ actAppInfo .setCallCount (oldInfo .getCallCount ());
124
+ actAppInfo .setPinMode (oldInfo .getPinMode ());
125
+ actAppInfo .setLabelMode (oldInfo .getLabelMode ());
126
+ actAppInfo .setOverrideLabel (oldInfo .getOverrideLabel ());
127
+ }
128
+ oldInfoIterator .remove ();
129
+ }
130
+ }
131
+ appInfoList .add (actAppInfo );
132
+ }
133
+ }
22
134
}
23
135
24
- new BackgroundGatherAsyncTask (context , appInfoListStore .load ()).execute ();
136
+ if (App .packageChangedListener == null ) {
137
+ appInfoListStore .save (appInfoList );
138
+ } else {
139
+ App .packageChangedListener .onPackageChange (appInfoList );
140
+ }
25
141
}
26
- }
142
+ }
0 commit comments