• 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 package com.android.car.settings.quicksettings;
17 
18 import android.car.drivingstate.CarUxRestrictions;
19 import android.car.user.CarUserManagerHelper;
20 import android.content.pm.UserInfo;
21 import android.os.Bundle;
22 import android.view.View;
23 import android.view.View.OnClickListener;
24 import android.widget.ImageView;
25 import android.widget.TextView;
26 
27 import androidx.car.widget.PagedListView;
28 
29 import com.android.car.settings.R;
30 import com.android.car.settings.common.BaseFragment;
31 import com.android.car.settings.common.CarUxRestrictionsHelper;
32 import com.android.car.settings.home.HomepageFragment;
33 import com.android.car.settings.users.UserIconProvider;
34 import com.android.car.settings.users.UserSwitcherFragment;
35 
36 /**
37  * Shows a page to access frequently used settings.
38  */
39 public class QuickSettingFragment extends BaseFragment {
40     private CarUserManagerHelper  mCarUserManagerHelper;
41     private UserIconProvider mUserIconProvider;
42     private QuickSettingGridAdapter mGridAdapter;
43     private PagedListView mListView;
44     private View mFullSettingBtn;
45     private View mUserSwitcherBtn;
46     private HomeFragmentLauncher mHomeFragmentLauncher;
47     private float mOpacityDisabled;
48     private float mOpacityEnabled;
49 
50     /**
51      * Returns an instance of this class.
52      */
newInstance()53     public static QuickSettingFragment newInstance() {
54         QuickSettingFragment quickSettingFragment = new QuickSettingFragment();
55         Bundle bundle = quickSettingFragment.getBundle();
56         bundle.putInt(EXTRA_ACTION_BAR_LAYOUT, R.layout.action_bar_quick_settings);
57         bundle.putInt(EXTRA_LAYOUT, R.layout.quick_settings);
58         bundle.putInt(EXTRA_TITLE_ID, R.string.settings_label);
59         quickSettingFragment.setArguments(bundle);
60         return quickSettingFragment;
61     }
62 
63     @Override
onActivityCreated(Bundle savedInstanceState)64     public void onActivityCreated(Bundle savedInstanceState) {
65         super.onActivityCreated(savedInstanceState);
66         mHomeFragmentLauncher = new HomeFragmentLauncher();
67         getActivity().findViewById(R.id.action_bar_icon_container).setOnClickListener(
68                 v -> getActivity().finish());
69 
70         mOpacityDisabled = getContext().getResources().getFloat(R.dimen.opacity_disabled);
71         mOpacityEnabled = getContext().getResources().getFloat(R.dimen.opacity_enabled);
72         mCarUserManagerHelper = new CarUserManagerHelper(getContext());
73         mUserIconProvider = new UserIconProvider(mCarUserManagerHelper);
74         mListView = (PagedListView) getActivity().findViewById(R.id.list);
75         mGridAdapter = new QuickSettingGridAdapter(getContext());
76         mListView.getRecyclerView().setLayoutManager(mGridAdapter.getGridLayoutManager());
77 
78         mFullSettingBtn = getActivity().findViewById(R.id.full_setting_btn);
79         mFullSettingBtn.setOnClickListener(mHomeFragmentLauncher);
80         mUserSwitcherBtn = getActivity().findViewById(R.id.user_switcher_btn);
81         mUserSwitcherBtn.setOnClickListener(v -> {
82             getFragmentController().launchFragment(UserSwitcherFragment.newInstance());
83         });
84 
85         View exitBtn = getActivity().findViewById(R.id.exit_button);
86         exitBtn.setOnClickListener(v -> getFragmentController().goBack());
87 
88         mGridAdapter
89                 .addTile(new WifiTile(getContext(), mGridAdapter, getFragmentController()))
90                 .addTile(new BluetoothTile(getContext(), mGridAdapter))
91                 .addTile(new DayNightTile(getContext(), mGridAdapter))
92                 .addTile(new CelluarTile(getContext(), mGridAdapter))
93                 .addSeekbarTile(new BrightnessTile(getContext()));
94         mListView.setAdapter(mGridAdapter);
95     }
96 
97     @Override
onStop()98     public void onStop() {
99         super.onStop();
100         mGridAdapter.stop();
101     }
102 
setupAccountButton()103     private void setupAccountButton() {
104         ImageView userIcon = (ImageView) getActivity().findViewById(R.id.user_icon);
105         UserInfo currentUserInfo = mCarUserManagerHelper.getCurrentForegroundUserInfo();
106         userIcon.setImageDrawable(mUserIconProvider.getUserIcon(currentUserInfo, getContext()));
107         userIcon.clearColorFilter();
108 
109         TextView userSwitcherText = (TextView) getActivity().findViewById(R.id.user_switcher_text);
110         userSwitcherText.setText(currentUserInfo.name);
111     }
112 
113     /**
114      * Quick setting fragment is distraction optimized, so is allowed at all times.
115      */
116     @Override
canBeShown(CarUxRestrictions carUxRestrictions)117     public boolean canBeShown(CarUxRestrictions carUxRestrictions) {
118         return true;
119     }
120 
121     @Override
onUxRestrictionChanged(CarUxRestrictions carUxRestrictions)122     public void onUxRestrictionChanged(CarUxRestrictions carUxRestrictions) {
123         // TODO: update tiles
124         applyRestriction(CarUxRestrictionsHelper.isNoSetup(carUxRestrictions));
125     }
126 
applyRestriction(boolean restricted)127     private void applyRestriction(boolean restricted) {
128         mHomeFragmentLauncher.showDOBlockingMessage(restricted);
129         mFullSettingBtn.setAlpha(restricted ? mOpacityDisabled : mOpacityEnabled);
130     }
131 
132     private class HomeFragmentLauncher implements OnClickListener {
133         private boolean mShowDOBlockingMessage;
134 
showDOBlockingMessage(boolean show)135         private void showDOBlockingMessage(boolean show) {
136             mShowDOBlockingMessage = show;
137         }
138 
139         @Override
onClick(View v)140         public void onClick(View v) {
141             if (mShowDOBlockingMessage) {
142                 getFragmentController().showDOBlockingMessage();
143             } else {
144                 getFragmentController().launchFragment(HomepageFragment.newInstance());
145             }
146         }
147     }
148 }
149