1 /*
2  * Copyright (C) 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.developeroptions.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.ims.ImsManager;
36 import com.android.internal.util.CollectionUtils;
37 import com.android.car.developeroptions.R;
38 import com.android.car.developeroptions.core.InstrumentedFragment;
39 import com.android.car.developeroptions.network.SubscriptionUtil;
40 import com.android.car.developeroptions.search.actionbar.SearchMenuController;
41 import com.android.car.developeroptions.support.actionbar.HelpMenuController;
42 import com.android.car.developeroptions.support.actionbar.HelpResourceProvider;
43 import com.android.car.developeroptions.widget.RtlCompatibleViewPager;
44 import com.android.car.developeroptions.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         Intent intent = getActivity().getIntent();
104         if (intent == null) {
105             return;
106         }
107         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             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 
196     @VisibleForTesting
isWfcEnabledByPlatform(SubscriptionInfo info)197     boolean isWfcEnabledByPlatform(SubscriptionInfo info) {
198         ImsManager imsManager = ImsManager.getInstance(getActivity(), info.getSimSlotIndex());
199         return imsManager.isWfcEnabledByPlatform();
200     }
201 
updateSubList()202     private void updateSubList() {
203         mSil = SubscriptionUtil.getActiveSubscriptions(
204                 getContext().getSystemService(SubscriptionManager.class));
205 
206         // Only config Wfc if it's enabled by platform.
207         if (mSil == null) {
208             return;
209         }
210         for (int i = 0; i < mSil.size(); ) {
211             if (!isWfcEnabledByPlatform(mSil.get(i))) {
212                 mSil.remove(i);
213             } else {
214                 i++;
215             }
216         }
217     }
218 
updateTitleForCurrentSub()219     private void updateTitleForCurrentSub() {
220         if (CollectionUtils.size(mSil) > 1) {
221             final int subId = mSil.get(mViewPager.getCurrentItem()).getSubscriptionId();
222             final String title = SubscriptionManager.getResourcesForSubId(getContext(), subId)
223                     .getString(R.string.wifi_calling_settings_title);
224             getActivity().getActionBar().setTitle(title);
225         }
226     }
227 }
228