1 /* 2 * Copyright (C) 2020 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 android.app; 18 19 import android.content.Context; 20 import android.content.Intent; 21 import android.content.pm.PackageManager; 22 import android.content.pm.ResolveInfo; 23 24 import java.util.List; 25 26 /** 27 * An observer / callback to create and register by 28 * {@link ActivityManager#registerHomeVisibilityObserver} so that it's triggered when 29 * visibility of home page changes. 30 * TODO: b/144351078 expose as SystemApi 31 * @hide 32 */ 33 public abstract class HomeVisibilityObserver { 34 private Context mContext; 35 private ActivityManager mActivityManager; 36 /** @hide */ 37 IProcessObserver.Stub mObserver; 38 /** @hide */ 39 boolean mIsHomeActivityVisible; 40 41 /** @hide */ init(Context context, ActivityManager activityManager)42 void init(Context context, ActivityManager activityManager) { 43 mContext = context; 44 mActivityManager = activityManager; 45 mIsHomeActivityVisible = isHomeActivityVisible(); 46 } 47 48 /** 49 * The API that needs implemented and will be triggered when activity on home page changes. 50 */ onHomeVisibilityChanged(boolean isHomeActivityVisible)51 public abstract void onHomeVisibilityChanged(boolean isHomeActivityVisible); 52 HomeVisibilityObserver()53 public HomeVisibilityObserver() { 54 mObserver = new IProcessObserver.Stub() { 55 @Override 56 public void onForegroundActivitiesChanged(int pid, int uid, boolean fg) { 57 boolean isHomeActivityVisible = isHomeActivityVisible(); 58 if (mIsHomeActivityVisible != isHomeActivityVisible) { 59 mIsHomeActivityVisible = isHomeActivityVisible; 60 onHomeVisibilityChanged(mIsHomeActivityVisible); 61 } 62 } 63 64 @Override 65 public void onForegroundServicesChanged(int pid, int uid, int fgServiceTypes) { 66 } 67 68 @Override 69 public void onProcessDied(int pid, int uid) { 70 } 71 }; 72 } 73 isHomeActivityVisible()74 private boolean isHomeActivityVisible() { 75 List<ActivityManager.RunningTaskInfo> tasks = mActivityManager.getRunningTasks(1); 76 if (tasks == null || tasks.isEmpty()) { 77 return false; 78 } 79 80 String top = tasks.get(0).topActivity.getPackageName(); 81 if (top == null) { 82 return false; 83 } 84 85 // We can assume that the screen is idle if the home application is in the foreground. 86 final Intent intent = new Intent(Intent.ACTION_MAIN, null); 87 intent.addCategory(Intent.CATEGORY_HOME); 88 89 ResolveInfo info = mContext.getPackageManager().resolveActivity(intent, 90 PackageManager.MATCH_DEFAULT_ONLY); 91 if (info != null && top.equals(info.activityInfo.packageName)) { 92 return true; 93 } 94 95 return false; 96 } 97 } 98