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