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.wifi.calling;
18 
19 import android.app.Activity;
20 import android.os.Bundle;
21 import android.telephony.SubscriptionManager;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.Button;
26 
27 import androidx.recyclerview.widget.DividerItemDecoration;
28 import androidx.recyclerview.widget.LinearLayoutManager;
29 import androidx.recyclerview.widget.RecyclerView;
30 import androidx.recyclerview.widget.RecyclerView.OnScrollListener;
31 
32 import com.android.internal.annotations.VisibleForTesting;
33 import com.android.internal.logging.nano.MetricsProto.MetricsEvent;
34 import com.android.settings.R;
35 import com.android.settings.core.InstrumentedFragment;
36 
37 import java.util.ArrayList;
38 import java.util.List;
39 
40 /**
41  * Fragment for displaying disclaimers for WFC.
42  */
43 public class WifiCallingDisclaimerFragment extends InstrumentedFragment
44         implements View.OnClickListener {
45     private static final String TAG = "WifiCallingDisclaimerFragment";
46 
47     private static final String STATE_IS_SCROLL_TO_BOTTOM = "state_is_scroll_to_bottom";
48 
49     private List<DisclaimerItem> mDisclaimerItemList = new ArrayList<DisclaimerItem>();
50     private Button mAgreeButton;
51     private Button mDisagreeButton;
52     private boolean mScrollToBottom;
53 
54     @Override
getMetricsCategory()55     public int getMetricsCategory() {
56         return MetricsEvent.WIFI_CALLING;
57     }
58 
59     @Override
onCreate(Bundle savedInstanceState)60     public void onCreate(Bundle savedInstanceState) {
61         super.onCreate(savedInstanceState);
62 
63         final Bundle args = getArguments();
64         final int subId = (args != null) ? args.getInt(WifiCallingSettingsForSub.EXTRA_SUB_ID)
65                 : SubscriptionManager.DEFAULT_SUBSCRIPTION_ID;
66 
67         mDisclaimerItemList = DisclaimerItemFactory.create(getActivity(), subId);
68         if (mDisclaimerItemList.isEmpty()) {
69             finish(Activity.RESULT_OK);
70             return;
71         }
72 
73         if (savedInstanceState != null) {
74             mScrollToBottom = savedInstanceState.getBoolean(
75                     STATE_IS_SCROLL_TO_BOTTOM, mScrollToBottom);
76         }
77     }
78 
79     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)80     public View onCreateView(LayoutInflater inflater, ViewGroup container,
81             Bundle savedInstanceState) {
82 
83         final View view = inflater.inflate(R.layout.wfc_disclaimer_fragment, container, false);
84 
85         mAgreeButton = view.findViewById(R.id.agree_button);
86         mAgreeButton.setOnClickListener(this);
87         mDisagreeButton = view.findViewById(R.id.disagree_button);
88         mDisagreeButton.setOnClickListener(this);
89 
90         final RecyclerView recyclerView = (RecyclerView) view.findViewById(
91                 R.id.disclaimer_item_list);
92         recyclerView.setLayoutManager(new LinearLayoutManager(getActivity()));
93         recyclerView.setAdapter(new DisclaimerItemListAdapter(mDisclaimerItemList));
94 
95         RecyclerView.ItemDecoration itemDecoration =
96                 new DividerItemDecoration(getActivity(), DividerItemDecoration.VERTICAL);
97         recyclerView.addItemDecoration(itemDecoration);
98 
99         recyclerView.addOnScrollListener(new RecyclerView.OnScrollListener() {
100             @Override
101             public void onScrolled(RecyclerView recyclerView, int dx, int dy) {
102                 super.onScrolled(recyclerView, dx, dy);
103                 if (!recyclerView.canScrollVertically(1 /* scrolling down */)) {
104                     mScrollToBottom = true;
105                     updateButtonState();
106                     recyclerView.removeOnScrollListener(this);
107                 }
108             }
109         });
110 
111         return view;
112     }
113 
114     @Override
onResume()115     public void onResume() {
116         super.onResume();
117         updateButtonState();
118     }
119 
120     @Override
onSaveInstanceState(Bundle outState)121     public void onSaveInstanceState(Bundle outState) {
122         super.onSaveInstanceState(outState);
123         outState.putBoolean(STATE_IS_SCROLL_TO_BOTTOM, mScrollToBottom);
124     }
125 
updateButtonState()126     private void updateButtonState() {
127         mAgreeButton.setEnabled(mScrollToBottom);
128     }
129 
130     @Override
onClick(View v)131     public void onClick(View v) {
132         if (v == mAgreeButton) {
133             for (DisclaimerItem item : mDisclaimerItemList) {
134                 item.onAgreed();
135             }
136             finish(Activity.RESULT_OK);
137         } else if (v == mDisagreeButton) {
138             finish(Activity.RESULT_CANCELED);
139         }
140     }
141 
142     @VisibleForTesting
finish(int result)143     void finish(int result) {
144         Activity activity = getActivity();
145         activity.setResult(result, null);
146         activity.finish();
147     }
148 }