1 /*
2  * Copyright (C) 2017 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.dialer.app.settings;
18 
19 import android.app.Fragment;
20 import android.content.Context;
21 import android.graphics.drawable.Icon;
22 import android.os.Bundle;
23 import android.preference.Preference;
24 import android.preference.PreferenceActivity;
25 import android.preference.PreferenceFragment;
26 import android.preference.PreferenceScreen;
27 import android.support.annotation.VisibleForTesting;
28 import android.telecom.PhoneAccount;
29 import android.telecom.PhoneAccountHandle;
30 import android.telecom.TelecomManager;
31 import java.util.List;
32 
33 /**
34  * Preference screen that lists SIM phone accounts to select from, and forwards the selected account
35  * to {@link #PARAM_TARGET_FRAGMENT}. Can only be used in a {@link PreferenceActivity}
36  */
37 public class PhoneAccountSelectionFragment extends PreferenceFragment {
38 
39   /** The {@link PreferenceFragment} to launch after the account is selected. */
40   public static final String PARAM_TARGET_FRAGMENT = "target_fragment";
41 
42   /**
43    * The arguments bundle to pass to the {@link #PARAM_TARGET_FRAGMENT}
44    *
45    * @see Fragment#getArguments()
46    */
47   public static final String PARAM_ARGUMENTS = "arguments";
48 
49   /**
50    * The key to insert the selected {@link PhoneAccountHandle} to bundle in {@link #PARAM_ARGUMENTS}
51    */
52   public static final String PARAM_PHONE_ACCOUNT_HANDLE_KEY = "phone_account_handle_key";
53 
54   /**
55    * The title of the {@link #PARAM_TARGET_FRAGMENT} once it is launched with {@link
56    * PreferenceActivity#startWithFragment(String, Bundle, Fragment, int)}, as a string resource ID.
57    */
58   public static final String PARAM_TARGET_TITLE_RES = "target_title_res";
59 
60   private String targetFragment;
61   private Bundle arguments;
62   private String phoneAccountHandleKey;
63   private int titleRes;
64 
65   @Override
onCreate(Bundle savedInstanceState)66   public void onCreate(Bundle savedInstanceState) {
67     super.onCreate(savedInstanceState);
68     targetFragment = getArguments().getString(PARAM_TARGET_FRAGMENT);
69     arguments = new Bundle();
70     arguments.putAll(getArguments().getBundle(PARAM_ARGUMENTS));
71     phoneAccountHandleKey = getArguments().getString(PARAM_PHONE_ACCOUNT_HANDLE_KEY);
72     titleRes = getArguments().getInt(PARAM_TARGET_TITLE_RES, 0);
73   }
74 
75   final class AccountPreference extends Preference {
76     private final PhoneAccountHandle phoneAccountHandle;
77 
AccountPreference( Context context, PhoneAccountHandle phoneAccountHandle, PhoneAccount phoneAccount)78     public AccountPreference(
79         Context context, PhoneAccountHandle phoneAccountHandle, PhoneAccount phoneAccount) {
80       super(context);
81       this.phoneAccountHandle = phoneAccountHandle;
82       setTitle(phoneAccount.getLabel());
83       setSummary(phoneAccount.getShortDescription());
84       Icon icon = phoneAccount.getIcon();
85       if (icon != null) {
86         setIcon(icon.loadDrawable(context));
87       }
88     }
89 
90     @VisibleForTesting
click()91     void click() {
92       onClick();
93     }
94 
95     @Override
onClick()96     protected void onClick() {
97       super.onClick();
98       PreferenceActivity preferenceActivity = (PreferenceActivity) getActivity();
99       arguments.putParcelable(phoneAccountHandleKey, phoneAccountHandle);
100       preferenceActivity.startWithFragment(targetFragment, arguments, null, 0, titleRes, 0);
101     }
102   }
103 
104   @Override
onResume()105   public void onResume() {
106     super.onResume();
107     setPreferenceScreen(getPreferenceManager().createPreferenceScreen(getContext()));
108     PreferenceScreen screen = getPreferenceScreen();
109 
110     TelecomManager telecomManager = getContext().getSystemService(TelecomManager.class);
111 
112     List<PhoneAccountHandle> accountHandles = telecomManager.getCallCapablePhoneAccounts();
113 
114     Context context = getActivity();
115     for (PhoneAccountHandle handle : accountHandles) {
116       PhoneAccount account = telecomManager.getPhoneAccount(handle);
117       if (account != null) {
118         final boolean isSimAccount =
119             0 != (account.getCapabilities() & PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
120         if (isSimAccount) {
121           screen.addPreference(new AccountPreference(context, handle, account));
122         }
123       }
124     }
125   }
126 }
127