• 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.homepage;
18 
19 import android.animation.LayoutTransition;
20 import android.app.ActivityManager;
21 import android.app.settings.SettingsEnums;
22 import android.os.Bundle;
23 import android.view.View;
24 import android.widget.FrameLayout;
25 import android.widget.ImageView;
26 import android.widget.Toolbar;
27 
28 import androidx.annotation.VisibleForTesting;
29 import androidx.fragment.app.Fragment;
30 import androidx.fragment.app.FragmentActivity;
31 import androidx.fragment.app.FragmentManager;
32 import androidx.fragment.app.FragmentTransaction;
33 
34 import com.android.settings.R;
35 import com.android.settings.accounts.AvatarViewMixin;
36 import com.android.settings.homepage.contextualcards.ContextualCardsFragment;
37 import com.android.settings.overlay.FeatureFactory;
38 
39 public class SettingsHomepageActivity extends FragmentActivity {
40 
41     @Override
onCreate(Bundle savedInstanceState)42     protected void onCreate(Bundle savedInstanceState) {
43         super.onCreate(savedInstanceState);
44 
45         setContentView(R.layout.settings_homepage_container);
46         final View root = findViewById(R.id.settings_homepage_container);
47         root.setSystemUiVisibility(
48                 View.SYSTEM_UI_FLAG_LAYOUT_HIDE_NAVIGATION | View.SYSTEM_UI_FLAG_LAYOUT_STABLE);
49 
50         setHomepageContainerPaddingTop();
51 
52         final Toolbar toolbar = findViewById(R.id.search_action_bar);
53         FeatureFactory.getFactory(this).getSearchFeatureProvider()
54                 .initSearchToolbar(this /* activity */, toolbar, SettingsEnums.SETTINGS_HOMEPAGE);
55 
56         final ImageView avatarView = findViewById(R.id.account_avatar);
57         final AvatarViewMixin avatarViewMixin = new AvatarViewMixin(this, avatarView);
58         getLifecycle().addObserver(avatarViewMixin);
59 
60         if (!getSystemService(ActivityManager.class).isLowRamDevice()) {
61             // Only allow contextual feature on high ram devices.
62             showFragment(new ContextualCardsFragment(), R.id.contextual_cards_content);
63         }
64         showFragment(new TopLevelSettings(), R.id.main_content);
65         ((FrameLayout) findViewById(R.id.main_content))
66                 .getLayoutTransition().enableTransitionType(LayoutTransition.CHANGING);
67     }
68 
showFragment(Fragment fragment, int id)69     private void showFragment(Fragment fragment, int id) {
70         final FragmentManager fragmentManager = getSupportFragmentManager();
71         final FragmentTransaction fragmentTransaction = fragmentManager.beginTransaction();
72         final Fragment showFragment = fragmentManager.findFragmentById(id);
73 
74         if (showFragment == null) {
75             fragmentTransaction.add(id, fragment);
76         } else {
77             fragmentTransaction.show(showFragment);
78         }
79         fragmentTransaction.commit();
80     }
81 
82     @VisibleForTesting
setHomepageContainerPaddingTop()83     void setHomepageContainerPaddingTop() {
84         final View view = this.findViewById(R.id.homepage_container);
85 
86         final int searchBarHeight = getResources().getDimensionPixelSize(R.dimen.search_bar_height);
87         final int searchBarMargin = getResources().getDimensionPixelSize(R.dimen.search_bar_margin);
88 
89         // The top padding is the height of action bar(48dp) + top/bottom margins(16dp)
90         final int paddingTop = searchBarHeight + searchBarMargin * 2;
91         view.setPadding(0 /* left */, paddingTop, 0 /* right */, 0 /* bottom */);
92     }
93 }