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.core.view.ViewCompat;
33 import androidx.core.view.WindowInsetsControllerCompat;
34 import androidx.fragment.app.Fragment;
35 import androidx.fragment.app.FragmentActivity;
36 import androidx.fragment.app.FragmentManager;
37 
38 import com.android.internal.annotations.VisibleForTesting;
39 import com.android.settings.R;
40 import com.android.settings.Utils;
41 import com.android.settingslib.core.lifecycle.HideNonSystemOverlayMixin;
42 
43 /**
44  * Dialog Activity to host Settings Slices.
45  *
46  * @deprecated this is no longer used after V and will be removed.
47  */
48 @Deprecated(forRemoval = true)
49 public class SettingsPanelActivity extends FragmentActivity {
50 
51     private static final String TAG = "SettingsPanelActivity";
52 
53     @VisibleForTesting
54     final Bundle mBundle = new Bundle();
55     @VisibleForTesting
56     boolean mForceCreation = false;
57     @VisibleForTesting
58     PanelFragment mPanelFragment;
59 
60     /**
61      * Key specifying which Panel the app is requesting.
62      */
63     public static final String KEY_PANEL_TYPE_ARGUMENT = "PANEL_TYPE_ARGUMENT";
64 
65     /**
66      * Key specifying the package which requested the Panel.
67      */
68     public static final String KEY_CALLING_PACKAGE_NAME = "PANEL_CALLING_PACKAGE_NAME";
69 
70     /**
71      * Key specifying the package name for which the
72      */
73     public static final String KEY_MEDIA_PACKAGE_NAME = "PANEL_MEDIA_PACKAGE_NAME";
74 
75     @Override
onCreate(@ullable Bundle savedInstanceState)76     protected void onCreate(@Nullable Bundle savedInstanceState) {
77         super.onCreate(savedInstanceState);
78         getApplicationContext().getTheme().rebase();
79         createOrUpdatePanel(true /* shouldForceCreation */);
80         getLifecycle().addObserver(new HideNonSystemOverlayMixin(this));
81     }
82 
83     @Override
onNewIntent(Intent intent)84     protected void onNewIntent(Intent intent) {
85         super.onNewIntent(intent);
86         setIntent(intent);
87         createOrUpdatePanel(mForceCreation);
88     }
89 
90     @Override
onResume()91     protected void onResume() {
92         super.onResume();
93         mForceCreation = false;
94     }
95 
96     @Override
onStop()97     protected void onStop() {
98         super.onStop();
99         if (mPanelFragment != null && !mPanelFragment.isPanelCreating()) {
100             mForceCreation = true;
101         }
102     }
103 
104     @Override
onConfigurationChanged(@onNull Configuration newConfig)105     public void onConfigurationChanged(@NonNull Configuration newConfig) {
106         super.onConfigurationChanged(newConfig);
107         mForceCreation = true;
108     }
109 
createOrUpdatePanel(boolean shouldForceCreation)110     private void createOrUpdatePanel(boolean shouldForceCreation) {
111         final Intent callingIntent = getIntent();
112         if (callingIntent == null) {
113             Log.e(TAG, "Null intent, closing Panel Activity");
114             finish();
115             return;
116         }
117 
118         final String action = callingIntent.getAction();
119         // We will use it once media output switch panel support remote device.
120         final String mediaPackageName = callingIntent.getStringExtra(EXTRA_PACKAGE_NAME);
121         mBundle.putString(KEY_PANEL_TYPE_ARGUMENT, action);
122         mBundle.putString(KEY_CALLING_PACKAGE_NAME, getCallingPackage());
123         mBundle.putString(KEY_MEDIA_PACKAGE_NAME, mediaPackageName);
124 
125         final FragmentManager fragmentManager = getSupportFragmentManager();
126         final Fragment fragment = fragmentManager.findFragmentById(R.id.main_content);
127 
128         // If fragment already exists and visible, we will need to update panel with animation.
129         if (!shouldForceCreation && fragment != null && fragment instanceof PanelFragment) {
130             mPanelFragment = (PanelFragment) fragment;
131             if (mPanelFragment.isPanelCreating()) {
132                 Log.w(TAG, "A panel is creating, skip " + action);
133                 return;
134             }
135 
136             final Bundle bundle = fragment.getArguments();
137             if (bundle != null
138                     && TextUtils.equals(action, bundle.getString(KEY_PANEL_TYPE_ARGUMENT))) {
139                 Log.w(TAG, "Panel is showing the same action, skip " + action);
140                 return;
141             }
142 
143             mPanelFragment.setArguments(new Bundle(mBundle));
144             mPanelFragment.updatePanelWithAnimation();
145         } else {
146             setContentView(R.layout.settings_panel);
147 
148             // Move the window to the bottom of screen, and make it take up the entire screen width.
149             final Window window = getWindow();
150             window.setGravity(Gravity.BOTTOM);
151             window.setLayout(WindowManager.LayoutParams.MATCH_PARENT,
152                     WindowManager.LayoutParams.WRAP_CONTENT);
153             setupNavigationBar();
154             mPanelFragment = new PanelFragment();
155             mPanelFragment.setArguments(new Bundle(mBundle));
156             fragmentManager.beginTransaction().add(R.id.main_content, mPanelFragment).commit();
157         }
158     }
159 
160     /**
161      * Adjust bottom edge and color.
162      */
setupNavigationBar()163     private void setupNavigationBar() {
164         // Extend the panel all the way to the bottom of the screen, as opposed to sitting on top of
165         // the navigation bar.
166         ViewCompat.setOnApplyWindowInsetsListener(getWindow().getDecorView(),
167                 (v, windowInsets) -> {
168                     v.setPadding(v.getPaddingLeft(), v.getPaddingTop(), v.getPaddingRight(), 0);
169                     return windowInsets; // propagate down to panel layout root element
170                 });
171 
172         // When using 3-button navigation in light mode, the system picks white navigation buttons
173         // which are not sufficiently contrasted from the panel background.
174         WindowInsetsControllerCompat windowInsetsController =
175                 ViewCompat.getWindowInsetsController(getWindow().getDecorView());
176 
177         if (windowInsetsController != null) {
178             boolean forceNavigationButtonsDark = !Utils.isNightMode(this);
179             windowInsetsController.setAppearanceLightNavigationBars(forceNavigationButtonsDark);
180         }
181     }
182 }
183