1 /*
2  * Copyright (C) 2018 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.settings.panel;
18 
19 import static com.android.settingslib.media.MediaOutputConstants.EXTRA_PACKAGE_NAME;
20 
21 import android.content.Intent;
22 import android.content.res.Configuration;
23 import android.os.Bundle;
24 import android.text.TextUtils;
25 import android.util.Log;
26 import android.view.Gravity;
27 import android.view.Window;
28 import android.view.WindowManager;
29 
30 import androidx.annotation.NonNull;
31 import androidx.annotation.Nullable;
32 import androidx.fragment.app.Fragment;
33 import androidx.fragment.app.FragmentActivity;
34 import androidx.fragment.app.FragmentManager;
35 
36 import com.android.internal.annotations.VisibleForTesting;
37 import com.android.settings.R;
38 import com.android.settingslib.core.lifecycle.HideNonSystemOverlayMixin;
39 
40 /**
41  * Dialog Activity to host Settings Slices.
42  */
43 public class SettingsPanelActivity extends FragmentActivity {
44 
45     private static final String TAG = "SettingsPanelActivity";
46 
47     @VisibleForTesting
48     final Bundle mBundle = new Bundle();
49     @VisibleForTesting
50     boolean mForceCreation = false;
51     @VisibleForTesting
52     PanelFragment mPanelFragment;
53 
54     /**
55      * Key specifying which Panel the app is requesting.
56      */
57     public static final String KEY_PANEL_TYPE_ARGUMENT = "PANEL_TYPE_ARGUMENT";
58 
59     /**
60      * Key specifying the package which requested the Panel.
61      */
62     public static final String KEY_CALLING_PACKAGE_NAME = "PANEL_CALLING_PACKAGE_NAME";
63 
64     /**
65      * Key specifying the package name for which the
66      */
67     public static final String KEY_MEDIA_PACKAGE_NAME = "PANEL_MEDIA_PACKAGE_NAME";
68 
69     @Override
onCreate(@ullable Bundle savedInstanceState)70     protected void onCreate(@Nullable Bundle savedInstanceState) {
71         super.onCreate(savedInstanceState);
72         getApplicationContext().getTheme().rebase();
73         createOrUpdatePanel(true /* shouldForceCreation */);
74         getLifecycle().addObserver(new HideNonSystemOverlayMixin(this));
75     }
76 
77     @Override
onNewIntent(Intent intent)78     protected void onNewIntent(Intent intent) {
79         super.onNewIntent(intent);
80         setIntent(intent);
81         createOrUpdatePanel(mForceCreation);
82     }
83 
84     @Override
onResume()85     protected void onResume() {
86         super.onResume();
87         mForceCreation = false;
88     }
89 
90     @Override
onStop()91     protected void onStop() {
92         super.onStop();
93         if (mPanelFragment != null && !mPanelFragment.isPanelCreating()) {
94             mForceCreation = true;
95         }
96     }
97 
98     @Override
onConfigurationChanged(@onNull Configuration newConfig)99     public void onConfigurationChanged(@NonNull Configuration newConfig) {
100         super.onConfigurationChanged(newConfig);
101         mForceCreation = true;
102     }
103 
createOrUpdatePanel(boolean shouldForceCreation)104     private void createOrUpdatePanel(boolean shouldForceCreation) {
105         final Intent callingIntent = getIntent();
106         if (callingIntent == null) {
107             Log.e(TAG, "Null intent, closing Panel Activity");
108             finish();
109             return;
110         }
111 
112         final String action = callingIntent.getAction();
113         // We will use it once media output switch panel support remote device.
114         final String mediaPackageName = callingIntent.getStringExtra(EXTRA_PACKAGE_NAME);
115         mBundle.putString(KEY_PANEL_TYPE_ARGUMENT, action);
116         mBundle.putString(KEY_CALLING_PACKAGE_NAME, getCallingPackage());
117         mBundle.putString(KEY_MEDIA_PACKAGE_NAME, mediaPackageName);
118 
119         final FragmentManager fragmentManager = getSupportFragmentManager();
120         final Fragment fragment = fragmentManager.findFragmentById(R.id.main_content);
121 
122         // If fragment already exists and visible, we will need to update panel with animation.
123         if (!shouldForceCreation && fragment != null && fragment instanceof PanelFragment) {
124             mPanelFragment = (PanelFragment) fragment;
125             if (mPanelFragment.isPanelCreating()) {
126                 Log.w(TAG, "A panel is creating, skip " + action);
127                 return;
128             }
129 
130             final Bundle bundle = fragment.getArguments();
131             if (bundle != null
132                     && TextUtils.equals(action, bundle.getString(KEY_PANEL_TYPE_ARGUMENT))) {
133                 Log.w(TAG, "Panel is showing the same action, skip " + action);
134                 return;
135             }
136 
137             mPanelFragment.setArguments(new Bundle(mBundle));
138             mPanelFragment.updatePanelWithAnimation();
139         } else {
140             setContentView(R.layout.settings_panel);
141 
142             // Move the window to the bottom of screen, and make it take up the entire screen width.
143             final Window window = getWindow();
144             window.setGravity(Gravity.BOTTOM);
145             window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
146                     WindowManager.LayoutParams.WRAP_CONTENT);
147             mPanelFragment = new PanelFragment();
148             mPanelFragment.setArguments(new Bundle(mBundle));
149             fragmentManager.beginTransaction().add(R.id.main_content, mPanelFragment).commit();
150         }
151     }
152 }
153