1 /*
2  * Copyright (C) 2016 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;
18 
19 import static android.content.pm.PackageManager.FEATURE_LEANBACK_ONLY;
20 import static android.content.pm.PackageManager.FEATURE_PICTURE_IN_PICTURE;
21 
22 import android.content.pm.PackageManager;
23 import android.content.res.Configuration;
24 import android.os.UserHandle;
25 import android.os.UserManager;
26 
27 import com.android.systemui.SystemUI;
28 import com.android.systemui.statusbar.CommandQueue;
29 
30 import java.io.FileDescriptor;
31 import java.io.PrintWriter;
32 
33 /**
34  * Controls the picture-in-picture window.
35  */
36 public class PipUI extends SystemUI implements CommandQueue.Callbacks {
37 
38     private BasePipManager mPipManager;
39 
40     private boolean mSupportsPip;
41 
42     @Override
start()43     public void start() {
44         PackageManager pm = mContext.getPackageManager();
45         mSupportsPip = pm.hasSystemFeature(FEATURE_PICTURE_IN_PICTURE);
46         if (!mSupportsPip) {
47             return;
48         }
49 
50         // Ensure that we are the primary user's SystemUI.
51         final int processUser = UserManager.get(mContext).getUserHandle();
52         if (processUser != UserHandle.USER_SYSTEM) {
53             throw new IllegalStateException("Non-primary Pip component not currently supported.");
54         }
55 
56         mPipManager = pm.hasSystemFeature(FEATURE_LEANBACK_ONLY)
57                 ? com.android.systemui.pip.tv.PipManager.getInstance()
58                 : com.android.systemui.pip.phone.PipManager.getInstance();
59         mPipManager.initialize(mContext);
60 
61         getComponent(CommandQueue.class).addCallback(this);
62         putComponent(PipUI.class, this);
63     }
64 
65     @Override
showPictureInPictureMenu()66     public void showPictureInPictureMenu() {
67         mPipManager.showPictureInPictureMenu();
68     }
69 
expandPip()70     public void expandPip() {
71         mPipManager.expandPip();
72     }
73 
hidePipMenu(Runnable onStartCallback, Runnable onEndCallback)74     public void hidePipMenu(Runnable onStartCallback, Runnable onEndCallback) {
75         mPipManager.hidePipMenu(onStartCallback, onEndCallback);
76     }
77 
78     @Override
onConfigurationChanged(Configuration newConfig)79     protected void onConfigurationChanged(Configuration newConfig) {
80         super.onConfigurationChanged(newConfig);
81         if (mPipManager == null) {
82             return;
83         }
84 
85         mPipManager.onConfigurationChanged(newConfig);
86     }
87 
88     @Override
dump(FileDescriptor fd, PrintWriter pw, String[] args)89     public void dump(FileDescriptor fd, PrintWriter pw, String[] args) {
90         if (mPipManager == null) {
91             return;
92         }
93 
94         mPipManager.dump(pw);
95     }
96 }
97