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