1 /*
2  * Copyright (C) 2015 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.settings.SettingsEnums;
20 import android.content.Intent;
21 import android.os.Bundle;
22 import android.provider.Settings;
23 import android.telephony.SubscriptionInfo;
24 import android.telephony.SubscriptionManager;
25 import android.util.Log;
26 import android.view.LayoutInflater;
27 import android.view.View;
28 import android.view.ViewGroup;
29 
30 import androidx.annotation.VisibleForTesting;
31 import androidx.fragment.app.Fragment;
32 import androidx.fragment.app.FragmentManager;
33 import androidx.fragment.app.FragmentPagerAdapter;
34 
35 import com.android.internal.util.CollectionUtils;
36 import com.android.settings.R;
37 import com.android.settings.core.InstrumentedFragment;
38 import com.android.settings.network.SubscriptionUtil;
39 import com.android.settings.network.ims.WifiCallingQueryImsState;
40 import com.android.settings.search.actionbar.SearchMenuController;
41 import com.android.settings.support.actionbar.HelpMenuController;
42 import com.android.settings.support.actionbar.HelpResourceProvider;
43 import com.android.settings.widget.RtlCompatibleViewPager;
44 import com.android.settings.widget.SlidingTabLayout;
45 
46 import java.util.List;
47 
48 /**
49  * "Wi-Fi Calling settings" screen. This is the container fragment which holds
50  * {@link WifiCallingSettingsForSub} fragments.
51  */
52 public class WifiCallingSettings extends InstrumentedFragment implements HelpResourceProvider {
53     private static final String TAG = "WifiCallingSettings";
54     private List<SubscriptionInfo> mSil;
55 
56     //UI objects
57     private RtlCompatibleViewPager mViewPager;
58     private WifiCallingViewPagerAdapter mPagerAdapter;
59     private SlidingTabLayout mTabLayout;
60 
61     private final class InternalViewPagerListener implements
62             RtlCompatibleViewPager.OnPageChangeListener {
63         @Override
onPageScrolled(int position, float positionOffset, int positionOffsetPixels)64         public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
65             // Do nothing.
66         }
67 
68         @Override
onPageSelected(int position)69         public void onPageSelected(int position) {
70             updateTitleForCurrentSub();
71         }
72 
73         @Override
onPageScrollStateChanged(int state)74         public void onPageScrollStateChanged(int state) {
75             // Do nothing.
76         }
77     }
78 
79     @Override
getMetricsCategory()80     public int getMetricsCategory() {
81         return SettingsEnums.WIFI_CALLING;
82     }
83 
84     @Override
onCreateView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState)85     public View onCreateView(LayoutInflater inflater, ViewGroup container,
86             Bundle savedInstanceState) {
87         final View view = inflater.inflate(R.layout.wifi_calling_settings_tabs, container, false);
88 
89         mTabLayout = view.findViewById(R.id.sliding_tabs);
90         mViewPager = (RtlCompatibleViewPager) view.findViewById(R.id.view_pager);
91 
92         mPagerAdapter = new WifiCallingViewPagerAdapter(getChildFragmentManager(), mViewPager);
93         mViewPager.setAdapter(mPagerAdapter);
94         mViewPager.addOnPageChangeListener(new InternalViewPagerListener());
95         maybeSetViewForSubId();
96         return view;
97     }
98 
maybeSetViewForSubId()99     private void maybeSetViewForSubId() {
100         if (mSil == null) {
101             return;
102         }
103         final Intent intent = getActivity().getIntent();
104         if (intent == null) {
105             return;
106         }
107         final int subId = intent.getIntExtra(Settings.EXTRA_SUB_ID,
108                 SubscriptionManager.INVALID_SUBSCRIPTION_ID);
109         if (SubscriptionManager.isValidSubscriptionId(subId)) {
110             for (SubscriptionInfo subInfo : mSil) {
111                 if (subId == subInfo.getSubscriptionId()) {
112                     mViewPager.setCurrentItem(mSil.indexOf(subInfo));
113                     break;
114                 }
115             }
116         }
117     }
118 
119     @Override
onCreate(Bundle icicle)120     public void onCreate(Bundle icicle) {
121         super.onCreate(icicle);
122         setHasOptionsMenu(true);
123         SearchMenuController.init(this /* host */);
124         HelpMenuController.init(this /* host */);
125 
126         // TODO: besides in onCreate, we should also update subList when SIM / Sub status
127         // changes.
128         updateSubList();
129     }
130 
131     @Override
onStart()132     public void onStart() {
133         super.onStart();
134 
135         if (mSil != null && mSil.size() > 1) {
136             mTabLayout.setViewPager(mViewPager);
137         } else {
138             mTabLayout.setVisibility(View.GONE);
139         }
140 
141         updateTitleForCurrentSub();
142     }
143 
144     @Override
getHelpResource()145     public int getHelpResource() {
146         return R.string.help_uri_wifi_calling;
147     }
148 
149     @VisibleForTesting
150     final class WifiCallingViewPagerAdapter extends FragmentPagerAdapter {
151         private final RtlCompatibleViewPager mViewPager;
152 
WifiCallingViewPagerAdapter(FragmentManager fragmentManager, RtlCompatibleViewPager viewPager)153         public WifiCallingViewPagerAdapter(FragmentManager fragmentManager,
154                 RtlCompatibleViewPager viewPager) {
155             super(fragmentManager);
156             mViewPager = viewPager;
157         }
158 
159         @Override
getPageTitle(int position)160         public CharSequence getPageTitle(int position) {
161             return String.valueOf(mSil.get(position).getDisplayName());
162         }
163 
164         @Override
getItem(int position)165         public Fragment getItem(int position) {
166             Log.d(TAG, "Adapter getItem " + position);
167             final Bundle args = new Bundle();
168             args.putBoolean(SearchMenuController.NEED_SEARCH_ICON_IN_ACTION_BAR, false);
169             args.putInt(WifiCallingSettingsForSub.FRAGMENT_BUNDLE_SUBID,
170                     mSil.get(position).getSubscriptionId());
171             final WifiCallingSettingsForSub fragment = new WifiCallingSettingsForSub();
172             fragment.setArguments(args);
173 
174             return fragment;
175         }
176 
177         @Override
instantiateItem(ViewGroup container, int position)178         public Object instantiateItem(ViewGroup container, int position) {
179             Log.d(TAG, "Adapter instantiateItem " + position);
180             return super.instantiateItem(container,
181                     mViewPager.getRtlAwareIndex(position));
182         }
183 
184         @Override
getCount()185         public int getCount() {
186             if (mSil == null) {
187                 Log.d(TAG, "Adapter getCount null mSil ");
188                 return 0;
189             } else {
190                 Log.d(TAG, "Adapter getCount " + mSil.size());
191                 return mSil.size();
192             }
193         }
194     }
195 
updateSubList()196     private void updateSubList() {
197         mSil = SubscriptionUtil.getActiveSubscriptions(
198                 getContext().getSystemService(SubscriptionManager.class));
199 
200         // Only config Wfc if it's enabled by platform.
201         if (mSil == null) {
202             return;
203         }
204         for (int i = 0; i < mSil.size(); ) {
205             final SubscriptionInfo info = mSil.get(i);
206             if (!queryImsState(info.getSubscriptionId()).isWifiCallingProvisioned()) {
207                 mSil.remove(i);
208             } else {
209                 i++;
210             }
211         }
212     }
213 
updateTitleForCurrentSub()214     private void updateTitleForCurrentSub() {
215         if (CollectionUtils.size(mSil) > 1) {
216             final int subId = mSil.get(mViewPager.getCurrentItem()).getSubscriptionId();
217             final String title = SubscriptionManager.getResourcesForSubId(getContext(), subId)
218                     .getString(R.string.wifi_calling_settings_title);
219             getActivity().getActionBar().setTitle(title);
220         }
221     }
222 
223     @VisibleForTesting
queryImsState(int subId)224     WifiCallingQueryImsState queryImsState(int subId) {
225         return new WifiCallingQueryImsState(getContext(), subId);
226     }
227 }
228