Lujiang Xue | eaff6c3 | 2018-04-10 08:20:29 -0700 | [diff] [blame^] | 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.content.pm.UserInfo; |
| 20 | import android.os.Bundle; |
| 21 | import android.view.View; |
| 22 | import android.widget.ImageView; |
| 23 | import android.widget.TextView; |
| 24 | |
| 25 | import androidx.car.widget.PagedListView; |
| 26 | |
| 27 | import com.android.car.settings.R; |
| 28 | import com.android.car.settings.common.BaseFragment; |
| 29 | import com.android.car.settings.common.CarUxRestrictionsHelper; |
| 30 | import com.android.car.settings.home.HomepageFragment; |
| 31 | import com.android.car.settings.users.UserIconProvider; |
| 32 | import com.android.car.settings.users.UsersListFragment; |
| 33 | import com.android.settingslib.users.UserManagerHelper; |
| 34 | |
| 35 | /** |
| 36 | * Shows a page to access frequently used settings. |
| 37 | */ |
| 38 | public class QuickSettingFragment extends BaseFragment { |
| 39 | private static final String TAG = "QS"; |
| 40 | |
| 41 | private static final float RESTRICTED_ALPHA = 0.5f; |
| 42 | private static final float UNRESTRICTED_ALPHA = 1f; |
| 43 | |
| 44 | private UserManagerHelper mUserManagerHelper; |
| 45 | private QuickSettingGridAdapter mGridAdapter; |
| 46 | private PagedListView mListView; |
| 47 | private View mFullSettingBtn; |
| 48 | private View mUserSwitcherBtn; |
| 49 | |
| 50 | /** |
| 51 | * Returns an instance of this class. |
| 52 | */ |
| 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 |
| 64 | public void onActivityCreated(Bundle savedInstanceState) { |
| 65 | super.onActivityCreated(savedInstanceState); |
| 66 | getActivity().findViewById(R.id.action_bar_icon_container).setOnClickListener( |
| 67 | v -> getActivity().finish()); |
| 68 | |
| 69 | mUserManagerHelper = new UserManagerHelper(getContext()); |
| 70 | mListView = (PagedListView) getActivity().findViewById(R.id.list); |
| 71 | mGridAdapter = new QuickSettingGridAdapter(getContext()); |
| 72 | mListView.getRecyclerView().setLayoutManager(mGridAdapter.getGridLayoutManager()); |
| 73 | |
| 74 | mFullSettingBtn = getActivity().findViewById(R.id.full_setting_btn); |
| 75 | mFullSettingBtn.setOnClickListener(v -> { |
| 76 | mFragmentController.launchFragment(HomepageFragment.getInstance()); |
| 77 | }); |
| 78 | mUserSwitcherBtn = getActivity().findViewById(R.id.user_switcher_btn); |
| 79 | mUserSwitcherBtn.setOnClickListener(v -> { |
| 80 | mFragmentController.launchFragment(UsersListFragment.newInstance()); |
| 81 | }); |
| 82 | |
| 83 | setupAccountButton(); |
| 84 | View exitBtn = getActivity().findViewById(R.id.exit_button); |
| 85 | exitBtn.setOnClickListener(v -> mFragmentController.goBack()); |
| 86 | |
| 87 | mGridAdapter |
| 88 | .addTile(new WifiTile(getContext(), mGridAdapter, mFragmentController)) |
| 89 | .addTile(new BluetoothTile(getContext(), mGridAdapter)) |
| 90 | .addTile(new DayNightTile(getContext(), mGridAdapter)) |
| 91 | .addSeekbarTile(new BrightnessTile(getContext())); |
| 92 | mListView.setAdapter(mGridAdapter); |
| 93 | } |
| 94 | |
| 95 | @Override |
| 96 | public void onStop() { |
| 97 | super.onStop(); |
| 98 | mGridAdapter.stop(); |
| 99 | } |
| 100 | |
| 101 | private void setupAccountButton() { |
| 102 | ImageView userIcon = (ImageView) getActivity().findViewById(R.id.user_icon); |
| 103 | UserInfo currentUserInfo = mUserManagerHelper.getForegroundUserInfo(); |
| 104 | userIcon.setImageDrawable( |
| 105 | UserIconProvider.getUserIcon( |
| 106 | currentUserInfo, mUserManagerHelper, getContext())); |
| 107 | |
| 108 | TextView userSwitcherText = (TextView) getActivity().findViewById(R.id.user_switcher_text); |
| 109 | userSwitcherText.setText(currentUserInfo.name); |
| 110 | } |
| 111 | |
| 112 | /** |
| 113 | * Quick setting fragment is distraction optimized, so is allowed at all times. |
| 114 | */ |
| 115 | @Override |
| 116 | public boolean canBeShown(CarUxRestrictions carUxRestrictions) { |
| 117 | return true; |
| 118 | } |
| 119 | |
| 120 | @Override |
| 121 | public void onUxRestrictionChanged(CarUxRestrictions carUxRestrictions) { |
| 122 | // TODO: update tiles |
| 123 | applyRestriction(CarUxRestrictionsHelper.isNoSetup(carUxRestrictions)); |
| 124 | } |
| 125 | |
| 126 | private void applyRestriction(boolean restricted) { |
| 127 | mFullSettingBtn.setAlpha(restricted ? RESTRICTED_ALPHA : UNRESTRICTED_ALPHA); |
| 128 | } |
| 129 | } |