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.AlertDialog;
19 import android.content.Context;
20 import android.content.DialogInterface;
21 import android.content.DialogInterface.OnShowListener;
22 import android.text.InputType;
23 import android.util.AttributeSet;
24 import android.view.View;
25 import android.view.View.OnClickListener;
26 import android.view.inputmethod.InputMethodManager;
27 import android.widget.CompoundButton;
28 import android.widget.CompoundButton.OnCheckedChangeListener;
29 import android.widget.EditText;
30 import android.widget.LinearLayout;
31 import android.widget.Switch;
32 import android.widget.TextView;
33 
34 import com.android.messaging.R;
35 import com.android.messaging.sms.MmsConfig;
36 import com.android.messaging.util.LogUtil;
37 
38 public class DebugMmsConfigItemView extends LinearLayout implements OnClickListener,
39         OnCheckedChangeListener, DialogInterface.OnClickListener {
40 
41     public interface MmsConfigItemListener {
onValueChanged(String key, String keyType, String value)42         void onValueChanged(String key, String keyType, String value);
43     }
44 
45     private TextView mTitle;
46     private TextView mTextValue;
47     private Switch mSwitch;
48     private String mKey;
49     private String mKeyType;
50     private MmsConfigItemListener mListener;
51     private EditText mEditText;
52 
DebugMmsConfigItemView(Context context, AttributeSet attributeSet)53     public DebugMmsConfigItemView(Context context, AttributeSet attributeSet) {
54         super(context, attributeSet);
55     }
56 
57     @Override
onFinishInflate()58     protected void onFinishInflate () {
59         mTitle = (TextView) findViewById(R.id.title);
60         mTextValue = (TextView) findViewById(R.id.text_value);
61         mSwitch = (Switch) findViewById(R.id.switch_button);
62         setOnClickListener(this);
63         mSwitch.setOnCheckedChangeListener(this);
64     }
65 
bind(final String key, final String keyType, final String value, final MmsConfigItemListener listener)66     public void bind(final String key, final String keyType, final String value,
67             final MmsConfigItemListener listener) {
68         mListener = listener;
69         mKey = key;
70         mKeyType = keyType;
71         mTitle.setText(key);
72 
73         switch (keyType) {
74             case MmsConfig.KEY_TYPE_BOOL:
75                 mSwitch.setVisibility(View.VISIBLE);
76                 mTextValue.setVisibility(View.GONE);
77                 mSwitch.setChecked(Boolean.valueOf(value));
78                 break;
79             case MmsConfig.KEY_TYPE_STRING:
80             case MmsConfig.KEY_TYPE_INT:
81                 mTextValue.setVisibility(View.VISIBLE);
82                 mSwitch.setVisibility(View.GONE);
83                 mTextValue.setText(value);
84                 break;
85             default:
86                 mTextValue.setVisibility(View.GONE);
87                 mSwitch.setVisibility(View.GONE);
88                 LogUtil.e(LogUtil.BUGLE_TAG, "Unexpected keytype: " + keyType);
89                 break;
90         }
91     }
92 
93     @Override
onCheckedChanged(CompoundButton buttonView, boolean isChecked)94     public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
95         mListener.onValueChanged(mKey, mKeyType, String.valueOf(isChecked));
96     }
97 
98     @Override
onClick(View v)99     public void onClick(View v) {
100         if (MmsConfig.KEY_TYPE_BOOL.equals(mKeyType)) {
101             return;
102         }
103         final Context context = getContext();
104         mEditText = new EditText(context);
105         mEditText.setText(mTextValue.getText());
106         mEditText.setFocusable(true);
107         if (MmsConfig.KEY_TYPE_INT.equals(mKeyType)) {
108             mEditText.setInputType(InputType.TYPE_CLASS_PHONE);
109         } else {
110             mEditText.setInputType(InputType.TYPE_TEXT_FLAG_NO_SUGGESTIONS);
111         }
112         final AlertDialog dialog = new AlertDialog.Builder(context)
113                 .setTitle(mKey)
114                 .setView(mEditText)
115                 .setPositiveButton(android.R.string.ok, this)
116                 .setNegativeButton(android.R.string.cancel, null)
117                 .create();
118         dialog.setOnShowListener(new OnShowListener() {
119             @Override
120             public void onShow(DialogInterface dialog) {
121                 mEditText.requestFocus();
122                 mEditText.selectAll();
123                 ((InputMethodManager) context.getSystemService(Context.INPUT_METHOD_SERVICE))
124                         .toggleSoftInput(InputMethodManager.SHOW_IMPLICIT, 0);
125             }
126         });
127         dialog.show();
128     }
129 
130     @Override
onClick(DialogInterface dialog, int which)131     public void onClick(DialogInterface dialog, int which) {
132         mListener.onValueChanged(mKey, mKeyType, mEditText.getText().toString());
133     }
134 }
135