davidln | 1445fc1 | 2019-01-07 14:08:22 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2019 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.car.settings.system; |
| 18 | |
| 19 | import static android.app.Activity.RESULT_OK; |
| 20 | |
| 21 | import android.content.Intent; |
| 22 | import android.os.Bundle; |
| 23 | import android.view.View; |
| 24 | import android.view.ViewTreeObserver; |
| 25 | import android.widget.Button; |
| 26 | |
| 27 | import androidx.annotation.LayoutRes; |
| 28 | import androidx.annotation.Nullable; |
| 29 | import androidx.annotation.VisibleForTesting; |
| 30 | import androidx.annotation.XmlRes; |
| 31 | import androidx.recyclerview.widget.RecyclerView; |
| 32 | |
| 33 | import com.android.car.settings.R; |
| 34 | import com.android.car.settings.common.ActivityResultCallback; |
| 35 | import com.android.car.settings.common.SettingsFragment; |
| 36 | import com.android.car.settings.security.CheckLockActivity; |
| 37 | |
| 38 | /** |
| 39 | * Presents the user with the option to reset the head unit to its default "factory" state. If a |
| 40 | * user confirms, the user is first required to authenticate and then presented with a secondary |
| 41 | * confirmation: {@link MasterClearConfirmFragment}. The user must scroll to the bottom of the page |
| 42 | * before proceeding. |
| 43 | */ |
| 44 | public class MasterClearFragment extends SettingsFragment implements ActivityResultCallback { |
| 45 | |
| 46 | // Arbitrary request code for starting CheckLockActivity when the reset button is clicked. |
| 47 | @VisibleForTesting |
| 48 | static final int CHECK_LOCK_REQUEST_CODE = 88; |
| 49 | |
| 50 | @Override |
| 51 | @XmlRes |
| 52 | protected int getPreferenceScreenResId() { |
| 53 | return R.xml.master_clear_fragment; |
| 54 | } |
| 55 | |
| 56 | @Override |
| 57 | @LayoutRes |
| 58 | protected int getActionBarLayoutId() { |
| 59 | return R.layout.action_bar_with_button; |
| 60 | } |
| 61 | |
| 62 | @Override |
| 63 | public void onActivityCreated(Bundle savedInstanceState) { |
| 64 | super.onActivityCreated(savedInstanceState); |
| 65 | Button masterClearButton = requireActivity().findViewById(R.id.action_button1); |
| 66 | masterClearButton.setText(requireContext().getString(R.string.master_clear_button_text)); |
| 67 | masterClearButton.setOnClickListener( |
| 68 | v -> startActivityForResult(new Intent(getContext(), CheckLockActivity.class), |
| 69 | CHECK_LOCK_REQUEST_CODE, /* callback= */ this)); |
| 70 | masterClearButton.setEnabled(false); |
| 71 | |
| 72 | masterClearButton.getViewTreeObserver().addOnGlobalLayoutListener( |
| 73 | new ViewTreeObserver.OnGlobalLayoutListener() { |
| 74 | @Override |
| 75 | public void onGlobalLayout() { |
| 76 | masterClearButton.setEnabled(isAtEnd()); |
| 77 | masterClearButton.getViewTreeObserver().removeOnGlobalLayoutListener(this); |
| 78 | } |
| 79 | }); |
| 80 | getListView().setOnScrollChangeListener((v, scrollX, scrollY, oldScrollX, oldScrollY) -> { |
| 81 | if (isAtEnd()) { |
| 82 | masterClearButton.setEnabled(true); |
| 83 | } |
| 84 | }); |
| 85 | } |
| 86 | |
| 87 | @Override |
| 88 | public void processActivityResult(int requestCode, int resultCode, @Nullable Intent data) { |
| 89 | if (requestCode == CHECK_LOCK_REQUEST_CODE && resultCode == RESULT_OK) { |
| 90 | launchFragment(new MasterClearConfirmFragment()); |
| 91 | } |
| 92 | } |
| 93 | |
| 94 | /** Returns {@code true} if the RecyclerView is completely displaying the last item. */ |
| 95 | private boolean isAtEnd() { |
| 96 | RecyclerView.LayoutManager layoutManager = getListView().getLayoutManager(); |
| 97 | if (layoutManager == null || layoutManager.getChildCount() == 0) { |
| 98 | return true; |
| 99 | } |
| 100 | |
| 101 | int childCount = layoutManager.getChildCount(); |
| 102 | View lastVisibleChild = layoutManager.getChildAt(childCount - 1); |
| 103 | |
| 104 | // The list has reached the bottom if the last child that is visible is the last item |
| 105 | // in the list and it's fully shown. |
| 106 | return layoutManager.getPosition(lastVisibleChild) == (layoutManager.getItemCount() - 1) |
| 107 | && layoutManager.getDecoratedBottom(lastVisibleChild) <= layoutManager.getHeight(); |
| 108 | } |
| 109 | } |