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
17package com.googlecode.android_scripting.activity;
18
19import android.app.Activity;
20import android.content.Context;
21import android.content.Intent;
22import android.os.Bundle;
23import android.text.Editable;
24import android.text.TextWatcher;
25import android.view.LayoutInflater;
26import android.view.View;
27import android.view.ViewGroup;
28import android.view.View.OnClickListener;
29import android.widget.BaseAdapter;
30import android.widget.Button;
31import android.widget.EditText;
32import android.widget.ListView;
33import android.widget.TextView;
34
35import com.googlecode.android_scripting.Constants;
36import com.googlecode.android_scripting.R;
37import com.googlecode.android_scripting.facade.FacadeConfiguration;
38import 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 */
48public class ApiPrompt extends Activity {
49  private MethodDescriptor mRpc;
50  private String[] mHints;
51  private String[] mValues;
52  private ApiPromptAdapter mAdapter;
53
54  @Override
55  public void onCreate(Bundle savedInstanceState) {
56    super.onCreate(savedInstanceState);
57    setContentView(R.layout.api_prompt);
58    mRpc =
59        FacadeConfiguration.getMethodDescriptor(getIntent().getStringExtra(
60            Constants.EXTRA_API_PROMPT_RPC_NAME));
61    mHints = mRpc.getParameterHints();
62    mValues = getIntent().getStringArrayExtra(Constants.EXTRA_API_PROMPT_VALUES);
63    mAdapter = new ApiPromptAdapter();
64    ((ListView) findViewById(R.id.list)).setAdapter(mAdapter);
65    ((Button) findViewById(R.id.done)).setOnClickListener(new OnClickListener() {
66      @Override
67      public void onClick(View v) {
68        Intent intent = new Intent();
69        intent.putExtra(Constants.EXTRA_API_PROMPT_RPC_NAME, mRpc.getName());
70        intent.putExtra(Constants.EXTRA_API_PROMPT_VALUES, mValues);
71        setResult(RESULT_OK, intent);
72        finish();
73      }
74    });
75    setResult(RESULT_CANCELED);
76  }
77
78  private class ApiPromptAdapter extends BaseAdapter {
79    @Override
80    public int getCount() {
81      return mHints.length;
82    }
83
84    @Override
85    public Object getItem(int position) {
86      return mValues[position];
87    }
88
89    @Override
90    public long getItemId(int position) {
91      return position;
92    }
93
94    @Override
95    public View getView(int position, View convertView, ViewGroup parent) {
96      View item;
97      if (convertView == null) {
98        LayoutInflater inflater =
99            (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
100        item =
101            inflater.inflate(R.layout.api_prompt_item, parent, false /* do not attach to root */);
102      } else {
103        item = convertView;
104      }
105      TextView description = (TextView) item.findViewById(R.id.api_prompt_item_description);
106      EditText value = (EditText) item.findViewById(R.id.api_prompt_item_value);
107      description.setText(mHints[position]);
108      value.setText(mValues[position]);
109      value.addTextChangedListener(new ValueWatcher(position));
110      return item;
111    }
112  }
113
114  private class ValueWatcher implements TextWatcher {
115    private final int mPosition;
116
117    public ValueWatcher(int position) {
118      mPosition = position;
119    }
120
121    @Override
122    public void afterTextChanged(Editable e) {
123    }
124
125    @Override
126    public void beforeTextChanged(CharSequence s, int start, int before, int count) {
127    }
128
129    @Override
130    public void onTextChanged(CharSequence s, int start, int before, int count) {
131      mValues[mPosition] = s.toString();
132    }
133  }
134}
135