Winson Chung | 95bccdc | 2018-01-03 11:45:47 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2017 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.systemui.pip.phone; |
| 18 | |
| 19 | import static android.app.AppOpsManager.MODE_ALLOWED; |
| 20 | import static android.app.AppOpsManager.OP_PICTURE_IN_PICTURE; |
| 21 | |
| 22 | import android.app.AppOpsManager; |
| 23 | import android.app.AppOpsManager.OnOpChangedListener; |
| 24 | import android.app.IActivityManager; |
| 25 | import android.content.ComponentName; |
| 26 | import android.content.Context; |
| 27 | import android.content.pm.ApplicationInfo; |
| 28 | import android.content.pm.PackageManager.NameNotFoundException; |
Tracy Zhou | 0f6525d | 2018-02-22 17:08:53 -0800 | [diff] [blame] | 29 | import android.os.Handler; |
Winson Chung | 95bccdc | 2018-01-03 11:45:47 -0800 | [diff] [blame] | 30 | import android.util.Pair; |
| 31 | |
| 32 | public class PipAppOpsListener { |
| 33 | private static final String TAG = PipAppOpsListener.class.getSimpleName(); |
| 34 | |
| 35 | private Context mContext; |
Tracy Zhou | 0f6525d | 2018-02-22 17:08:53 -0800 | [diff] [blame] | 36 | private Handler mHandler; |
Winson Chung | 95bccdc | 2018-01-03 11:45:47 -0800 | [diff] [blame] | 37 | private IActivityManager mActivityManager; |
| 38 | private AppOpsManager mAppOpsManager; |
Eliot Courtney | 293d86e | 2019-01-08 16:42:39 +0900 | [diff] [blame] | 39 | private Callback mCallback; |
Winson Chung | 95bccdc | 2018-01-03 11:45:47 -0800 | [diff] [blame] | 40 | |
| 41 | private AppOpsManager.OnOpChangedListener mAppOpsChangedListener = new OnOpChangedListener() { |
| 42 | @Override |
| 43 | public void onOpChanged(String op, String packageName) { |
| 44 | try { |
| 45 | // Dismiss the PiP once the user disables the app ops setting for that package |
| 46 | final Pair<ComponentName, Integer> topPipActivityInfo = |
| 47 | PipUtils.getTopPinnedActivity(mContext, mActivityManager); |
| 48 | if (topPipActivityInfo.first != null) { |
| 49 | final ApplicationInfo appInfo = mContext.getPackageManager() |
| 50 | .getApplicationInfoAsUser(packageName, 0, topPipActivityInfo.second); |
| 51 | if (appInfo.packageName.equals(topPipActivityInfo.first.getPackageName()) && |
| 52 | mAppOpsManager.checkOpNoThrow(OP_PICTURE_IN_PICTURE, appInfo.uid, |
| 53 | packageName) != MODE_ALLOWED) { |
Eliot Courtney | 293d86e | 2019-01-08 16:42:39 +0900 | [diff] [blame] | 54 | mHandler.post(() -> mCallback.dismissPip()); |
Winson Chung | 95bccdc | 2018-01-03 11:45:47 -0800 | [diff] [blame] | 55 | } |
| 56 | } |
| 57 | } catch (NameNotFoundException e) { |
| 58 | // Unregister the listener if the package can't be found |
| 59 | unregisterAppOpsListener(); |
| 60 | } |
| 61 | } |
| 62 | }; |
| 63 | |
| 64 | public PipAppOpsListener(Context context, IActivityManager activityManager, |
Eliot Courtney | 293d86e | 2019-01-08 16:42:39 +0900 | [diff] [blame] | 65 | Callback callback) { |
Winson Chung | 95bccdc | 2018-01-03 11:45:47 -0800 | [diff] [blame] | 66 | mContext = context; |
Tracy Zhou | 0f6525d | 2018-02-22 17:08:53 -0800 | [diff] [blame] | 67 | mHandler = new Handler(mContext.getMainLooper()); |
Winson Chung | 95bccdc | 2018-01-03 11:45:47 -0800 | [diff] [blame] | 68 | mActivityManager = activityManager; |
| 69 | mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE); |
Eliot Courtney | 293d86e | 2019-01-08 16:42:39 +0900 | [diff] [blame] | 70 | mCallback = callback; |
Winson Chung | 95bccdc | 2018-01-03 11:45:47 -0800 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | public void onActivityPinned(String packageName) { |
| 74 | // Register for changes to the app ops setting for this package while it is in PiP |
| 75 | registerAppOpsListener(packageName); |
| 76 | } |
| 77 | |
| 78 | public void onActivityUnpinned() { |
| 79 | // Unregister for changes to the previously PiP'ed package |
| 80 | unregisterAppOpsListener(); |
| 81 | } |
| 82 | |
| 83 | private void registerAppOpsListener(String packageName) { |
| 84 | mAppOpsManager.startWatchingMode(OP_PICTURE_IN_PICTURE, packageName, |
| 85 | mAppOpsChangedListener); |
| 86 | } |
| 87 | |
| 88 | private void unregisterAppOpsListener() { |
| 89 | mAppOpsManager.stopWatchingMode(mAppOpsChangedListener); |
| 90 | } |
Eliot Courtney | 293d86e | 2019-01-08 16:42:39 +0900 | [diff] [blame] | 91 | |
| 92 | /** Callback for PipAppOpsListener to request changes to the PIP window. */ |
| 93 | public interface Callback { |
| 94 | /** Dismisses the PIP window. */ |
| 95 | void dismissPip(); |
| 96 | } |
| 97 | } |