blob: b09d6e163b77c94c9ab34d16c7719a3a68c3be6b [file] [log] [blame]
Winson Chung95bccdc2018-01-03 11:45:47 -08001/*
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
17package com.android.systemui.pip.phone;
18
19import static android.app.AppOpsManager.MODE_ALLOWED;
20import static android.app.AppOpsManager.OP_PICTURE_IN_PICTURE;
21
22import android.app.AppOpsManager;
23import android.app.AppOpsManager.OnOpChangedListener;
24import android.app.IActivityManager;
25import android.content.ComponentName;
26import android.content.Context;
27import android.content.pm.ApplicationInfo;
28import android.content.pm.PackageManager.NameNotFoundException;
Tracy Zhou0f6525d2018-02-22 17:08:53 -080029import android.os.Handler;
Winson Chung95bccdc2018-01-03 11:45:47 -080030import android.util.Pair;
31
32public class PipAppOpsListener {
33 private static final String TAG = PipAppOpsListener.class.getSimpleName();
34
35 private Context mContext;
Tracy Zhou0f6525d2018-02-22 17:08:53 -080036 private Handler mHandler;
Winson Chung95bccdc2018-01-03 11:45:47 -080037 private IActivityManager mActivityManager;
38 private AppOpsManager mAppOpsManager;
Eliot Courtney293d86e2019-01-08 16:42:39 +090039 private Callback mCallback;
Winson Chung95bccdc2018-01-03 11:45:47 -080040
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 Courtney293d86e2019-01-08 16:42:39 +090054 mHandler.post(() -> mCallback.dismissPip());
Winson Chung95bccdc2018-01-03 11:45:47 -080055 }
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 Courtney293d86e2019-01-08 16:42:39 +090065 Callback callback) {
Winson Chung95bccdc2018-01-03 11:45:47 -080066 mContext = context;
Tracy Zhou0f6525d2018-02-22 17:08:53 -080067 mHandler = new Handler(mContext.getMainLooper());
Winson Chung95bccdc2018-01-03 11:45:47 -080068 mActivityManager = activityManager;
69 mAppOpsManager = (AppOpsManager) context.getSystemService(Context.APP_OPS_SERVICE);
Eliot Courtney293d86e2019-01-08 16:42:39 +090070 mCallback = callback;
Winson Chung95bccdc2018-01-03 11:45:47 -080071 }
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 Courtney293d86e2019-01-08 16:42:39 +090091
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}