1 /*
2  * Copyright (C) 2016 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License"); you may not
5  * use this file except in compliance with the License. You may obtain a copy of
6  * 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, WITHOUT
12  * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
13  * License for the specific language governing permissions and limitations under
14  * the License.
15  */
16 
17 package com.googlecode.android_scripting.activity;
18 
19 import android.app.Activity;
20 import android.content.Context;
21 import android.content.Intent;
22 import android.os.Bundle;
23 import android.text.Editable;
24 import android.text.TextWatcher;
25 import android.view.LayoutInflater;
26 import android.view.View;
27 import android.view.ViewGroup;
28 import android.view.View.OnClickListener;
29 import android.widget.BaseAdapter;
30 import android.widget.Button;
31 import android.widget.EditText;
32 import android.widget.ListView;
33 import android.widget.TextView;
34 
35 import com.googlecode.android_scripting.Constants;
36 import com.googlecode.android_scripting.R;
37 import com.googlecode.android_scripting.facade.FacadeConfiguration;
38 import com.googlecode.android_scripting.rpc.MethodDescriptor;
39 
40 /**
41  * Prompts for API parameters.
42  *
43  * <p>
44  * This activity is started by {@link ApiBrowser} to prompt user for RPC call parameters.
45  * Input/output interface is RPC name and explicit parameter values.
46  *
47  * @author igor.v.karp@gmail.com (Igor Karp)
48  */
49 public class ApiPrompt extends Activity {
50   private MethodDescriptor mRpc;
51   private String[] mHints;
52   private String[] mValues;
53   private ApiPromptAdapter mAdapter;
54 
55   @Override
onCreate(Bundle savedInstanceState)56   public void onCreate(Bundle savedInstanceState) {
57     super.onCreate(savedInstanceState);
58     setContentView(R.layout.api_prompt);
59     mRpc =
60         FacadeConfiguration.getMethodDescriptor(getIntent().getStringExtra(
61             Constants.EXTRA_API_PROMPT_RPC_NAME));
62     mHints = mRpc.getParameterHints();
63     mValues = getIntent().getStringArrayExtra(Constants.EXTRA_API_PROMPT_VALUES);
64     mAdapter = new ApiPromptAdapter();
65     ((ListView) findViewById(R.id.list)).setAdapter(mAdapter);
66     ((Button) findViewById(R.id.done)).setOnClickListener(new OnClickListener() {
67       @Override
68       public void onClick(View v) {
69         Intent intent = new Intent();
70         intent.putExtra(Constants.EXTRA_API_PROMPT_RPC_NAME, mRpc.getName());
71         intent.putExtra(Constants.EXTRA_API_PROMPT_VALUES, mValues);
72         setResult(RESULT_OK, intent);
73         finish();
74       }
75     });
76     setResult(RESULT_CANCELED);
77   }
78 
79   private class ApiPromptAdapter extends BaseAdapter {
80     @Override
getCount()81     public int getCount() {
82       return mHints.length;
83     }
84 
85     @Override
getItem(int position)86     public Object getItem(int position) {
87       return mValues[position];
88     }
89 
90     @Override
getItemId(int position)91     public long getItemId(int position) {
92       return position;
93     }
94 
95     @Override
getView(int position, View convertView, ViewGroup parent)96     public View getView(int position, View convertView, ViewGroup parent) {
97       View item;
98       if (convertView == null) {
99         LayoutInflater inflater =
100             (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
101         item =
102             inflater.inflate(R.layout.api_prompt_item, parent, false /* do not attach to root */);
103       } else {
104         item = convertView;
105       }
106       TextView description = (TextView) item.findViewById(R.id.api_prompt_item_description);
107       EditText value = (EditText) item.findViewById(R.id.api_prompt_item_value);
108       description.setText(mHints[position]);
109       value.setText(mValues[position]);
110       value.addTextChangedListener(new ValueWatcher(position));
111       return item;
112     }
113   }
114 
115   private class ValueWatcher implements TextWatcher {
116     private final int mPosition;
117 
ValueWatcher(int position)118     public ValueWatcher(int position) {
119       mPosition = position;
120     }
121 
122     @Override
afterTextChanged(Editable e)123     public void afterTextChanged(Editable e) {
124     }
125 
126     @Override
beforeTextChanged(CharSequence s, int start, int before, int count)127     public void beforeTextChanged(CharSequence s, int start, int before, int count) {
128     }
129 
130     @Override
onTextChanged(CharSequence s, int start, int before, int count)131     public void onTextChanged(CharSequence s, int start, int before, int count) {
132       mValues[mPosition] = s.toString();
133     }
134   }
135 }
136