Merge "Add HomeVisibilityObserver APIs in ActivityManager."
This commit is contained in:
@@ -311,10 +311,12 @@ package android.app {
|
||||
method @NonNull public java.util.Collection<java.util.Locale> getSupportedLocales();
|
||||
method @RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) public int getUidImportance(int);
|
||||
method @RequiresPermission(android.Manifest.permission.KILL_UID) public void killUid(int, String);
|
||||
method public void registerHomeVisibilityObserver(@NonNull android.app.HomeVisibilityObserver);
|
||||
method @RequiresPermission(android.Manifest.permission.PACKAGE_USAGE_STATS) public void removeOnUidImportanceListener(android.app.ActivityManager.OnUidImportanceListener);
|
||||
method public void setDeviceLocales(@NonNull android.os.LocaleList);
|
||||
method @RequiresPermission(android.Manifest.permission.RESTRICTED_VR_ACCESS) public static void setPersistentVrThread(int);
|
||||
method @RequiresPermission(android.Manifest.permission.MANAGE_USERS) public boolean switchUser(@NonNull android.os.UserHandle);
|
||||
method public void unregisterHomeVisibilityObserver(@NonNull android.app.HomeVisibilityObserver);
|
||||
}
|
||||
|
||||
public static interface ActivityManager.OnUidImportanceListener {
|
||||
@@ -501,6 +503,11 @@ package android.app {
|
||||
field public static final String ACTION_DOWNLOAD_COMPLETED = "android.intent.action.DOWNLOAD_COMPLETED";
|
||||
}
|
||||
|
||||
public abstract class HomeVisibilityObserver {
|
||||
ctor public HomeVisibilityObserver();
|
||||
method public abstract void onHomeVisibilityChanged(boolean);
|
||||
}
|
||||
|
||||
public abstract class InstantAppResolverService extends android.app.Service {
|
||||
ctor public InstantAppResolverService();
|
||||
method public final void attachBaseContext(android.content.Context);
|
||||
|
||||
@@ -75,6 +75,7 @@ import com.android.internal.os.RoSystemProperties;
|
||||
import com.android.internal.os.TransferPipe;
|
||||
import com.android.internal.util.FastPrintWriter;
|
||||
import com.android.internal.util.MemInfoReader;
|
||||
import com.android.internal.util.Preconditions;
|
||||
import com.android.server.LocalServices;
|
||||
|
||||
import org.xmlpull.v1.XmlSerializer;
|
||||
@@ -4330,4 +4331,35 @@ public class ActivityManager {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Register with {@link HomeVisibilityObserver} with ActivityManager.
|
||||
* @hide
|
||||
*/
|
||||
@SystemApi
|
||||
public void registerHomeVisibilityObserver(@NonNull HomeVisibilityObserver observer) {
|
||||
Preconditions.checkNotNull(observer);
|
||||
try {
|
||||
observer.init(mContext, this);
|
||||
getService().registerProcessObserver(observer.mObserver);
|
||||
// Notify upon first registration.
|
||||
observer.onHomeVisibilityChanged(observer.mIsHomeActivityVisible);
|
||||
} catch (RemoteException e) {
|
||||
throw e.rethrowFromSystemServer();
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* Unregister with {@link HomeVisibilityObserver} with ActivityManager.
|
||||
* @hide
|
||||
*/
|
||||
@SystemApi
|
||||
public void unregisterHomeVisibilityObserver(@NonNull HomeVisibilityObserver observer) {
|
||||
Preconditions.checkNotNull(observer);
|
||||
try {
|
||||
getService().unregisterProcessObserver(observer.mObserver);
|
||||
} catch (RemoteException e) {
|
||||
throw e.rethrowFromSystemServer();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
98
core/java/android/app/HomeVisibilityObserver.java
Normal file
98
core/java/android/app/HomeVisibilityObserver.java
Normal file
@@ -0,0 +1,98 @@
|
||||
/*
|
||||
* Copyright (C) 2020 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 android.app;
|
||||
|
||||
import android.annotation.SystemApi;
|
||||
import android.content.Context;
|
||||
import android.content.Intent;
|
||||
import android.content.pm.PackageManager;
|
||||
import android.content.pm.ResolveInfo;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* An observer / callback to create and register by
|
||||
* {@link ActivityManager#registerHomeVisibilityObserver} so that it's triggered when
|
||||
* visibility of home page changes.
|
||||
* @hide
|
||||
*/
|
||||
@SystemApi
|
||||
public abstract class HomeVisibilityObserver {
|
||||
private Context mContext;
|
||||
private ActivityManager mActivityManager;
|
||||
/** @hide */
|
||||
IProcessObserver.Stub mObserver;
|
||||
/** @hide */
|
||||
boolean mIsHomeActivityVisible;
|
||||
|
||||
/** @hide */
|
||||
void init(Context context, ActivityManager activityManager) {
|
||||
mContext = context;
|
||||
mActivityManager = activityManager;
|
||||
mIsHomeActivityVisible = isHomeActivityVisible();
|
||||
}
|
||||
|
||||
/**
|
||||
* The API that needs implemented and will be triggered when activity on home page changes.
|
||||
*/
|
||||
public abstract void onHomeVisibilityChanged(boolean isHomeActivityVisible);
|
||||
|
||||
public HomeVisibilityObserver() {
|
||||
mObserver = new IProcessObserver.Stub() {
|
||||
@Override
|
||||
public void onForegroundActivitiesChanged(int pid, int uid, boolean fg) {
|
||||
boolean isHomeActivityVisible = isHomeActivityVisible();
|
||||
if (mIsHomeActivityVisible != isHomeActivityVisible) {
|
||||
mIsHomeActivityVisible = isHomeActivityVisible;
|
||||
onHomeVisibilityChanged(mIsHomeActivityVisible);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onForegroundServicesChanged(int pid, int uid, int fgServiceTypes) {
|
||||
}
|
||||
|
||||
@Override
|
||||
public void onProcessDied(int pid, int uid) {
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
private boolean isHomeActivityVisible() {
|
||||
List<ActivityManager.RunningTaskInfo> tasks = mActivityManager.getRunningTasks(1);
|
||||
if (tasks == null || tasks.isEmpty()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
String top = tasks.get(0).topActivity.getPackageName();
|
||||
if (top == null) {
|
||||
return false;
|
||||
}
|
||||
|
||||
// We can assume that the screen is idle if the home application is in the foreground.
|
||||
final Intent intent = new Intent(Intent.ACTION_MAIN, null);
|
||||
intent.addCategory(Intent.CATEGORY_HOME);
|
||||
|
||||
ResolveInfo info = mContext.getPackageManager().resolveActivity(intent,
|
||||
PackageManager.MATCH_DEFAULT_ONLY);
|
||||
if (info != null && top.equals(info.activityInfo.packageName)) {
|
||||
return true;
|
||||
}
|
||||
|
||||
return false;
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user