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 package com.android.messaging.ui.debug;
17 
18 import android.app.Fragment;
19 import android.content.Context;
20 import android.os.Bundle;
21 import android.telephony.SubscriptionInfo;
22 import android.view.LayoutInflater;
23 import android.view.View;
24 import android.view.ViewGroup;
25 import android.widget.AdapterView;
26 import android.widget.AdapterView.OnItemSelectedListener;
27 import android.widget.ArrayAdapter;
28 import android.widget.BaseAdapter;
29 import android.widget.ListView;
30 import android.widget.Spinner;
31 import android.widget.TextView;
32 
33 import com.android.messaging.R;
34 import com.android.messaging.datamodel.data.ParticipantData;
35 import com.android.messaging.sms.MmsConfig;
36 import com.android.messaging.ui.debug.DebugMmsConfigItemView.MmsConfigItemListener;
37 import com.android.messaging.util.OsUtil;
38 import com.android.messaging.util.PhoneUtils;
39 
40 import java.util.ArrayList;
41 import java.util.Collections;
42 import java.util.Iterator;
43 import java.util.List;
44 
45 /**
46  * Show list of all MmsConfig key/value pairs and allow editing.
47  */
48 public class DebugMmsConfigFragment extends Fragment {
49     @Override
onCreateView(final LayoutInflater inflater, final ViewGroup container, final Bundle savedInstanceState)50     public View onCreateView(final LayoutInflater inflater, final ViewGroup container,
51             final Bundle savedInstanceState) {
52         final View fragmentView = inflater.inflate(R.layout.mms_config_debug_fragment, container,
53                 false);
54         final ListView listView = (ListView) fragmentView.findViewById(android.R.id.list);
55         final Spinner spinner = (Spinner) fragmentView.findViewById(R.id.sim_selector);
56         final Integer[] subIdArray = getActiveSubIds();
57         ArrayAdapter<Integer> spinnerAdapter = new ArrayAdapter<Integer>(getActivity(),
58                 android.R.layout.simple_spinner_item, subIdArray);
59         spinnerAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item);
60         spinner.setAdapter(spinnerAdapter);
61         spinner.setOnItemSelectedListener(new OnItemSelectedListener() {
62             @Override
63             public void onItemSelected(AdapterView<?> parent, View view, int position, long id) {
64                 listView.setAdapter(new MmsConfigAdapter(getActivity(), subIdArray[position]));
65 
66                 final int[] mccmnc = PhoneUtils.get(subIdArray[position]).getMccMnc();
67                 // Set the title with the mcc/mnc
68                 final TextView title = (TextView) fragmentView.findViewById(R.id.sim_title);
69                 title.setText("(" + mccmnc[0] + "/" + mccmnc[1] + ") " +
70                         getActivity().getString(R.string.debug_sub_id_spinner_text));
71             }
72 
73             @Override
74             public void onNothingSelected(AdapterView<?> parent) {
75             }
76         });
77 
78         return fragmentView;
79     }
80 
getActiveSubIds()81     public static Integer[] getActiveSubIds() {
82         if (!OsUtil.isAtLeastL_MR1()) {
83             return new Integer[] { ParticipantData.DEFAULT_SELF_SUB_ID };
84         }
85         final List<SubscriptionInfo> subRecords =
86                 PhoneUtils.getDefault().toLMr1().getActiveSubscriptionInfoList();
87         if (subRecords == null) {
88             return new Integer[0];
89         }
90         final Integer[] retArray = new Integer[subRecords.size()];
91         for (int i = 0; i < subRecords.size(); i++) {
92             retArray[i] = subRecords.get(i).getSubscriptionId();
93         }
94         return retArray;
95     }
96 
97     private class MmsConfigAdapter extends BaseAdapter implements
98             DebugMmsConfigItemView.MmsConfigItemListener {
99         private final LayoutInflater mInflater;
100         private final List<String> mKeys;
101         private final MmsConfig mMmsConfig;
102 
MmsConfigAdapter(Context context, int subId)103         public MmsConfigAdapter(Context context, int subId) {
104             mInflater = (LayoutInflater) context.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
105             mMmsConfig = MmsConfig.get(subId);
106             mKeys = new ArrayList<>(mMmsConfig.keySet());
107             Iterator<String> it = mKeys.iterator();
108             while (it.hasNext()) {
109                 // Remove a config if the MmsConfig.sKeyTypeMap doesn't have it.
110                 if (MmsConfig.getKeyType(it.next()) == null) {
111                     it.remove();
112                 }
113             }
114             Collections.sort(mKeys);
115         }
116 
117         @Override
getView(final int position, final View convertView, final ViewGroup parent)118         public View getView(final int position, final View convertView, final ViewGroup parent) {
119             final DebugMmsConfigItemView view;
120             if (convertView != null && convertView instanceof DebugMmsConfigItemView) {
121                 view = (DebugMmsConfigItemView) convertView;
122             } else {
123                 view = (DebugMmsConfigItemView) mInflater.inflate(
124                         R.layout.debug_mmsconfig_item_view, parent, false);
125             }
126             final String key = mKeys.get(position);
127             view.bind(key,
128                     MmsConfig.getKeyType(key),
129                     String.valueOf(mMmsConfig.getValue(key)),
130                     this);
131             return view;
132         }
133 
134         @Override
onValueChanged(String key, String keyType, String value)135         public void onValueChanged(String key, String keyType, String value) {
136             mMmsConfig.update(keyType, key, value);
137             notifyDataSetChanged();
138         }
139 
140         @Override
getCount()141         public int getCount() {
142             return mKeys.size();
143         }
144 
145         @Override
getItem(int position)146         public Object getItem(int position) {
147             return mKeys.get(position);
148         }
149 
150         @Override
getItemId(int position)151         public long getItemId(int position) {
152             return position;
153         }
154     }
155 }
156