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 */ 48 public 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