• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2019 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.quickstep;
18 
19 import static android.content.Intent.ACTION_PACKAGE_ADDED;
20 import static android.content.Intent.ACTION_PACKAGE_CHANGED;
21 import static android.content.Intent.ACTION_PACKAGE_REMOVED;
22 
23 import static com.android.launcher3.util.PackageManagerHelper.getPackageFilter;
24 import static com.android.systemui.shared.system.PackageManagerWrapper.ACTION_PREFERRED_ACTIVITY_CHANGED;
25 import static com.android.systemui.shared.system.QuickStepContract.SYSUI_STATE_HOME_DISABLED;
26 
27 import android.content.BroadcastReceiver;
28 import android.content.ComponentName;
29 import android.content.Context;
30 import android.content.Intent;
31 import android.content.IntentFilter;
32 import android.content.pm.ResolveInfo;
33 
34 import com.android.systemui.shared.system.PackageManagerWrapper;
35 
36 import java.util.ArrayList;
37 
38 /**
39  * Class to keep track of the current overview component based off user preferences and app updates
40  * and provide callers the relevant classes.
41  */
42 public final class OverviewComponentObserver {
43     private final BroadcastReceiver mUserPreferenceChangeReceiver = new BroadcastReceiver() {
44         @Override
45         public void onReceive(Context context, Intent intent) {
46             updateOverviewTargets();
47         }
48     };
49     private final BroadcastReceiver mOtherHomeAppUpdateReceiver = new BroadcastReceiver() {
50         @Override
51         public void onReceive(Context context, Intent intent) {
52             updateOverviewTargets();
53         }
54     };
55     private final Context mContext;
56     private final ComponentName mMyHomeComponent;
57     private String mUpdateRegisteredPackage;
58     private ActivityControlHelper mActivityControlHelper;
59     private Intent mOverviewIntent;
60     private Intent mHomeIntent;
61     private int mSystemUiStateFlags;
62     private boolean mIsHomeAndOverviewSame;
63 
OverviewComponentObserver(Context context)64     public OverviewComponentObserver(Context context) {
65         mContext = context;
66 
67         Intent myHomeIntent = new Intent(Intent.ACTION_MAIN)
68                 .addCategory(Intent.CATEGORY_HOME)
69                 .setPackage(mContext.getPackageName());
70         ResolveInfo info = context.getPackageManager().resolveActivity(myHomeIntent, 0);
71         mMyHomeComponent = new ComponentName(context.getPackageName(), info.activityInfo.name);
72 
73         mContext.registerReceiver(mUserPreferenceChangeReceiver,
74                 new IntentFilter(ACTION_PREFERRED_ACTIVITY_CHANGED));
75         updateOverviewTargets();
76     }
77 
onSystemUiStateChanged(int stateFlags)78     public void onSystemUiStateChanged(int stateFlags) {
79         boolean homeDisabledChanged = (mSystemUiStateFlags & SYSUI_STATE_HOME_DISABLED)
80                 != (stateFlags & SYSUI_STATE_HOME_DISABLED);
81         mSystemUiStateFlags = stateFlags;
82         if (homeDisabledChanged) {
83             updateOverviewTargets();
84         }
85     }
86 
87     /**
88      * Update overview intent and {@link ActivityControlHelper} based off the current launcher home
89      * component.
90      */
updateOverviewTargets()91     private void updateOverviewTargets() {
92         ComponentName defaultHome = PackageManagerWrapper.getInstance()
93                 .getHomeActivities(new ArrayList<>());
94 
95         final String overviewIntentCategory;
96         ComponentName overviewComponent;
97         mHomeIntent = null;
98 
99         if ((mSystemUiStateFlags & SYSUI_STATE_HOME_DISABLED) == 0 &&
100                 (defaultHome == null || mMyHomeComponent.equals(defaultHome))) {
101             // User default home is same as out home app. Use Overview integrated in Launcher.
102             overviewComponent = mMyHomeComponent;
103             mActivityControlHelper = new LauncherActivityControllerHelper();
104             mIsHomeAndOverviewSame = true;
105             overviewIntentCategory = Intent.CATEGORY_HOME;
106 
107             if (mUpdateRegisteredPackage != null) {
108                 // Remove any update listener as we don't care about other packages.
109                 mContext.unregisterReceiver(mOtherHomeAppUpdateReceiver);
110                 mUpdateRegisteredPackage = null;
111             }
112         } else {
113             // The default home app is a different launcher. Use the fallback Overview instead.
114             overviewComponent = new ComponentName(mContext, RecentsActivity.class);
115             mActivityControlHelper = new FallbackActivityControllerHelper();
116             mIsHomeAndOverviewSame = false;
117             overviewIntentCategory = Intent.CATEGORY_DEFAULT;
118 
119             mHomeIntent = new Intent(Intent.ACTION_MAIN)
120                     .addCategory(Intent.CATEGORY_HOME)
121                     .setComponent(defaultHome);
122             // User's default home app can change as a result of package updates of this app (such
123             // as uninstalling the app or removing the "Launcher" feature in an update).
124             // Listen for package updates of this app (and remove any previously attached
125             // package listener).
126             if (defaultHome == null) {
127                 if (mUpdateRegisteredPackage != null) {
128                     mContext.unregisterReceiver(mOtherHomeAppUpdateReceiver);
129                 }
130             } else if (!defaultHome.getPackageName().equals(mUpdateRegisteredPackage)) {
131                 if (mUpdateRegisteredPackage != null) {
132                     mContext.unregisterReceiver(mOtherHomeAppUpdateReceiver);
133                 }
134 
135                 mUpdateRegisteredPackage = defaultHome.getPackageName();
136                 mContext.registerReceiver(mOtherHomeAppUpdateReceiver, getPackageFilter(
137                         mUpdateRegisteredPackage, ACTION_PACKAGE_ADDED, ACTION_PACKAGE_CHANGED,
138                         ACTION_PACKAGE_REMOVED));
139             }
140         }
141 
142         mOverviewIntent = new Intent(Intent.ACTION_MAIN)
143                 .addCategory(overviewIntentCategory)
144                 .setComponent(overviewComponent)
145                 .setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
146         if (mHomeIntent == null) {
147             mHomeIntent = mOverviewIntent;
148         }
149     }
150 
151     /**
152      * Clean up any registered receivers.
153      */
onDestroy()154     public void onDestroy() {
155         mContext.unregisterReceiver(mUserPreferenceChangeReceiver);
156 
157         if (mUpdateRegisteredPackage != null) {
158             mContext.unregisterReceiver(mOtherHomeAppUpdateReceiver);
159             mUpdateRegisteredPackage = null;
160         }
161     }
162 
163     /**
164      * Get the current intent for going to the overview activity.
165      *
166      * @return the overview intent
167      */
getOverviewIntent()168     public Intent getOverviewIntent() {
169         return mOverviewIntent;
170     }
171 
172     /**
173      * Get the current intent for going to the home activity.
174      */
getHomeIntent()175     public Intent getHomeIntent() {
176         return mHomeIntent;
177     }
178 
179     /**
180      * Returns true if home and overview are same activity.
181      */
isHomeAndOverviewSame()182     public boolean isHomeAndOverviewSame() {
183         return mIsHomeAndOverviewSame;
184     }
185 
186     /**
187      * Get the current activity control helper for managing interactions to the overview activity.
188      *
189      * @return the current activity control helper
190      */
getActivityControlHelper()191     public ActivityControlHelper getActivityControlHelper() {
192         return mActivityControlHelper;
193     }
194 }
195